Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java inheritance interview questions and answers

1
Which of the below statements are wrong in relation with multiple inheritance in java?
  1. an interface can extend multiple interfaces directly.
  2. a class can extend only one class directly
  3. multiple inheritance of interfaces is possible but multiple inheritance of classes is not possible directly.
  4. There is no solution to achieve multiple inheritance in java.
Show Answer
Share Facebook gmail Twitter
2
What is a default constructor in java?
  1. Any constructor that doesn't have parameters is called as default constructor.
  2. If a class doesn't have any constructor provided by programmer, then java compiler will add a default constructor with out parameters which will call super class constructor internally with super() call. This is called as default constructor.
  3. Java compiler will add a default constructor with out parameters which will call super class constructor internally with super() call. This will be added to all the classes no matter whether it is already having constructor or not. This is called as default constructor.
  4. Java compiler will add a default constructor with out parameters which will not call super class constructor internally with super() call. This will be added to all the classes no matter whether it is already having constructor or not. This is called as default constructor.
Show Answer
Share Facebook gmail Twitter
3
Why does java adds default constructor?
  1. It is the rule of sun java, no specific reason.
  2. Because at least one constructor is required to create object.
  3. Because your class has to call super class constructor to make sure super class is initializing all its variables. It is not the constructor who creates object, rather it is new operator.
  4. you can't create object with out constructor.
Show Answer
Share Facebook gmail Twitter
4
Example for default constructors in java:
what is the output of below Java program?
class Base{
        public Base(){
                System.out.println("Base class constructor");
        }
        public void disp(){
                System.out.println("Base class display");
        }
}
class Der extends Base{
        
}
public class ConstructorDemo {
        public static void main(String[] args) {
                Der d = new Der();
                d.disp();
        }
}
  1. it gives compile time error because there is no constructor in derived class to create object for derived class.
  2. Base class display
  3. Base class constructor
    Base class display
  4. it gives compile time error because base class has constructor which we are supposed to call from derived class. but derived class is not having any constructor.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
8
what is the output of below Java program?
class Dot{
        int x,y;
        public Dot(int i, int j){
                x = i;
                y = j;
        }
}
class Rect extends Dot{
        int len, bred;
        public Rect(int side){
                this(side, side);
        }
        public Rect(int l, int b){
                super(0,0);
                len = l;
                bred = b;
        }
        public void findArea(){
                System.out.println(len*bred);
        }
}
public class ConstructorDemo {
        public static void main(String[] args) {
                Rect r = new Rect(5);
                r.findArea();
        }
}
  1. 25
  2. 0
  3. compile time error because we are calling Rect(5) with one parameter, from Rect(int side) we are not calling super(i,j) with 2 parameters which is required. so it generates compile time error.
  4. run time error because we are calling Rect(5) with one parameter, from Rect(int side) we are not calling super(i,j) with 2 parameters which is required. so it generates compile time error.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
10
Which of the below options are wrong about overriding?
  1. you can't override private methods of base class
  2. if you have same static method with same name, same signature in base and derived class it is also called as overriding.
  3. you can't override final methods of base class.
  4. to tell a function is overriding or not, it should have same signature and return type as in base class.
Show Answer
Share Facebook gmail Twitter
11
Is it possible to override a constructor?
  1. you can override constructor of base class in derived class
  2. you can't override base class constructor in derived class.
  3. neither you can over ride nor you can call super class constructor in sub class.
  4. you can't override base class constructors only if it is private.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
13
Is it possible to inherit and override final methods?
  1. final methods will not be inherited.
  2. final methods will be inherited but you can't override in derived class.
  3. final methods will be inherited only if it is not private and derived class is in the same package. but you can't override it.
  4. final methods will be inherited and can be overridden also.
Show Answer
Share Facebook gmail Twitter
14
Is it possible to inherit and override static methods?
  1. static methods will not be inherited.
  2. static methods will be inherited if it is not private and derived class is in same package. and also you can override static methods of base class in derived class.
  3. static methods will be inherited if it is not private and derived class is in same package. you can't override static methods but you can hide static methods of base class in derived class.
  4. static methods will be inherited but you can neither override nor hide them.
Show Answer
Share Facebook gmail Twitter
15
what type of variables will be inherited to derived classes?
  1. all non private variables(both instance variables and static variables) of base class will be inherited to derived class (of same package).
  2. only instance variables (which are not private) will be inherited to derived class (of same package).
  3. option 1 is true, but final variables won't get inherited.
  4. option 2 is true, but final variables won't get inherited.
Show Answer
Share Facebook gmail Twitter
16
Is it possible to have variables with same names in base class and derived class? If so, is it overriding or hiding?
  1. you can only override base class variables in derived class (by having same variable name in base and derived class)
  2. you can only hide base class variables in derived class (by having same variable name in base and derived class)
  3. both overriding and hiding is applicable for base class variables in derived class (overriding - if it is instance variable, hiding - if it is static variable)
  4. you can neither override nor hide the variables. you can't have same variable names in base class and derived class.
Show Answer
Share Facebook gmail Twitter
17
What is the purpose of clone() function, of Object class?
  1. It is empty, if some body wants, they have to provide their own implementation for the same.
  2. it provides the shallow copy implementation, but if you want deep copy, then you have to override and implement on your own.
  3. it provides both shallow copy and deep copy of an object into a new object.
  4. none
Show Answer
Share Facebook gmail Twitter
18
When clone function is called on an object, will it do shallow copy or deep copy?
  1. clone will create a new object, and copies all the elements of current object which called it. but in that object if you have any references which points to other dynamically created objects, then those objects won't be created for you, it just copies those references. This is called as shallow copy.
  2. clone will create a new object, and copies all the elements of current object which called it. in that object if you have any references which points to other dynamically created objects, then those objects will also be created for you. This is called as deep copy.
  3. who ever wants to implement deep copy, they don't have to call object's clone() method in their over ridden version.
  4. clone will not create a new object, it is the responsibility of programmer to provide a new object with sufficient space for it.
Show Answer
Share Facebook gmail Twitter
19
What does equals() function of Object class will do?
  1. It will check if the content is equal or not.
  2. it will check if address is equal or not
  3. it checks both address and content, if they are equal or not.
  4. Object class equals() function is empty, it doesn't have any implementation.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :