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

Objects and Collections - Finding the output Questions and Answers

81.

What will be the output of the program?

import java.util.*;

public class Sketch81
{
    public static void main(String[] args)
    {
        Collection<String> c = new ArrayList<String>();
        c.add("b"); c.add("a");
        System.out.println(c instanceof List);
    }
}

Answer: A

The variable is declared Collection, but instanceof asks about the object, which really is an ArrayList and therefore a List. Declaring a narrower interface limits which methods may be called, never what the object is. Calling get through this variable would not compile.

Please log in or register to report this question.

82.

What will be the output of the program?

import java.util.*;

public class Item82
{
    public static void main(String[] args)
    {
        List<Character> hand = new ArrayList<Character>(
            Arrays.asList('J', 'Q', 'K', 'A'));
        Collections.swap(hand, 0, 3);
        Collections.rotate(hand, 2);
        System.out.println(hand);
    }
}

Answer: B

swap exchanges positions 0 and 3, turning [J, Q, K, A] into [A, Q, K, J]. rotate by 2 then shifts every element two places right, wrapping the tail around to the front, which gives [K, J, A, Q]. Both work in place and return nothing.

Please log in or register to report this question.

83.

What will be the output of the program?

import java.util.*;

public class Sample83
{
    public static void main(String[] args)
    {
        List<String> src = new ArrayList<String>(
            Arrays.asList("x", "y"));
        List<String> dst = new ArrayList<String>(
            Arrays.asList("1", "2", "3"));
        Collections.copy(dst, src);
        System.out.println(dst);
    }
}

Answer: C

copy overwrites the leading elements of the destination and leaves the rest alone, so the third entry survives. It requires the destination to be at least as long as the source and throws IndexOutOfBoundsException otherwise; it never grows the list.

Please log in or register to report this question.

84.

What will be the output of the program?

import java.util.*;

public class Probe84
{
    public static void main(String[] args)
    {
        Map<String, Integer> inventory = new TreeMap<String, Integer>();
        inventory.put("apple", 4);
        Collection<Integer> counts = inventory.values();
        inventory.put("pear", 6);
        inventory.put("apple", 9);
        System.out.println(counts.size() + " " + counts.contains(9));
    }
}

Answer: D

values is a live view rather than a snapshot, so both the new entry and the replaced value show through it: the size reads 2 and the updated 9 is present. No exception arises because nothing is iterating; fail-fast behaviour only applies once an iterator is under way.

Please log in or register to report this question.

85.

What will be the output of the program?

import java.util.*;

public class Demo85
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>();
        System.out.println(l.equals(new LinkedList<String>()) + " " +
            l.equals(new HashSet<String>()));
    }
}

Answer: A

Two empty Lists are equal whatever their implementation class, because List equality is defined across the interface. A List is never equal to a Set, however, since neither side's contract accepts the other, so the second test is false even though both are empty.

Please log in or register to report this question.

86.

What will be the output of the program?

import java.util.*;

public class Listing86
{
    static class Item
    {
        String sku;
        Item(String sku) { this.sku = sku; }

        public boolean equals(Object o)
        {
            return ((Item) o).sku.equals(sku);
        }

        public int hashCode() { return sku.hashCode(); }
    }

    public static void main(String[] args)
    {
        Set<Item> set = new HashSet<Item>();
        set.add(new Item("x"));
        System.out.println(set.contains("x"));
    }
}

Answer: B

contains passes the String to Item.equals, which casts its argument to Item without an instanceof guard and throws ClassCastException. A correct equals begins by testing the type and returns false for anything else, which is why the instanceof check is not optional.

Please log in or register to report this question.

87.

What will be the output of the program?

import java.util.*;

public class Fragment87
{
    public static void main(String[] args)
    {
        Set<String> tags = new LinkedHashSet<String>();
        Collections.addAll(tags, "red", "green");
        Iterator<String> walker = tags.iterator();
        System.out.print(walker.next());
        tags.remove("green");
        System.out.print(walker.next());
    }
}

Answer: C

The first next runs before any change and prints red. Removing an element afterwards is a structural modification, so the next step notices the modification count has moved and throws. The check happens on each call rather than when the iterator is created, which is why part of the output appears first.

Please log in or register to report this question.

88.

What will be the output of the program?

import java.util.*;

public class Example88
{
    public static void main(String[] args)
    {
        Map<Integer, String> m = new HashMap<Integer, String>();
        for (int i = 0; i < 3; i++) m.put(i, "v" + i);
        m.remove(1);
        System.out.println(m.containsKey(1) + " " + m.size() + " " + m.get(1));
    }
}

Answer: D

Correct answer: The program prints false 2 null

The loop puts keys 0, 1 and 2, and remove(1) takes one out. So containsKey(1) is false, the size is 2, and get(1) returns null. Note that a null from get is ambiguous on its own - it means either absent or mapped to null - which is exactly why containsKey exists as a separate method.

Please log in or register to report this question.

89.

What will be the output of the program?

import java.util.*;

public class Program89
{
    public static void main(String[] args)
    {
        List<String> one = Collections.singletonList("only");
        List<String> copy = new ArrayList<String>(one);
        copy.add("more");
        System.out.println(
            one.size() + " " + copy.size() + " " + one.contains("more"));
    }
}

Answer: A

singletonList returns an immutable list of exactly one element, so it cannot be added to, but copying it into an ArrayList gives an ordinary list that can grow. The original stays at 1 and knows nothing of the addition. Copying is the way to build on one of these fixed collections.

Please log in or register to report this question.

90.

What will be the output of the program?

import java.util.*;

public class Trial90
{
    public static void main(String[] args)
    {
        List<Integer> l = new ArrayList<Integer>(
            Arrays.asList(3, 1, 2));
        List<Integer> unmod = Collections.unmodifiableList(l);
        l.add(4);
        System.out.println(unmod.size());
    }
}

Answer: B

The wrapper is a read-only VIEW, not a frozen copy, so it forbids changes made THROUGH it while still reflecting changes made to the list underneath. The size therefore reads 4. Guaranteeing immutability means wrapping a copy that nothing else holds a reference to.

Please log in or register to report this question.

Loading…

Topics

    No topic!
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