AmbitionBox

Discover Best Places to work in India

Java Programming

Java Programming Questions and Answers

Showing 1 - 10 of 10 questions

1

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
2

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
3

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
4

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
5

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
6

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
7

Which of the following statements is false?

a

A. A set is a collection that does not allow duplicates.

b

B. A map can store duplicate values.

c

C. A list is a collection that is ordered by index.

d

D. A list is a collection that cannot have duplicates.

correct answer d
8

Which of the following statements is not true about the hashcode?

a

A. The hashcode values of two equal objects must be equal.

b

B. The hashcode values of two unequal objects must be unequal.

c

C. If the hashcode values of two unequal objects are always unequal, it improves the performance.

d

D. A function that always returns a constant is not a very efficient function.

correct answer b
9

Consider the following code fragment:

1. int i = 5;

2. printIt(i);

3. void printIt(Integer wi) {

4. int j = wi;

5. System.out.println("The value is: " + j);

6. }

What is the output of this code?

a

A. The value is: 5

b

B. Compiler error at line 3

c

C. Compiler error at line 2

d

D. Runtime error

correct answer a
10

Which of the following classes implements the java.util.List interface?

a

A. Java.util.HashMapTable

b

B. Java.util.LinkedList

c

C. Java.util.HashTable

d

D. Java.util.OrderedList

correct answer b
Select a company to compare with

vs

Similar Companies