Essential Java Interview Questions and Answers

Java Super Keyword

Question: Explain the functionality of the super keyword in Java.

Answer: The super keyword refers to the immediate parent class object. It is used to:

  • Access parent class data members hidden by child class data members.
  • Call parent class methods when overridden in the child class.
  • Invoke the parent class constructor using super().

Throw vs Throws in Java

Question: Differentiate between throw and throws keywords in Java.

Answer: throw is used inside a method or block to explicitly throw an exception object. throws is used in the method declaration to indicate that a method may pass one or more exceptions to the caller. Example: throw new IOException(); and void read() throws IOException.

Overloaded Main Methods

Question: What will be the output of the overloaded main program that calls main(1) and main(2,3)?

Answer: Execution starts from main(String[] a), then main(1) prints 2 and main(2,3) prints 5. Therefore, the output is 2 5. The overloaded main methods act like ordinary methods; the JVM starts only from main(String[] args).

Inheritance and Method Overriding

Question: What will be the output of the inheritance program where B overrides display() and prints i+5 after setting i=3?

Answer: The output is 8. Because the object is of class B and class B overrides display(), the method prints i + 5 = 3 + 5 = 8.

String Comparison in Java

Question: For xyz="abcde", xy=new String("abcde"), x=new String(xyz), what are the outputs of xyz==xy, xyz==x, xyz.equals(xy), xy.equals(x)?

Answer: Outputs: false, false, true, true. == compares references, while equals() compares contents. xy and x are separate heap objects, so == gives false, but all hold the same text so equals() gives true.

ActionListener Syntax

Question: Write the syntax for adding an ActionListener to a button in Java.

Answer: JButton b = new JButton("Click"); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ /* code */ } });

Java Thread Priority

Question: Explain thread priority in Java. How is it set?

Answer: Thread priority is a numeric hint to the scheduler indicating the relative importance of a thread. It ranges from 1 to 10: MIN_PRIORITY=1, NORM_PRIORITY=5, MAX_PRIORITY=10. It is set using setPriority(), for example: t1.setPriority(Thread.MAX_PRIORITY);

Abstract Classes and Methods

Question: Is it compulsory for an abstract class to have at least one abstract method? Can an abstract method be declared private?

Answer: No, an abstract class need not contain any abstract method. No, an abstract method cannot be private because it must be overridden in a subclass, whereas a private method is not visible outside its class.

Try-Catch-Finally Blocks

Question: Is it valid to have a try block without a catch block?

Answer: Yes, if the try block is followed by a finally block. A try block must be followed by at least one catch block or one finally block or both. A standalone try block is illegal.

JDBC ResultSet Object

Question: What is the job of the ResultSet object in a JDBC connection?

Answer: ResultSet stores the records returned by a query, usually a SELECT query, and lets the programmer traverse them row by row using next() and fetch column values using methods like getInt(), getString(), and getFloat().