Frequently Asked Java Interview Questions and Answers

  • By Pooja Bhavsar
  • January 27, 2024
  • JAVA Programming
Frequently Asked Java Interview Questions and Answers

Frequently Asked Java Interview Questions and Answers

Prepare for your Java job interview with confidence using our comprehensive list of frequently asked Java interview questions and answers.

 

Q. 1 Difference between super and this keyword

this keyword calls CURRENT class members(e.g means it used to call one method/constructor inside another method/constructor,but both should be defined inside SAME classes)

this keyword =>  is used to call variable, method, and constructor within the current class

super keyword calls SUPER class members (e.g means it is used to call one  method/constructor inside another method/constructor, but both should be defined inside DIFFERENT classes)

Super keyword=> is used to call variable, method, constructor from super class to subclass

 

Q. 2 What is the difference between Class(concrete class), abstract class, and interface?

=> Nonabstract/concrete class having only concrete methods. Abstract class has abstract and concrete methods. Whereas, the interface is having ONLY ABSTRACT METHODS…..

 

Q. 3  Explain extend and implements keywords in Java.

=> Extends keyword is used in case of inheritance to inherit the properties of the parent class into the child class. Implements keyword is used to implement abstract methods of the interface into class. In short,

  1. if we have a class-to-the-class relationship we can use the extends keyword
  2. if we have a class-to-interface relationship we can use the implements keyword

iii. if we have an interface-to-interface relationship we can use the extends keyword

 

Q.4 Define setter and getter in Java? Why do we need to implement setters and getters in class?

=> Setters are the methods that are used to set values to the properties of an object and getters are used to get or read values of the properties. By using setters and getters we can set or get a single property of an object at a time. Setters and getters are also used to achieve encapsulation in programming.

 

Q.5 Explain different types of interfaces in Java?

=>  Types of interfaces :

  1. Marker  interface   => having zero abstract methods.

     e.g  Cloneable and serializable are the examples of Marker interfaces because they are not having any abstract methods.

ii.Single Abstract Method(SAM) or functional interface => having only one abstract method

   e.g ActionListener and Predicate are examples of functional interfaces because this are having exactly one abstract method like actionPerformed() and test() respectively

 For Free, Demo classes Call: 020-71173125

Registration Link: Java Classes in Pune!

 

Q.6. What is multiple inheritance in Java? Why we can’t achieve multiple inheritances in Java through classes?

=>  In Multiple inheritance we are having multiple parents having single child OR In multiple inheritance, a single child class can be able to Inherit properties of Multiple parent classes. But achieving multiple inheritances in java through classes creates an ambiguity problem to the compiler, How?

Let’s discuss the following code where we are going to achieve multiple inheritance through classes.

Class  Dept_Computer

{

   void display()

   {

   }

}

Class  Dept_Electrical

{

   void display()

   {

   }

class Students extends  Dept_Computer,  Dept_Electrical

{

}

public class MultipleInheritance

{

     public static void main(String args[])

     {

         Student s=new Student();

            s.display();

     }

}

In the above code, both  Dept_Computer and Dept_Electrical have having same method like display() and class Student inhering both display() methods from the Dept_Computer, and Dept_Electrical class so in this case when we are calling the display() method using the object of Student class then compiler will not be able to decide which class display() method Student wants to call, means here it will create an ambiguity to the compiler. Hence, to prevent such an ambiguity problem multiple inheritance is not achievable in Java through the classes. But we can achieve multiple inheritance through interfaces in Java which is given below.

 

interface DeptComputer

{

void display();

}

 

interface DeptElectrical 

{

void display();

}

class Student implements DeptComputer ,DeptElectrical 

{

     public void display() 

    {

System.out.println(“implementing display method of interfaces like DeptComp,Dept_ele “);

   }

}

public class Multiple_inheritance {

public static void main(String[] args) {

// TODO Auto-generated method stub

 

Student s1=new Student();

s1.display();

}

}

 For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

7. Can we overload the main method in Java?

Yes, we can overload the main() method in Java but among all the overloaded formats of the main method only the main() will get executed which consists of (String args[]) as an argument/parameter.

Let’s consider the below example,

public class MainMethodOverloading {

public static void main(double[] args) 

{

      System.out.println(“hi”);

}

   

public static void main(String[] args) 

{

      System.out.println(“hello”);

}

 

public static void main(int[] args) 

{

      System.out.println(“bye”);

}

 

}

For the above program will get “hello” in the output.

Q.8 Can we declare a method as a final and abstract?

No, we cannot declare a method as final and abstract both in Java because the final method is not overridden by the child classes whereas we need to override or define it by the child class to provide implementation of abstract methods

Q.9 Why are the methods in interfaces defaultly defined as public?

Because interface just has a method declaration and not having method definition hence we need to define/implements the abstract methods of interfaces in another class, and that class might be defined in another package so from anywhere the interfaces must be implemented/accessible by the classes hence the abstract methods of interfaces are defined as public

Q.10 What if the static modifier is removed from the signature of the main method?

If we remove the “static” modifier from the main method so in that case the program will compile by the compiler properly but at the time of executing the program JVM will throw “NoSuchMethodError” .

Do watch the video on Java: Click Here 

Author:-

Pooja Bhavsar
Call the Trainer and Book your free demo class for JAVA now!!!

© Copyright 2020 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*