Mock Exam Question on Threads

download Mock Exam Question on Threads

of 2

Transcript of Mock Exam Question on Threads

  • 8/19/2019 Mock Exam Question on Threads

    1/2

    1. Study the Java program given below. Line numbers have been included so that you can refer

    to them in your answer if you wish.

    1. class Car {2. public void steerCar() {3. for (int i = 0; i < 5; i++) {4. if (i 2 == 0) { !! true if i is 0 or an even nu"ber 5. #$ste".out.println(%&read.current%&read().'eta"e() + steers left);

    *. else {,. #$ste".out.println(%&read.current%&read().'eta"e() + steers ri'&t);-. . tr$ {/0. %&read.sleep(/0);//. catc& (nterrupted1ception e) { /2. /3. /4. 15. class Driver extends Thread {/*. private Car car;/,. public river(#trin' na"e Car car) {

    /-. t&is.car = car;/. seta"e(na"e); !! set t&e na"e of t&e %&read20. 2/. public void run() {22. car.steerCar();23. 24. 25. public class ThreadClassExercise {2*. public static void "ain(#trin'6 ar's) {2,. #$ste".out.println(777 drivin' lesson starts 777);2-. Car car = ne8 Car();2. river learner = ne8 river(9ev car);30. river instructor = ne8 river(rina car);3/. learner.start();32. instructor.start();33. #$ste".out.println(777 drivin' lesson ends 777);34. 35.

    (a) Give sample output that the program could produce when run. Comment on the

    output in detail and identify ways in which the output may differ from that which you

    give.

    utput ! """#riving lesson starts"""  """"#riving lesson ends"""

      Lev steers left

    $rina steers left

    Lev steers right

    $rina steers right

    Lev steers left

    $rina steers left

    Lev steers right

    $rina steers right

    Lev steers left

    $rina steers left

  • 8/19/2019 Mock Exam Question on Threads

    2/2

    (b) %&plain the purpose of the 'eyword s$nc&roni:ed  in Java. $llustrate your answer by

    discussing what would happen if it were added to the declaration of the method

    steerCar() in the code above.