Java Interview Questions | 2 | -Interview of Geeks

Core java Interview Questions Interview Questions | 2

51-100 out of 300 Questions

There is the list of 300 core Java interview questions. If there is any core Java interview question that has been asked to you, kindly post it in the ask question section. We assure that you will get here the 90% frequently asked interview questions and answers.

The answers to the Core Java interview questions are short and to the point. The core Java interview questions are categorized in Basics of Java interview questions, OOPs interview questions, String Handling interview questions, Multithreading interview questions, collection interview questions, JDBC interview questions, etc.


Core Java - OOP's Concepts: Inheritance Questions

  1. 51. What is "this" keyword in java?

    The this keyword is a reference variable that refers to the current object. There are the various uses of this keyword in Java. It can be used to refer to current class properties such as instance methods, variable, constructors, etc. It can also be passed as an argument into the methods or constructors. It can also be returned from the method as the current class instance.

    This keyword diagram
  2. 52. What are the main uses of this keyword?

    There are the following uses of this keyword.

    • this can be used to refer to the current class instance variable.
    • this can be used to invoke current class method (implicitly)
    • this() can be used to invoke the current class constructor.
    • this can be passed as an argument in the method call.
    • this can be passed as an argument in the constructor call.
    • this can be used to return the current class instance from the method.

  3. 53. Can we assign the reference to this variable?

    No, this cannot be assigned to any value because it always points to the current class object and this is the final reference in Java. However, if we try to do so, the compiler error will be shown. Consider the following example.

    public class Test
    {
    public Test()
    {
    this = null;
    System.out.println("Test class constructor called");
    }
    public static void main (String args[])
    {
    Test t = new Test();
    }
    }

    Output

    Test.java:5: error: cannot assign a value to final variable this
    this = null;
    ^
    1 error

  4. 54. Can this keyword be used to refer static members?

    Yes, It is possible to use this keyword to refer static members because this is just a reference variable which refers to the current class object. However, as we know that, it is unnecessary to access static variables through objects, therefore, it is not the best practice to use this to refer static members. Consider the following example.

    public class Test
    {
    static int i = 10;
    public Test ()
    {
    System.out.println(this.i);
    }
    public static void main (String args[])
    {
    Test t = new Test();
    }
    }

    Output

    10
  5. 55. How can constructor chaining be done using this keyword?

    Constructor chaining enables us to call one constructor from another constructor of the class with respect to the current class object. We can use this keyword to perform constructor chaining within the same class. Consider the following example which illustrates how can we use this keyword to achieve constructor chaining.

    public class Employee
    {
    int id,age;
    String name, address;
    public Employee (int age)
    {
    this.age = age;
    }
    public Employee(int id, int age)
    {
    this(age);
    this.id = id;
    }
    public Employee(int id, int age, String name, String address)
    {
    this(id, age);
    this.name = name;
    this.address = address;
    }
    public static void main (String args[])
    {
    Employee emp = new Employee(105, 22, "Vikas", "Delhi");
    System.out.println("ID: "+emp.id+" Name:"+emp.name+" age:"+emp.age+" address: "+emp.address);
    }

    }

    Output

    ID: 105 Name:Vikas age:22 address: Delhi
  6. 56. What are the advantages of passing this into a method instead of the current class object itself?

    As we know, that this refers to the current class object, therefore, it must be similar to the current class object. However, there can be two main advantages of passing this into a method instead of the current class object.


    1. this is a final variable. Therefore, this cannot be assigned to any new value whereas the current class object might not be final and can be changed.
    2. this can be used in the synchronized block.

  7. 57. What is the Inheritance?

    Inheritance is a mechanism by which one object acquires all the properties and behavior of another object of another class. It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
    There are five types of inheritance in Java.

    • Single-level inheritance
    • Multi-level inheritance
    • Multiple Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance

    Multiple inheritance is not supported in Java through class.


  8. 58. Why is Inheritance used in Java?

    There are various advantages of using inheritance in Java that is given below.

    • Inheritance provides code reusability. The derived class does not need to redefine the method of base class unless it needs to provide the specific implementation of the method.
    • Runtime polymorphism cannot be achieved without using inheritance.
    • We can simulate the inheritance of classes with the real-time objects which makes OOPs more realistic.
    • Inheritance provides data hiding. The base class can hide some data from the derived class by making it private.
    • Method overriding cannot be achieved without inheritance. By method overriding, we can give a specific implementation of some basic method contained by the base class.

  9. 59. Which class is the superclass for all the classes?

    The object class is the superclass of all other classes in Java.


  10. 60. Why is multiple inheritance not supported in java?

    To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.

    Since the compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have the same method or different, there will be a compile time error.

    class A{
    void msg(){System.out.println("Hello");}
    }
    class B{
    void msg(){System.out.println("Welcome");}
    }
    class C extends A,B{//suppose if it were

    Public Static void main(String args[]){
    C obj=new C();
    obj.msg();//Now which msg() method would be invoked?
    }
    }

    Output

    Compile Time Error
  11. 61. What is aggregation?

    Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as a has-a relationship. For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City, State, and pin-code. In other words, we can say that Employee (class) has an object of Address class. Consider the following example.


    Address.java

    public class Address {
    String city,state,country;
    public Address(String city, String state, String country) {
    this.city = city;
    this.state = state;
    this.country = country;
    }

    }

    Employee.java

    public class Emp {
    int id;
    String name;
    Address address;

    public Emp(int id, String name,Address address) {
    this.id = id;
    this.name = name;
    this.address=address;
    }

    void display(){
    System.out.println(id+" "+name);
    System.out.println(address.city+" "+address.state+" "+address.country);
    }

    public static void main(String[] args) {
    Address address1=new Address("gzb","UP","india");
    Address address2=new Address("gno","UP","india");

    Emp e=new Emp(111,"varun",address1);
    Emp e2=new Emp(112,"arun",address2);

    e.display();
    e2.display();

    }
    }

    Output

    111 varun
    gzb UP india
    112 arun
    gno UP india

  12. 62. What is composition?

    Holding the reference of a class within some other class is known as composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition. In other words, we can say that composition is the particular case of aggregation which represents a stronger relationship between two objects. Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.


  13. 63. What is the difference between aggregation and composition?

    Aggregation represents the weak relationship whereas composition represents the strong relationship. For example, the bike has an indicator (aggregation), but the bike has an engine (composition).


  14. 64. Why does Java not support pointers?

    The pointer is a variable that refers to the memory address. They are not used in Java because they are unsafe(unsecured) and complex to understand.


  15. 65. What is super in java?

    The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by super reference variable. The super() is called in the class constructor implicitly by the compiler if there is no super or this.

    class Animal{
    Animal(){System.out.println("animal is created");}
    }
    class Dog extends Animal{
    Dog(){
    System.out.println("dog is created");
    }
    }
    class TestSuper4{
    public static void main(String args[]){
    Dog d=new Dog();
    }
    }

    Output

    animal is created
    dog is created

  16. 66. How can constructor chaining be done by using the super keyword?
    class Person
    {
    String name,address;
    int age;
    public Person(int age, String name, String address)
    {
    this.age = age;
    this.name = name;
    this.address = address;
    }
    }
    class Employee extends Person
    {
    float salary;
    public Employee(int age, String name, String address, float salary)
    {
    super(age,name,address);
    this.salary = salary;
    }
    }
    public class Test
    {
    public static void main (String args[])
    {
    Employee e = new Employee(22, "Mukesh", "Delhi", 90000);
    System.out.println("Name: "+e.name+" Salary: "+e.salary+" Age: "+e.age+" Address: "+e.address);
    }
    }

    Output

    Name: Mukesh Salary: 90000.0 Age: 22 Address: Delhi
  17. 67. What are the main uses of the super keyword?

    There are the following uses of super keyword.

    • super can be used to refer to the immediate parent class instance variable.
    • super can be used to invoke the immediate parent class method.
    • super() can be used to invoke immediate parent class constructor.

  18. 68. What are the differences between this and super keyword?

    There are the following differences between this and super keyword.

    • The super keyword always points to the parent class contexts whereas this keyword always points to the current class context.
    • The super keyword is primarily used for initializing the base class variables within the derived class constructor whereas this keyword primarily used to differentiate between local and instance variables when passed in the class constructor.
    • The super and this must be the first statement inside constructor otherwise the compiler will throw an error.

  19. 69. What is the output of the following Java program?
    class Person
    {
    public Person()
    {
    System.out.println("Person class constructor called");
    }
    }
    public class Employee extends Person
    {
    public Employee()
    {
    System.out.println("Employee class constructor called");
    }
    public static void main (String args[])
    {
    Employee e = new Employee();
    }
    }

    Output

    Person class constructor called
    Employee class constructor called

  20. 70. Can you use this() and super() both in a constructor?

    No, because this() and super() must be the first statement in the class constructor.

    public class Test{
    Test()
    {
    super();
    this();
    System.out.println("Test class object is created");
    }
    public static void main(String []args){
    Test t = new Test();
    }
    }

    Output

    Test.java:5: error: call to this must be first statement in constructor
  21. 71. What is object cloning?

    The object cloning is used to create the exact copy of an object. The clone() method of the Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.

    protected Object clone() throws CloneNotSupportedException
  22. Core Java - OOP's Concepts: Method Overloading Interview Questions

  23. 72. What is method overloading?

    Method overloading is the polymorphism technique which allows us to create multiple methods with the same name but different signature. We can achieve method overloading in two ways.

    • Changing the number of arguments
    • Changing the return type

    Method overloading increases the readability of the program. Method overloading is performed to figure out the program quickly.


  24. 73. Why is method overloading not possible by changing the return type in java?

    In Java, method overloading is not possible by changing the return type of the program due to avoid the ambiguity.

    class Adder{
    static int add(int a,int b){return a+b;}
    static double add(int a,int b){return a+b;}
    }
    class TestOverloading3{
    public static void main(String[] args){
    System.out.println(Adder.add(11,11));//ambiguity
    }}

    Output

    Compile Time Error: method add(int, int) is already defined in class Adder
  25. 74. Can we overload the methods by making them static?

    No, We cannot overload the methods by just applying the static keyword to them(number of parameters and types are the same). Consider the following example.

    public class Animal
    {
    void consume(int a)
    {
    System.out.println(a+" consumed!!");
    }
    static void consume(int a)
    {
    System.out.println("consumed static "+a);
    }
    public static void main (String args[])
    {
    Animal a = new Animal();
    a.consume(10);
    Animal.consume(20);
    }
    }

    Output

    Animal.java:7: error: method consume(int) is already defined in class Animal
    static void consume(int a)
    ^

    Animal.java:15: error: non-static method consume(int) cannot be referenced from a static context
    Animal.consume(20);
    ^

    2 errors

  26. 75. Can we overload the main() method?

    Yes, we can have any number of main methods in a Java program by using method overloading.


  27. 76. What is method overloading with type promotion?

    By Type promotion is method overloading, we mean that one data type can be promoted to another implicitly if no exact matching is found .


    As displayed in the above diagram, the byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int, long, float or double and so on. Consider the following example.

    class OverloadingCalculation1{
    void sum(int a,long b){System.out.println(a+b);}
    void sum(int a,int b,int c){System.out.println(a+b+c);}

    public static void main(String args[]){
    OverloadingCalculation1 obj=new OverloadingCalculation1();
    obj.sum(20,20);//now second int literal will be promoted to long
    obj.sum(20,20,20);
    }
    }

    Output

    40
    60

  28. 77. What is the output of the following Java program?
    class OverloadingCalculation3{
    void sum(int a,long b){System.out.println("a method invoked");}
    void sum(long a,int b){System.out.println("b method invoked");}

    public static void main(String args[]){
    OverloadingCalculation3 obj=new OverloadingCalculation3();
    obj.sum(20,20);//now ambiguity
    }
    }

    Output

    OverloadingCalculation3.java:7: error: reference to sum is
    obj.sum(20,20);//now ambiguity
    ^
    both method sum(int,long) in OverloadingCalculation3
    and method sum(long,int) in OverloadingCalculation3 match
    1 error

  29. Core Java - OOP's Concepts: Method Overriding Interview Questions

  30. 78. What is method overriding:

    If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to implement the interface methods.

    • The method must have the same name as in the parent class.
    • The method must have the same signature as in the parent class.
    • Two classes must have an IS-A relationship between them.

  31. 79. Can we override the static method?

    No, you can't override the static method because they are the part of the class, not the object.


  32. 80. Why can we not override static method?

    It is because the static method is the part of the class, and it is bound with class whereas instance method is bound with the object, and static gets memory in class area, and instance gets memory in a heap.


  33. 81. Can we override the overloaded method?

    Yes.


  34. 82. Difference between method Overloading and Overriding.
    Method Overloading Method Overriding
    1) Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its superclass.
    2) Method overloading occurs within the class. Method overriding occurs in two classes that have IS-A relationship between them.
    3) In this case, the parameters must be different. In this case, the parameters must be the same.

  35. 83. Can we override the private methods?

    No, we cannot override the private methods because the scope of private methods is limited to the class and we cannot access them outside of the class.


  36. 84. Can we change the scope of the overridden method in the subclass?

    Yes, we can change the scope of the overridden method in the subclass. However, we must notice that we cannot decrease the accessibility of the method. The following point must be taken care of while changing the accessibility of the method.

    • The private can be changed to protected, public, or default.
    • The protected can be changed to public or default.
    • The default can be changed to public.
    • The public will always remain public.

  37. 85.Can we modify the throws clause of the superclass method while overriding it in the subclass?

    Yes, we can modify the throws clause of the superclass method while overriding it in the subclass. However, there are some rules which are to be followed while overriding in case of exception handling.

    • If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception, but it can declare the unchecked exception.
    • If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

  38. 86. What is the output of the following Java program?
    class Base
    {
    void method(int a)
    {
    System.out.println("Base class method called with integer a = "+a);
    }

    void method(double d)
    {
    System.out.println("Base class method called with double d ="+d);
    }
    }

    class Derived extends Base
    {
    @Override
    void method(double d)
    {
    System.out.println("Derived class method called with double d ="+d);
    }
    }

    public class Main
    {
    public static void main(String[] args)
    {
    new Derived().method(10);
    }
    }

    The output of the following program is:

    Base class method called with integer a = 10
  39. 87. Can you have virtual functions in Java?

    Yes, all functions in Java are virtual by default.


  40. 88. What is covariant return type?

    Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type. The covariant return type specifies that the return type may vary in the same direction as the subclass.

    class A{
    A get(){return this;}
    }

    class B1 extends A{
    B1 get(){return this;}
    void message(){System.out.println("welcome to covariant return type");}
    public static void main(String args[]){
    new B1().get().message();
    }
    }

    Output

    Output: welcome to covariant return type
  41. 89. What is the output of the following Java program?
    class Base
    {
    public void baseMethod()
    {
    System.out.println("BaseMethod called ...");
    }
    }
    class Derived extends Base
    {
    public void baseMethod()
    { System.out.println("Derived method called ...");
    }
    }
    public class Test
    {
    public static void main (String args[])
    {
    Base b = new Derived();
    b.baseMethod();
    }
    }

    Output

    Derived method called ...
  42. Core Java - OOP's Concepts: Final keyword Interview Questions

  43. 90. What is the final variable?

    In Java, the final variable is used to restrict the user from updating it. If we initialize the final variable, we can't change its value. In other words, we can say that the final variable once assigned to a value, can never be changed after that. The final variable which is not assigned to any value can only be assigned through the class constructor.


    class Bike9{
    final int speedlimit=90;//final variable
    void run(){
    speedlimit=400;
    }
    public static void main(String args[]){
    Bike9 obj=new Bike9();
    obj.run();
    }
    }//end of class

    Output

    Compile Time Error
  44. 91. What is the final method?

    If we change any method to a final method, we can't override it.

    class Bike{
    final void run(){System.out.println("running");}
    }

    class Honda extends Bike{
    void run(){System.out.println("running safely with 100kmph");}

    public static void main(String args[]){
    Honda honda= new Honda();
    honda.run();
    }
    }

    Output

    Compile Time Error
  45. 92. What is the final class?

    If we make any class final, we can't inherit it into any of the subclasses.

    final class Bike{}

    class Honda1 extends Bike{
    void run(){System.out.println("running safely with 100kmph");}

    public static void main(String args[]){
    Honda1 honda= new Honda1();
    honda.run();
    }
    }

    Output

    Compile Time Error
  46. 93. What is the final blank variable?

    A final variable, not initialized at the time of declaration, is known as the final blank variable. We can't initialize the final blank variable directly. Instead, we have to initialize it by using the class constructor. It is useful in the case when the user has some data which must not be changed by others, for example, PAN Number. Consider the following example:

    class Student{
    int id;
    String name;
    final String PAN_CARD_NUMBER;
    ...
    }

  47. 94. Can we initialize the final blank variable?

    Yes, if it is not static, we can initialize it in the constructor. If it is static blank final variable, it can be initialized only in the static block.


  48. 95. Can you declare the main method as final?

    Yes, We can declare the main method as public static final void main(String[] args){}.


  49. 96. What is the output of the following Java program?
    class Main {
    public static void main(String args[]){
    final int i;
    i = 20;
    System.out.println(i);
    }
    }

    Output

    20
  50. 97. What is the output of the following Java program?
    class Base
    {
    protected final void getInfo()
    {
    System.out.println("method of Base class");
    }
    }

    public class Derived extends Base
    {
    protected final void getInfo()
    { System.out.println("method of Derived class");
    }
    public static void main(String[] args)
    {
    Base obj = new Base();
    obj.getInfo();
    }
    }

    Output

    Derived.java:11: error: getInfo() in Derived cannot override getInfo() in Base
    protected final void getInfo()
    ^
    overridden method is final
    1 error

  51. 98. Can we declare a constructor as final?

    The constructor can never be declared as final because it is never inherited. Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final. However, if you try to do so, The compiler will throw an error.


  52. 99. Can we declare an interface as final?

    No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition. Therefore, there is no sense to make an interface final. However, if you try to do so, the compiler will show an error.


  53. 100. What is the difference between the final method and abstract method?

    The main difference between the final method and abstract method is that the abstract method cannot be final as we need to override them in the subclass to give its definition.



Last Updated:


Trending Technologies Interview Questions


@InterviewofGeeks, Some rights reserved.