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.
| Score | Suggested level |
|---|---|
| 21–25 | Strong understanding of core Java |
| 16–20 | Good foundation with a few topics to review |
| 10–15 | Basic understanding; revisit core concepts |
| 0–9 | Start with Java fundamentals and practise again |
Java Fundamentals Quiz Questions
1. Which component executes Java bytecode?
- Java Development Kit
- Java Virtual Machine
- Java source compiler
- 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?
.java.jar.class.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?
public void start()static void execute()public static void main(String[] args)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?
bitbooleanboolbinary
Answer
Correct answer: boolean. A Java boolean variable can contain either true or false.
5. Which keyword creates a new object in Java?
createclassnewobject
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?
int value = 10 + 2 * 3;
System.out.println(value);
- 36
- 16
- 30
- 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?
===:=equals
Answer
Correct answer: ==. The == operator compares primitive values. A single = performs assignment.
8. Which loop always executes its body at least once?
forwhiledo-while- 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?
- Skips only the current iteration
- Terminates the nearest loop
- Restarts the loop
- 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?
- Only floating-point types
- Compatible integral types, enum values, and strings
- Any Java object
- 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?
inheritsimplementsextendssuper
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?
- A constructor must return
void. - A constructor has the same name as its class and no return type.
- A constructor can be called only once in an application.
- 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?
- Defining methods with the same name but different parameter lists
- Replacing a superclass method with an identical method in a subclass
- Calling one method from another
- 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?
- Declaring several methods in the same class
- Providing a subclass implementation of an inherited instance method
- Changing only a method’s return value
- 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?
- Yes, without restrictions
- Yes, but only for abstract classes
- No, Java classes support single class inheritance
- 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?
- Strings are mutable.
- Strings are primitive values.
- Strings are immutable objects.
- 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?
==equals()compareReference()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
- 0
- 1
- 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?
ArrayListHashSetLinkedListArrayDeque
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?
MapSetQueueList
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?
catchthrowsfinalassert
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?
- It declares a checked exception.
- It contains code intended to run after exception handling, subject to abnormal JVM termination.
- It prevents all exceptions.
- It repeats the
tryblock.
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?
staticprivatefinalconst
Answer
Correct answer: final. A method declared final cannot be overridden by subclasses.
24. What is the output of the following Java code?
String first = "Java";
String second = new String("Java");
System.out.println(first == second);
System.out.println(first.equals(second));
truefollowed bytruefalsefollowed bytruetruefollowed byfalsefalsefollowed byfalse
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?
- Encapsulation
- Runtime polymorphism
- Constructor chaining
- 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
| Question | Answer | Question | Answer | Question | Answer |
|---|---|---|---|---|---|
| 1 | JVM | 10 | Integral types, enums, and strings | 19 | HashSet |
| 2 | .class | 11 | extends | 20 | Map |
| 3 | main method | 12 | Same class name, no return type | 21 | catch |
| 4 | boolean | 13 | Same name, different parameters | 22 | Cleanup after exception handling |
| 5 | new | 14 | Subclass implementation | 23 | final |
| 6 | 16 | 15 | No, one superclass | 24 | false, then true |
| 7 | == | 16 | Strings are immutable | 25 | Runtime polymorphism |
| 8 | do-while | 17 | equals() | ||
| 9 | Terminates the nearest loop | 18 | 0 |
Java Code Quiz for Additional Practice
Study the following program and determine its output before opening the answer.
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
==withequals(). - 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, andMap. - Trace references when two variables point to the same object.
- Review immutability, access modifiers, constructors, and the
staticandfinalkeywords.
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, andthrows. - Ensure code and output blocks use the appropriate PrismJS-compatible classes.
- Retest the answer-key numbering after adding, removing, or reordering questions.
TutorialKart.com