File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hunt Exam Question

 
Rasri Anand
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look behind the if statement.. there is a semicolon
 
Rasri Anand
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had completely missed that. Thanks.
 
Gang Liu
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure about the meaning of "Test4" in the if statement: Test4.this.flag.
Could anybody give some hint?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Pai
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic