aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another one from Dan Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another one from Dan" Watch "Another one from Dan" New topic
Author

Another one from Dan

Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
Question 8
interface C {
char w = DD.echo('w');
char x = DD.echo('x');
}
interface D extends C {
char y = DD.echo('y');
char z = DD.echo('z');
char a = DD.echo(w);
}
class DD implements D {
static char echo(char c) {
System.out.print(c);
return c;
}
public static void main (String[] args) {
System.out.print("Main");
DD dd = new DD();
System.out.println(a);
}
}

What is the result of attempting to compile and run the above program?
a. Prints: yzwMain
b. Prints: Mainyzw
c. Prints: wxyzwMain
d. Prints: Mainwxyzw
e. Prints: yzwxwwMain
f. Prints: Mainyzwxww
g. Runtime Exception
h. Compiler Error
i. None of the Above

The answer is D here.. i'm wondering why dont the interface variables get initialized unless explicitly called.. becoz interfaces are also implitly static and once they are loaded they should be intialized?? Whats the deal here!


SCJP 2
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Found it! Look here for a recent discussion of the same problem.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Saniya,
Does the other thread answer your question?
In short, Java tries to save time by only initializing what is needed.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Another one from Dan