Data types, variables, and constants – Java MCQs

11.) What is the size of the char data type in Java?

A) 1 byte
B) 2 bytes
C) 4 bytes
D) 8 bytes

Answer: Option B

12.) Which data type should be used to store true/false values when a single byte is sufficient?

A) byte
B) boolean
C) int
D) char

Answer: Option A

13.) What is the maximum value that can be stored in a long data type in Java?

A) 2^31 – 1
B) 2^63 – 1
C) 2^32 – 1
D) 2^64 – 1

Answer: Option B

14.) Which data type should be used to store small whole numbers in Java to save memory?

A) short
B) long
C) double
D) byte

Answer: Option D

15.) Which data type should be used to represent a single Unicode character in Java?

A) String
B) char
C) Character
D) int

Answer: Option B

16.) What is the default value of a char variable in Java?

A) ‘A’
B) ‘a’
C) 0
D) ‘\u0000’

Answer: Option D

17.) What is the scope of a local variable in Java?

A) It is accessible throughout the entire program.
B) It is accessible only within the method or block in which it is declared.
C) It is accessible within the same class and its subclasses.
D) It is accessible throughout the entire package.

Answer: Option B

18.) Which of the following is true about instance variables in Java?

A) They are declared with the “final” keyword.
B) They are static and shared among all instances of the class.
C) They are local variables declared within a method.
D) They are associated with an instance of a class and have unique values for each object.

Answer: Option D

19.) In Java, what is the purpose of declaring variables as “final”?

A) To make them constant and unchangeable.
B) To make them accessible globally.
C) To increase their visibility across different classes.
D) To allocate more memory for them.

Answer: Option A

Explanation: In Java, the “final” keyword is used to define a constant variable, which means its value cannot be changed once it is assigned a value.

20.) In Java, what is the naming convention for constant variables?

A) All uppercase letters are separated by underscores (e.g., MY_CONSTANT).
B) All lowercase letters are separated by underscores (e.g., my_constant).
C) CamelCase (e.g., MyConstant).
D) PascalCase (e.g., My_Constant).

Answer: Option A

Explanation: The naming convention for constant variables in Java is to use all uppercase letters, and words are separated by underscores to improve readability and indicate that the variable is a constant.