JavaQuizzes

Quiz Guidelines

 

Multiple Choice Question

QID: 
492

What will the following code print when run?

class A {
}
class AA extends A { 
}
public class TestClass {
    public static void main(String[] args) throws Exception {
        A a = new A();
        AA aa = new AA();
        a = aa;
        System.out.println("a = "+a.getClass());
        System.out.println("aa = "+aa.getClass());
    }
}

Select 1 option

A. It will not compile.
B. It will throw ClassCastException at runtime. 
C. a = class AA
aa = class AA 
D. a = class A
aa = class AA

Multiple Choice Question

QID: 
493

Which of the following are valid classes?

Select 1 option

A. public class ImaginaryNumber extends Number {
}
B. public class ThreeWayBoolean extends Boolean {
}
C. public class NewSystem extends System {
}
D. public class ReverseString extends String {
}

Multiple Choice Question

QID: 
494

What will be printed when the following code is compiled and run?

class A {
public int getCode(){ return 2;}
}
class AA extends A {
public long getCode(){ return 3;}
} public class TestClass {
public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
} public int getCode() {
return 1;
}  }

Select 1 option

A. 2 3
B. 2 2
C. It will throw an exception at run time.
D. The code will not compile.

Multiple Choice Question

QID: 
495

Consider the following code in TestClass.java file:

package p;
private class TC extends java.util.HashMap{
public TC(){
super(100);
System.out.println("TC created");
}
}public class TestClass extends TC{
public TestClass(){
System.out.println("TestClass created");
}
public static void main(String[] args){ new TestClass(); }
}

What will be the output when TestClass is run?

Select 1 option

A. "TestClass created" as well as "TC created".
B. It will not compile because HashMap is a final class.
C. Only "TestClass created" will be printed.
D. Only "TC created" will be printed.
E. None of the above are correct.

Multiple Choice Question

QID: 
496

Which of the following access control keywords can be used to enable all the subclasses to access a method defined in the base class?

Select 2 options 

A. public 
B. private 
C. protected 
D. No keyword is needed. 

Pages