Dan Chisholm's
Java Programmer Certification Mock Exam

Please Help Save a Tree!

The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.

Today, you can find my book at amazon.com.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1None of the above  Please note that this question asks which variable can NOT be substituted. Class Z is a non-static member class of class A; therefore, all of the fields and methods of class A (static, non-static, and private) are available to class Z.  
2s2   Please note that this question asks which variable can NOT be substituted. Class Z is a static member class of class B; therefore, all of the static fields and methods of the enclosing class B are available to Z. For example, from within the static member class Z, the static field s1 of the enclosing class B can be accessed using the simple name s1. The simple name s1 does not need to be qualified with a reference to an instance of the enclosing class B, because s1 is not associated with a particular instance of the enclosing class. In contrast, non-static fields and methods of the enclosing class B are associated with a particular instance of class B. From the static context of class Z, the instance fields and methods of the enclosing class B can be accessed only if the simple name of the instance field or method is qualified with a reference to a specific instance of class B. Suppose a reference variable r1 refers to an instance of the enclosing class B. Then the instance member s2 of the enclosing class instance referenced by r1 could be accessed using the expression r1.s2.  
3a  b  c  e  s1  s2  s3  s5  Class Z is a local class defined within the code block of method m1 of class C. All of the fields and methods of class C (static, non-static, and private) are available to Class Z. Additionally, the parameters of method m1 that are declared final are available to class Z. If a final local variable declaration appears before the declaration of class Z, then it is also available to class Z. Parameter s6 and variable s4 are not final and are therefore not available to class Z. There are at least two reasons why non-final variables are not available to a local inner class. The first reason is because the life cycle of a local variable might be shorter than that of a local class. A local variable lives on the stack and disappears as soon as the method is exited. A local class, however, might continue to exist even after the method has been exited. The second reason is due to multithreading issues. Suppose a local class extends the Thread class or implements the Runnable interface, and it is used to spawn a new Thread from within the method m1. If the local variables of method m1 were available to the local class, then a mechanism for synchronizing access to the variables would be required.  
4Compile-time error  An instance of class Z must be associated with an enclosing instance of class D. In a static context, an unqualified class instance creation expression of the form new ClassType(ArgumentListopt) ClassBodyopt can not be used to create an instance of an inner class. Instead, a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt) ClassBodyopt is required to create an association between an instance of the enclosing class and the new instance of the inner class. The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by a class instance creation expression such as new D(). In a static context, the expression new D().new Z() can be used to create the new instance of the enclosing class D and the new instance of the inner class Z.  
5Prints: Z  Class Z is a static member class of class E. Static member classes are similar to ordinary top-level classes with the added advantage that all of the static fields and methods of the enclosing class (including those that are private) are available to the member class. The class instance creation expression new E.Z() creates an instance of the static nested class E.Z. The instance of the static nested class is not associated with any instance of the enclosing class, and no instance of the enclosing class is created. For that reason, only the letter "Z" is printed.  
6Prints: Z.m1 Y.m1  The private fields and methods of the member classes are available to the enclosing class. The private fields and methods of the member classes are also available to other member classes.  
7G.this.s1  The qualified this expression ClassName.this.name can be used to access shadowed variables declared within an enclosing class.  
8g  h  j  10  A non-static nested class is called an inner class. An inner class can not declare a static field unless it is a compile-time constant. Even though f is final, it does not have a value at compile time; so it causes a compilation error. Member variable g also causes a compile-time error, because it is static. The static initializer of NonStaticInner causes a compile-time error, because inner classes (i.e. non-static nested classes) can not declare static initializers.  
9 The non-static members of an enclosing class are not directly available to a static nested class. From within StaticNested, the non-static members of the enclosing class can not be referred to by a simple name. Instead, a qualified name is required. Suppose a reference variable r1 refers to an instance of the enclosing class Red. Then the instance member c of the enclosing class instance referenced by r1 could be accessed using the qualified name r1.c.  
10 A member interface is implicitly static; therefore, it can not be declared as a member of a non-static nested class.  
11Prints: A0B0A1B1A1B2  Class A is the enclosing class of the inner class B. An instance of class B must be associated with an enclosing instance of class A. In a static context such as the main method, instantiation of a named inner class requires the use of a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt). The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by another class instance creation expression. At line 2, the qualified class instance creation expression new A().new B() first creates a new instance of the enclosing class A, then it creates an instance of B. The new instance of A is the first instance created; so the name is A0. The new instance of B is the first instance created; so the name is B0. At line 3, the qualified class instance creation expression a1.new B() creates an instance of B that is associated with a previously existing instance of class A that is referenced by variable a1. The instance of class A referenced by variable a1 is the second instance created so the name is A1. The new instance of B is the second instance created; so the name is B1. At line 4, a new instance of B is created and associated with the instance of A this is referenced by variable a1.  
12Prints: A1B0A0B1A0B2  Class A is the enclosing class of the inner class B. An instance of class B must be associated with an enclosing instance of class A. In a static context, instantiation of a named inner class requires the use of a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt). The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by another class instance creation expression. If the enclosing class is not an inner class, then the enclosing class could be instantiated with an unqualified class instance creation expression such as the following, new EnclosingClass(). The qualified class instance creation expression new A().new B() first creates a new instance of A, then it creates an instance of B. The new instance of A is the second instance created; so the name is A1. The new instance of B is the first instance created; so the name is B0. In the qualified class instance creation expression this.new B(), the keyword this, denotes a reference to the enclosing instance on which the method m2 has been invoked. A new instance of the enclosing class is not created; so the name of the enclosing instance is A0. The new instance of B is the second instance created; so the name is B1. Since method m3 is an instance method, the inner class B can be instantiated using the unqualified class instance creation expression new B(). The enclosing instance is the instance on which the method m3 has been invoked. It is the same instance that is referenced by the keyword this and the reference variable a1.  
13Prints: A1B0A0B1A0B2  Class A is the enclosing class of the inner class B. An instance of class B must be associated with an enclosing instance of class A. In a static context, instantiation of a named inner class requires the use of a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt). The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by another class instance creation expression. If the enclosing class is not an inner class, then the enclosing class could be instantiated with an unqualified class instance creation expression such as the following, new EnclosingClass(). At line 1, the qualified class instance creation expression new A().new B() first creates a new instance of A, then it creates an instance of B. The new instance of A is the second instance created; so the name is A1. The new instance of B is the first instance created; so the name is B0. At line 2, a new instance of the named inner class B is created using the class instance creation expression new A.B(). The fully qualified name of class B is A.B. Since the class instance creation expression new A.B() appears within a method that is a member of class A, the use of the fully qualified name is unnecessary. Within method A.m2, the class instance creation expression new A.B() could be replaced by the expression new B() without changing the result. Using either expression, a new instance of class B is created without creating a new instance of class A. Instead, the new instance of class B is associated with the same instance of class A on which the method m2 has been invoked. It is the same instance of class A that is referenced by the keyword this and the reference variable a1. Since it was the first instance created, the name is A0. The new instance of B is the second instance created; so the name is B1. At line 3, a new instance of the named inner class B is created using the unqualified class instance creation expression new B(). The new instance of B is the third instance created; so the name is B2. The new instance of the inner class is associated with the same instance of the enclosing class on which the method m3 has been invoked. It is the same instance that is referenced by the keyword this and the reference variable a1. Since it was the first instance created, the name is A0.  
14f  g  Compile-time error at line 3  Compile-time error at line 4  Class A is the enclosing class of the inner class B. An instance of class B must be associated with an enclosing instance of class A. In a static context, instantiation of a named inner class requires the use of a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt). The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by another class instance creation expression. If the enclosing class is not an inner class, then the enclosing class could be instantiated with an unqualified class instance creation expression such as the following, new EnclosingClass(). The qualified class instance creation expression new A().new B() first creates a new instance of A, then it creates an instance of B. The qualified class instance creation expression this.new B() generates a compile-time error, because the keyword this can not be used within a static method. Since methods m1, m2 and m3 are static methods, an instance of the named inner class B can not be created inside any of the three methods using an unqualified class instance creation expression of the form new ClassType(ArgumentListopt).  
 
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
 

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