Time Remaining :00:00:00

Questions to be answered 15

Java Fundamental Programming Puzzles - Generics / Collections Puzzle 1



Question No : 1

Given a pre-generics implementation of a method:

public static int sum(List list) {
    int sum = 0;
    for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
        int i = ((Integer)iter.next()).intValue();
        sum += i;
    }
    return sum;
}

What three changes allow the class to be used with generics and avoid an unchecked warning?

Question No : 2

A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of add(0, object), but does NOT need to support quick random access. What supports these requirements?

Question No : 3

Given:

// insert code here
private N min, max;
public N getMin() { return min; }
public N getMax() { return max; }
public void add(N added) {
if (min == null || added.doubleValue() < min.doubleValue())
min = added;
if (max == null || added.doubleValue() > max.doubleValue())
max = added;
}
}

Which of the two following, inserted at line 1, will allow the code to compile?

Question No : 4

import java.util.*;

public class Explorer2 {
    public static void main(String[] args) {
        TreeSet s = new TreeSet();
        TreeSet subs = new TreeSet();
        for(int i = 606; i < 613; i++)
        if(i%2 == 0) s.add(i);
        subs = (TreeSet)s.subSet(608, true, 611, true);
        s.add(629);
        System.out.println(s + " " + subs);
    }
}

What is the output?

Question No : 5

public class Score implements Comparable {
	private int wins, losses;
	public Score(int w, int l) { wins = w; losses = l; }
	public int getWins() { return wins; }
	public int getLosses() { return losses; }
	public String toString() {
		return "<" + wins + "," + losses + ">";
	}
	// insert code here
}

Which method will complete this class?

Question No : 6

public class Person {
    private name;
    public Person(String name) {
        this.name = name;
    }
    public int hashCode() {
        return 420;
    }
}

Question No : 7

Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?

Question No : 8

Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?

Question No : 9

Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

Question No : 10

public class TrickyNum<extends Object> {
    
    private X x;
    
    public TrickyNum(X x) {
        this.x = x;
    }
    
    private double getDouble() {
        return x.doubleValue();
    }
    
    public static void main(String args[]) {
        TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));
        System.out.print(a.getDouble());
    }
}

What is the output

Question No : 11

import java.util.*;

public class TryMe {
    public static void main(String args[]) {
        Queue q = new PriorityQueue<String>();
        q.add("3");
        q.add("1");
        q.add("2");
        System.out.print(q.poll() + " ");
        System.out.print(q.peek() + " ");
        System.out.print(q.peek());
    }
}

What is the output

Question No : 12

public class TrickyNum<extends Object> {
    private X x;
    
    public TrickyNum(X x) {
        this.x = x;
    }
    
    private double getDouble() {
        return ((Double) x).doubleValue();
    }
    
    public static void main(String args[]) {
        TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));
        System.out.print(a.getDouble());
    }
}

What is the output?

Question No : 13

public class TrickyNum<extends Object> {
    
    private X x;
    
    public TrickyNum(X x) {
        this.x = x;
    }
    
    private double getDouble() {
        return x.doubleValue();
    }
    
    public static void main(String args[]) {
        TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));
        System.out.print(a.getDouble());
    }
}

What is the output?

Question No : 14

import java.util.*;

class Empty {   
}

class Extended extends Empty {  
}

public class TryMe {    
    public static void doStuff1(List<Empty> list) {
        // some code
    }
    public static void doStuff2(List list) {        
        // some code
    }
    public static void doStuff3(List<? extends Empty> list) {
        // some code            
    }
        
    public static void main(String args[]) {
        List<Empty> list1 = new LinkedList<Empty>();
        List<Extended> list2 = new LinkedList<Extended>();
        // more code here
    }
}

Given the above code, select correct call of the methods from below options.

Question No : 15

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TryMe {
    public static void main(String args[]) {
        List list = new LinkedList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        
        Collections.reverse(list);
        Iterator iter = list.iterator();
        
        for (Object o : iter) {
            System.out.print(o + " ");
        }
    }
}

What is the output?