Polymorphism

Q: What will be output by the code?
class A {
    @Override
    public String toString() {
        return "1";
    }
}

class B extends A {
    public B() {
        super();
    }
}

class C extends B {
    public C() {
        super();
    }
}

public class Main {
    public static void main(String[] args) {
        B obj1 = new B();
        A obj2 = new A();
        C obj3 = new C();
        System.out.println(obj1.toString() + " " + obj2.toString() + " " + obj3.toString());
    }
}
A: 1 1 1
Q: From the below snippet, is it possible to overload the main() method?
public class Main{
	public static void main(String[] args){
		System.out.println("main(String[] args)");
	}
 
	public static void main(String args1){
		System.out.println("main(String arg1)");
	}
 
	public static void main(String arg1, String arg2){
		System.out.println("main(String arg1, String arg2)");
	}
}
A: Yes
Q: Which method of base class X, the derived class Y cannot override?
class X {
	
	final public void m1() {

	}

	public void m2() {

	}

}
class Y extends X {

	// override?
}
A: m1()
Q: What is the output of the code?
class A {
    protected int i;

    public A() {
        multiply(15);
        System.out.println(i);
    }

    public void multiply(int i) {
        this.i = 5 * i;
    }
}

class B extends A {
    public B() {
        super();
    }

    public void multiply(int i) {
        this.i = 3 * i;
    }
}

public class Main {
    public static void main(String[] args) {
        B obj = new B();
    }
}
A: 45
Q: What will be the output of the following Java code?
class A {
    protected int i;

    public A() {
        multiply(15);
    }

    public void multiply(int i) {
        this.i = 4 * i;
    }
}

class B extends A {
    public B() {
        super();
        System.out.println(i);
    }

    public void multiply(int i) {
        this.i = 2 * i;
    }
}

public class Main {
    public static void main(String[] args) {
        B obj = new B();
    }
}
A: 30
Q: Which of the following is not true about method overloading in polymorphism?
A: Same method signature which is of type static and same number of parameters but of different type
Q: What will be the output of the following program?
class overload {
    int x;
    double y;
    void add(int a , int b) {
        x = a + b;
    }
    void add(double c , double d) {
        y = c + d;
    }
    overload() {
        this.x = 0;
        this.y = 0;
    }        
}    
class Overload_methods {
    public static void main(String args[]) {
        overload obj = new overload();   
        int a = 2;
        double b = 3.2;
        obj.add(a, a);
        obj.add(b, b);
        System.out.println(obj.x + " " + obj.y);     
    }
}
A: 4 6.4
Q: Which of the following is a correct implementation of method overloading?
A: void methodA(){} void methodA(int a){}
Q: In order for the following code to be correct, what must be the type of the reference variable card?
_________ card;

card = new Valentine( "Joe", 14 ) ;
card.greeting();

card = new Holiday( "Bob" ) ; 
card.greeting();

card = new Birthday( "Emily", 12 ) ; 
card.greeting();
A: Card
Q: What is the output of the following program?
class Test 
{ 
    void myMethod() 
    { 
        System.out.println("Test"); 
    } 
} 
public class Derived extends Test 
{ 
    void myMethod() 
    { 
        System.out.println("Derived"); 
    } 
      
    public static void main(String[] args) 
    { 
        Derived object = new Test(); 
        object.myMethod(); 
    } 
} 
A: Compilation error
Q: What will be the output for the code?
class Demo {
    protected int x;

    public Demo() {
        this.x = 1;
    }

    public void change() {
        this.x = 10;
    }
}

class DemoDerived extends Demo {
    public void change() {
        this.x = this.x + 1;
    }
}

public class Main {
    public static void main(String[] args) {
        DemoDerived obj = new DemoDerived();
        obj.change();
        System.out.println(obj.x);
    }
}
A: 2
Q: Find the output of the below program:
class A {
  void f1 () {
    System.out.print("class-A ");
  }
}
class B extends A {
  void f1 () {
    System.out.print("Class-B ");
  }
} 
 
class Main {
  public static void main (String s[]) {
    A refA = new A();
    A refB = new B();
 
    refA.f1();
    refB.f1();
  }
}
A: class-A class-B
Q: Which of the following is a type of polymorphism in Java programming?
A: Compile time polymorphism
Q: What is the output of the following program?
class Test1 
{ 
    protected void getData() 
    { 
        System.out.println("Inside Test1"); 
    } 
} 
class Test2 extends Test1
{ 
    protected void getData() 
    { 
        System.out.println("Inside Test2"); 
    } 
} 
  
public class Test 
{ 
    public static void main(String[] args) 
    { 
        Test1 obj = new Test2(); 
        obj.getData(); 
    } 
} 
A: Inside Test2
Q: In the below Java code, whose "car" will be called?
class Father {

	public void car() {
		System.out.println("Father's Car");
	}
}

class Son extends Father {

	public void car() {
		System.out.println("Son's Car");
	}
}

public class Sample {

	public static void main(String[] args) {

		Son john = new Son();
		john.car();
    }
}
A: Son’s Car