Class Declarations
Questions
Question 1 public class Basics {} class Basics1 {} protected class Basics2 {} private class Basics3 {} Class Basics4 {}
// // // // //
1 2 3 4 5
Suppose these are top-level class declarations and not nested class declarations; and suppose that all of the declarations are contained in one file named Basics.java. Compile-time errors are generated at which lines? a. b. c. d. e.
1 2 3 4 5
Question 2 public public public public
class class class class
Basics {} Basics2 {} Basics3 {} Basics4 {}
// // // //
1 2 3 4
Suppose these are top-level class declarations and not nested class declarations; and suppose that all of the declarations are contained in one file named Basics.java. A compile-time error is not generated at which line? a. b. c. d. e.
1 2 3 4 None of the above
Question 3 Which of the following modifiers can be applied to a class that is not a nested class? a. b. c. d. e. f.
public protected private abstract static final
Question 4 Which of the follow statements is true. a. b. c. d. e. f.
An anonymous class can be declared abstract. A local class can be declared abstract. An abstract class can be instantiated. An abstract class is implicitly final. An abstract class must declare at least one abstract method. An abstract class can not extend a concrete class.
Question 5 public class A {int i1; void m1() {}}
page:1
Class Declarations
Questions
Which of the following statements are true? a. b. c. d. e. f. g. h.
class A extends Object. Field i1 is implicitly public, because class A is public. Method m1 is implicitly public, because class A is public. The compiler will insert a default constructor implicitly. The default constructor has no throws clause. The default constructor of class A has package access. The default constructor accepts one parameter for each field in class A. The default constructor invokes the no-parameter constructor of the superclass.
Question 6 abstract class A {} transient class G {} private class C {} static class E {}
// // // //
1 2 3 4
Suppose these are top-level class declarations and not nested class declarations. Which of these declarations would not produce a compile-time error? a. b. c. d. e.
1 2 3 4 None of the above
Question 7 protected class D {} // 1 synchronized class F {} // 2 volatile class H {} // 3 final class B {} // 4 Suppose these are top-level class declarations and not nested class declarations. Which of these declarations would not produce a compile-time error? a. b. c. d. e.
1 2 3 4 None of the above
Answers Answers: Certified Java Programmer Mock Exam No.
Answer
Remark
1
c d 3 4 5 e
If a class C is declared as a member of an enclosing class then C may be declared using no access modifier or any of the three access modifiers, private, protected or public. However, if class C is not a local class, anonymous class or a member of an enclosing class or interface; then C may be declared with the public modifier or with package access (i.e. no modifier). The other two access modifiers, private and protected, are not applicable to any class that is not a member class. The class declaration, Class Basics4 {}, generates a compile-time error, because all of the letters of the reserved word class must be lower case.
2
a 1
Only one class in a source code file can be declared public. The other classes may not
page:2
Class Declarations
Questions
Answers: Certified Java Programmer Mock Exam No.
Answer
Remark be public. Therefore, the declarations for classes Basics2, Basics3 and Basics4 generate compile-time errors.
3
a d public abstract final f
b A local class can be declared abstract. 4
class A extends Object. The compiler will insert a default constructor implicitly. The default constructor has no throws clause. The default constructor invokes the no-parameter constructor of the superclass.
The access modifiers, protected and private, can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. The static modifier can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. The public modifier can be applied to a top level class to allow the class to be accessed from outside of the package. The abstract modifier prevents the class from being instantiated. An abstract class may include zero, one or more abstract methods. The final modifier prevents a class from being extended. An anonymous class can not be extended; therefore, an anonymous class can not be declared abstract. A local class can be abstract. An abstract class can not be instantiated. If a class declaration contains an abstract method, then the class must also be declared abstract. A class can be declared abstract even if it does not contain an abstract method. An abstract class can never be declared final. Field i1 and m1 both have package access. When no constructor is declared explicitly the compiler will insert one implicitly. The implicitly declared default constructor will have the same access privileges as the class. In this case, the class is public, so the default constructor is also public. The default constructor accepts no parameters and throws no exceptions.
5
a d e h
6
a 1
The modifiers, private and static, can be applied to a nested class, but can not be applied to a class that is not nested. A class that is not nested can have public or package access, but not private. The transient modifier can not be applied to any class; because it is a field modifier.
7
d 4
The modifier, protected, can not be applied to a top level class, but can be applied to a nested class. The synchronized modifier can not be applied to any class, because it is a method modifier. The modifier, volatile, can not be applied to any class; because it is a field modifier.
page:3