• 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

Question for Beginners Only

 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question is for beginners only, to test their acuity in preparation for the test! If you are an expert, please don't reply to allow others with less experience the practice. Thanks

Will the following code compile? If not, why? If yes, what gets printed?
<PRE>public class Test {
int total = 1;
public static void main (String[] args) {
int total;
float f = 0.0f;
char c = 'C';
System.out.println("total = " + total);
System.out.println("float f = " + f);
System.out.println("char c = " + c);
}
}</PRE>
See a variation of this question on next post.
[This message has been edited by Tony Alicea (edited February 04, 2000).]
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following second question is for beginners only, to test their acuity in preparation for the test! If you are an expert, please don't reply to allow others with less experience the practice. Thanks


Will the following code compile? If not, why? If yes, what gets printed?
<PRE>public class Test {
static int total = 1;
public static void main (String[] args) {
int total;
float f = 0.0f;
char c = 'C';
System.out.println("total = " + total);
System.out.println("float f = " + f);
System.out.println("char c = " + c);
}
}</PRE>
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think both of 'em won't compile as the automatic variable 'total' in method 'main' isn't initialized. The instance(static in second question) variable 'total' gets 'hidden' as automatic one in method is there with same name.
I'm a beginner, tell me I'm wrong so that I can begin again.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right! You win! I dunno what you won but as soon as I figure it out, I'll tell you!
I'll think of some other questions later...
Thanks for playing
 
Rajiv Ahuja
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll look forward to yours "only for beginners" questions.
 
He puts the "turd" in "saturday". Speaking of which, have you smelled 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