Data types, variables, and constants – Java MCQs

1.) How many primitive data types are there in Java?

A) 4
B) 6
C) 8
D) 10

Answer: Option C

Explanation: Java has eight primitive data types,

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean

2.) Which of the following is not a primitive data type in Java?

A) int
B) float
C) char
D) string

Answer: Option D

3.) What is the size of the int data type in Java?

A) 4 bits
B) 8 bits
C) 16 bits
D) 32 bits

Answer: Option D

Explanation: In Java, the int data type is a 32-bit signed integer, which means it occupies 4 bytes in memory. Each byte consists of 8 bits and since an int is 32 bits, it takes 32 / 8 = 4 bytes of memory.

4.) What is the default value of a boolean variable in Java?

A) true
B) false
C) null
D) 0

Answer: Option B

5.) Which keyword is used to declare a variable accessible across all class instances and shared among them?

A) final
B) constant
C) static
D) None of these

Answer: Option C

6.) What is the range of the byte data type in Java?

A) -128 to 127
B) 0 to 255
C) -32,768 to 32,767
D) -2,147,483,648 to 2,147,483,647

Answer: Option A

7.) What is the largest primitive data type in Java?

A) int
B) long
C) double
D) float

Answer: Option B

Explanation: The largest primitive data type in Java is the long data type. It is a 64-bit signed integer type, capable of storing whole numbers in the range from approximately -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

In terms of memory size, both double and long are the same, as they both require 64 bits (8 bytes) of memory.

So, if you need to store large whole numbers, use long. On the other hand, if you need to store decimal numbers with high precision and a wide range of values, use double.

8.) What is the default value of an object reference (non-primitive) data type in Java if it is not initialized?

A) 0
B) 1
C) false
D) null

Answer: Option D

9.) Which data type is used to store characters with a larger range, including non-English characters?

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

Answer: Option B

10.) What is the size of the double data type in Java?

A) 2 bytes
B) 4 bytes
C) 8 bytes
D) 16 bytes

Answer: Option C