Question 1
public class Basics {} // 1
class Basics1 {} // 2
protected class Basics2 {} // 3
private class Basics3 {} // 4
Class Basics4 {} // 5Suppose 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 {} // 4Suppose 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() {}}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 {} // 4Suppose 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 {} // 4Suppose 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
No.
|
Answer
|
Remark
|
|
1
|
c d
e
|
3 4 5
|
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 be public. Therefore, the declarations for
classes Basics2, Basics3 and Basics4
generate compile-time errors.
|
3
|
a d
f
|
public abstract final
|
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.
|
4
|
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 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.
|
5
|
a d
e h
|
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.
|
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.
|
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.
|
|
No comments:
Post a Comment