Meaningless Drivel is fun!
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a strange code" Watch "a strange code" New topic

a strange code

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); {
private boolean flag = false;
public void 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.
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
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...

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 ??
ps: i hope u understood my long and convoluted sentences
[This message has been edited by Geek (edited September 14, 2000).]
I agree. Here's the link:
subject: a strange code
It's not a secret anymore!