Get your CodeRanch badge!*
The moose likes Mock Exam Errata and the fly likes John Hunt M.E Q17 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "John Hunt M.E Q17" Watch "John Hunt M.E Q17" New topic
Author

John Hunt M.E Q17

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
Please Explain:
public class Test{
static int total = 10;
public static void main (String args[]) {
new Test();
}
public Test() {
System.out.println(" In test ");
System.out.println(this);
int temp = this.total;
if(temp >5) {
System.out.println(temp);
}
}
}
A.Class will not compile
B. Compiler reports error at line 2
C. Compiler reports error at line 9
D. The value 10 is one of the elements printed to standard output
E. Class compiles but generates runtime error
Answer is D. Can somebody explain?
Thanks
Konda Balabbigari
Ranch Hand

Joined: Jan 02, 2001
Posts: 35
Hi Vineet
public class Test{
static int total = 10;
public static void main (String args[]) {
new Test();
}
public Test() {
System.out.println(" In test ");
System.out.println(this);
int temp = this.total;
if(temp >5) {
System.out.println(temp);
}
}
}
In this there is no confusion in the coding it's straight question. The local variable temp has value of 10 because of the line
int temp = this.total;
and the following condition checks and prints the value
if(temp >5) {
System.out.println(temp);
}
If you need further information on static varaibles please visit my site.
Thanks
Konda Balabbigari
Web: http://www.javacaps.com

Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
Hello,
When your new object is created, the println statements are executed. The first prints out " In Test". The second prints out the memory location of the Test object (I think, I'm still a greenhorn!). The third prints out the value of the objects "total" member, which in this case is 10.
The "D" answer just says that 10 will be one of the things printed out, not the only one.
Hope this helps,
Pat B.
Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
Thakyou Pat,
I did not know that the line
System.out.println(this);
will print the memory location of the object. But your answer
explained that. Thanks
Maja Grabovac
Greenhorn

Joined: Feb 27, 2001
Posts: 16
println(this) basically prints this.toString(), so if your toString method is overridden it might print out something else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: John Hunt M.E Q17
 
Similar Threads
sample Q
q on testonline.com
Access to static variable
Static variable
Questions from John Hunt mock