Home / Java Programming / Declarations and Access Control :: Pointing out the correct statements

Java Programming :: Declarations and Access Control

  1. 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; } }

  2. A.
    1 only
    B.
    2 only
    C.
    3 and 5
    D.
    1 and 4

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

  4. A.
    1 and 3
    B.
    2 and 4
    C.
    1 and 5
    D.
    2 and 6

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

  6. A.
    1 only
    B.
    2 and 5
    C.
    3 and 4
    D.
    3 and 5

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

  8. A.
    The code compiles and runs, with output this 420 parent 420.
    B.
    If line 18 is removed, the code will compile and run.
    C.
    If line 20 is removed, the code will compile and run.
    D.
    An exception is thrown at runtime.