Question 1 of 25
Hints left: 3
Q1
If super class and subclass have same variable name, which keyword should be used to use super class?
Q2
Using which of the following, multiple inheritance in Java can be implemented?
Q3
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(); } }
Q4
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); } }
Q5
Determine output: class A{ public void method1(){ System.out.print("Class A method1"); } } class B extends A{ public void method2(){ System.out.print("Class B method2"); } } class C extends B{ public void method2(){ System.out.print("Class C method2"); } public void method3(){ System.out.print("Class C method3"); } } public class Test{ public static void main(String args[]){ A a = new A(); C c = new C(); c.method2(); a = c; a.method3(); } }
Q6
All classes in Java are inherited from which class?
Q7
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(); } }
Q8
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(); } }
Q9
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); } }
Q10
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()); } }
Q11
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(); } }
Q12
Which of these method of Object class is used to obtain class of an object at run time?
Q13
What will be printed after executing following program code? class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; } } public class Test{ public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } }
Q14
If a class inheriting an abstract class does not define all of its function then it will be known as?
Q15
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(); } }
Q16
Which of these method of Object class can clone an object?
Q17
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(); } }
Q18
What will be the output? class Parent{ public void method(){ System.out.println("Hi i am parent"); } } public class Child extends Parent{ protected void method(){ System.out.println("Hi i am Child"); } public static void main(String args[]){ Child child = new Child(); child.method(); } }
Q19
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 */ } }
Q20
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(); } }
Q21
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); } }
Q22
A class member declared protected becomes a member of subclass of which type?
Q23
What will be the output? interface A{ public void method1(); } class One implements A{ public void method1(){ System.out.println("Class One method1"); } } class Two extends One{ public void method1(){ System.out.println("Class Two method1"); } } public class Test extends Two{ public static void main(String[] args){ A a = new Two(); a.method1(); } }
Q24
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 ?
Q25
What is the result of compiling and running this program? class Mammal{ void eat(Mammal m){ System.out.println("Mammal eats food"); } } class Cattle extends Mammal{ void eat(Cattle c){ System.out.println("Cattle eats hay"); } } class Horse extends Cattle{ void eat(Horse h){ System.out.println("Horse eats hay"); } } public class Test{ public static void main(String[] args){ Mammal h = new Horse(); Cattle c = new Horse(); c.eat(h); } }