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.
What is the primary role of the Java Virtual Machine (JVM) during Java program execution?
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.
Which object-oriented principle allows one class to inherit fields and methods from another class in Java?
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.
Which statement correctly creates an integer array of size 5 in Java?
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.
How do you handle a checked exception in Java to prevent compile-time errors?
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.
If you declare an array as 'int[] scores = new int[3];', what is the index of the last element?
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.
Which class is the common ancestor (superclass) of all Java classes?
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.
What commonly causes a NullPointerException in Java?
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.
How is encapsulation achieved in Java?
Explanation: Encapsulation restricts access to object data by making fields private and providing access through public methods. Static methods and inheritance serve different purposes.
Which is the correct way to get the number of elements in a Java array named 'arr'?
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.
Which type of exception must be either caught or declared to be thrown in Java?
Explanation: Checked exceptions must be handled explicitly in code, ensuring compile-time safety. Unchecked exceptions and errors do not require mandatory handling.