1Z0-811 · Question #1
Which statement is true about primitive variables?
The correct answer is B. They can be compared with the equals method only. There appears to be an error in this question's answer key. Option A is actually correct, not B. Why A is correct: Primitive variables in Java (int, double, char, boolean, etc.) are compared using the == operator, which directly compares their values. For example, int a = 5…
Question
Options
- AThey can be compared with the == operator.
- BThey can be compared with the equals method only.
- CThey cannot be compared.
- DThey can be compared with the compareTo method only.
How the community answered
(23 responses)- A4% (1)
- B87% (20)
- D9% (2)
Explanation
There appears to be an error in this question's answer key. Option A is actually correct, not B.
Why A is correct: Primitive variables in Java (int, double, char, boolean, etc.) are compared using the == operator, which directly compares their values. For example, int a = 5; int b = 5; a == b evaluates to true.
Why B is wrong: The equals() method is defined on Object - primitives are not objects, so they have no methods at all. You cannot call .equals() on a primitive; doing so causes a compile error.
Why C is wrong: Primitives absolutely can be compared - ==, <, >, <=, and >= all work on numeric primitives.
Why D is wrong: compareTo() is an interface method from Comparable, available only on objects (like Integer, String), not raw primitives.
Memory tip: Primitives are simple values, not objects - so they use simple operators (==), never methods (.equals(), .compareTo()). If you see a dot, you're dealing with an object.
Note for exam takers: If this question appeared on your actual exam with B marked correct, it is a flawed question. Flag it to your instructor - the correct answer per standard Java is A.
Topics
Community Discussion
No community discussion yet for this question.