wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan' s Question ! 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 » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan' s Question !

Vinod Sinha
Ranch Hand

Joined: Oct 16, 2003
Posts: 43
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

Joined: Oct 17, 2003
Posts: 150
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

Joined: Aug 03, 2003
Posts: 66
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


Download a copy of <a href="http://www.javarules.com" target="_blank" rel="nofollow"><i>"Mastering The Fundamentals of The Java Programming Language"</i></a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan' s Question !
 
Similar Threads
Dan's Question : Fundamental Classes
Boolean wrapper objects
Question from Dans Test
wrapper class doubt
questions about assert