Monday, January 25, 2010

SCJP Sample questions

Q1. Which of these statements are legal. Select the three correct answers.

A. int arr[][] = new int[5][5];

B. int[]arr[] = new int[5][5];

C. int[] arr = new int[5][];

D. int[] arr = new int[][5];

Correct Answer: A,B,C

02

Class C {

public static void main(String[] args) {

int[]a1[]=new int[3][3]; //3

int a2[4]={3,4,5,6}; //4

int a2[5]; //5

}}

What is the result of attempting to compile and run the program ?

1.compiletime error at lines 3,4,5

2.compiltime error at line 4,5

3.compiletime error at line 3

4.Runtime Exception

5.None of the above

Ans: 2

Explanation:

no value shoud be specified in the rightsidebrackets when constructing an array

Q3. How can you force garbage collection of an object?

A. Garbage collection cannot be forced.
B. Call System.gc().
C. Call System.gc() passing in a reference to the object to be garbage collected.
D. Call Runtime.gc().
E. Set all references to the object to new values(null, for example).

Correct Answer: A

Q4. Consider these classes, defined in separate source files,

public class Test1{

public float aMethod(float a, float b) throws IOException{

}

}
1. public class Test2 extends Test1{
2.
3. }

Which of the following methods would be legal at line 2 in class Test2?

A. float aMethod(float a, float b){}
B. public int aMethod(int a, int b) throws Exception{ }

C. public float aMethod(float a, float b) throws Exception{ }

D. public float aMethod(float p, float q){ }


Correct Answer: B,D

Explanation:

B and D are correct. B is legal as it is an example of method overloading.

A is illegal because it is less accessible than the original method, because method in Test1 is public. And for any overriding method, accessibility must not be more restricted than the original method.

C is illegal because for overriding method, it must not throw checked exception of classess that are not possible for the origincal classes.

Q5. In Java, an abstract class cannot be sub-classed.

A. True

B. False


Correct Answer: B. False
Explanation:

The answer is false, in java abstract class must be sub classessed to make it concrete class.

Q6. TreeMap class is used to implement which collection interface. Select the one correct answer

A. Set
B. SortedSet
C. List
D. Tree
E. SortedMap

Correct Answer: E

Q7. What is the name of collection interface used to maintain unique elements.

_____________________ (Fill-in-the-blank)

Correct Answer: Set interface

Q8. A monitor called mon has 5 threads in its waiting pool; all these waiting threads have the same priority. One of the threads is thread1. How can you notify thread1 so that it alone moves from Waiting state to Ready State?

A. Execute notify(thread1); from within synchronized code of mon.

B. Execute mon.notify(thread1); from synchronized code of any object.

C. Execute thread1.notify(); from synchronized code of any object.

D. Execute thread1.notify(); from any code(synchronized or not) of any object.

E. You cannot specify which thread will get notified.


Correct Answer: E



Explanation:

E is correct, when you call notify() on a monitor, you have no control over which waiting thread gets notified.


Q9. What happens when the following code is compiled and run. Select the one correct answer.

for(int i = 1; i < style=""> for(int j = 3; j >= 1; j--)
assert i!=j : i;

A. The class compiles and runs, but does not print anything.

B. The number 1 gets printed with AssertionError

C. The number 2 gets printed with AssertionError

D. The number 3 gets printed with AssertionError

E. The program generates a compilation error.

Correct Answer: B

Explanation:correct answer is B. When i and j are both 1, assert condition is false, and AssertionError gets generated.

Q10. What is the optput of this code fragment.

1. int X=3; int Y =10;

2. System.out.println(y%x);

A. 0

B. 1

C. 2

D. 3

Correct Answer: B

Explanation:

B is a correct answer. Dividing 10 by 3 gives 3 reminder 1, and this 1 forms the result of the modulo expression. Same rule applies for the negative numbers, you should ignore the signs during calculation part, and simply attach the sign of the Left-hand operand to the result.