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

Objects and Collections - Finding the output Questions and Answers

21.

What will be the output of the program?

import java.util.*;

public class Sketch21
{
    public static void main(String[] args)
    {
        int[] raw = {1, 2, 3};
        System.out.println(Arrays.asList(raw).size());
    }
}

Answer: A

asList takes a varargs parameter of a reference type, and int[] is itself an object rather than an array of objects, so the whole array becomes a single element. The list holds one int[] and its size is 1. An Integer[] would have been spread into three elements.

Please log in or register to report this question.

22.

What will be the output of the program?

import java.util.*;

public class Item22
{
    public static void main(String[] args)
    {
        Integer[] boxed = {10, 20, 30};
        List<Integer> view = Arrays.asList(boxed);
        boxed[0] = 99;
        System.out.println(view.size() + " " + view.get(0));
    }
}

Answer: B

An Integer[] is an array of references, so varargs spreads it and the list holds three elements, unlike the int[] case where the whole array arrives as one object. The list is also a VIEW over that array, not a copy, so writing to the array afterwards is visible through it and the first element reads 99.

Please log in or register to report this question.

23.

What will be the output of the program?

import java.util.*;

public class Sample23
{
    public static void main(String[] args)
    {
        List<String> roles = new ArrayList<String>();
        Collections.addAll(roles, "admin", "editor", "viewer");
        String previous = roles.set(1, "author");
        System.out.println(
            previous + " -> " + roles.get(1) + " size " + roles.size());
    }
}

Answer: C

set replaces the element at the index and returns whatever was there before, so previous holds editor while position 1 now holds author. The size is unchanged at 3 because nothing was inserted. Returning the displaced value is what distinguishes set from the two-argument add, which shifts and grows the list.

Please log in or register to report this question.

24.

What will be the output of the program?

import java.util.*;

public class Probe24
{
    public static void main(String[] args)
    {
        List<Integer> ticks = new ArrayList<Integer>(
            Arrays.asList(10, 30));
        ticks.add(1, 20);
        ticks.add(40);
        System.out.println(ticks + " last=" + ticks.get(ticks.size() - 1));
    }
}

Answer: D

The two-argument add inserts at the index and shifts everything from there one place right, so 20 lands between 10 and 30 without overwriting anything. The single-argument add appends, putting 40 at the end, which is what the last element reports. Each call grows the list by one.

Please log in or register to report this question.

25.

What will be the output of the program?

import java.util.*;

public class Demo25
{
    public static void main(String[] args)
    {
        List<String> a = Arrays.asList("x", "y");
        List<String> b = Arrays.asList("y", "x");
        System.out.println(a.equals(b));
    }
}

Answer: A

List equality is defined by order as well as contents: two lists are equal only when the elements at each position match. Reversing them therefore makes the lists unequal. A Set comparison of the same two collections would have been true, since a Set has no positions.

Please log in or register to report this question.

26.

What will be the output of the program?

import java.util.*;

public class Snippet26
{
    public static void main(String[] args)
    {
        Set<String> a = new HashSet<String>(
            Arrays.asList("x", "y"));
        Set<String> b = new LinkedHashSet<String>(
            Arrays.asList("y", "x"));
        System.out.println(a.equals(b));
    }
}

Answer: B

Set equality asks only whether the two sets contain the same elements, so order is irrelevant and the different implementation classes do not matter either. Both hold x and y, so the result is true. This is exactly where Set and List part company.

Please log in or register to report this question.

27.

What will be the output of the program?

import java.util.*;

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

Answer: C

keySet returns a view backed by the map rather than a copy, so removing a key through it removes the whole entry. The map is left with one entry. The view supports removal but not addition, since adding a key alone would have no value to attach.

Please log in or register to report this question.

28.

What will be the output of the program?

import java.util.*;

public class Fragment28
{
    public static void main(String[] args)
    {
        Map<String, Integer> m = new HashMap<String, Integer>();
        m.put("a", 1); m.put("b", 1);
        System.out.println(
            m.values().size() + " " + new HashSet<Integer>(m.values()).size());
    }
}

Answer: D

values returns a Collection, not a Set, so duplicate values are kept and its size matches the number of entries, which is 2. Copying it into a HashSet collapses the duplicates and gives 1. Keys must be unique; values need not be.

Please log in or register to report this question.

29.

What will be the output of the program?

import java.util.*;

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

Answer: A

frequency counts how many elements equal the given value, comparing with equals, so the two occurrences of 2 give 2. It scans the whole collection rather than stopping at the first match, which is what distinguishes it from contains or indexOf.

Please log in or register to report this question.

30.

What will be the output of the program?

import java.util.*;

public class Program30
{
    public static void main(String[] args)
    {
        List<String> l = Collections.nCopies(3, "a");
        System.out.println(l + " " + l.size());
    }
}

Answer: B

nCopies returns an immutable list containing the same reference the requested number of times, so it prints [a, a, a] with a size of 3. The list is a compact view rather than a real array of three slots, and attempting to modify it throws UnsupportedOperationException.

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