SCJP Questions Discussion
Submitted by Ahmed Hashim on Fri, 2006-01-06 14:53.
If you have any question in SCJP and you want to discuss to know the correct answer post it here.
I found a question in Testking which i think the provided answer is wrong. Here is the questions:
* Question 1
package test1;
public class Test1 {
static int x = 42;
}
package test2;
public class Test2 extends test1.Test1 {
public static void main(String[] args) {
System.out.println("x =" + x);
}
}
What is the result?%%%
A. x = 0%%%
B. x = 42%%%
C. Compilation fails because of an error in line 2 of class Test2.%%%
D. Compilation fails because of an error in line 3 of class Test1.%%%
E. Compilation fails because of an error in line 4 of class Test2.%%%
__Answer: C__
Do you think this answer is true?
In my opinion this answer is not true. The correct answer is E. The test2 Class wont be able to access the member variable x in the parent class because the x access modifier is default "which is packege" so you can't access it from outside the packe even if though the subclass.
Changing x to private will solve the problem.
* Question 2
Another wrong answer in the TestKing thing.
public class Test{
public static String output = "";
public static void foo(int i)
{
try
{
if(i == 1)
{
throw new Exception();
}
output += "1";
}
catch (Exception e) {
output += "2";
}
finally{
output +="3";
}
output += "4";
}
public static void main(String[] args) {
foo(0);
foo(1);
System.out.println(output);
}
}
What is the value of the variable output at line 23 "which is foo(1) call"
Answer: 13423
This answer is wrong, the correct answer is 134234.
May be it is an error in the printing :)
*Question 3
class X
{
public static void main(String[] args) {
try {
badMethod();
System.out.println("a");
} catch (Exception e) {
System.out.println("b");
}
finally
{
System.out.println("c");
}
System.out.println("d");
}
public static void badMethod()
{
throw new Error();
}
}
What is the result:
A. ABCD
B. Compilation fails
C. C is printed before exiting with an error message
D. BC is printed before exiting with an error message
E. BCD is printed before exiting with an error message
The Answer in TestKing is B which is wrong
The correct answer is C





