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

Objects and Collections - Finding the output Questions and Answers

51.

What will be the output of the program?

import java.util.*;

public class Listing51
{
    public static void main(String[] args)
    {
        List<String> a = new ArrayList<String>(
            Arrays.asList("x", "y"));
        List<String> b = a;
        b.add("z");
        System.out.println(a.size() + " " + (a == b));
    }
}

Answer: C

Assigning a list to another variable copies the reference, not the contents, so both names refer to one object. Adding through b is visible through a, giving a size of 3, and the identity test is true. A copy needs new ArrayList<String>(a).

Please log in or register to report this question.

52.

What will be the output of the program?

import java.util.*;

public class Fragment52
{
    public static void main(String[] args)
    {
        List<StringBuilder> original = new ArrayList<StringBuilder>();
        original.add(new StringBuilder("ab"));

        List<StringBuilder> copy = new ArrayList<StringBuilder>(
            original);
        copy.get(0).append("c");
        copy.add(new StringBuilder("new"));

        System.out.println(original.size() + " " + original.get(0));
    }
}

Answer: D

The copy constructor makes a SHALLOW copy: a new list holding the same element references. Adding to the copy therefore leaves the original at size 1, but mutating the shared StringBuilder is visible through both, so the original's element reads abc. Only the list is new; the objects inside it are not.

Please log in or register to report this question.

53.

What will be the output of the program?

import java.util.*;

public class Example53
{
    static void grow(List<String> l)
    {
        l.add("added");
        l = new ArrayList<String>();
        l.add("ignored");
    }

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

Answer: A

Java passes the reference by value: the method can modify the object it points at, so the add is visible to the caller, but reassigning the parameter only changes the method's own copy of the reference. The second list is discarded when the method returns.

Please log in or register to report this question.

54.

What will be the output of the program?

import java.util.*;

public class Program54
{
    public static void main(String[] args)
    {
        Map<String, Integer> m = new HashMap<String, Integer>();
        m.put("a", 1);
        Integer missing = m.get("zzz");
        System.out.println(missing);
        int unboxed = m.get("zzz");
    }
}

Answer: B

get returns null for an absent key rather than throwing, so the first print shows null. Assigning that same null to an int forces unboxing, which calls intValue on a null reference and throws NullPointerException. The map lookup is safe; the unboxing is not.

Please log in or register to report this question.

55.

What will be the output of the program?

import java.util.*;

public class Trial55
{
    public static void main(String[] args)
    {
        Set<String> s = new HashSet<String>();
        s.add("a"); s.add("b"); s.add("c");
        s.removeAll(Arrays.asList("b", "z"));
        System.out.println(s.size());
    }
}

Answer: C

removeAll takes out every element that also appears in the argument and quietly ignores those that are absent, so b goes and the missing z is no error. Two elements remain. It returns a boolean saying whether anything changed, which is discarded here.

Please log in or register to report this question.

56.

What will be the output of the program?

import java.util.*;

public class Case56
{
    public static void main(String[] args)
    {
        List<Integer> l = new ArrayList<Integer>(
            Arrays.asList(1, 2, 3));
        for (int i = 0; i < l.size(); i++)
        {
            if (l.get(i) == 2) l.remove(i);
        }
        System.out.println(l);
    }
}

Answer: D

An index loop holds no iterator, so there is nothing to invalidate and no ConcurrentModificationException arises. The 2 is removed and [1, 3] prints. The subtler risk with this pattern is skipping the element that shifts into the freed slot, which does not bite here because the removal is the last match.

Please log in or register to report this question.

57.

What will be the output of the program?

import java.util.*;

public class Sketch57
{
    public static void main(String[] args)
    {
        List<String> log = new ArrayList<String>(
            Arrays.asList("keep", "drop", "drop", "drop", "keep"));
        for (int i = log.size() - 1; i >= 0; i--)
        {
            if (log.get(i).equals("drop")) log.remove(i);
        }
        System.out.println(log);
    }
}

Answer: A

Walking backwards removes every match, because a removal only shifts elements that the loop has already passed. All three drops go and [keep, keep] remains. The same loop running forwards would skip whichever element slid into the freed index, leaving some behind.

Please log in or register to report this question.

58.

What will be the output of the program?

import java.util.*;

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

Answer: B

The copy constructor builds a new map holding the same entries, so writing to the copy cannot affect the original and the two report 1 and 99. It is a shallow copy: the KEYS and VALUES are shared references, which matters only when they are themselves mutable.

Please log in or register to report this question.

59.

What will be the output of the program?

import java.util.*;

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

Answer: C

sort orders its argument in place and returns nothing, so only the copy is rearranged. The original still begins with b while the sorted copy begins with a. Sorting without copying first is the usual way to lose an ordering you needed.

Please log in or register to report this question.

60.

What will be the output of the program?

import java.util.*;

public class Probe60
{
    public static void main(String[] args)
    {
        Set<Integer> s = new LinkedHashSet<Integer>(
            Arrays.asList(3, 1, 3, 2));
        System.out.println(s);
    }
}

Answer: D

A LinkedHashSet keeps insertion order and rejects duplicates, so the repeated 3 is dropped without moving the original, giving [3, 1, 2]. A TreeSet would have sorted them and a HashSet would promise no order at all. Re-adding an existing element does not refresh its position.

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