Java OCA OCP Practice Question 2181

Question

What will be the result of attempting to compile and run the following program?

class Extender extends Thread {
  public Extender() { }
  public Extender(Runnable runnable) {super(runnable);}
  public void run() {System.out.print("|Extender|");}
}

public class Implementer implements Runnable {
  public void run() {System.out.print("|Implementer|");}
  public static void main(String[] args) {
    new Extender(new Implementer()).start();                   // (1)
    new Extender().start();                                    // (2)
    new Thread(new Implementer()).start();                     // (3)
  }//from w w  w.j  a v  a  2 s . c  o m
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will compile without errors and will print |Extender| twice and |Implementer| once, in some order, every time the program is run.
  • (c) The program will compile without errors and will print|Extender| once and |Implementer| twice, in some order, every time the program is run.
  • (d) The program will compile without errors and will print |Extender| once and |Implementer| once, in some order, every time the program is run
  • (e) The program will compile without errors and will simply terminate without any output when run.
  • (f) The program will compile without errors, and will print |Extender| once and |Implementer| once, in some order, and terminate because of an runtime error.


(b)

Note

(1) results in the run() method of the Extender class being called, which overrides the method from the Thread class, as does (2).

(3) results in the run() method of the Implementer class being called.

Invoking the start() method on a subclass of the Thread class always results in the overridden run() method being called, regardless of whether a Runnable is passed in a constructor of the subclass.




PreviousNext

Related