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

Objects and Collections - Finding the output Questions and Answers

1.

What will be the output of the program?

import java.util.*;

public class Sample01
{
    public static void main(String[] args)
    {
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(10); nums.add(20); nums.add(30);
        nums.remove(1);
        System.out.println(nums);
    }
}

Answer: A

There are two remove methods and the int literal 1 selects remove(int index), not remove(Object). The element at index 1 is 20, so it is taken out and the list becomes [10, 30]. Removing the VALUE 1 would have needed nums.remove(Integer.valueOf(1)).

Please log in or register to report this question.

2.

What will be the output of the program?

import java.util.*;

public class Probe02
{
    public static void main(String[] args)
    {
        List<Integer> codes = new ArrayList<Integer>(
            Arrays.asList(5, 7, 9, 5));
        codes.remove(Integer.valueOf(5));
        System.out.println(codes.size() + " " + codes);
    }
}

Answer: B

Wrapping the argument forces the remove(Object) overload, so the list is searched for the VALUE 5 and only the FIRST match is removed, leaving [7, 9, 5] with a size of 3. remove(Object) stops after one element; removeAll would have taken both. Written as remove(5) it would have been read as an index instead.

Please log in or register to report this question.

3.

What will be the output of the program?

import java.util.*;

public class Demo03
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>();
        l.add("a"); l.add("b");
        l.remove(1);
        System.out.println(l);
    }
}

Answer: C

For a List of String there is no ambiguity: an int argument can only mean the index overload, so element 1, which is b, is removed and [a] remains. The overload trap only bites when the element type is Integer, because then both methods accept an int.

Please log in or register to report this question.

4.

What will be the output of the program?

import java.util.*;

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

Answer: D

Correct answer: The program prints true false 1

Set.add returns whether the set was changed by the call. The first add inserts the element and returns true; the second finds it already present, changes nothing and returns false. The size is therefore 1. That boolean return is worth knowing in its own right - it is the idiomatic way to test and insert in one operation.

Please log in or register to report this question.

5.

What will be the output of the program?

import java.util.*;

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

Answer: A

put returns the PREVIOUS value for the key, or null when there was none. The first call returns null, the second returns the 1 it replaced, and the map still holds a single entry because the key was reused. Putting a duplicate key overwrites rather than adding.

Please log in or register to report this question.

6.

What will be the output of the program?

import java.util.*;

public class Fragment06
{
    public static void main(String[] args)
    {
        Map<String, String> m = new HashMap<String, String>();
        m.put(null, "one");
        m.put(null, "two");
        System.out.println(m.get(null) + " " + m.size());
    }
}

Answer: B

HashMap permits one null key, so this stores an entry and then overwrites its value, leaving the map with a single entry holding two. Hashtable and TreeMap both reject a null key with a NullPointerException, which is the distinction being tested.

Please log in or register to report this question.

7.

What will be the output of the program?

import java.util.*;

public class Example07
{
    public static void main(String[] args)
    {
        Set<Integer> s = new TreeSet<Integer>();
        s.add(30); s.add(10); s.add(20);
        System.out.println(s);
    }
}

Answer: C

A TreeSet keeps its elements sorted by their natural ordering, which for Integer is ascending numeric order, so insertion order is irrelevant and the output is [10, 20, 30]. A HashSet would give no ordering guarantee and a LinkedHashSet would preserve insertion order.

Please log in or register to report this question.

8.

What will be the output of the program?

import java.util.*;

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

Answer: D

Collections.sort orders the list in place using the natural ordering of its elements, and for String that is lexicographic. The list becomes [a, b, c]. Note the method returns void: assigning its result would not compile.

Please log in or register to report this question.

9.

What will be the output of the program?

import java.util.*;

public class Trial09
{
    static class Point
    {
        int x;
        Point(int x) { this.x = x; }

        public boolean equals(Object o)
        {
            return o instanceof Point && ((Point) o).x == x;
        }
    }

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

Answer: A

Point overrides equals but not hashCode, so two equal points almost certainly land in different buckets and the set never compares them. Both are stored and the size is 2. Overriding equals without hashCode breaks every hash-based collection in exactly this way.

Please log in or register to report this question.

10.

What will be the output of the program?

import java.util.*;

public class Case10
{
    static class Tag
    {
        String name;
        Tag(String name) { this.name = name; }

        public boolean equals(Object o)
        {
            return o instanceof Tag && ((Tag) o).name.equals(name);
        }

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

    public static void main(String[] args)
    {
        Map<Tag, Integer> counts = new HashMap<Tag, Integer>();
        counts.put(new Tag("red"), 1);
        counts.put(new Tag("red"), 2);
        System.out.println(counts.size() + " " + counts.get(new Tag("red")));
    }
}

Answer: B

Tag overrides equals AND hashCode consistently, so the second put finds the existing key, replaces its value and leaves one entry. A lookup with a third, equal Tag also succeeds, giving 1 and 2. Without hashCode, as in the previous question, each of these would have been treated as a distinct key.

Please log in or register to report this question.

Loading…
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