Java Programming Questions and Answers
Which of the following methods is not defined in the Thread class?
What would happen when a thread executes the following statement in its run() method?
sleep(500);
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?
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?
Which of the following is a method of the Object class?
Which of the following methods guarantee putting a thread out of the running state?
Which of the following is an invalid Thread constructor?