This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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)?
 
A feeble attempt to tell you about our stuff that makes us money
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic