MCQ Practice13 min read

Java MCQ with Answers: 25 Questions That Catch People Out

By the QUFF Team

Java MCQs are not a test of how much Java you have read - they are a test of whether you know what the language actually does. Almost every question that separates candidates in a placement test is an output question or an edge case: integer division truncating, string literals sharing a reference, Integer caching flipping == from true to false at 128. This set is twenty-five such questions with the answer and, more usefully, the reason. Commit to an option before you read the explanation.

A laptop showing a code editor with curly braces, angle brackets, a terminal window and a database icon, representing programming

Data types and operators

What is the size of an int in Java? (a) 16 bits (b) 32 bits (c) 64 bits (d) Platform dependent

(b) 32 bits. Unlike C, Java fixes the width of every primitive by specification: byte 8, short 16, int 32, long 64, float 32, double 64, char 16. 'Platform dependent' is the C answer, and it is the distractor that catches students who learnt C first.

Which of these is NOT a primitive type in Java? (a) byte (b) short (c) String (d) char

(c) String. String is a class in java.lang, not a primitive. There are exactly eight primitives: byte, short, int, long, float, double, char and boolean.

What is the output of System.out.println(10 / 3); ?

3. Both operands are ints, so integer division truncates toward zero and discards the remainder - it does not round. Write 10 / 3.0 to get 3.333... Forgetting this is the most common arithmetic bug in beginner Java.

What is the output of System.out.println(0.1 + 0.2 == 0.3); ?

false. Doubles are binary floating-point, and neither 0.1 nor 0.2 is exactly representable, so the sum is very slightly off 0.3. Never compare floating-point values with ==; compare the absolute difference against a small tolerance instead.

What is the output of System.out.println(1 + 2 + "3" + 4 + 5); ?

3345. The + operator is left-associative, so 1 + 2 is arithmetic and gives 3; from the first String onward every + becomes concatenation: "3" + "3" then "4" then "5". Changing the order changes the answer entirely.

What is the range of a byte in Java? (a) 0 to 255 (b) −128 to 127 (c) −127 to 128 (d) 0 to 127

(b) −128 to 127. A byte is a signed 8-bit two's-complement integer, so the negative range extends one further than the positive. Java has no unsigned byte, which is why 0 to 255 is wrong.

What is printed by: int x = 'A'; System.out.println(x); ?

65. A char is an unsigned 16-bit integer type holding a Unicode code unit, and assigning it to an int is a widening conversion, so you get the code point of 'A'.

Strings and references

String s1 = "hello"; String s2 = "hello"; What does s1 == s2 return?

true. Identical string literals are interned in the string constant pool, so both references point to the same object. This is a special case for literals, not a general rule about strings.

String s1 = "hello"; String s2 = new String("hello"); What does s1 == s2 return?

false. new String() always creates a fresh object on the heap, so the references differ even though the contents match. s1.equals(s2) returns true - which is exactly why you compare strings with equals and never with ==.

Which class should you use for heavy string modification in a single thread? (a) String (b) StringBuilder (c) StringBuffer (d) CharArray

(b) StringBuilder. String is immutable, so every modification allocates a new object. StringBuilder is mutable and unsynchronised, making it the right choice single-threaded; StringBuffer is its synchronised - and therefore slower - equivalent.

Integer a = 127, b = 127; Integer c = 128, d = 128; What do a == b and c == d return?

a == b is true, c == d is false. Autoboxing caches Integer objects from −128 to 127, so 127 reuses a cached instance while 128 creates new objects. This is the single most notorious Java MCQ, and it is also why you should compare wrapper objects with equals.

Practice this now

Classes, objects and OOP

Which keyword prevents a class from being subclassed? (a) static (b) final (c) private (d) sealed

(b) final. A final class cannot be extended - String is the classic example. On a method, final prevents overriding; on a variable, it prevents reassignment.

What happens if a class declares no constructor?

The compiler inserts a default no-argument constructor that simply calls super(). Note that it does this only when you declare none at all - the moment you write any constructor, the default disappears, which is why adding a parameterised constructor can break code that used new MyClass().

Can a Java class extend more than one class? (a) Yes (b) No (c) Only abstract classes (d) Only with an interface

(b) No. Java allows single class inheritance to avoid the diamond problem for state, but a class may implement any number of interfaces - which is how it achieves multiple inheritance of behaviour.

What is the superclass of every Java class?

java.lang.Object. Every class inherits from it directly or indirectly, which is why toString, equals, hashCode and getClass are available on any object.

Can a static method be overridden? (a) Yes (b) No (c) Only in an abstract class (d) Only if it is public

(b) No. Static methods belong to the class, so there is no instance to dispatch on. Declaring the same signature in a subclass hides the parent method, and which one runs depends on the reference type at compile time - not on the object.

What does the super() call do?

It invokes the parent class constructor. It must be the first statement in a constructor, and if you omit it the compiler inserts a no-argument super() for you - which fails to compile if the parent has no no-arg constructor.

Practice this now

Exceptions and control flow

Which of these is a checked exception? (a) NullPointerException (b) ArithmeticException (c) IOException (d) ArrayIndexOutOfBoundsException

(c) IOException. Checked exceptions extend Exception but not RuntimeException, and the compiler forces you to catch or declare them. The other three are unchecked runtime exceptions, which usually signal programming bugs rather than recoverable conditions.

Does a finally block execute if the try block contains a return statement?

Yes. The finally block runs before the method actually returns - the return value is computed, finally executes, then control leaves. The only realistic exceptions are System.exit() and an abnormal JVM termination.

What does System.out.println(10 / 0); do for integers, and what about 10.0 / 0?

The integer form throws ArithmeticException: / by zero. The floating-point form does not throw - it evaluates to Infinity, and 0.0 / 0.0 gives NaN. Integer and floating-point division by zero behave completely differently, which is a favourite MCQ.

Which statement about a switch on a String is correct? (a) Not allowed (b) Allowed since Java 7 (c) Allowed only with enums (d) Allowed only with int

(b) Allowed since Java 7. switch works on byte, short, char, int, their wrappers, enums and String. It does not work on long, float, double or boolean.

Practice this now

Collections and generics

Which collection interface forbids duplicate elements? (a) List (b) Set (c) Queue (d) Map

(b) Set. A List is an ordered sequence allowing duplicates, a Queue holds elements for processing, and a Map stores key-value pairs where keys are unique but values may repeat.

What is the average time complexity of get() on a HashMap? (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n)

(a) O(1) on average. Hashing locates the bucket directly. Heavy collisions degrade it - toward O(log n) in modern JDKs, which convert an over-full bucket from a linked list into a balanced tree.

Which map keeps its keys in sorted order? (a) HashMap (b) LinkedHashMap (c) TreeMap (d) Hashtable

(c) TreeMap, which is backed by a red-black tree and gives O(log n) operations. LinkedHashMap preserves insertion order rather than sorted order, and HashMap guarantees no order at all.

Why must equals() and hashCode() be consistent for a HashMap key?

Because a HashMap uses hashCode to choose the bucket and equals to find the entry inside it. Two objects that are equal but return different hash codes land in different buckets, so a lookup silently fails to find a key you did know you inserted.

What is generic type erasure? (a) Generics are removed at compile time (b) Generics are checked at run time (c) Generics apply only to collections (d) Generics prevent casting

(a) Generic type information is erased after compilation, so List<String> and List<Integer> are the same class at run time. This is why you cannot write new T[] or check instanceof List<String>.

Static, final and the JVM

What can a static method NOT do? (a) Be called without an object (b) Access static fields (c) Use the this reference (d) Be overloaded

(c) Use the this reference. A static method belongs to the class rather than to any instance, so there is no this - which also means it cannot touch instance fields or call instance methods directly.

What is the default value of an uninitialised boolean instance field?

false. Instance and static fields get defaults: 0 for numeric types, '\u0000' for char, false for boolean and null for references. Local variables get no default at all - using one before assignment is a compile error.

What is autoboxing?

The automatic conversion between a primitive and its wrapper class, such as int to Integer when adding to a List<Integer>. The reverse is unboxing - and unboxing a null Integer throws NullPointerException, which is a common source of surprise.

Which interface must a class implement to be run by a Thread, and which method must it define?

Runnable, defining public void run(). Calling run() directly executes it on the current thread; only start() creates a new thread of execution - a distinction MCQs test frequently.

How to use MCQs so they actually teach you

The value of an MCQ is not the answer - it is the reason the other three options were plausible. A student who marks the right option and moves on learns nothing about the misconception the question was built around.

  • Commit to an option before reading the explanation. An answer you did not commit to teaches you nothing.
  • For every miss, write one line on why the wrong option looked right. That line is the actual lesson.
  • Trace output questions on paper. Predicting from pattern recognition is exactly what these questions are designed to punish.
  • Re-attempt your misses after 48 hours, not immediately - immediate re-attempts test short-term memory only.
  • Then run the same topics as timed quizzes, because placement MCQs come with a clock and speed is a separate skill.

The bottom line

Now go test yourself

Java MCQs cluster around a small set of ideas that the language handles in a specific and slightly surprising way: integer division truncating, == comparing references, the string pool and the Integer cache, checked versus unchecked exceptions, and what the compiler adds when you leave something out. Get those right and most of the question bank falls with them.

Now put the recall under a clock. Run a timed Java quiz on QUFF, read the explanation on every miss, and note in one line why the wrong option tempted you - that habit is what converts a solved MCQ into knowledge you keep.

FAQs

Frequently asked questions

What kind of Java MCQs come in placement tests?

Mostly output questions and edge cases - integer division, operator precedence in string concatenation, == versus equals, the Integer cache, checked versus unchecked exceptions, collection choices and complexities, and static versus instance behaviour.

Why does 0.1 + 0.2 == 0.3 return false in Java?

Because doubles use binary floating-point and neither 0.1 nor 0.2 has an exact binary representation, so their sum differs very slightly from 0.3. Compare floating-point values by checking whether the absolute difference is within a small tolerance.

Why does Integer a = 127, b = 127 give a == b as true, but 128 gives false?

Autoboxing caches Integer instances from −128 to 127, so 127 reuses the same cached object while 128 allocates new ones. It is the standard argument for comparing wrapper objects with equals rather than ==.

What is the difference between == and equals in Java?

For primitives == compares values. For objects == compares references - whether both point to the same object - while equals compares contents as defined by the class. Strings must be compared with equals unless you specifically intend a reference check.

Can static methods be overridden in Java?

No. They belong to the class, not to instances, so there is no dynamic dispatch. A same-signature static method in a subclass hides the parent's version and resolves on the reference type at compile time.

How many Java MCQs should I practise before a placement test?

Aim for depth over volume - a few hundred well-reviewed questions beat thousands skimmed. What matters is writing down, for each miss, why the wrong option looked right, since MCQs are built around specific misconceptions.

Are Java MCQs enough to clear a technical interview?

No. MCQs sharpen your recall of language behaviour, which is what written tests measure, but interviews additionally require writing working code and explaining design choices aloud. Use both.

Where can I practise Java MCQs online for free?

QUFF has a free adaptive Java quiz with no sign-up. It escalates as you score and explains every answer, so each question teaches the underlying rule rather than just marking you right or wrong.

Related quizzes

Put it into practice

Keep reading

Related articles

Browse all articles →

Test yourself in two minutes

Six adaptive questions, every answer explained by an AI tutor. Free.

▶ Start an AI quiz