Java Multiple Choice Questions & Answers on Inheritance for Freshers

1. Which of these keyword must be used to inherit a class?

a) super
b) this
c) extent
d) extends
Answer: d
2. Which of these keywords is used to refer to member of base class from a sub class?

a) upper
b) super
c) this
d) None of the mentioned
Answer: b

Explanation: whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
3. A class member declared protected becomes member of subclass of which type?

a) public member
b) private member
c) protected member
d) static member
Answer: b

Explanation: A class member declared protected becomes private member of subclass.
4. Which of these is correct way of inheriting class A by class B?

a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}
Answer: c
5. Which two classes use the Shape class correctly? (Choose two.)
A. public class Circle implements Shape 
   {
    private int radius;
   }
B. public abstract class Circle extends Shape 
   {
    private int radius;
   }
C. public class Circle extends Shape 
   {
   private int radius;
   public void draw();
   }
D. public abstract class Circle implements Shape 
   {
    private int radius;
    public void draw();
   }
E. public class Circle extends Shape 
   {
    private int radius;
    public void draw()
    {
     /* code here */
    }
   }
F. public abstract class Circle implements Shape 
   {
     private int radius;
     public void draw() 
     { 
      /* code here */ 
     }
   }
a) B,E
b) A,C
c) C,E
d) T,H
Answer: a

Explanation: If one is extending any class, then they should use extends keyword not implements.
6. What is the output of this program?
  1.     class A {
  2.         int i;
  3.         void display() {
  4.             System.out.println(i);
  5.         }
  6.     }    
  7.     class B extends A {
  8.         int j;
  9.         void display() {
  10.             System.out.println(j);
  11.         }
  12.     }    
  13.     class inheritance_demo {
  14.         public static void main(String args[])
  15.         {
  16.             B obj = new B();
  17.             obj.i=1;
  18.             obj.j=2;   
  19.             obj.display();     
  20.         }
  21.    }
a) 0
b) 1
c) 2
d) Compilation Error
Answer: c

Explanation: class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
7. What is the output of this program?
  1.     class A {
  2.         int i;
  3.     }    
  4.     class B extends A {
  5.         int j;
  6.         void display() {
  7.             super.i = j + 1;
  8.             System.out.println(j + " " + i);
  9.         }
  10.     }    
  11.     class inheritance {
  12.         public static void main(String args[])
  13.         {
  14.             B obj = new B();
  15.             obj.i=1;
  16.             obj.j=2;   
  17.             obj.display();     
  18.         }
  19.    }
a) 2 2
b) 3 3
c) 2 3
d) 3 2
Answer: c
8. What is the output of this program?
  1.     class A {
  2.         public int i;
  3.         private int j;
  4.     }    
  5.     class B extends A {
  6.         void display() {
  7.             super.j = super.i + 1;
  8.             System.out.println(super.i + " " + super.j);
  9.         }
  10.     }    
  11.     class inheritance {
  12.         public static void main(String args[])
  13.         {
  14.             B obj = new B();
  15.             obj.i=1;
  16.             obj.j=2;   
  17.             obj.display();     
  18.         }
  19.    }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d

Explanation: class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
9. What is the output of this program?
  1.     class A {
  2.         public int i;
  3.         public int j;
  4.         A() {
  5.             i = 1;
  6.             j = 2;
  7.  }
  8.     }    
  9.     class B extends A {
  10.         int a;
  11.         B() {
  12.             super();
  13.         } 
  14.     }    
  15.     class super_use {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             System.out.println(obj.i + " " + obj.j)     
  20.         }
  21.    }
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
Answer: a

Explanation: Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.
10. What is the output of this program?
  1.     class A {
  2.         public int i;
  3.         protected int j;
  4.     }    
  5.     class B extends A {
  6.         int j;
  7.         void display() {
  8.             super.j = 3;
  9.             System.out.println(i + " " + j);
  10.         }
  11.     }    
  12.     class Output {
  13.         public static void main(String args[])
  14.         {
  15.             B obj = new B();
  16.             obj.i=1;
  17.             obj.j=2;   
  18.             obj.display();     
  19.         }
  20.    }
a) 1 2
b) 2 1
c) 1 3
d) 3 1
Answer: a

Explanation: Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.

Related

Multiple Choice Questions 513798708955456128

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item