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
1c  d  Methods declared within an interface are implicitly public. If no access modifier is included in the method declaration; then, the declaration is implicitly public. An attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error.  
2a  e  abstract  public  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. An abstract method can not also be declared private, static, final, native or synchronized; so the same restriction applies to methods declared within an interface.  
3None of the above  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. An abstract method can not also be declared private, static, final, native or synchronized; so the same restriction applies to methods declared within an interface. Transient and volatile are not method modifiers.  
4c  d  e  Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error.  
5b  c  The relationship between a class and its superclass is an example of an "is-a" relationship.  The relationship between a class and an object referenced by a field within the class is an example of a "has-a" relationship.  Inheritance is an example of an "is-a" relationship, because the subclass "is-a" specialized type of the superclass. The relationship between a class and an object referenced by a field declared within the class is an example of a "has-a" relationship, because the class "has-a" object.  
6c  d  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared.  
7a  b  c  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. The final, synchronized and native modifiers can not appear in the declaration of an abstract method, and can not be applied to an abstract method declared within an interface.  
8c  d  e  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared.  
9 All methods declared within an interface are implicitly abstract. The final, synchronized and native modifiers can not appear in the declaration of an abstract method, and can not be applied to an abstract method declared within an interface.  
10Compile-time error  In the declaration of interface B, the keyword, extends, has been replaced by the keyword, implements.  
11Compile-time error  Fields declared within an interface are implicitly public, final, and static. A compile-time error is generated in response to the attempt to increment the value of i.  
12 All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error. An abstract class that implements an interface is free to override any of the inherited method declarations with another abstract method declaration.  
13None of the above  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration within an interface, the usage is redundant and is discouraged. The modifiers, final, synchronized and native, can not appear in the declaration of an abstract method, but they can be added to an implementation of an abstract method.  
14The relationship between Cat and Dog is an example of an appropriate use of inheritance.  An appropriate inheritance relationship includes a subclass that "is-a" special kind of the superclass. The relationship between the Dog subclass and the Pet superclass is an example of an appropriate inheritance relationship, because a Dog "is-a" Pet. The relationship between the Cat subclass and the Dog superclass is not an example of an appropriate use of inheritance, because a Cat is not a special kind of a Dog. The goal of the OO paradigm is to develop software models that are accurate and reusable. If the software model is not accurate, then it probably is not reusable and the goals of the OO paradigm are not achieved. Code reuse and maintenance becomes increasingly difficult when inheritance is used to model inappropriate relationships. For example, suppose that somebody implements a herdSheep method in the Dog class. The Cat subclass would inherit the method and suddenly each instance of Cat would acquire the unwanted capability to make an attempt to herd sheep. It is difficult to imagine that a Cat would perform well in that role, so additional maintenance would be required to resolve the problem.  
15Prints: SA SB CA CB  The static initializer of the super class runs before the static initializer of the subclass. The body of the superclass constructor runs to completion before the body of the subclass constructor runs to completion.  
16Compile-time error at line 4  Class C10 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C10; then, no compile-time error occurs. Line 4 generates the compile-time error, because it is the first to access the name field as a member of class C10.  
17Prints: I10.s10,I20.s20,I20  Class C20 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C20; then, no compile-time error occurs. Although line 4 may appear to generate the compile-time error it does not, because name is accessed directly as a member of interface I20. Therefore, the compiler does not encounter an ambiguity.  
18Prints: D,C,B,A  A field of a superclass can be inherited by a subclass if the superclass field is not private and not hidden by a field declaration in the subclass and is accessible to code in the subclass. The field D.s1 hides C.s1, and C.s1 hides B.s1, and B.s1 hides A.s1. The keyword this serves as a reference to the object on which a method has been invoked. In the field access expression this.s1 appearing on line 1, the keyword this denotes a reference to the object of type D on which method m1 has been invoked. In the field access expression ((C)this).s1 appearing on line 2, the reference denoted by the keyword this is cast from type D to type C. The field that is accessed at run-time depends on the compile-time type of the reference; so the field access expression ((C)this).s1 refers the the variable s1 declared in class C.  
19Prints: B,SuperB,A,SuperA  The expression A.this.s1 is an example of a qualified this expression. It accesses the variable s1 declared in class A. The expression A.super.s1 is equivalent to ((SuperA)A.this).s1. It accesses the variable s1 declared in class SuperA.  
20Prints: DDDD  The instance method that is invoked depends on the run-time type of the object--not the compile-time type of the reference. In each case, the method m1 is invoked on an object of type D; so the implementation of m1 in type D is selected each time.  
21Compile-time error at 2  The method invocation expression a1.m2() generates a compile-time error, because the named method, m2, is declared in class B, but the reference is of the superclass type, A. The reference a1 is of type A; so a1 is able to access only those methods that are declared in class A and subclass methods that override those of class A. Only one method, m1, is declared in A; so a reference of type A can be used to invoke A.m1 or an overriding implementation of m1 that is declared in a subclass of A. Class B extends A and overrides method m1. A reference of type A can be used to invoke method m1 on an instance of type B. Class B declares an additional method, m2, that does not override a method of class A; so a reference of type A can not invoke B.m2.  
 
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.