Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam Que

 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
At line 4 what value does the flag takes. According to author it takes false and hence prints Sample.
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.

 
Ken Pullin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - If I've looked at it correctly, it should display "Sample" each time you execute the code. Since the ";" is after the "if (Test.this.flag)" the look stops there and the rest is just a block of code that would always get executed.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,
Do you mean to say the if construct has no body and so nothing to do with flag and so sample is invoked and prints Sample.
Please reply.
Thanks,
Madhuri.
 
Ken Pullin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if I understand you correctly. Basically what I said was if you have a if statement such as:
if (true);
{
System.out.println("Whatever");
}
it doesn't matter if what value is in the if portion, because the ";" ends the "If" - the block of code is not associated with the if - does that help?>?

 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya I got you .Thank you onceagain.
Madhuri.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic