Java Programming Questions and Answers
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?
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?
Which of the following is an illegal line of code?
Which of the following collections can you use to store key-value pairs and is thread safe?
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?
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?
Which of the following statements is false?
Which of the following statements is not true about the hashcode?
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?
Which of the following classes implements the java.util.List interface?