wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a strange code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a strange code" Watch "a strange code" New topic
Author

a strange code

rainbow
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
look at below question:
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.
Please pay attention to the bold sentence,it runs strangely.

deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi rainbow,
Assuming that interface A is available. The answer will be 'B' i.e. the program terminates correctly and produces no output. the culprit here is the ; in following line
if (Test4.this.flag) ;
if u remove the ; then u'll see the o/p sample printed on your console.
regards
deekasha
rainbow
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
hi:
The code shouldn't include "implements A",it's a mistake.Ignore this,the correct answer should be A.if remove ";",the result will be B.It is truth.If you don't beleive it,you can try to run it.
Rong Chen
Ranch Hand

Joined: Aug 07, 2000
Posts: 30
this is kind of a joke.
i don't think it will appear in Sun's exam.
but good to remember, anyway...
Geek
Greenhorn

Joined: Sep 09, 2000
Posts: 21
Hello rainbow,
Didn't get what was strange about the code. When you had ";" after the if statement the conditional statement ended with the ";", so sample() was executed as it was the next statement.
On the other hand when u removed the ";", sample() was in the conditional statement and becuase the value of flag is false, the execution of sample() is skipped and hence you get nothing printed. Got that ??
Geek
ps: i hope u understood my long and convoluted sentences
[This message has been edited by Geek (edited September 14, 2000).]


geek@mantraonline.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: a strange code
 
Similar Threads
John Hunt Question 62
inner class question from HUNT
Mock Exam Que
Explaination reg inner classes
Doubt in if()