Core Java Interview questions for 4 years experienced

Sharing is caring

This is a question bank on Core Java interview questions for 4 years experienced candidates with best possible answers. These questions can help freshers as well as candidates having 2-3 years of experience going to appear in interview.

What do you mean by platform independent language?

A language which can be compiled on one system and run on any system irrespective of the Operating System being used. Eg : Java – it is platform independent because the byte code can be run on any system.

Is Java a pure object-oriented language? Why?

Java is not a pure object-oriented language because Java supports eight primitive types namely Boolean, byte, char, int, float, double, long, short.

What are the basic characteristics of a pure object oriented language?

A pure object oriented language supports or treats everything inside a program as object. The following are 7 qualities of a pure object oriented language

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction
  5. All predefined types of objects
  6. All user defined types are objects
  7. Operations performed on the object should be through method exposed at the object.

What are constructors in Java?

As the name suggests, constructors are block of code in a class which constructs or initializes an object. These block-of-code have same name as the class and are automatically called which an object of the class is created. They do not have any return type.

How many types of constructors are there?

There are two types of constructors.

  • Default Constructor: These are the no argument constructors of a class. They are created by default even when no constructor is defined by user. They are mostly used for object creation.
  • Parameterized constructor: The constructors which takes argument as input are called parameterized constructor. They are used to initialize object using the values passed as arguments.

What is the difference between equals() and == operator in Java?

The == operator does a reference comparison. So, it checks if the two variables refer to same memory location and not just having same values. The equals() method checks if the objects have same values. By default the equals() method of Object is used unless explicitly defined for an object.

Refer to the following snippet

String s1 = new String(“INDIA”);

       String s2 = new String(“INDIA”);

       System.out.println(s1 == s2);

What is the output of s1 ==  s2 and why?

Here the comparison will return false because s1 and s2 refers to 2 different objects created at 2 different memory locations. So == operator will return false.

Refer to the following snippet

String s3 = “INDIA”;

       String s4 = “INDIA”;

       System.out.println(s3 == s4);

What is the output of s3 ==  s4 and why?

This operation will return true. Here as the object INDIA was already present in String pool, so instead of creating a new object, s4 pointed to the same object and just the expression evaluates to true.

What are the uses of super keyword?

The super keyword is used as reference variable that is used to refer to the immediate parent class object. When subclass is created, first an instance of parent class is created

  • Using super, we can refer to instance variable of immediate parent class
  • Using super, we can call the method of immediate parent class
  • The method super() is used to call the constructor of the immediate parent class

What is singleton class?

Singleton class is a java class which can have ONE and ONLY ONE object created at a time. This is a part of singleton design pattern which restricts external object creation by making the constructor of a class private and having an instance creation method which always checks and return existing object before creating a new one.

public class HomeTeam {

private static HomeTeam homeTeam;

private HomeTeam() {

}

public static HomeTeam getInstance() {
    if(homeTeam ==  null) {
        homeTeam = new HomeTeam();
    }
    return homeTeam;
}

}

What is constructor chaining in Java?

Constructor chaining is the process of calling one constructor of a class from another constructor. Constructor chaining can be done in 2 ways:

  • Within same class: The this() keyword is used to call constructors in same class
  • From the base class: The super() keyword is used to call constructor of immediate parent class.

How creation of string using new differ from just assigning value to a String variable?

When we use new String(“MyValue”); then a new object is created in memory. But if we directly assign a string value to a String variable, then Java first checks String pool to check if that value is present. If it is present, then the same memory is reference by our new variable. If the value is not found in pool, then a new memory location is allocated for the value.

After going through these questions and answers, it is recommended to undergo the quiz which will help you gain confidence.


Sharing is caring

Leave a Reply

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