aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hunt Exam Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Hunt Exam Question" Watch "Hunt Exam Question" New topic
Author

Hunt Exam Question

Rasri Anand
Greenhorn

Joined: Apr 19, 2002
Posts: 16
This is Qno 62 from John Hunt's mock exam:
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.
I thought the answer should be B, as the flag has been set to false. Am I missing something very obvious here? The correct answer is A. I ran the code and it does print "Sample".
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
look behind the if statement.. there is a semicolon
Rasri Anand
Greenhorn

Joined: Apr 19, 2002
Posts: 16
I had completely missed that. Thanks.
Gang Liu
Greenhorn

Joined: Apr 22, 2002
Posts: 2
I am not sure about the meaning of "Test4" in the if statement: Test4.this.flag.
Could anybody give some hint?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
lgang08,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do have one. Please change your display name to comply with The JavaRanch Naming Policy
------------------------------------
Concerning your question:
Originally posted by lgang08:
I am not sure about the meaning of "Test4" in the if statement: Test4.this.flag.

To better understand what an inner class is, I'd suggest reading Sun's Java Tutorial Trail on Inner Classes.
Good Luck.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
Hi,
I feel the answer should be D as the program does not compile in the first place. Test4 program implements interface A but there is no defination of Interface A or the implementation of any methods in Interface A in Test4. Correct answer should be D.
If your remove the Interface A declaration from the class defination the program compiles and gives output of Sample.
-Arun
Gang Liu
Greenhorn

Joined: Apr 22, 2002
Posts: 2
To better understand what an inner class is, I'd suggest reading Sun's Java Tutorial Trail on Inner Classes.

Thanks for the reminding and answer!
I have read this tutorial before. The answer of my question can not be found directly from the tutorial.
Actually I want to ask why in this if statement the outter class name Test4 can be used to refer to the flag. How can you know you can use this format to refer to a outter class's field variable? In which book or in which part of the sun tutorial can you find similar usage like Test4.this.flag(I can not find a example like this in Sun's inner class tutorial)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hunt Exam Question
 
Similar Threads
A question of JTest.
a strange code
John Hunt Question 62
Mock Exam Que
inner class question from HUNT