• 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

Initialization question from Mughal's mock exam

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which 3 statements concerning the value of a member variable are true, when no explicit assignments have been made?

1) The value of an int is undetermined.
2) The value of all numeric types is zero.
3) The compiler may issue an error if the variable is used before it is initialized.
4) The value of a String variable is "" (empty string).
5) The value of all object variables is null.

2 and 5 are true, is there another one which is true? Because the question asks for 3 statements.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the third one is right. Because the class and instance variables are by default initialised. But when it comes to local variables they aren't initialized. For example.



Hope this solves your doubt.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A member variable is always intialized to a null-like value when an instance of a class is created. E.g. Strings and Objects are initialized to null, ints are initialized to 0, longs to 0L, etc.

Raghu, in your example what you are describing is a local variable. A variable local to the main method.

So I think that the answers to Johns questions about the member variables are (2) and (5).

...However, I think that a final member variable can cause a compiler error because you can leave its initialization until the constructor. If you forget to initialize the variable in the constructor, the compiler will flag it.

Arthur
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

RaghuSham, your statement is wrong because member variables means Class variable not the Local variables.

John:
I am also able to see the two correct answers only. What will be the third answer?
 
John Brown
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Arthur.
Kiran, 3 would be true if we're talking about final member variables, because if a final variable is used in a constructor before it gets initialized, you have an error.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic