40 Exam Questions on JVM Architecture, Modifiers, Interfaces, Abstract Classes, and Exception Handling. Click "View Model Answer" to see full marks scoring criteria.
Part 1: Foundational Questions (Questions 1 - 20)
Q01Easy
What does JVM stand for and what is its role in Java execution?
Model Answer [3 Marks]: Java Virtual Machine. An abstract computing machine that provides a runtime execution environment for Java bytecodes (`.class` files).
Q02Easy
Explain the concept of Bytecode in Java.
Model Answer [3 Marks]: An intermediate, platform-independent instruction set generated by `javac` compiler that is executed by the JVM interpreter/JIT compiler.
Q03Easy
What is the JIT Compiler in the JVM?
Model Answer [3 Marks]: Just-In-Time Compiler. It compiles frequently called bytecode segments directly into native machine code at runtime for high performance.
Q04Easy
Differentiate between Heap memory and Stack memory in JVM.
Model Answer [4 Marks]: Heap: Shared memory storing instantiated Objects. Stack: Per-thread memory storing stack frames, local variables, and method call chains.
Q05Easy
List the 4 access modifiers in Java in order of decreasing visibility.
Model Answer [4 Marks]: 1. public 2. protected 3. default (package-private) 4. private
Q06Easy
What does the static keyword mean when applied to a field?
Model Answer [3 Marks]: The field belongs to the Class itself rather than object instances, and is shared among all instances in Metaspace memory.
Q07Easy
What does the final keyword mean when applied to a class?
Model Answer [3 Marks]: Prevents the class from being inherited or extended by any subclass (e.g. java.lang.String).
Q08Easy
What does the final keyword mean when applied to a method?
Model Answer [3 Marks]: Prevents the method from being overridden by any subclass.
Q09Easy
What does the final keyword mean when applied to a variable?
Model Answer [3 Marks]: Makes the variable an unmodifiable constant (cannot be reassigned after initialization).
Q10Easy
State the parent root class of all Java exception classes.
Model Answer [2 Marks]: java.lang.Throwable (which splits into Exception and Error).
Q11Easy
What is a Checked Exception? Give an example.
Model Answer [4 Marks]: An exception checked at compile-time by the Java compiler. Must be caught or declared. Example: IOException.
Q12Easy
What is an Unchecked Exception? Give an example.
Model Answer [4 Marks]: An exception deriving from RuntimeException caused by programming logic errors, not checked by compiler. Example: NullPointerException.
Q13Easy
What is the purpose of the finally block in exception handling?
Model Answer [3 Marks]: Contains cleanup code (closing files/connections) that ALWAYS executes regardless of whether an exception was thrown or caught.
Q14Easy
Explain the difference between throw and throws.
Model Answer [4 Marks]: throw is used inside a method body to throw an exception object. throws is used in a method signature to declare potential checked exceptions.
Q15Easy
What causes a NullPointerException?
Model Answer [3 Marks]: Attempting to call a method or access an attribute on an uninitialized reference variable set to null.
Q16Easy
What causes a NumberFormatException?
Model Answer [3 Marks]: Attempting to convert an invalid String into a numeric primitive (e.g. Integer.parseInt("hello")).
Q17Easy
What causes an ArrayIndexOutOfBoundsException?
Model Answer [3 Marks]: Attempting to access an array element using an index that is negative or $\ge \text{array.length}$.
Q18Easy
How do you create a Custom Exception class in Java?
Model Answer [4 Marks]: By extending the Exception class (for checked custom exceptions) or RuntimeException (for unchecked custom exceptions).
Q19Easy
What is a Package in Java?
Model Answer [3 Marks]: A namespace folder structure that groups related classes and interfaces together and prevents naming collisions.
Q20Easy
What is the purpose of the import statement in Java?
Model Answer [2 Marks]: To allow code to reference classes declared in other packages without using fully qualified class names.
Part 2: Medium / Applied Questions (Questions 21 - 30)
Q21Medium
Explain the Classloader Subsystem phases in the JVM (Loading, Linking, Initialization).
Model Answer [6 Marks]: 1. Loading: Reads `.class` file bytecode into memory. 2. Linking: Verifies bytecode safety, prepares static fields with default values, and resolves symbolic references. 3. Initialization: Assigns explicit values to static variables and executes static initializer blocks.
Q22Medium
Compare Abstract Class vs Interface in terms of default/static methods introduced in Java 8.
Model Answer [5 Marks]: Java 8 added default methods (with body) to interfaces to allow backward compatibility without breaking implementing classes. However, interfaces still cannot maintain instance state variables or constructors, whereas abstract classes can.
Q23Medium
What is Try-With-Resources in Java 7+ and why is it preferred over standard try-catch-finally?
Model Answer [5 Marks]: A try statement that automatically closes resources (files, database connections implementing `AutoCloseable`) upon exiting, eliminating boilerplate `finally` close blocks.
Q24Medium
Explain what happens if an exception is thrown in a try block but there is NO matching catch block.
Model Answer [5 Marks]: The finally block (if present) executes first. Then the unhandled exception propagates up the call stack to the caller method. If unhandled at main(), the thread terminates printing a stack trace.
Q25Medium
Can a static method access non-static instance fields of the class directly? Why or why not?
Model Answer [4 Marks]: NO! Static methods execute without an active object instance (no this pointer exists). Instance fields require a specific heap object instance reference to be accessed.
Q26Medium
Explain the protected access modifier rule regarding subclasses in different packages.
Model Answer [5 Marks]: A protected member is accessible to a subclass in a different package ONLY through inheritance (via `this` or `super`), but NOT via an instance reference of the parent class created inside that subclass!
Q27Medium
What is the difference between Error and Exception in Java?
Model Answer [5 Marks]: Error: Represents fatal system-level conditions outside application control (e.g. `OutOfMemoryError`, `StackOverflowError`). Applications should NOT attempt to catch them. Exception: Represents recoverable conditions caused by code or environment that applications should handle.
Q28Medium
Explain what a Static Initializer Block is in Java.
Model Answer [4 Marks]: A block declared with static { ... } that executes ONCE when the class is first loaded into JVM memory by the Classloader, before any constructors are called.
Q29Medium
What is the output of attempting to reassign a final object reference variable: final Person p = new Person("John"); p.setName("Mary");? Does this compile?
Model Answer [5 Marks]: YES, it compiles and works!final on an object reference prevents reassigning the reference pointer itself (`p = new Person(...)`), but does NOT make the internal object state immutable.
Q30Medium
Explain Exception Chaining in Java.
Model Answer [4 Marks]: Wrapping an underlying lower-level exception inside a higher-level business exception using throw new CustomException("Failed", causeException) to preserve original root cause stack traces.
Part 3: Hard / Examiner Trick Questions (Questions 31 - 40)
Q31Examiner Trick
TRICK: If a try block calls System.exit(0);, does the finally block execute?
Model Answer [5 Marks]: NO!System.exit(0) immediately terminates the entire JVM process, bypassing all finally blocks.
Q32Examiner Trick
TRICK: In catch block ordering, what happens if you place catch(Exception e) BEFORE catch(IOException e)?
Model Answer [5 Marks]: Compile Error! Unreachable code error. Subclass exceptions (`IOException`) must be caught BEFORE superclass exceptions (`Exception`) because the superclass block would swallow all exceptions.
Q33Examiner Trick
TRICK: Can a overridden method in a subclass declare a broader checked exception than the superclass method?
Model Answer [5 Marks]: NO! Compile Error. An overriding method cannot throw NEW or BROADER checked exceptions than declared by the parent method signature (it can throw narrower, fewer, or unchecked exceptions).
Q34Examiner Trick
TRICK: Can a method be declared both abstract and private in Java?
Model Answer [4 Marks]: NO! Compile Error. abstract requires a subclass to override the method, whereas private prevents subclasses from accessing or overriding it.
Q35Examiner Trick
TRICK: What is the default access modifier of members declared inside a Java Interface if no access modifier is specified?
Model Answer [4 Marks]: `public` (NOT default/package-private!). All interface methods and fields are implicitly public.
Q36Examiner Trick
TRICK: Does Java support transient variables? What is their purpose?
Model Answer [4 Marks]: Yes. The transient modifier prevents a field from being serialized when an object is converted to a byte stream (used for sensitive fields like passwords).
Q37Examiner Trick
TRICK: Can a constructor be declared static or final?
Model Answer [4 Marks]: NO! Compile Error. Constructors cannot be static (they instantiate instances) or final (they are not inherited or overridden).
Q38Examiner Trick
TRICK: If a method returns a value inside a try block, and also returns inside the finally block, which value is returned?
Model Answer [5 Marks]: The return value from the finally block overrides the return value from the try or catch block! (Considered a bad practice).
Q39Examiner Trick
TRICK: What is the difference between java.lang.NoClassDefFoundError and ClassNotFoundException?
Model Answer [5 Marks]: ClassNotFoundException is a checked exception occurring when dynamically loading a class string (e.g. `Class.forName()`). NoClassDefFoundError is a fatal Error occurring when a class was present at compile-time but missing at runtime.
Q40Examiner Trick
TRICK: Why is java.lang.OutOfMemoryError classified under Error rather than Exception?
Model Answer [4 Marks]: Because running out of heap memory is a fatal JVM execution state failure from which normal application logic cannot recover.