• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hunt Exam Question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look behind the if statement.. there is a semicolon
 
Rasri Anand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had completely missed that. Thanks.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic