Skip to main content

Frequently asked interview questions and answeres for Java Engineers?

Most asked interview questions and answers for Java developers on Core Java

Core java interview questions and answers

1. Which are OOPs concept in Java?

  1. Class
  2. Object
  3. Abstraction
  4. Encapsulation
  5. Inheritance
  6. Polymorphism  

Learn more about above concept with example :

2. What contain Real-world object?

Real-worls object contains State and Behavior.

3. What is Wrapper class in Java?

Wrapper classes used for converting Primitive Data Type to Objects.

Sometimes, you already seen terms like, Integer.parseInt() or Double.parseDouble(). So this are wrapper classes for converting our Primitive types to Object.

4. What is Autoboxing in Java?

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, int to Integer, double to Double.

public class Demo {
    
    public static void main(String[] args) {
        int intPrimitive = 10;
        char charPrimitive = '2';

        // Done Autoboxing automatically from int to Integer
        Integer intObject = intPrimitive;
        System.out.println(intObject.getClass()+" : "+intObject);
        System.out.println("int to Character : "+Character.getNumericValue(charPrimitive));   
    }
}

Output :

class java.lang.Integer : 10
int to Character : 2

5. What is Unboxing in Java?

Unboxing is the inverse of Autoboxing. It is the process of converting a Wrapper class object into its corresponding Primitive Datatype. For example, an Integer object would be converted to a primitive data type int. 

public class Demo {
    
    public static void main(String[] args) {
        Integer wrapperInteger = 2;
        
        //Done Unboxing, from Integer to int
        int intPrimitive = wrapperInteger;
        System.out.println(intPrimitive);
    }
}

6. What is advantages of Wrapper classes in Java?

  • The primary advantage of Wrapper Classes is that we need Wrapper objects to function with collections which is only possible with the help of Wrapper classes.
  • Wrapper classes have objects we can store null as a value. We could not store null in variables of primitive datatype.

7. What is Constructor in Java?

A class contains constructors that are invoked to create objects from the class blueprint.

In simple term, Constructor is a block of code that initializes the newly created object.

Constructor declarations look like method declaration, except that they use the name of the class and have no return type.

8. List rules of Constructor.

  1. Constructor name should be same as Class name
  2. Constructor does not contains any return type. 

9. What happens when we does not define any constructor in java?

A default (no-argument) constructor is automatically created if we does not define any constructor in Java.

10. When we will get "The constructor is undefined" Error in Java?

If we write any constructor other than no-argument constructor, then compiler does not provided default no-arg constructor. And when we try to compile that code it gives "The constructor is undefined" error.

Refer following code.

public class Demo {
    
    private int intPrimitive;

    Demo(int var1) {
         this.intPrimitive = var1;
    }
    
    public static void main(String[] args) {
        
        // The line below will cause a compile time error.
        // Here we get "The constuctor Demo() is undefined" Error

        Demo obj = new Demo();
        System.out.println(obj.intPrimitive);
    }
}

If we define following no-argument constructor in above code, then above error resolve.

public Demo() {
}

11. What if you implement only parameterized constructor in class?  

It will throw a compilation error as we seen in above question. The reason is, the statement Demo obj = new Demo(); is invoking a default constructor which we don’t have in our above code.

12. Can we define more than 1 Constructor in Java?

Yes, we can define more than 1 Constructor in Java (with different arguments).

13. Can an abstract class have a constructor in Java?

Yes, an abstract class can have a constructor.

14. Can an Interface contains constructor in Java?

No, Interface does not contains constructor in Java

15. What is access specifier for Default constructor in Java?

Default constructor have Public access specifier.

16. Can we declare Private constructor in Java?

Yes, we can declare Private constructor in Java. And private constructor have scope only within class.

17. Can we overload Constructor? or Is Constructor overloading Possible in Java?

Yes, we overload constructor in Java. we can create multiple constructors with same name but with different parameters in class.

Learn more about Method Overloading in Java for understand Overloading concept.

18. Is Constructor overriding Possible in Java? 

No, Constructor overriding is not possible in Java 

Constructor looks like a method but name should be as class name and no return value.

Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding. Super class name and Sub class names are different.

Learn about Method overriding concept.

19. What is use of this keyword?

this’ is a reference variable that refers to the current object.

Most programmers use when there's an overlapping local variable with the same name. (for example, Setter methods).

20. What is super keyword?

The super keyword in Java is a reference variable which is used to refer immediate parent class object.

super() is added in each class constructor automatically by compiler if there is no super() or this().

We can use super keyword with following condition :-

  1. Variables - refer parent class instant variable
  2. Methods - refer parent class methods
  3. Constructors - refer parent class constructors

Learn deeply about super keyword in Java with Examples :

21. Can we use super() and this() both in constructor?

No, we can not. Because super() and this() must be at first line of constructor.

If we write both then, compiler gives following error.

    Constructor call must be the first statement in a constructor

22. What is the output of following code :

class Parent {
    
    void method() {
        System.out.println("Parent class method");
    }
}

class Child extends Parent {
    
    void method() {
        System.out.println("Child class method");
    }
}

class Demo {
    
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.method();
    }
}

Output :

Child class method

Explanation :

the method of Parent class, method() is overriden in Child class. So it is concept of runtime polymorphism. In Demo class we are creating object of Child class with Parent reference. So At runtime Child class method is called.

23. What is the output of following code :

class Parent {
    
    static void method() {
        System.out.println("Parent class method");
    }
}

class Child extends Parent {
    
    static void method() {
        System.out.println("Child class method");
    }
}

class Demo {
    
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.method();
    }
}

Output :

Parent class method

Explanation :

We are using static method and static method can not be overridden. So Parent class method is called.

Learn more about Overriding in Java :

24. Can we declare Constructor as final?

Short Answer :- No Java constructor can not be declared as final.

Long answer :- Constructors are not inherited, so can't be overridden and the final keyword restricts further modification so there is no point for restriction on constructor.

If we try to declare constructor as final, compiler gives error.

25. Can we declare interface as final?

Short Answer :- No, interface can not be declare as final in Java.

Long Answer :- Interface contains only abstract methods, So we need a class that implements interface and provides body to abstract methods. If we declare interface as final, we can not implements interface so interface can not be final in Java.

Learn more about Interface :

26. What is Garbage Collector in Java?

The garbage collector is a component of the Java virtual machine which is responsible for reclaiming memory from dead objects. It's one of the key components and allows an application developer to focus on application development rather than doing memory management. 

27. How can we call Garbage Collector (GC) explicitly in Java?

System.gc() 

OR

Runtime.getRuntime().gc() 

28. What is static binding in Java?

The binding which resolves at compile time (by compiler) known as Static binding in Java. Static binding also known as Early binding.

Binding of all the static, private and final methods is done at compile-time.

Method Overloading is best example of static binding.

Method overloading is determined at compile time. The compiler decides, based on the compile time type of the parameters passed to a method call, which method having the given name should be invoked.  

29. What is dynamic binding in Java?

When the binding can not done at compile time known as Dynamic binding.

Dynamic binding also known as late binding.

Method overriding is best example of dynamic binding.

Method overriding determined by the runtime type of an object. At runtime, the method that gets executed can be a method of some sub-class that wasn't even written when the code that makes the call was compiled. 

30. What is instanceOf in Java?

The instanceOf is used for compare instance type in Java. It return true or false.

String str = "Java";
//returns true
System.out.println(str instanceof String);  

31. Difference between == and equals() in Java?

  • == (double equals to) used for reference comparison.
  • equals() used for Value comparison.


Related Articles :

 

 

Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last