Vidyalelo
Java Programming · all questions

Inheritence
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

86

Questions

3/5

Page

Pick an option on a question to see the right answer and solution.

What will be the output?
class One{
      final int a = 15;
}

class Two extends One{
      final int a = 20;
}

public class Test extends Two{
      final int a = 30;

      public static void main(String args[]){
            Test t = new One();
            System.out.print(t.a);
      }
}

Select an option to see the answer and solution.

What will be the output?
class A{
      int i = 10;
      public void printValue(){
            System.out.print("Value-A");
      }
}

class B extends A{
      int i = 12;
      public void printValue(){
            System.out.print("Value-B");
      }
}

public class Test{
      public static void main(String args[]){
            A a = new B();
            a.printValue();
            System.out.print(a.i);
      }
}

Select an option to see the answer and solution.

What will be the result after compiling this code?
class SuperClass{
      public int doIt(String str, Integer... data)throws Exception{
            String signature = "(String, Integer[])";
            System.out.println(str + " " + signature);
            return 1;
      }
}

public class Test extends SuperClass{
      public int doIt(String str, Integer... data){
            String signature = "(String, Integer[])";
            System.out.println("Overridden: " + str + " " +signature);
            return 0;
      }

      public static void main(String... args){
            SuperClass sb = new Test();
            sb.doIt("hello", 3);
      }
}

Select an option to see the answer and solution.

class A{
      A(String s){}

      A(){}
}

class B extends A{
      B(){}
      B(String s){
            super(s);
      }
      void test(){
            // insert code here
      }
}

Which of the below code can be insert at line 7 to make clean compilation ?

Select an option to see the answer and solution.

Determine output:
class A{
      public void printValue(){
            System.out.println("Value-A");
      }
}
class B extends A{
      public void printNameB(){
            System.out.println("Name-B");
      }
}
class C extends A{
      public void printNameC(){
            System.out.println("Name-C");
      }
}

public class Test{
      public static void main (String[] args){
            B b = new B();
            C c = new C();
            newPrint(b);
            newPrint(c);
      }
      public static void newPrint(A a){
            a.printValue();
      }
 }

Select an option to see the answer and solution.

Determine output:
class A{
      public void printName(){
            System.out.println("Name-A");
      }
}
class B extends A{
      public void printName(){
            System.out.println("Name-B");
      }
}
class C extends A{
      public void printName(){
            System.out.println("Name-C");
      }
}

public class Test{
      public static void main (String[] args){
           B b = new B();
           C c = new C();
            b = c;
            newPrint(b);
      }
      public static void newPrint(A a){
            a.printName();
      }
}

Select an option to see the answer and solution.

What is the output for the below code?
class A{
      private void printName(){
            System.out.println("Value-A");
      }
}
class B extends A{
      public void printName(){
            System.out.println("Name-B");
      }
}
public class Test{
      public static void main (String[] args){
            B b = new B();
            b.printName();
      }
}

Select an option to see the answer and solution.

What will be the result of compiling and running the given code?
class A{
      int b=10;
      private A(){
            this.b=7;
      }
      int f(){
            return b;
      }
}
class B extends A{
      int b;
}
public class Test{
      public static void main(String[] args){
            A a = new B();
            System.out.println(a.f());
      }
}

Select an option to see the answer and solution.

What will be the result of compiling and executing the following program code?
class Vehicle{
      public void printSound(){
            System.out.print("vehicle");
      }
}
class Car extends Vehicle{
      public void printSound(){
            System.out.print("car");
      }
}
class Bike extends Vehicle{
      public void printSound(){
            System.out.print("bike");
      }
}
public class Test{
      public static void main(String[] args){
            Vehicle v = new Car();
            Bike b = (Bike) v;
        
            v.printSound();
            b.printSound();
      }
}

Select an option to see the answer and solution.

Determine output:
class Small{
      public Small(){
            System.out.print("a ");
      }
}
class Small2  extends Small{
      public Small2(){
            System.out.print("b ");
      }
}
class Small3 extends Small2{
      public Small3(){
            System.out.print("c ");
      }
}
public class Test{     
      public static void main(String args[]){
            new Small3();
      }
}

Select an option to see the answer and solution.

Using which of the following, multiple inheritance in Java can be implemented?

Select an option to see the answer and solution.

Which two classes use the Shape class correctly?
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 */ 
     }
   }

Select an option to see the answer and solution.

Which of these method of Object class is used to obtain class of an object at run time?

Select an option to see the answer and solution.

If a class inheriting an abstract class does not define all of its function then it will be known as?

Select an option to see the answer and solution.

If super class and subclass have same variable name, which keyword should be used to use super class?

Select an option to see the answer and solution.

A class member declared protected becomes a member of subclass of which type?

Select an option to see the answer and solution.

Which of these method of Object class can clone an object?

Select an option to see the answer and solution.

All classes in Java are inherited from which class?

Select an option to see the answer and solution.

What will be the output of the following Java program?
abstract class A 
{
    int i;
    abstract void display();
}    
class B extends A 
{
    int j;
    void display() 
    {
        System.out.println(j);
    }
}    
class Abstract_demo 
{
    public static void main(String args[])
    {
        B obj = new B();
        obj.j=2;
        obj.display();    
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class A 
{
    int i;
    void display() 
    {
        System.out.println(i);
    }
}    
class B extends A 
{
    int j;
    void display() 
    {
        System.out.println(j);
    }
}    
class method_overriding 
{
    public static void main(String args[])
    {
        B obj = new B();
        obj.i=1;
        obj.j=2;   
        obj.display();     
    }
}

Select an option to see the answer and solution.