• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

John Hunt M.E Q17

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
println(this) basically prints this.toString(), so if your toString method is overridden it might print out something else.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic