AmbitionBox

Discover Best Places to work in India

Java Programming

Java Programming Questions and Answers

Showing 21 - 27 of 27 questions

21

Which of the following methods is not defined in the Thread class?

a

A. yield ( )

b

B. sleep (..)

c

C. run ( )

d

D. wait ( )

correct answer d
22

What would happen when a thread executes the following statement in its run() method?

sleep(500);

a

A. It is going to stop execution and restart execution exactly after 500 milliseconds.

b

B. It is going to stop execution and restart execution not earlier than 500 milliseconds.

c

C. It will result in compiler error because sleep ( ) method cannot be called inside run ( ) method.

d

D. It will result in compiler error because the sleep ( ) method does not take any argument.

correct answer b
23

Consider the following code fragment:

1. public class ThreadTest {

2. public static void main(String[] args) {

3. Counter ct = new Counter();

4. ct.start();

5. System.out.println("The thread has been started");

6. }

7. }

8. class Counter extends Thread {

9. protected void run() {

10. System.out.println("Hello");

11. }

12. }

What would be the output of this code fragment?

a

A. The thread has been started.

     Hello

b

B. Hello

    The thread has been started.

c

C. Either A or B

d

D. Compiler error on line 9

correct answer d
24

Consider the following code:

1. public class ThreadOrder {

2. static int count=0;

3. public static void main(String[] args) {

4. Counter ct = new Counter();

5. Tracker trk1 = new Tracker(ct, "thread one");

6. Tracker trk2 = new Tracker(ct, "thread two");

7. trk1.start();

8. trk2.start();

9. }

10.}

11. class Tracker extends Thread {

12. Counter ct;

13. String message;

14. Tracker(Counter ct, String msg) {

15. this.ct = ct;

16. message = msg;

17. }

18. public void run() {

19. System.out.println(message);

20. }

21. }

22. class Counter {

23. private int count=0;

24. public int nextCounter() {

25. synchronized(this) {

26. count++;

27. return count;

28. }

29. }

30. }

What would be the output of this code fragment?

a

A. thread 1

     thread 2

b

B. thread 2

    thread 1

c

C. Sometimes A, sometimes B

d

D. Runtime exception on line 8

correct answer c
25

Which of the following is a method of the Object class?

a

A. yield ( )

b

B. sleep (..)

c

C. run ( )

d

D. wait ( )

correct answer
26

Which of the following methods guarantee putting a thread out of the running state?

a

A. wait ( )

b

B. yield ( )

c

C. kill ( )

d

D. notify ( )

correct answer a
27

Which of the following is an invalid Thread constructor?

a

A. Thread ( )

b

B. Thread ( into millisec)

c

C. Thread ( Runnable r)

d

D. Thread ( Runnable R, String name)

correct answer b
Select a company to compare with

vs

Similar Companies