02-Declarations and Access Controls PDF
02-Declarations and Access Controls PDF
1. What is the most restrictive access modifier that will allow members of one class to have
access to members of another class in the same package?
A. public
B. abstract
C. protected
D. synchronized
E. default access
ANSWER: E
A and C are wrong because public and protected are less restrictive. B and D are wrong because
abstract and synchronized are not access modifiers.
2. Given a method in a protected class, what access modifier do you use to restrict access to
that method to only the other members of the same class?
A. final
B. static
C. private
D. protected
E. volatile
F. default access
ANSWER: C.
The private access modifier limits access to members of the same class.
A, B, D, E, and F are wrong because protected and default are the wrong access modifiers, and
final, static, and volatile are modifiers but not access modifiers.
1. abstract class A {
2. abstract short m1() ;
3. short m2() { return (short) 420; }
4. }
5.
6. abstract class B extends A {
7. // missing code ?
8. short m1() { return (short) 42; }
9. }
ANSWER: A, C, and E
A and C are correct, because an abstract class does not need to implement any of its superclass’
methods. E is correct because as it stands, it is a valid concrete extension of class A.
B is wrong because an abstract class does not need to implement any of its superclass’ methods.
D is wrong because a class that extends another class is free to add new methods. F is wrong
because it is legal to extend an abstract class from a concrete class.
4. Which two of the following are legal declarations for nonnested classes and interfaces?
(Choose two.)
ANSWER: C, F
A is wrong because a class cannot be abstract and final—there would be no way to use such a
class. B is wrong because interfaces and classes cannot be marked as static. D and E are wrong
because classes and interfaces cannot be marked as protected.
A. 1
B. 2
C. 3
D. 4
E. 5
F. All of them
A, B, C, D, and F are incorrect because the only illegal declaration is 3; transient applies only to
variable declarations, not to method declarations. As you can see from these other examples,
method declarations can be very extensive.
1. package testpkg.p1;
2. public class ParentUtil {
3. public int x = 420;
4. protected int doStuff() { return x; }
5. }
1. package testpkg.p2;
2. import testpkg.p1.ParentUtil;
3. public class ChildUtil extends ParentUtil {
4. public static void main(String [] args) {
5. new ChildUtil().callStuff();
6. }
7. void callStuff() {
8. System.out.print("this " + this.doStuff() );
9. ParentUtil p = new ParentUtil();
10. System.out.print(" parent " + p.doStuff() );
11. }
12. }
A. The code compiles and runs, with output this 420 parent 420.
B. If line 8 is removed, the code will compile and run.
C. If line 10 is removed, the code will compile and run.
D. Both lines 8 and 10 must be removed for the code to compile.
E. An exception is thrown at runtime.
ANSWER: C
The ParentUtil instance p cannot be used to access the doStuff() method. Because doStuff() has
protected access, and the ChildUtil class is not in the same package as the ParentUtil class,
doStuff() can be accessed only by instances of the ChildUtil class (a subclass of ParentUtil).
interface Count {
short counter = 0;
void countUp();
}
A. 012
B. 123
C. 0123
D. 1234
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: E
The code will not compile because the variable counter is an interface variable that is by default
final static. The compiler will complain at line 12 when the code attempts to increment counter.
1. import java.util.*;
2. public class NewTreeSet2 extends NewTreeSet {
3. public static void main(String [] args) {
4. NewTreeSet2 t = new NewTreeSet2();
5. t.count();
6. }
7. }
8. protected class NewTreeSet {
9. void count() {
A. 024
B. 0246
C. Compilation fails at line 4
D. Compilation fails at line 5
E. Compilation fails at line 8
F. Compilation fails at line 10
ANSWER: E
Nonnested classes cannot be marked protected (or final for that matter), so the compiler will
fail at line 8.
1.
2. public class NewTreeSet extends java.util.TreeSet{
3. public static void main(String [] args) {
4. java.util.TreeSet t = new java.util.TreeSet();
5. t.clear();
6. }
7. public void clear() {
8. TreeMap m = new TreeMap();
9. m.clear();
10. }
11. }
Which two statements, added independently at line 1, allow the code to compile? (Choose two.)
A. No statement is required
B. import java.util.*;
C. import.java.util.Tree*;
D. import java.util.TreeSet;
E. import java.util.TreeMap;
ANSWER: B and E
TreeMap is the only class that must be imported. TreeSet does not need an import statement
because it is described with a fully qualified name.
A is incorrect because TreeMap must be imported. C is incorrect syntax for an import statement.
D is incorrect because it will not import TreeMap, which is required.
ANSWER: A and E
B and C are incorrect because interface variables cannot be either protected or transient. D and
F are incorrect because interface methods cannot be final or static.
11. Which of the following class level (nonlocal) variable declarations will not compile?
A. protected int a;
B. transient int b = 3;
C. public static final int c;
D. volatile int d;
E. private synchronized int e;
ANSWER: E
A and B will compile because protected and transient are legal variable modifiers.
C will compile because when a variable is declared final it does not have to be initialized with a
value at the same time. D will compile because volatile is a proper variable modifier.
Interface Implementation
1. interface DoMath {
2. double getArea(int rad); }
3.
4. interface MathPlus {
5. double getVol(int b, int h); }
6.
7.
8.
Which two code fragments inserted at lines 7 and 8 will compile? (Choose two.)
ANSWER: C and E
C are E are correct because interfaces and abstract classes do not need to fully implement the
interfaces they extend or implement (respectively).
13. Which three are valid method signatures in an interface? (Choose three.)
ANSWER: B, C, and E
A, is incorrect because an interface method must be public; if it is not explicitly declared public
it will be made public implicitly. D is incorrect because interface methods cannot be static.
14. Which two statements are true for any concrete class implementing the java.lang.Runnable
interface? (Choose two.)
A. You can extend the Runnable interface as long as you override the public run()
method.
B. The class must contain a method called run() from which all code for that thread will be
initiated.
C. The class must contain an empty public void method named run().
D. The class must contain a public void method named runnable().
E. The class definition must include the words implements Threads and contain a method called
run().
F. The mandatory method must be public, with a return type of void, must be called run(), and
cannot take any arguments.
ANSWER: B and F
A is incorrect because classes can never extend interfaces. C is incorrect because the run()
method is typically not empty; if it were, the thread would do nothing. D is incorrect because
the mandatory method is run(). E is incorrect because the class implements Runnable.
1. interface Base {
2. boolean m1 ();
3. byte m2(short s);
4. }
ANSWER: C and D
C is correct because an abstract class doesn’t have to implement any or all of its interface’s
methods. D is correct because the method is correctly implemented ((7 > 4) is a boolean).
A is incorrect because interfaces don’t implement anything. B is incorrect because classes don’t
extend interfaces. E is incorrect because interface methods are implicitly public, so the methods
being implemented must be public.