AI-assisted, automatically checked• Updated August 2026

Java MCQs

Multiple choice questions on core Java, object-oriented programming, inheritance, JVM, exceptions, and collections.

61 MCQs3 subtopicsProgrammingAll Levels
Key Concept Overview & Exam Context

Review core principles of Java before answering questions. These questions simulate placement technical assessments (TCS, Infosys, Amazon), university exams (CS/IT), and technical certifications.

Target Readiness
Placements & Job Interviews
Question Format
Multiple-Choice with Explanations
Explained Solutions
Written explanations

Core Java Fundamentals Quiz

10 questions · Interactive timed test · Instant score & review

Java MCQ Questions

Page 1 of 2 (61 total)
Q1

What is the purpose of the 'final' keyword when applied to a variable in Java?

medium
View Answer & Explanation
Correct Answer: It prevents the variable from being reassigned after its initial value is set
Explanation:
Once a final variable is assigned a value, attempting to reassign it causes a compile-time error. This is commonly used for constants, though for object references it only prevents reassigning the reference, not changing the object's internal state.
Q2

What is method overloading in Java?

easy
View Answer & Explanation
Correct Answer: Defining multiple methods in the same class with the same name but different parameter lists
Explanation:
Overloading lets a class expose several versions of a method that differ in the number or type of parameters, and the compiler chooses which one to call based on the arguments provided at the call site.
Q3

How does method overriding differ from method overloading?

medium
View Answer & Explanation
Correct Answer: Overriding redefines a method inherited from a superclass with the same signature, while overloading creates methods with the same name but different parameters
Explanation:
Overriding is resolved at runtime based on the actual object type, which is how Java achieves runtime polymorphism, while overloading is resolved at compile time based on the method signature used in the call.
Q4

What does the 'static' keyword mean when applied to a method in Java?

easy
View Answer & Explanation
Correct Answer: The method belongs to the class rather than any specific instance and can be called without creating an object
Explanation:
Static methods are shared across all instances of a class and are invoked using the class name directly. Because they belong to the class, they cannot access instance variables or non-static methods without an object reference.
Q5

What is the purpose of a constructor in Java?

easy
View Answer & Explanation
Correct Answer: To initialize a new object when it is created
Explanation:
A constructor shares the same name as its class, has no return type, and runs automatically when the 'new' keyword creates an object, giving the object its initial state.
Q6

What is the difference between '==' and the equals() method when comparing Java objects?

medium
View Answer & Explanation
Correct Answer: '==' compares object references, while equals() compares the actual content unless overridden
Explanation:
For objects, '==' checks whether two references point to the same memory location, while equals() is meant to check logical equality, though its default implementation in Object also just compares references unless a class overrides it.
Q7

What is the purpose of the 'this' keyword in Java?

easy
View Answer & Explanation
Correct Answer: It refers to the current instance of the class in which it is used
Explanation:
'this' is commonly used inside a constructor or method to distinguish an instance variable from a parameter with the same name, or to pass the current object as an argument to another method.
Q8

What happens when an array index is accessed that is outside the bounds of the array in Java?

easy
View Answer & Explanation
Correct Answer: An ArrayIndexOutOfBoundsException is thrown at runtime
Explanation:
Java performs bounds checking on array access, and any attempt to read or write outside the valid index range results in an unchecked runtime exception rather than undefined behavior.
Q9

What is autoboxing in Java?

medium
View Answer & Explanation
Correct Answer: The automatic conversion between a primitive type and its corresponding wrapper class
Explanation:
Autoboxing lets Java automatically convert an int to an Integer, or a double to a Double, when needed, such as when adding a primitive to a collection that only stores objects. The reverse process is called unboxing.
Q10

What is the key difference between an abstract class and an interface in Java, in terms of state?

medium
View Answer & Explanation
Correct Answer: An abstract class can hold instance fields with state, while a traditional interface cannot
Explanation:
Abstract classes support instance variables that track object state across method calls, which is one reason a class might extend an abstract class rather than implement an interface when shared state is needed.
Q11

What is the purpose of the 'super' keyword in Java?

easy
View Answer & Explanation
Correct Answer: To refer to the immediate parent class and access its members or constructor
Explanation:
'super' is commonly used to call a parent class constructor from a subclass constructor, or to access a parent class method that has been overridden in the current class.
Q12

What is a NullPointerException typically caused by?

easy
View Answer & Explanation
Correct Answer: Attempting to call a method or access a field on a reference that points to null
Explanation:
This is one of the most common runtime exceptions in Java, occurring whenever code tries to use an object reference that has not been assigned an actual object.
Q13

What is the purpose of garbage collection in Java?

medium
View Answer & Explanation
Correct Answer: To automatically reclaim memory occupied by objects that are no longer reachable by the program
Explanation:
Java manages memory automatically through a garbage collector, which periodically scans the heap and frees memory from objects that no longer have any reachable references, removing the need for manual deallocation.
Q14

Which access modifier makes a member accessible only within its own class?

easy
View Answer & Explanation
Correct Answer: private
Explanation:
A private member can only be accessed from within the same class it is declared in, not from subclasses or other classes, even those in the same package.
Q15

What is the difference between StringBuilder and String in Java?

medium
View Answer & Explanation
Correct Answer: StringBuilder is mutable and can be changed after creation, while String is immutable
Explanation:
Every operation on a String, such as concatenation, actually creates a new String object, while StringBuilder modifies its internal buffer directly, which makes it more efficient for building strings through many operations in a loop.
Q16

What does the 'instanceof' operator check in Java?

medium
View Answer & Explanation
Correct Answer: Whether an object is an instance of a specific class or a class that implements a specific interface
Explanation:
instanceof returns a boolean and is often used before casting an object to a more specific type, to avoid a ClassCastException at runtime.
Q17

What is the purpose of a try-with-resources statement in Java?

medium
View Answer & Explanation
Correct Answer: To automatically close resources like files or streams once the try block finishes, without needing an explicit finally block
Explanation:
Any resource used in a try-with-resources statement must implement the AutoCloseable interface, and Java guarantees its close() method runs automatically, even if an exception is thrown inside the block.
Q18

What does the term 'immutable' mean when describing the String class in Java?

easy
View Answer & Explanation
Correct Answer: Once a String object is created, its contents cannot be changed
Explanation:
Any method that appears to modify a String, such as concatenation or replace, actually returns a brand new String object rather than altering the original one in place.
Q19

What is the default access modifier in Java when no modifier is explicitly specified?

medium
View Answer & Explanation
Correct Answer: Package-private, meaning accessible only within the same package
Explanation:
When no access modifier is written, Java applies default access, sometimes called package-private, which restricts visibility to classes located in the same package.
Q20

What is the purpose of an interface in Java?

easy
View Answer & Explanation
Correct Answer: To define a contract of method signatures that implementing classes must provide, without dictating how they are implemented
Explanation:
An interface specifies what a class must be able to do, while leaving the actual implementation details up to each class that implements it, which allows unrelated classes to share a common set of behaviors.
Q21

What is the purpose of the 'finally' block in Java exception handling?

medium
View Answer & Explanation
Correct Answer: It contains code that always runs whether or not an exception was thrown
Explanation:
The finally block is commonly used to close resources like files or database connections, since it executes regardless of whether the try block succeeded or an exception occurred.
Q22

What does the 'synchronized' keyword do in Java?

medium
View Answer & Explanation
Correct Answer: It restricts access to a method or block so only one thread can execute it at a time
Explanation:
Synchronized is used to prevent race conditions when multiple threads share the same data. Only one thread can hold the lock on the synchronized section at any given moment.
Q23

What is the difference between an abstract class and a concrete class in Java?

easy
View Answer & Explanation
Correct Answer: An abstract class can have unimplemented methods and cannot be instantiated directly, while a concrete class provides full implementations and can be instantiated
Explanation:
Abstract classes are meant to be extended, providing a common base with some shared logic while leaving specific behavior to subclasses.
Q24

What is the purpose of the 'throws' keyword in a Java method signature?

medium
View Answer & Explanation
Correct Answer: It declares that a method might throw a specific checked exception, which the caller must handle or propagate
Explanation:
Using throws is different from actually throwing an exception with the throw keyword. It simply warns callers that they need to be ready to catch or rethrow the listed exception.
Q25

What is a package in Java used for?

easy
View Answer & Explanation
Correct Answer: It groups related classes and interfaces together and helps avoid naming conflicts
Explanation:
Packages also control access through package-private visibility and make large codebases easier to organize and navigate.
Q26

What is the purpose of the 'transient' keyword in Java?

hard
View Answer & Explanation
Correct Answer: It marks a field so that it is skipped during the default serialization of an object
Explanation:
Fields marked transient are not written out when an object is serialized, which is useful for sensitive data or values that can be recomputed after deserialization.
Q27

What is the difference between a HashMap and a TreeMap in Java?

medium
View Answer & Explanation
Correct Answer: A HashMap does not guarantee any particular order of keys, while a TreeMap keeps keys sorted
Explanation:
TreeMap implements a sorted structure internally, usually a red-black tree, so iterating over it returns keys in natural or custom sorted order, unlike the unpredictable order of a HashMap.
Q28

What is the purpose of the Java 'Comparable' interface?

medium
View Answer & Explanation
Correct Answer: It allows objects of a class to define a natural ordering used for sorting
Explanation:
A class implements Comparable and overrides compareTo to specify how its instances should be ordered, which is what collections like TreeSet and Collections.sort rely on.
Q29

What happens when you divide an integer by zero in Java?

medium
View Answer & Explanation
Correct Answer: It throws an ArithmeticException at runtime
Explanation:
Integer division by zero is not defined and Java raises an ArithmeticException. Dividing a floating point number by zero, however, produces Infinity or NaN instead of an exception.
Q30

What is the purpose of the 'volatile' keyword in Java?

hard
View Answer & Explanation
Correct Answer: It ensures that a variable's value is always read from and written to main memory rather than a thread's local cache
Explanation:
Volatile helps with visibility between threads so that a change made by one thread is seen by others right away, though it does not provide atomicity for compound operations.
Q31

What is method chaining in Java?

medium
View Answer & Explanation
Correct Answer: Calling multiple methods on the same object in a single statement, where each method returns the object itself
Explanation:
Builder classes commonly use method chaining, where each setter method returns 'this' so calls can be strung together fluently.
Q32

What is the purpose of an enum in Java?

easy
View Answer & Explanation
Correct Answer: It defines a fixed set of named constant values as a distinct type
Explanation:
Enums are useful when a variable should only take one of a small, known set of values, such as days of the week or status codes, and they offer type safety over plain integers or strings.
Q33

What is the difference between checked and unchecked exceptions in Java?

medium
View Answer & Explanation
Correct Answer: Checked exceptions must be declared or handled at compile time, while unchecked exceptions are not required to be handled
Explanation:
Checked exceptions like IOException extend Exception but not RuntimeException, forcing the compiler to check that they are caught or declared. Unchecked exceptions extend RuntimeException and skip this requirement.
Q34

What is the purpose of the Java 'Scanner' class?

easy
View Answer & Explanation
Correct Answer: It is used to read input, such as from the keyboard or a file, in a convenient way
Explanation:
Scanner provides simple methods like nextInt() and nextLine() to parse different types of input, making it a common choice for beginner programs that read from the console.
Q35

What is the difference between an interface and an abstract class regarding multiple inheritance in Java?

medium
View Answer & Explanation
Correct Answer: A class can implement multiple interfaces but can only extend one class, including abstract classes
Explanation:
This is one reason interfaces are often used to achieve behavior that resembles multiple inheritance in Java, since the language deliberately avoids the ambiguity of extending multiple classes.
Q36

What is the purpose of the 'equals()' method when overridden in a custom Java class?

medium
View Answer & Explanation
Correct Answer: It defines what it means for two objects of that class to be logically equal, beyond simple reference comparison
Explanation:
By default, equals() from Object compares references, meaning two separate objects with identical data would be considered unequal unless equals() is overridden to compare their actual field values.
Q37

What does the Java keyword 'protected' control?

medium
View Answer & Explanation
Correct Answer: It allows access to a member from within its own class, subclasses, and other classes in the same package
Explanation:
Protected sits between private and public in terms of restriction, and it is particularly useful when a subclass in a different package still needs access to a parent class's members.
Q38

What is the purpose of the 'toString()' method in Java?

easy
View Answer & Explanation
Correct Answer: It provides a readable String representation of an object, often overridden for meaningful output
Explanation:
By default, toString() returns something like the class name plus a hash code, which is rarely useful, so classes commonly override it to return something more descriptive.
Q39

What is the purpose of the Java 'finalize()' method, historically speaking?

hard
View Answer & Explanation
Correct Answer: It was intended to allow an object to perform cleanup actions right before it is garbage collected
Explanation:
In practice, finalize() was unreliable and unpredictable in timing, which is why it has been deprecated in favor of alternatives like try-with-resources and explicit cleanup methods.
Q40

What is the significance of the 'main' method signature 'public static void main(String[] args)' in Java?

easy
View Answer & Explanation
Correct Answer: It is the entry point the JVM looks for to start executing a standalone Java application
Explanation:
The method must be exactly public, static, and void, and it must accept a String array, because the JVM calls it directly without creating an instance of the class first.
Q41

What is the purpose of the Java 'ArrayList' class?

easy
View Answer & Explanation
Correct Answer: It implements a resizable array that can grow or shrink as elements are added or removed
Explanation:
Unlike a regular array, an ArrayList automatically manages its internal capacity, which makes it convenient when the number of elements is not known ahead of time.
Q42

What does it mean for a Java method to be 'overloaded' based on return type alone?

hard
View Answer & Explanation
Correct Answer: It is not allowed, since Java does not permit overloading methods based solely on a different return type
Explanation:
Method overloading in Java requires a different parameter list, not just a different return type, because the compiler needs the parameters to decide which overload to call.
Q43

What is the purpose of the Java 'Object' class?

medium
View Answer & Explanation
Correct Answer: It is the root class from which every other class in Java implicitly inherits
Explanation:
Because every class ultimately extends Object, methods like toString(), equals(), and hashCode() are available on any Java object even if they were never explicitly defined.
Q44

What is the purpose of a static initializer block in a Java class?

hard
View Answer & Explanation
Correct Answer: It runs once, when the class is first loaded, to initialize static fields or perform one-time setup
Explanation:
Static initializer blocks are useful for setting up static data that requires more logic than a simple assignment, and they run before any instance of the class is created.
Q45

Which of the following is NOT a Java primitive data type?

easy
View Answer & Explanation
Correct Answer: String
Explanation:
String is an object reference class in Java (java.lang.String), whereas int, boolean, and char are primitive types.
Q46

Which keyword is used by a class to inherit from another class in Java?

easy
View Answer & Explanation
Correct Answer: extends
Explanation:
The extends keyword is used by a child class to inherit fields and methods from a parent class in Java.
Q47

Which of the following is NOT a Java primitive data type?

easy
View Answer & Explanation
Correct Answer: String
Explanation:
String is an object reference class in Java (java.lang.String), whereas int, boolean, and char are primitive types.
Q48

Which of the following keywords is used to inherit a class in Java?

easy
View Answer & Explanation
Correct Answer: extends
Explanation:
The "extends" keyword is used to inherit properties and methods from a superclass in Java.
Q49

Which of the following is a checked exception in Java?

medium
View Answer & Explanation
Correct Answer: IOException
Explanation:
IOException is a checked exception that must be declared in a throws clause or caught, whereas NullPointerException, ArithmeticException, and ArrayIndexOutOfBoundsException are unchecked RuntimeExceptions.
Q50

Which Java collection class does NOT guarantee the insertion order of elements?

easy
View Answer & Explanation
Correct Answer: HashSet
Explanation:
HashSet and HashMap do not guarantee any specific order when iterating over elements. LinkedHashSet and ArrayList maintain insertion order.
Showing 1 to 50 of 61 questions