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

Objects and Collections - Finding the output Questions and Answers

61.

What will be the output of the program?

import java.util.*;

public class Demo61
{
    static class Key
    {
        StringBuilder id;
        Key(String s) { id = new StringBuilder(s); }

        public boolean equals(Object o)
        {
            return o instanceof Key && (
                (Key) o).id.toString().equals(id.toString());
        }

        public int hashCode() { return id.toString().hashCode(); }
    }

    public static void main(String[] args)
    {
        Set<Key> set = new HashSet<Key>();
        Key k = new Key("a");
        set.add(k);
        k.id.append("b");
        System.out.println(set.contains(k) + " " + set.size());
    }
}

Answer: A

The key was filed under the hash of a, and mutating it afterwards changes the hash to that of ab, so the lookup searches the wrong bucket and reports false. The element is still in the set, so the size is 1: it has become unreachable rather than removed. Anything used as a hash key must be effectively immutable.

Please log in or register to report this question.

62.

What will be the output of the program?

import java.util.*;

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

Answer: B

clear empties the list but does not discard it, so isEmpty is true, the size is 0 and the reference is still a perfectly good object rather than null. An emptied collection and a null reference behave very differently at the next method call.

Please log in or register to report this question.

63.

What will be the output of the program?

import java.util.*;

public class Listing63
{
    public static void main(String[] args)
    {
        Map<String, List<String>> m = new HashMap<String, List<String>>();
        List<String> shared = new ArrayList<String>();
        m.put("x", shared);
        m.put("y", shared);
        m.get("x").add("v");
        System.out.println(m.get("y").size() + " " + m.size());
    }
}

Answer: C

Both keys were given the SAME list object, so appending through one key is visible through the other and the size reads 1. The map itself holds two entries because the keys differ. Storing a value twice stores the reference twice, never a copy.

Please log in or register to report this question.

64.

What will be the output of the program?

import java.util.*;

public class Fragment64
{
    public static void main(String[] args)
    {
        List<String> l = new ArrayList<String>();
        l.add(null);
        l.add(null);
        System.out.println(
            l.size() + " " + l.contains(null) + " " + l.indexOf(null));
    }
}

Answer: D

An ArrayList accepts nulls like any other element and does not collapse them, so two adds give a size of 2. contains and indexOf both handle a null argument, reporting that it is present at position 0. A TreeSet or a Hashtable would have refused the null outright.

Please log in or register to report this question.

65.

What will be the output of the program?

import java.util.*;

public class Example65
{
    static class Animal
    {
        public String toString() { return "animal"; }
    }

    static class Dog extends Animal
    {
        public String toString() { return "dog"; }
    }

    public static void main(String[] args)
    {
        List<Animal> pets = new ArrayList<Animal>();
        pets.add(new Dog());
        Animal first = pets.get(0);
        System.out.println(first + " " + (first instanceof Dog));
    }
}

Answer: A

The list is declared to hold Animal, but the object stored is a Dog and both toString and instanceof ask about the runtime type. The declared type of the variable governs which methods may be CALLED, never which implementation runs, so the output is dog and true.

Please log in or register to report this question.

66.

What will be the output of the program?

import java.util.*;

public class Program66
{
    public static void main(String[] args)
    {
        Map<String, Integer> scores = new TreeMap<String, Integer>();
        scores.put("b", 2); scores.put("a", 1); scores.put("c", 3);
        System.out.println(
            ((TreeMap<String, Integer>) scores).firstKey() + " " + scores);
    }
}

Answer: B

A TreeMap keeps its keys in natural order regardless of insertion order, so firstKey is a and the map prints sorted. The cast is needed only because firstKey belongs to SortedMap rather than to the Map interface the variable is declared with.

Please log in or register to report this question.

67.

What will be the output of the program?

import java.util.*;

public class Trial67
{
    public static void main(String[] args)
    {
        List<Integer> l = new ArrayList<Integer>(
            Arrays.asList(10, 20, 30));
        System.out.println(
            l.contains(20) + " " + l.indexOf(40) + " " + l.get(l.indexOf(30)));
    }
}

Answer: C

contains reports the presence of a value by equality, indexOf returns -1 for something absent rather than throwing, and the last call looks up the index of 30 and reads it back. Feeding an unchecked -1 from indexOf into get is the bug this sentinel invites.

Please log in or register to report this question.

68.

What will be the output of the program?

import java.util.*;

public class Case68
{
    public static void main(String[] args)
    {
        Set<Integer> primes = new TreeSet<Integer>(
            Arrays.asList(2, 3, 5, 7));
        Set<Integer> odds = new TreeSet<Integer>(
            Arrays.asList(1, 3, 5, 7, 9));
        Set<Integer> both = new TreeSet<Integer>(primes);
        both.retainAll(odds);
        System.out.println(both + " " + primes.size());
    }
}

Answer: D

retainAll turns the receiver into the intersection, so the copy keeps the odd primes 3, 5 and 7 and drops 2. It modifies only the collection it is called on, so the original primes set is untouched at 4 elements. Copying first is what preserves it; calling retainAll on primes directly would have shrunk it.

Please log in or register to report this question.

69.

What will be the output of the program?

import java.util.*;

public class Sketch69
{
    public static void main(String[] args)
    {
        List<String> months = new ArrayList<String>(
            Arrays.asList("jan", "feb", "mar", "apr", "may", "jun"));
        months.subList(1, 4).clear();
        System.out.println(months + " size " + months.size());
    }
}

Answer: A

A subList is a view onto the backing list, so clearing it deletes that range from the original. The half-open bounds take positions 1, 2 and 3, which are feb, mar and apr, leaving three months. This is the documented idiom for removing a range in a single statement.

Please log in or register to report this question.

70.

What will be the output of the program?

import java.util.*;

public class Item70
{
    public static void main(String[] args)
    {
        Iterator<String> it = new ArrayList<String>(
            Arrays.asList("a", "b")).iterator();
        it.next();
        it.remove();
        it.remove();
    }
}

Answer: B

remove deletes the element the iterator last returned, so it may be called at most once per call to next. The second remove has nothing to delete and throws IllegalStateException. Calling remove before any next fails the same way.

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