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

Objects and Collections - Finding the output Questions and Answers

31.

What will be the output of the program?

import java.util.*;

public class Trial31
{
    public static void main(String[] args)
    {
        Set<String> tags = new TreeSet<String>();
        tags.add("beta");
        tags.add("alpha");
        System.out.print(tags.first() + " ");
        tags.add(null);
        System.out.println(tags);
    }
}

Answer: C

first returns the lowest element under the natural ordering, which is alpha, and that prints. Adding null then fails: a TreeSet must compare each new element against those present, and comparing null throws NullPointerException. A HashSet accepts a single null happily, needing only a hash and equality.

Please log in or register to report this question.

32.

What will be the output of the program?

import java.util.*;

public class Case32
{
    public static void main(String[] args)
    {
        Map<String, String> m = new Hashtable<String, String>();
        m.put("k", null);
        System.out.println(m.size());
    }
}

Answer: D

Hashtable is the legacy synchronised map and permits neither a null key nor a null value, throwing NullPointerException on either. HashMap allows one null key and any number of null values, which is the distinction being drawn.

Please log in or register to report this question.

33.

What will be the output of the program?

import java.util.*;

public class Sketch33
{
    public static void main(String[] args)
    {
        Queue<Integer> pq = new PriorityQueue<Integer>(
            Arrays.asList(30, 10, 20));
        System.out.print(pq.poll());
        System.out.print(pq.poll());
        System.out.print(pq.poll());
    }
}

Answer: A

A PriorityQueue hands elements out in priority order, which for Integer is ascending, so successive polls give 10, 20 then 30 regardless of insertion order. Note that only removal is ordered: printing the queue itself would show internal heap order, which is not sorted.

Please log in or register to report this question.

34.

What will be the output of the program?

import java.util.*;

public class Item34
{
    public static void main(String[] args)
    {
        Deque<String> stack = new ArrayDeque<String>();
        stack.push("a");
        stack.push("b");
        System.out.println(stack.pop() + stack.peek());
    }
}

Answer: B

push and pop both work at the head of a Deque, so this behaves as a stack: b goes on top and comes off first. peek then reports the new head without removing it, which is a. The result is therefore ba, last-in-first-out.

Please log in or register to report this question.

35.

What will be the output of the program?

import java.util.*;

public class Sample35
{
    public static void main(String[] args)
    {
        List<Integer> l = new ArrayList<Integer>(
            Arrays.asList(1, 2, 3, 4));
        boolean changed = l.retainAll(Arrays.asList(2, 4, 6));
        System.out.println(changed + " " + l);
    }
}

Answer: C

retainAll keeps only the elements also present in the argument, so 1 and 3 go and 2 and 4 stay. The 6 is never added, because retainAll only ever removes. It returns true because the list changed, which is how the bulk operations report their effect.

Please log in or register to report this question.

36.

What will be the output of the program?

import java.util.*;

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

Answer: D

addAll returns whether the collection changed as a result of the call. Adding an empty collection changes nothing, so it returns false and the size stays at 2. The return value reports a change, not success, which is the same convention add and remove follow.

Please log in or register to report this question.

37.

What will be the output of the program?

import java.util.*;

public class Demo37
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>(
            Arrays.asList("a", "b"));
        Object[] plain = l.toArray();
        String[] typed = l.toArray(new String[0]);
        System.out.println(plain.length + " " + typed[1]);
    }
}

Answer: A

The no-argument toArray can only promise Object[], while the array-taking form uses the supplied type and returns String[], allocating a bigger array when the given one is too small. Both hold the same two elements, so the length is 2 and the second element is b.

Please log in or register to report this question.

38.

What will be the output of the program?

import java.util.*;

public class Snippet38
{
    public static void main(String[] args)
    {
        List<String> l = Collections.emptyList();
        System.out.print(l.size());
        l.add("a");
    }
}

Answer: B

emptyList returns an immutable shared instance, so the size prints as 0 and the add then throws UnsupportedOperationException. It exists to avoid allocating a new empty list, which is only safe because nothing can modify it.

Please log in or register to report this question.

39.

What will be the output of the program?

import java.util.*;

public class Listing39
{
    public static void main(String[] args)
    {
        int[] a = {3, 1, 2};
        int[] b = {3, 1, 2};
        System.out.println(
            (a == b) + " " + Arrays.equals(a, b) + " " + a.equals(b));
    }
}

Answer: C

Arrays do not override equals, so both == and the inherited equals compare references and are false for two separately created arrays. Arrays.equals is the static helper that compares lengths and then elements, and it is true. The middle result is the only one that looks at contents.

Please log in or register to report this question.

40.

What will be the output of the program?

import java.util.*;

public class Fragment40
{
    public static void main(String[] args)
    {
        int[] a = {5, 1, 4};
        int[] copy = Arrays.copyOfRange(a, 1, 3);
        Arrays.sort(a);
        System.out.println(Arrays.toString(copy) + " " + Arrays.toString(a));
    }
}

Answer: D

copyOfRange takes the start index inclusive and the end exclusive, so it copies positions 1 and 2, giving [1, 4]. It is a copy rather than a view, so sorting the original afterwards leaves it untouched while a becomes [1, 4, 5]. Arrays.sort works in place and returns nothing.

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