• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

inner class question from HUNT

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
//I THOUGHT THIS SHOULD NOT COMPILE SINCE THE INNER CLASS IS CREATED INCORRECTLY (new Inner()), without creating an instance of the outer class ???

 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inner class is created correctly since you inside of the outer class, you don't need to reference the outer class when you create the inner class.
Now it still won't compile becuase it says it "implements A" and A is not defined anywhere or ever mentioned in the question, so you don't know what methods A has that you need to implement. However, I don't think that is what they are getting at with this question, so assume that A compiles fine, is in the same package and only has one method sample(). Now what is the answer?
Bill
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IF v go according to Bill, then the output should b "A" ie prints "Sample". This is because of the semicolon after the if statement. So even though if condition is false it will execute sample(). Correct me if i m wrong.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right umang. That little semicolon is easy to miss too.
Bill
 
I think I'll just lie down here for a second. And ponder this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic