Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1Compile-time error  Variables declared inside of a block or method are called local variables; they are not automatically initialized. The compiler will generate an error as a result of the attempt to access the local variables before a value has been assigned.  
2Compile-time error at line 5.  The local variable y has not been initialized so the attempt to access the variable results in a compile-time error.  
3Prints: 0null  The numeric sum of variables a, b, c, d and e is zero. The zero is converted to a String and concatenated with s.  
4Prints: 0,0,0,0,0,null  The default value of type char is the null character. When it is cast to an int the value is interpreted as zero.  
5Prints: 0,0.0,0.0,false,null  Generally speaking, numeric type variables are initialized to zero. Primitive boolean variables are initialized to false. Reference type variables are initialized to null.  
6Compile-time error  Local variables are not initialized automatically, and must be initialized explicitly before attempting to access the value. The local variable i3 will not be initialized if i1 is less than or equal to zero; so the result is a compile-time error.  
7Compile-time error at line 4.  Anytime a field is accessed from within a static context it is very important to verify that the field is also static. If the field is instead an instance variable then the result is a Compile-time error.  

Copyright © 2002-2003, Dan Chisholm
All rights reserved.