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

Objects and Collections - Finding the output Questions and Answers

11.

What will be the output of the program?

import java.util.*;

public class Sketch11
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>(
            Arrays.asList("a", "b", "c"));
        for (String s : l)
        {
            if (s.equals("b")) l.remove(s);
        }
        System.out.println(l);
    }
}

Answer: C

Structurally modifying a list while a for-each iterator is walking it invalidates that iterator, and the next call to next() throws ConcurrentModificationException. The removal itself succeeds first, but the loop cannot continue. Iterator.remove is the supported way to do this.

Please log in or register to report this question.

12.

What will be the output of the program?

import java.util.*;

public class Item12
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>(
            Arrays.asList("a", "b", "c"));
        Iterator<String> it = l.iterator();
        while (it.hasNext())
        {
            if (it.next().equals("b")) it.remove();
        }
        System.out.println(l);
    }
}

Answer: D

Correct answer: The program prints [a, c]

Removing through the iterator is the supported way to delete during iteration: the iterator updates its own expected modification count, so no ConcurrentModificationException arises and the traversal continues correctly. The result is the list without "b". Calling l.remove("b") inside the loop instead would have thrown on the next call to next.

Please log in or register to report this question.

13.

What will be the output of the program?

import java.util.*;

public class Sample13
{
    public static void main(String[] args)
    {
        List<String> l = Arrays.asList("a", "b");
        l.set(0, "z");
        l.add("c");
        System.out.println(l);
    }
}

Answer: A

Arrays.asList returns a fixed-size view backed by the array. Replacing an element with set is allowed and succeeds, but add changes the size and throws UnsupportedOperationException. Wrapping it in new ArrayList<String>(...) gives a list that can grow.

Please log in or register to report this question.

14.

What will be the output of the program?

import java.util.*;

public class Probe14
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>(
            Arrays.asList("a", "b", "c", "d"));
        System.out.println(l.subList(1, 3));
    }
}

Answer: B

subList takes the start index inclusive and the end index exclusive, so positions 1 and 2 are included and position 3 is not, giving [b, c]. The size is always end minus start. The result is a view, so changes to it write through to the backing list.

Please log in or register to report this question.

15.

What will be the output of the program?

import java.util.*;

public class Demo15
{
    public static void main(String[] args)
    {
        LinkedList<String> queue = new LinkedList<String>();
        queue.addFirst("b");
        queue.addLast("c");
        queue.addFirst("a");
        System.out.println(queue + " " + queue.peekLast());
    }
}

Answer: C

addFirst pushes onto the head and addLast onto the tail, so b then c gives [b, c], and adding a at the head gives [a, b, c]. peekLast reports the tail without removing it, which is c. LinkedList implements Deque, which is where these methods come from.

Please log in or register to report this question.

16.

What will be the output of the program?

import java.util.*;

public class Snippet16
{
    public static void main(String[] args)
    {
        List<Integer> l = new ArrayList<Integer>();
        l.add(127); l.add(127);
        System.out.println(
            (l.get(0) == l.get(1)) + " " + l.get(0).equals(l.get(1)));
    }
}

Answer: D

Autoboxing an int between -128 and 127 is required to return a cached Integer, so both entries reference the same object and the identity test is true. equals compares the values and is also true. The same code with 128 gives false for the first test.

Please log in or register to report this question.

17.

What will be the output of the program?

import java.util.*;

public class Listing17
{
    public static void main(String[] args)
    {
        Integer a = 1000, b = 1000;
        Set<Integer> seen = new HashSet<Integer>();
        seen.add(a);
        seen.add(b);
        System.out.println((a == b) + " " + seen.size());
    }
}

Answer: A

1000 sits outside the guaranteed wrapper cache range of -128 to 127, so the two boxing conversions may create distinct objects and the identity test is false. The HashSet is unaffected by that: it uses equals and hashCode, which Integer implements by value, so the second add is a duplicate and the size is 1.

Please log in or register to report this question.

18.

What will be the output of the program?

import java.util.*;

public class Fragment18
{
    static String lookup(Map<Integer, String> m, Object key)
    {
        return m.get(key);
    }

    public static void main(String[] args)
    {
        Map<Integer, String> ids = new HashMap<Integer, String>();
        ids.put(7, "seven");
        System.out.println(lookup(ids, 7) + " " + lookup(ids, 7L));
    }
}

Answer: B

The key was stored as an Integer. Looking up 7 boxes to an Integer and matches, but 7L boxes to a Long, and Long.equals(Integer) is false whatever the numeric value, so the second lookup misses and returns null. Map.get accepts any Object, so nothing warns you at compile time.

Please log in or register to report this question.

19.

What will be the output of the program?

import java.util.*;

public class Example19
{
    static class Box { int v = 1; }

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

Answer: C

Box does not override equals, so it inherits identity comparison from Object and no two separately created boxes are ever equal. contains searches by equality and therefore returns false, even though the field values match. Overriding equals is what would make this true.

Please log in or register to report this question.

20.

What will be the output of the program?

import java.util.*;

public class Program20
{
    public static void main(String[] args)
    {
        Map<String, Integer> m = new LinkedHashMap<String, Integer>();
        m.put("b", 2); m.put("a", 1); m.put("c", 3);
        System.out.println(m.keySet());
    }
}

Answer: D

A LinkedHashMap preserves insertion order, so the keys come back in the order they were first put: b, a, c. A plain HashMap makes no ordering promise and a TreeMap would sort them. This is the reason to choose LinkedHashMap over HashMap.

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