Java Data Types MCQ

Java is a statically-typed language, meaning that every variable must first be declared to use a data type. This strong typing is a cornerstone of the language's robustness. For those new to Java, having a good grasp of data types is fundamental. Test your foundational knowledge with this Java Data Types Quiz!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which of the following is a primitive data type in Java?

a) String
b) int
c) Array
d) Object

Answer:

b) int

Explanation:

In Java, int is one of the eight primitive data types. The others are byte, short, long, float, double, char, and boolean.

2. What is the default value of a boolean in Java?

a) 0
b) false
c) null
d) undefined

Answer:

b) false

Explanation:

The default value for a boolean data type is false.

3. What is the size of a double data type in Java?

a) 4 bytes
b) 2 bytes
c) 8 bytes
d) 16 bytes

Answer:

c) 8 bytes

Explanation:

In Java, the double data type occupies 8 bytes.

4. Which of the following can be a valid value for a char data type?

a) "A"
b) 'A'
c) 65
d) Both b and c

Answer:

d) Both b and c

Explanation:

In Java, the char data type can represent a single character using single quotes like 'A' or an ASCII value like 65.

5. Which data type value can an int variable not store?

a) -32768
b) 100000
c) 2147483648
d) -2147483648

Answer:

c) 2147483648

Explanation:

The int data type in Java has a range from -2147483648 to 2147483647.

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

a) float
b) void
c) char
d) short

Answer:

b) void

Explanation:

void is not a primitive data type. It indicates that a method does not return any value.

7. Which data type would be most suitable for storing the price of an item in a store?

a) int
b) double
c) boolean
d) char

Answer:

b) double

Explanation:

The double data type is most suitable for representing prices because it can handle decimal values.

8. What is the default value of a char in Java?

a) 0
b) '\0'
c) ' '
d) null

Answer:

b) '\0'

Explanation:

The default value of a char data type in Java is the null character '\0'.

9. Which of these is an immutable class in Java?

a) StringBuilder
b) String
c) int
d) CharSequence

Answer:

b) String

Explanation:

In Java, String objects are immutable, meaning their values cannot be changed after they are created.

10. How many bits is the long data type in Java?

a) 32
b) 16
c) 64
d) 128

Answer:

c) 64

Explanation:

The long data type in Java is 64 bits or 8 bytes.

Hopefully, this quiz provided an informative and enjoyable challenge. Understanding data types is a critical step in mastering Java. Keep exploring and refining your knowledge, and remember, every coding journey is unique, so embrace yours!