General Knowledge Quiz on Java Inheritance

1) In Java, a class can extend multiple other classes.
a) True
b) False
c) Sometimes
d) None of the above

2) What will be the output of this program?
class A
{
    int  b = 50;
}
 
class B extends A
{
    int b = 20;
}
 
public class MainClass
{
    public static void main(String[] args)
    {
        A a = new B();
 
        System.out.println(a.b);
    }
}
a) Compiler error
b) 20
c) 50
d) None of the above

3) Can a class extend itself?.
a) Yes
b) No
c) Sometimes
d) None of the above

4) A sub class can inherit private members of the parent class.
a) False
b)Conditionally
c) True
d) None of the above

5) In Java, A subclass can, however, inherit the members of its superclass that are not private.
a) false
b) true - but only when the members are superlative.
c) true
d) Both b and c

6) A superclass method cannot be overridden in a subclass to declare an appropriate implementation for the subclass.
a) True
b) False
c) Depends on the implementation
d) None of the above

7) What is a superclass?
a) child class
b) derived class
c) declared class
d) parent class

8) What is a subclass in java?
a) child class
b) derived class
c) both a and b
d) parent class

9) The first task of any subclass constructor is to call its_________________________ either explic- itly or implicitly, to ensure that the instance variables inherited from the superclass are initialized properly
a) Init() method
b) Start() method
c) Garbage Collector
d) direct superclass’s constructor

10) Usage of java super Keyword
a) super is used to refer immediate parent class instance variable.
b) super() is used to invoke immediate parent class constructor.
c) super is used to invoke immediate parent class method.
d) all of the above
e) None of the above

11) A subclass can explicitly invoke a constructor of its superclass by using the superclass constructor call syntax __________________________
a) super()
b) super
c) superlative()
d) parent()

12) _____________ is a form of software reusability in which new classes acquire the members of existing classes and embellish those classes with new capabilities.
a) creating a subclass
b) inheritance
c) both a and b above
d) None of the above

13) A superclass’s ___________________ members can be accessed in the superclass declaration and in subclass declarations.
a) public
b) private
c) protected
d) a and c above

14) Can a subclass' member be overridden?
a) Yes
b) No
c) Only with a caveate in the superclass
d) None of the above

15) What restriction is there on using the super reference in a constructor?
a) It can only be used in the parent's constructor.
b) Only one child class can use it.
c) It must be used in the last statement of the subclass' constructor.
d) It must be used in the first statement of the subclass' constructor.

16) Does a subclass inherit both member variables and methods?
a) No—only member variables are inherited.
b) No—only methods are inherited.
c) Yes—both are inherited - but not those identified as private.
d) Yes—but only one or the other are inherited.

17) A class Car and its subclass Yugo both have a method run() as part of their class definition. What will the following code do?
Yugo junker = new Yugo() ;
junker.show();
a) The show() method defined in Yugo will be called.
b) The show() method defined in Car will be called.
c) The compiler will complain that run() has been defined twice.
d) Overloading will be used to pick which run() is called.

18) Can an object subclass another object?
a) Yes—as long as single inheritance is followed.
b) No—inheritance is only between classes.
c) Only when one has been defined in terms of the other.
d) Yes—when one object is used in the constructor of another.

19) To inherit a class, which of these keyword must be used ?
a) this
b) extent
c) super
d) expands
e) extends

20) Which of these keywords is used to refer to member of base class from a sub class?
a) this
b) super
c) upper
d) None of the above

21) A declared protected class member becomes member of which type in the subclass?
a) j static member
b) public member
c) private member
d) protected member

22) Which of these is correct way of inheriting class A by class B?
a) class B extends A {}
b) class B extends A {}
c) class B extends class A {}
d) class B + class A {}

23) Which of the following statements are false?
a) protected members of a class can be inherited by a sub class, and become private members of the sub class.
b) private members of class can only be accessed by other members of the class.
c) protected members of a class can be inherited by a sub class.
d) private members of class can be inherited by a sub class, and become protected members in sub class.

24) Can a constructor be inherited?
a) A constructor can be inherited
b) A public constructor can be inherited
c) A protected constructor can be inherited
d) A constructor cannot be inherited

25) What is Inheritance?
a) one object acquires all the behaviours of another object of another class.
b) one object acquires all the behaviour of another object of another class.
c) When one object acquires all the properties and behaviour of another object of another class.
d) one object acquires all the methods of another object of another class.

26) Which class can not be subclassed in java
a) end class
b) parent class
c) Object class
d) final class

27) Which class is the class every other class is derived from?
a) derivation class
b) parent class
c) Object class
d) Beginningclass

28) Can a subclass "hide" the superclass version of a method with the same name?
a) No
b) Yes if it is overloaded.
c) Yes if it is overriden
d) none of the above

29) Class B extends A. Both have a method with the same name but with different calling signatures The method is
a) overridden
b) overloaded
c) both of the above
d) none of the above

30) Given a parent class and a subclass, which of the following is true?
a) Because of single inheritance, the subclass can have no subclasses.
b) Because of single inheritance, the subclass can have only one parent.
c) Because of single inheritance, the subclass can have only one child classes.
d) Because of single inheritance, the subclass can have no siblings.

31) Given the following code, what is the output?
  
class Base {
	
    public void show() {
		
       System.out.println("Base show() called");
		
    }

}



class Derived extends Base {

        public void show() {
	
            System.out.println("Derived show() called");
	
       }

}



public class Main {
		
    public static void main(String[] args) {
		
        Base bb = new Derived();;
			
        bb.show();
		
    }

}
	
 
a) Base show() called
b) Main show() called
c) Derived show() called
d) None of the above

Inheritance Quiz script provided by
Total applications works



HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#