Machine-translated by Google. Questions are authored in English.
Top

Objects and Collections - Finding the output Questions and Answers

91.

What will be the output of the program?

import java.util.*;

public class Case91
{
    public static void main(String[] args)
    {
        Map<String, Integer> hashed = new HashMap<String, Integer>();
        Map<String, Integer> sorted = new TreeMap<String, Integer>();
        for (String k : new String[] {"z", "a"})
        {
            hashed.put(k, k.length());
            sorted.put(k, k.length());
        }
        System.out.println(hashed.equals(sorted) + " " + sorted.toString());
    }
}

Answer: C

Map equality is defined across the interface as the same set of entries, so the implementation class is irrelevant and a HashMap can equal a TreeMap. Ordering plays no part in equality but does decide printing, and a TreeMap sorts its keys, so it shows a before z.

Please log in or register to report this question.

92.

What will be the output of the program?

import java.util.*;

public class Sketch92
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>(
            Arrays.asList("b", "a", "c"));
        String[] arr = l.toArray(new String[0]);
        Arrays.sort(arr);
        System.out.println(arr[0] + " " + l.get(0));
    }
}

Answer: D

toArray builds a new array holding the same references, so sorting it rearranges the array only and the list keeps its original order. The array starts with a while the list still starts with b. Nothing here is a view; the array and the list are independent containers.

Please log in or register to report this question.

93.

What will be the output of the program?

import java.util.*;

public class Item93
{
    public static void main(String[] args)
    {
        List<Integer> l = new ArrayList<Integer>();
        l.add(1);
        l.add(null);
        int total = 0;
        for (Integer v : l) total += v;
        System.out.println(total);
    }
}

Answer: A

The list happily stores a null, and the loop variable is an Integer, so no conversion happens there. The unboxing occurs in the addition, where the null Integer must become an int, and that throws NullPointerException on the second iteration after the first has already succeeded.

Please log in or register to report this question.

94.

What will be the output of the program?

import java.util.*;

public class Sample94
{
    public static void main(String[] args)
    {
        Set<String> seats = new TreeSet<String>();
        Collections.addAll(seats, "A1", "B2");
        boolean again = seats.add("A1");
        boolean gone = seats.remove("C3");
        System.out.println(
            again + " " + gone + " " + seats.size() + " " + seats.first());
    }
}

Answer: B

add returns false because A1 is already present and the set is unchanged, and remove returns false because C3 was never there. Neither throws. The size stays at 2, and first reports the lowest key under the natural ordering, A1. These methods report a CHANGE, not success.

Please log in or register to report this question.

95.

What will be the output of the program?

import java.util.*;

public class Probe95
{
    public static void main(String[] args)
    {
        Map<String, Integer> m = new HashMap<String, Integer>();
        m.put("a", 1);
        for (Map.Entry<String, Integer> e : m.entrySet())
        {
            System.out.println(e.getKey() + "=" + e.getValue());
        }
    }
}

Answer: C

Iterating entrySet gives each key and value together, so building the text by hand produces a=1. Looping over keySet and calling get for each key gives the same result at the cost of a second lookup per entry, which is why entrySet is preferred.

Please log in or register to report this question.

96.

What will be the output of the program?

import java.util.*;

public class Demo96
{
    static class Box
    {
        int v;
        Box(int v) { this.v = v; }

        public boolean equals(Object o)
        {
            return o instanceof Box && ((Box) o).v == v;
        }

        public int hashCode() { return 1; }
    }

    public static void main(String[] args)
    {
        Set<Box> s = new HashSet<Box>();
        s.add(new Box(1));
        s.add(new Box(2));
        System.out.println(s.size() + " " + s.contains(new Box(2)));
    }
}

Answer: D

A constant hashCode is legal: the contract only requires equal objects to share a hash, not unequal ones to differ. Both boxes land in the same bucket, where equals separates them, so the size is 2 and the lookup succeeds. The cost is performance, since the bucket degenerates into a linear scan.

Please log in or register to report this question.

97.

What will be the output of the program?

import java.util.*;

public class Listing97
{
    public static void main(String[] args)
    {
        List<String> queue = new ArrayList<String>(
            Arrays.asList("a", "b", "c"));
        List<String> done = new ArrayList<String>(queue);
        queue.removeAll(done);
        System.out.println(
            queue.isEmpty() + " " + done.size() + " " + queue.size());
    }
}

Answer: A

removeAll deletes every element that also appears in the argument, and the argument is a copy of the whole list, so everything goes and the queue is empty. It modifies only the receiver, so the copy still holds its 3 elements. An emptied list is still a perfectly good object.

Please log in or register to report this question.

98.

What will be the output of the program?

import java.util.*;

public class Fragment98
{
    public static void main(String[] args)
    {
        Map<String, String> m = new LinkedHashMap<String, String>();
        m.put("a", "1");
        m.put("b", "2");
        Iterator<String> it = m.keySet().iterator();
        it.next();
        it.remove();
        System.out.println(m);
    }
}

Answer: B

Removing through the key-set iterator is the supported way to delete during iteration, and because the set is a view the whole entry goes from the map. Insertion order leaves a first, so a is the one removed and {b=2} remains.

Please log in or register to report this question.

99.

What will be the output of the program?

import java.util.*;

public class Example99
{
    public static void main(String[] args)
    {
        List<Object> l = new ArrayList<Object>();
        l.add(l);
        System.out.println(l.size());
    }
}

Answer: C

A list may hold a reference to itself, so the add succeeds and the size is 1. Nothing recurses until something walks the structure: printing the list rather than its size would call toString on itself and the javadoc leaves that behaviour undefined.

Please log in or register to report this question.

100.

What will be the output of the program?

import java.util.*;

public class Program100
{
    public static void main(String[] args)
    {
        Map<String, Integer> m = new HashMap<String, Integer>();
        String key = new String("k");
        m.put(key, 1);
        System.out.println(m.get("k") + " " + m.get(new String("k")));
    }
}

Answer: D

HashMap finds a key with hashCode and equals, never with reference identity, and String implements both by value. Every one of these three String objects therefore agrees, so both lookups return 1. This is exactly why a mutable key, or one without those methods, breaks lookups.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Objects and Collections - Finding the output Questions and Answers || Objects and Collections - Finding the output MCQ Questions and Answers || Objects and Collections - Finding the output GK Questions and Answers || Objects and Collections - Finding the output GK MCQ Questions || Objects and Collections - Finding the output Multiple Choice Questions and Answers || Objects and Collections - Finding the output GK || GK on Objects and Collections - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming