Java Essentials: JVM, OOP, Arrays & Exceptions Made Simple Quiz

Boost your confidence for Java interviews by reviewing key concepts like JVM operation, core OOP principles, handling arrays, and exception management in backend development. These easy questions reinforce your grasp of Java fundamentals.

  1. JVM Purpose

    What is the primary role of the Java Virtual Machine (JVM) during Java program execution?

    1. It translates Java bytecode into machine code at runtime
    2. It packages Java programs into JAR files
    3. It compiles Java code to bytecode

    Explanation: The JVM runs Java bytecode by converting it to machine code during program execution, enabling platform independence. Compiling Java code to bytecode is done by the compiler, and packaging into JAR files is handled by build tools.

  2. OOP Concept

    Which object-oriented principle allows one class to inherit fields and methods from another class in Java?

    1. Encapsulation
    2. Inheritance
    3. Polymorphism

    Explanation: Inheritance enables a new class to use properties of an existing class, promoting code reuse. Encapsulation is about restricting access to data, while polymorphism handles method behavior differences.

  3. Array Initialization

    Which statement correctly creates an integer array of size 5 in Java?

    1. int[] nums = new int[5];
    2. int nums = [5];
    3. int array(5) = new int;

    Explanation: The correct syntax for creating an integer array of size 5 is 'int[] nums = new int[5];'. The second option lacks proper array declaration, and the third uses incorrect syntax.

  4. Handling Exceptions

    How do you handle a checked exception in Java to prevent compile-time errors?

    1. Use 'break' statement
    2. Wrap the code in a try-catch block
    3. Use a finally block

    Explanation: Wrapping code in a try-catch block allows you to handle checked exceptions and avoid compile-time errors. A finally block is used for code that always runs, and 'break' is for loop control.

  5. Array Index

    If you declare an array as 'int[] scores = new int[3];', what is the index of the last element?

    1. 2
    2. 3
    3. 1

    Explanation: Java arrays use zero-based indexing, so the last element of a size 3 array is at index 2. Index 3 is out of bounds, and index 1 is the second element.

  6. Superclass of Java Classes

    Which class is the common ancestor (superclass) of all Java classes?

    1. Parent
    2. Object
    3. Base

    Explanation: All classes in Java implicitly inherit from the 'Object' class, which provides fundamental methods. 'Base' and 'Parent' are not defined classes in the standard API.

  7. NullPointerException Cause

    What commonly causes a NullPointerException in Java?

    1. Array index out of bounds
    2. Division by zero
    3. Calling a method on a null object reference

    Explanation: A NullPointerException occurs when you try to access members of an object that is null. Division by zero raises ArithmeticException, and accessing invalid array indexes causes ArrayIndexOutOfBoundsException.

  8. OOP - Encapsulation

    How is encapsulation achieved in Java?

    1. By using private fields and public methods
    2. By extending classes
    3. By using static methods

    Explanation: Encapsulation restricts access to object data by making fields private and providing access through public methods. Static methods and inheritance serve different purposes.

  9. Syntax - Array Length

    Which is the correct way to get the number of elements in a Java array named 'arr'?

    1. arr.size()
    2. arr.count()
    3. arr.length

    Explanation: The 'length' property returns the size of an array in Java. Arrays do not have 'size()' or 'count()' methods; those are used with other collections.

  10. Exception Hierarchy

    Which type of exception must be either caught or declared to be thrown in Java?

    1. Errors
    2. Checked exceptions
    3. Unchecked exceptions

    Explanation: Checked exceptions must be handled explicitly in code, ensuring compile-time safety. Unchecked exceptions and errors do not require mandatory handling.