Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan' s Question !

 
Vinod Sinha
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 5
class MWC105 {
static boolean b1;
public static void main(String[] args) {
boolean[] array = new boolean[1];
boolean b2;
System.out.print(b1+",");
System.out.print(array[0]+",");
System.out.print(b2);
}}
What is the result of attempting to compile and run the program?
a. Prints: true,true,true
b. Prints: false,false,false
c. Prints: null,null,null
d. Prints: false,true,false
e. Compile-time error
f. Run-time error
g. None of the above
The answer should be (f) but it is given as (e) ?
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinod
The answer is infact (e).
This is what I got when I 'tried' to compile it:

Please remember that local variables are not automatically initialized and have to initialized explicitly before being used in expressions. But elements in an array (local or otherwise) are initialized automatically if not explicitly initialized.
Thanks
Harwinder
[ October 19, 2003: Message edited by: Harwinder Bhatia ]
 
Doug Dunn
Author
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the actual compiler error:
Test.java:8: variable b2 might not have been initialized
System.out.print(b2);
^
1 error
This is a simple definite assignment problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic