Java Programming - Declarations and Access Control

Exercise : Declarations and Access Control - Pointing out the correct statements
interface DoMath 
    double getArea(int rad); 
interface MathPlus 
    double getVol(int b, int h); 
/* Missing Statements ? */
which two code fragments inserted at end of the program, will allow to compile?
  1. class AllMath extends DoMath { double getArea(int r); }
  2. interface AllMath implements MathPlus { double getVol(int x, int y); }
  3. interface AllMath extends DoMath { float getAvg(int h, int l); }
  4. class AllMath implements MathPlus { double getArea(int rad); }
  5. abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) { return rad * rad * 3.14; } }
1 only
2 only
3 and 5
1 and 4
Answer: Option

(3) are (5) are correct because interfaces and abstract classes do not need to fully implement the interfaces they extend or implement (respectively).

(1) is incorrect because a class cannot extend an interface. (2) is incorrect because an interface cannot implement anything. (4) is incorrect because the method being implemented is from the wrong interface.

Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
  1. You can extend the Runnable interface as long as you override the public run() method.
  2. The class must contain a method called run() from which all code for that thread will be initiated.
  3. The class must contain an empty public void method named run().
  4. The class must contain a public void method named runnable().
  5. The class definition must include the words implements Threads and contain a method called run().
  6. The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.
1 and 3
2 and 4
1 and 5
2 and 6
Answer: Option

(2) and (6). When a thread's run() method completes, the thread will die. The run() method must be declared public void and not take any arguments.

(1) is incorrect because classes can never extend interfaces. (3) is incorrect because the run() method is typically not empty; if it were, the thread would do nothing. (4) is incorrect because the mandatory method is run(). (5) is incorrect because the class implements Runnable.

/* Missing statements ? */
public class NewTreeSet extends java.util.TreeSet
    public static void main(String [] args) 
        java.util.TreeSet t = new java.util.TreeSet();
    public void clear() 
        TreeMap m = new TreeMap();
which two statements, added independently at beginning of the program, allow the code to compile?
  1. No statement is required
  2. import java.util.*;
  4. import java.util.TreeSet;
  5. import java.util.TreeMap;
1 only
2 and 5
3 and 4
3 and 5
Answer: Option

(2) and (5). 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.

(1) is incorrect because TreeMap must be imported. (3) is incorrect syntax for an import statement. (4) is incorrect because it will not import TreeMap, which is required.

Which three statements are true?
  1. The default constructor initialises method variables.
  2. The default constructor has the same access as its class.
  3. The default constructor invokes the no-arg constructor of the superclass.
  4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.
  5. The compiler creates a default constructor only when there are no other constructors for the class.
1, 2 and 4
2, 3 and 5
3, 4 and 5
1, 2 and 3
Answer: Option

(2) sounds correct as in the example below

class CoffeeCup { 
    private int innerCoffee; 
    public CoffeeCup() { 
    public void add(int amount) { 
    innerCoffee += amount; 
The compiler gives default constructors the same access level as their class. In the example above, class CoffeeCup is public, so the default constructor is public. If CoffeeCup had been given package access, the default constructor would be given package access as well.

(3) is correct. The Java compiler generates at least one instance initialisation method for every class it compiles. In the Java class file, the instance initialisation method is named "<init>." For each constructor in the source code of a class, the Java compiler generates one <init>() method. If the class declares no constructors explicitly, the compiler generates a default no-arg constructor that just invokes the superclass's no-arg constructor. As with any other constructor, the compiler creates an <init>() method in the class file that corresponds to this default constructor.

(5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class.


package testpkg.p1;
public class ParentUtil 
    public int x = 420;
    protected int doStuff() { return x; }

package testpkg.p2;
import testpkg.p1.ParentUtil;
public class ChildUtil extends ParentUtil 
    public static void main(String [] args) 
        new ChildUtil().callStuff();
    void callStuff() 
        System.out.print("this " + this.doStuff() ); /* Line 18 */
        ParentUtil p = new ParentUtil();
        System.out.print(" parent " + p.doStuff() ); /* Line 20 */
which statement is true?
The code compiles and runs, with output this 420 parent 420.
If line 18 is removed, the code will compile and run.
If line 20 is removed, the code will compile and run.
An exception is thrown at runtime.
Answer: Option

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).

Option A, B and D are incorrect because of the access rules described previously.