Você está na página 1de 3

Class Declarations Questions

Question 1

public class Basics {} // 1


class Basics1 {} // 2
protected class Basics2 {} // 3
private class Basics3 {} // 4
Class Basics4 {} // 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. 1
b. 2
c. 3
d. 4
e. 5

Question 2

public class Basics {} // 1


public class Basics2 {} // 2
public class Basics3 {} // 3
public class Basics4 {} // 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. 1
b. 2
c. 3
d. 4
e. None of the above

Question 3

Which of the following modifiers can be applied to a class that is not a nested class?

a. public
b. protected
c. private
d. abstract
e. static
f. final

Question 4

Which of the follow statements is true.

a. An anonymous class can be declared abstract.


b. A local class can be declared abstract.
c. An abstract class can be instantiated.
d. An abstract class is implicitly final.
e. An abstract class must declare at least one abstract method.
f. 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. class A extends Object.


b. Field i1 is implicitly public, because class A is public.
c. Method m1 is implicitly public, because class A is public.
d. The compiler will insert a default constructor implicitly.
e. The default constructor has no throws clause.
f. The default constructor of class A has package access.
g. The default constructor accepts one parameter for each field in class A.
h. The default constructor invokes the no-parameter constructor of the superclass.

Question 6

abstract class A {} // 1
transient class G {} // 2
private class C {} // 3
static class E {} // 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. 1
b. 2
c. 3
d. 4
e. 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. 1
b. 2
c. 3
d. 4
e. None of the above

Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
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
c
enclosing class or interface; then C may be declared with the public modifier or with
1 d 3 4 5
package access (i.e. no modifier). The other two access modifiers, private and
e
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.
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
a
member of an enclosing class, but can not be applied to a local class or a class that is
3 d public abstract final
not nested inside another class. The public modifier can be applied to a top level class
f
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.
b A local class can be declared abstract. 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
4 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.
class A extends Object. The compiler
Field i1 and m1 both have package access. When no constructor is declared explicitly
a will insert a default constructor
the compiler will insert one implicitly. The implicitly declared default constructor
d implicitly. The default constructor has
5 will have the same access privileges as the class. In this case, the class is public, so
e no throws clause. The default
the default constructor is also public. The default constructor accepts no parameters
h constructor invokes the no-parameter
and throws no exceptions.
constructor of the superclass.
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
6 a 1
package access, but not private. The transient modifier can not be applied to any
class; because it is a field modifier.
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
7 d 4
is a method modifier. The modifier, volatile, can not be applied to any class; because
it is a field modifier.

page:3

Você também pode gostar