1. If class B is run :
public class A {
public static void m1() {
System.out.println("in m1() of A");
}
}
public class B extends A {
public static void main (String [] args) {
B b = new B();
b.m1();
}
public static void m1() {
System.out.println("in m1() of B");
}
}
a) Gives output: in m1() of B b) Compile error, cannot override static method c) Gives output: in m1() of A, followed by in m1() of B d) Gives output: in m1() of B, followed by in m1() of A
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Gives output: in m1() of B.
Correct! Gives output: in m1() of B.
2. What is the ouput when Test is run?
public abstract interface MyInt {
void m1() ;
}
public class Test implements MyInt{
public Test() {
super();
}
public static void main(String [] args) {
new Test().m1();
}
public void m1() {
System.out.println("In m1()");
}
}
a) m1() should be public in MyInt b) Wrong usage of super() in Test c) Interface cannot be declared abstract d) Gives ouput: In m1()
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d)Gives ouput: In m1().
Correct! d)Gives ouput: In m1().
3. For following code :
public class Test {
public static void main(String [] args) {
try {
new Test().m1();
}
catch(Throwable e) {
System.out.println(e);
}
}
public void m1 () {
throw new RuntimeException("Error");
}
}
a) method should be : m1() throws RuntimeException b) Throwable should not be caught c) Gives output: java.lang.RuntimeException: Error b) RuntimeException cannot be thrown
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Gives output: java.lang.RuntimeException: Error
4. What is the output?
public class Test {
public Test () {
System.out.print("B");
}
static {
System.out.print("C");
}
public void printMe() {
System.out.print("D");
}
public static void main (String[] args) {
System.out.print("A");
Test t = new Test();
t.printMe();
}
}
a) ABCD b) ACBD c) CADB d) CABD
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) CABD. Static block is executed first
Correct! CABD. Static block is executed first
5. What is the output?
public class Test {
String name="John";
public Test() {
this(name);
}
public Test(String name) {
System.out.print(name);
}
public static void main(String[] arg)
{
Test t1 = new Test();
Test t2 = new Test(" Peter");
}
}
a) John Peter b) John c) Peter d) Compile Error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Compile Error. Cannot reference instance variable 'name' before supertype constructor has been called
Correct! Compile Error Cannot reference instance variable 'name' before supertype constructor has been called
6. If following is run as 'java MyClass hello' :
abstract class MyClass {
public static void main (String.. args) {
System.out.println("MyClass, " + args[0]);
}
}
a) Gives output : MyClass, hello b) Cannot run an abstract class c) Cannot use String... d) Class has to be public
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a)Gives output : MyClass, hello
Correct! Gives output : MyClass, hello
7. For the following :
1. public class MyImp {
2. public void printMe() {
3. System.out.print("printMe ");
4. }
5. public class InsideOne {
6. public void getMe() {
7. System.out.print("getMe ");
8. }
9. }
10. public static class MyImpInner extends MyImp {
11. }
12. public static void main (String [] args) {
13. MyImp mi = new MyImp();
14. MyImp.InsideOne mio = mi.new InsideOne();
15. mio.getMe();
16. MyImp.MyImpInner mii = new MyImp.MyImpInner();
17. mii.printMe();
18. }
19. }
a) Compile error at line
10 b) Compile error at line
14 c) Gives output: getMe printMe d) Compile error at line 16
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Gives output: getMe printMe