This Java quiz contains multiple-choice questions with answers and short explanations. It covers Java syntax, data types, operators, control statements, object-oriented programming, exceptions, strings, collections, and common interview concepts.

Answer each question before opening the explanation. The questions begin with basic Java concepts and gradually move to intermediate topics.

Java Quiz Instructions and Score Guide

  • Each question has one correct answer.
  • Assign one point for every correct response.
  • Read the explanation after selecting your answer.
  • Try the code-based questions without running the program first.
ScoreSuggested level
21–25Strong understanding of core Java
16–20Good foundation with a few topics to review
10–15Basic understanding; revisit core concepts
0–9Start with Java fundamentals and practise again

Java Fundamentals Quiz Questions

1. Which component executes Java bytecode?

  1. Java Development Kit
  2. Java Virtual Machine
  3. Java source compiler
  4. Java Archive tool
Answer

Correct answer: Java Virtual Machine. The JVM loads and executes compiled Java bytecode. The JDK includes development tools such as the compiler, while the JVM provides the runtime execution environment.

2. What is the usual file extension for compiled Java bytecode?

  1. .java
  2. .jar
  3. .class
  4. .exe
Answer

Correct answer: .class. The Java compiler converts source code from a .java file into bytecode stored in one or more .class files.

3. Which method is the standard entry point of a standalone Java application?

  1. public void start()
  2. static void execute()
  3. public static void main(String[] args)
  4. public int main()
Answer

Correct answer: public static void main(String[] args). The JVM invokes this method to begin executing a standard Java application.

4. Which Java primitive type stores a true or false value?

  1. bit
  2. boolean
  3. bool
  4. binary
Answer

Correct answer: boolean. A Java boolean variable can contain either true or false.

5. Which keyword creates a new object in Java?

  1. create
  2. class
  3. new
  4. object
Answer

Correct answer: new. The new operator allocates memory for an object and invokes its constructor.

Java Operators and Control Statements Quiz

6. What is the output of this Java expression?

</>
Copy
int value = 10 + 2 * 3;
System.out.println(value);
  1. 36
  2. 16
  3. 30
  4. 18
Answer

Correct answer: 16. Multiplication has higher precedence than addition, so Java evaluates 2 * 3 first and then adds 10.

7. Which operator compares two primitive values for equality?

  1. =
  2. ==
  3. :=
  4. equals
Answer

Correct answer: ==. The == operator compares primitive values. A single = performs assignment.

8. Which loop always executes its body at least once?

  1. for
  2. while
  3. do-while
  4. Enhanced for
Answer

Correct answer: do-while. Its condition is evaluated after the loop body, so the statements inside the loop run at least once.

9. What does the break statement do inside a loop?

  1. Skips only the current iteration
  2. Terminates the nearest loop
  3. Restarts the loop
  4. Ends the Java program
Answer

Correct answer: It terminates the nearest loop. Execution continues with the first statement after that loop. The continue statement skips the remainder of the current iteration instead.

10. Which types can be used directly as a Java switch selector?

  1. Only floating-point types
  2. Compatible integral types, enum values, and strings
  3. Any Java object
  4. Only boolean values
Answer

Correct answer: Compatible integral types, enum values, and strings. Traditional Java switch selectors support types such as byte, short, char, int, their wrappers, enums, and String. They do not directly support boolean, long, float, or double.

Java Object-Oriented Programming Quiz

11. Which keyword allows one class to inherit from another class?

  1. inherits
  2. implements
  3. extends
  4. super
Answer

Correct answer: extends. A class uses extends to inherit accessible fields and methods from another class.

12. Which statement about Java constructors is correct?

  1. A constructor must return void.
  2. A constructor has the same name as its class and no return type.
  3. A constructor can be called only once in an application.
  4. Every constructor must be declared static.
Answer

Correct answer: A constructor has the same name as its class and no return type. Constructors initialize newly created objects. Writing a return type, including void, makes the declaration a method rather than a constructor.

13. What is method overloading in Java?

  1. Defining methods with the same name but different parameter lists
  2. Replacing a superclass method with an identical method in a subclass
  3. Calling one method from another
  4. Declaring a method as final
Answer

Correct answer: Defining methods with the same name but different parameter lists. The compiler selects an overloaded method from the number and types of arguments supplied at the call site.

14. What is method overriding in Java?

  1. Declaring several methods in the same class
  2. Providing a subclass implementation of an inherited instance method
  3. Changing only a method’s return value
  4. Hiding every field in a superclass
Answer

Correct answer: Providing a subclass implementation of an inherited instance method. The overriding method must have a compatible signature and return type. Runtime polymorphism determines which implementation is invoked.

15. Can a Java class directly extend more than one class?

  1. Yes, without restrictions
  2. Yes, but only for abstract classes
  3. No, Java classes support single class inheritance
  4. No, Java does not support inheritance
Answer

Correct answer: No, Java classes support single class inheritance. A class can extend one superclass, but it can implement multiple interfaces.

Java Strings, Arrays, and Collections Quiz

16. Which statement about Java String objects is correct?

  1. Strings are mutable.
  2. Strings are primitive values.
  3. Strings are immutable objects.
  4. Strings cannot contain spaces.
Answer

Correct answer: Strings are immutable objects. Operations that appear to change a string produce a new String object rather than modifying the original object.

17. Which method should normally be used to compare the contents of two strings?

  1. ==
  2. equals()
  3. compareReference()
  4. matchesObject()
Answer

Correct answer: equals(). The equals() method compares string contents. The == operator checks whether two references point to the same object.

18. What is the first valid index of a Java array?

  1. -1
  2. 0
  3. 1
  4. It depends on the array type
Answer

Correct answer: 0. Java arrays use zero-based indexing. An array of length n has valid indices from 0 through n - 1.

19. Which collection normally stores unique elements with no guaranteed iteration order?

  1. ArrayList
  2. HashSet
  3. LinkedList
  4. ArrayDeque
Answer

Correct answer: HashSet. A HashSet does not permit duplicate elements and does not guarantee insertion order.

20. Which Java collection stores key-value pairs?

  1. Map
  2. Set
  3. Queue
  4. List
Answer

Correct answer: Map. A map associates each key with a value. Common implementations include HashMap, LinkedHashMap, and TreeMap.

Java Exceptions and Advanced Core Concepts Quiz

21. Which block is used to handle an exception thrown by a try block?

  1. catch
  2. throws
  3. final
  4. assert
Answer

Correct answer: catch. One or more catch blocks can follow a try block to handle matching exception types.

22. What is the purpose of a finally block?

  1. It declares a checked exception.
  2. It contains code intended to run after exception handling, subject to abnormal JVM termination.
  3. It prevents all exceptions.
  4. It repeats the try block.
Answer

Correct answer: It contains code intended to run after exception handling. A finally block is commonly used for cleanup. For resources implementing AutoCloseable, try-with-resources is usually preferable.

23. Which keyword prevents a method from being overridden?

  1. static
  2. private
  3. final
  4. const
Answer

Correct answer: final. A method declared final cannot be overridden by subclasses.

24. What is the output of the following Java code?

</>
Copy
String first = "Java";
String second = new String("Java");
System.out.println(first == second);
System.out.println(first.equals(second));
  1. true followed by true
  2. false followed by true
  3. true followed by false
  4. false followed by false
Answer

Correct answer: false followed by true. The two variables refer to different objects, so == is false. Their character sequences are equal, so equals() returns true.

25. Which feature allows the same method call to invoke different overridden implementations at runtime?

  1. Encapsulation
  2. Runtime polymorphism
  3. Constructor chaining
  4. Autoboxing
Answer

Correct answer: Runtime polymorphism. When a superclass or interface reference points to a subclass object, Java selects the overridden instance method according to the object’s runtime type.

Java Quiz Answer Key

QuestionAnswerQuestionAnswerQuestionAnswer
1JVM10Integral types, enums, and strings19HashSet
2.class11extends20Map
3main method12Same class name, no return type21catch
4boolean13Same name, different parameters22Cleanup after exception handling
5new14Subclass implementation23final
61615No, one superclass24false, then true
7==16Strings are immutable25Runtime polymorphism
8do-while17equals()
9Terminates the nearest loop180

Java Code Quiz for Additional Practice

Study the following program and determine its output before opening the answer.

</>
Copy
class Counter {
    private int value;

    Counter(int value) {
        this.value = value;
    }

    void increment() {
        value++;
    }

    int getValue() {
        return value;
    }
}

public class Main {
    public static void main(String[] args) {
        Counter first = new Counter(5);
        Counter second = first;

        second.increment();
        System.out.println(first.getValue());
    }
}
Show output and explanation
6

Both variables refer to the same Counter object. Calling increment() through second changes the object also referenced by first.

How to Prepare for Java Interview Quiz Questions

Java interview quizzes often test the difference between concepts that look similar. Focus on understanding the result of a program rather than memorising definitions alone.

  • Compare == with equals().
  • Review overloading, overriding, inheritance, abstraction, and polymorphism.
  • Practise operator precedence and loop control statements.
  • Understand checked and unchecked exceptions.
  • Know the differences among List, Set, Queue, and Map.
  • Trace references when two variables point to the same object.
  • Review immutability, access modifiers, constructors, and the static and final keywords.

Frequently Asked Questions About the Java Quiz

Is this Java quiz suitable for beginners?

Yes. The first questions cover basic syntax, primitive types, operators, loops, and the Java execution environment. Later questions introduce object-oriented programming, collections, exceptions, and reference behaviour.

Does this Java quiz include answers and explanations?

Yes. Every question includes the correct answer and a brief explanation of the relevant Java rule or concept.

Which topics should I study for an advanced Java quiz?

After core Java, study generics, streams, lambda expressions, concurrency, class loading, the memory model, annotations, reflection, JDBC, and JVM behaviour. Advanced quizzes may also ask about performance implications and API contracts.

Are Java quiz questions useful for interview preparation?

They are useful for identifying gaps in core concepts, especially when each answer is reviewed carefully. Interview preparation should also include writing programs, debugging code, explaining design choices, and solving practical problems.

How can I improve my Java quiz score?

Record the topics behind incorrect answers, review those concepts, and then retake the quiz without looking at the answer key. For code questions, trace variable values, object references, conditions, and method calls line by line.

Java Quiz Editorial QA Checklist

  • Verify that every Java question has only one unambiguous correct answer.
  • Compile and run each code example with a supported Java version before publication.
  • Confirm that explanations distinguish reference equality from content equality.
  • Check that collection questions do not claim ordering guarantees that the selected implementation does not provide.
  • Review exception terminology, especially checked exceptions, unchecked exceptions, throw, and throws.
  • Ensure code and output blocks use the appropriate PrismJS-compatible classes.
  • Retest the answer-key numbering after adding, removing, or reordering questions.