AmbitionBox

Discover Best Places to work in India

Java Programming

Java Programming Questions and Answers

Showing 131 - 140 of 150 questions

131

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
132

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
133

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
134

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
135

Consider the following code fragment:

Integer w1 = new Integer(2);

Integer w2 = new Integer(2);

if(w1 == w2)

{

System.out.println("w1 is equal to w2!");

}

else

{

System.out.println("w1 is not equal to w2!");

}

What is the output of this code?

a

A. Compiler error

b

B. Runtime error

c

C. w1 is equal to w2!

d

D. w1 is not equal to w2!

correct answer d
136

Consider the following code fragment:

1. ArrayList list = new ArrayList();

2. list.add(new ObjectOne());

3. list.add(new ObjectOne());

4. list.add(new ObjectOne());

5. Collections.sort(list);

6. class ObjectOne {

7. private int x = 0;

8. private int y = 0;

9. }

What is the output of this code fragment?

a

A. Compiler error at line 5

b

B. Runtime error at line 5

c

C. Compiler error at line 3

d

D. Runtime error at line 3

correct answer a
137

Which of the following is an illegal line of code?

a

A. HashMap hmap = new HashMap();

b

B. ArrayList list = new ArrayList();

c

C. List list2 = new ArrayList ();

d

D. All of the above

correct answer b
138

Which of the following collections can you use to store key-value pairs and is thread safe?

a

A. HashTable

b

B. HashMap

c

C. TreeMap

d

D. Vector

correct answer a
139

Consider the following code fragment:

1. import java.io.*;

2. class MyClass{

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

4. NumberStore ns = new NumberStore();

5. System.out.println("The number: " + ns.getInteger());

6. ns.setInteger(10);

7. System.out.println("The number: " + ns.getInteger());

8. }

9. }

10. class NumberStore {

11. int i =5;

12. public void setInteger(Integer x){

13. System.out.println("The number passed in is: " + x);

14. i = x;

15. }

16. public int getInteger( ){

17. return i;

18. }

19.}

What is the output?

a

A. Compiler error at line 6

b

B. Execution error at line 13

c

C. Compiler error at line 3

d

D. The number:5

     The number passed in is:10

     The number: 10

correct answer d
140

Consider the following code fragment:

1. ArrayList list = new ArrayList();

2. list.add(new Integer(1));

3. list.add(new Integer(2));

4. list.add(new Integer(3));

5. Iterator itr = list.iterator();

6. for(Integer wij:list){

7. System.out.println("number: " + wij);

8. }

What is the output from this code fragment?

a

A. number: 1

     number: 2

     number: 3

b

B. Compiler error at line 5

c

C. Compiler error at line 6

d

D. Runtime error

correct answer a
Select a company to compare with

vs

Similar Companies