Welcome Guest | Sign in | Register

Exercise:

Section 1

01.  What will be the output of the program?
class Super{
public Integer getLength(){
return new Integer(4);
}
}

public class Sub extends Super{
public Long getLength(){
return new Long(5);
}

public static void main(String[] args){
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(
sooper.getLength().toString() + "," + sub.getLength().toString() );
}
}
A. 4, 4 B. 4, 5
C. 5, 4 D. Compilation fails.

Answer and Explanation

Answer: Compilation fails.

Explanation:
Option D is correct, compilation fails - The return type of getLength ( ) in the super class is an object of reference type Integer and the return type in the sub class is an object of reference type Long.
In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed.  

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What will be the output of the program?
import java.util.*;
public class NewTreeSet2 extends NewTreeSet{
public static void main(String [] args){
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet{
void count(){
for (int x = 0; x < 7; x++,x++ ){
System.out.print(" " + x);
}
}
}
A. 0 2 4 6 B. 0 2 4
C. Compilation fails at line 10 D. Compilation fails at line 2

Answer and Explanation

Answer: Compilation fails at line 10

Explanation:
Non-nested classes cannot be marked protected (or final for that matter), so the compiler will fail at protected class NewTreeSet.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. interface Base{
boolean m1 ( );
byte m2(short s);
}

Which of the two code fragments will compile?
1. interface Base2 implements Base {}
2. abstract class Class2 extends Base
{ public boolean m1(){ return true; }}
3. abstract class Class2 implements Base {}
4. abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }}
5. abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }} 
A. 1 and 2 B. 2 and 3
C. 3 and 4 D. 3 and 4

Answer and Explanation

Answer: 3 and 4

Explanation:
(3) is correct because an abstract class doesn't have to implement any or all of its interface's methods. (4) is correct because the method is correctly implemented ((7 > 4) is a boolean).
(1) is incorrect because interfaces don't implement anything. (2) is incorrect because classes don't extend interfaces. (5) is incorrect because interface methods are implicitly public, so the methods being implemented must be public.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Which three form part of correct array declarations?
1. public int a [ ]
2. static int [ ] a
3. public [ ] int a
4. private int a [3]
5. private int [3] a [ ]
6. public final int [ ] a 
A. 1, 3, 4 B. 2, 4, 5
C. 1, 2, 6 D. 2, 5, 6

Answer and Explanation

Answer: 1, 2, 6

Explanation:
(1), (2) and (6) are valid array declarations.
Option (3) is not a correct array declaration. The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a
Option (4) is not a correct array declaration. The compiler complains with: ']' expected. A closing bracket is expected in place of the 3. The following works: private int a []
Option (5) is not a correct array declaration. The compiler complains with 2 errors:
']' expected. A closing bracket is expected in place of the 3 and
expected A variable name is expected after a[ ] .

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
1. You can extend the Runnable interface as long as you override the public run ( ) method.
2. The class must contain a method called run ( ) from which all code for that thread will be initiated.
3. The class must contain an empty public void method named run ( ).
4. The class must contain a public void method named runnable ( ).
5. The class definition must include the words implements Threads and contain a method called run ( ).
6. The mandatory method must be public, with a return type of void, must be called run ( ), and cannot take any arguments.
A. 1 and 3 B. 2 and 4
C. 1 and 5 D. 2 and 6

Answer and Explanation

Answer: 2 and 6

Explanation:
(2) and (6). When a thread's run ( ) method completes, the thread will die. The run ( ) method must be declared public void and not take any arguments.
(1) is incorrect because classes can never extend interfaces. (3) is incorrect because the run ( ) method is typically not empty; if it were, the thread would do nothing. (4) is incorrect because the mandatory method is run ( ). (5) is incorrect because the class implements Runnable.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.