Hi,
Question 61 from
John Hunt's Mock exam
Q. 61
Consider the following program:
public class Test { public static void main (String args []) { boolean a = false; if (a = true)
System.out.println("Hello");
Else
System.out.println("Goodbye");
}
}
What is the result:
A. Program produces no output but terminates correctly.
B. Program does not terminate.
C. Prints out "Hello"
D. Prints out "Goodbye"
Answer C .
When I run this code, a compiler error is generated due to the Else, that is interpreted as to be a class, so not defined.
Answer C is OK when Else is changed into
else...
Obvious, but...
Another question:
Examine the following code which includes an inner class:
public final class Test4 implements A {
class Inner {
void test() {
if (Test4.this.flag); {
sample();
}
}
}
private boolean flag = false;
public void sample() {
System.out.println("Sample");
}
public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}
What is the result:
A. Prints out "Sample"
B. Program produces no output but terminates correctly.
C. Program does not terminate.
D. The program will not compile
Select the most appropriate answer.
Answer A.
But without interface A's definition, the code won't definitely not compile.
...
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287