Friday, October 28, 2011

Java Interview Questions Part-7 (Collection Framework)





Q1.       Given:



34.   HashMap props = new HashMap();

35.   props.put("key45", "some value");

36.   props.put("key12", "some other value");

37.   props.put("key39", "yet another value");

38.   Set s = props.keySet();

39.   // insert code here



What, inserted at line 39, will sort the keys in the props HashMap?



A.   Arrays.sort(s);                                             B.   s = new TreeSet(s);

C.   Collections.sort(s);                                       D.   s = new SortedSet(s);



Q2.       Click the Exhibit button.



Which statement is true about the set variable on line 12?






A.    The set variable contains all six elements from the coll collection, and the order is guaranteed to be

      preserved.

B.    The set variable contains only three elements from the coll collection, and the order is guaranteed to

be preserved.

C.   The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to

      be preserved.

D.   The set variable contains only three elements from the coll collection, but the order is NOT

       guaranteed to be preserved.



Q3.       Given:



23. Object [] myObjects = {

24.        new Integer(12),

25.        new String("foo"),

26.        new Integer(5),

27.        new Boolean(true)

28. };

29. Arrays.sort(myObjects);

30. for(int i=0; i<myObjects.length; i++) {

31.        System.out.print(myObjects[i].toString());

32.        System.out.print(" ");

33. }



What is the result?



A.   Compilation fails due to an error in line 23.

B.   Compilation fails due to an error in line 29.

C.   A ClassCastException occurs in line 29.

D.   A ClassCastException occurs in line 31.

E.   The value of all four objects prints in natural order.



Q4.       Given:



1.    public class Person {

2.         private String name;

3.         public Person(String name) { this.name = name; }

4.         public boolean equals(Person p) {

5.                     return p.name.equals(this.name);

6.         }

7.    }



Which statement is true?



A.   The equals method does NOT properly override the Object.equals method.

B.   Compilation fails because the private attribute p.name cannot be accessed in line 5.

C.   To work correctly with hash-based data structures, this class must also implement the hashCode

      method.

D.   When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent       

      duplicates.



Q5.       Given:



import java.util.*;



public class Old {

public static Object get0(List list) {

return list.get(0);

}

}



Which three will compile successfully? (Choose three.)



A.   Object o = Old.get0(new LinkedList());

B.   Object o = Old.get0(new LinkedList<?>());

C.   String s = Old.get0(new LinkedList<String>());

D.   Object o = Old.get0(new LinkedList<Object>());

E.   String s = (String)Old.get0(new LinkedList<String>());



Q6.       Given:



1.   import java.util.*;

2.   public class Example {

3.         public static void main(String[] args) {

4.                     // insert code here

5.                     set.add(new Integer(2));

6.                     set.add(new Integer(1));

7.                     System.out.println(set);

8.           }

9.   }



Which code, inserted at line 4, guarantees that this program will output [1, 2]?



A. Set set = new TreeSet();

B. Set set = new HashSet();

C. Set set = new SortedSet();

D. List set = new SortedList();

E. Set set = new LinkedHashSet();



Q7.       Given:



public static Collection get() {



            Collection sorted = new LinkedList();

                       

            sorted.add("B");

            sorted.add("C");

            sorted.add("A");

                       

            return sorted;

}

public static void main(String[] args) {



            for (Object obj: get())

                        System.out.print(obj + ", ");

}



What is the result?



A.   A, B, C,                                           B.   B, C, A,                              C.   Compilation fails.

D.   The code runs with no output.           E.   An exception is thrown at runtime.



Q8.       Given:



public static Iterator reverse(List list) {



            Collections.reverse(list);

            return list.iterator();

}



public static void main(String[] args) {



            List list = new ArrayList();



            list.add("1");

            list.add("2");

            list.add("3");

                       

            for (Object obj: reverse(list))

                        System.out.print(obj + ", ");

}



What is the result?



A.   3, 2, 1,              B.   1, 2, 3,              C.   Compilation fails.           D.   The code runs with no output.

E.   An exception is thrown at runtime.



Q9.       Given:



            import java.util.*;



            public class PQ {



                        public static void main(String[] args) {



                                    PriorityQueue<String> pq = new PriorityQueue<String>();

                                   

                                    pq.add("carrot");

                                    pq.add("apple");

                                    pq.add("banana");



                                    System.out.println(pq.poll() + ":" + pq.peek());

                        }

            }



What is the result?



A.   apple:apple                         B.   carrot:apple                         C.   apple:banana

D.   banana:apple                      E.   carrot:carrot                        F.   carrot:banana



Q10.     Given:



import java.util.*;



public class WrappedString {



            private String s;

            public WrappedString(String s) { this.s = s; }

                       

            public static void main(String[] args)  {



                        HashSet<Object> hs = new HashSet<Object>();

                                               

                        WrappedString ws1 = new WrappedString("aardvark");

                                               

                        WrappedString ws2 = new WrappedString("aardvark");



                        String s1 = new String("aardvark");

                                               

                        String s2 = new String("aardvark");



                        hs.add(ws1);

                        hs.add(ws2);

                        hs.add(s1);

                        hs.add(s2);

                                               

                        System.out.println(hs.size());

            }

}



What is the result?



A.  0                 B.  1                 C.  2                 D.  3                 E.  4                  F.  Compilation fails.

G.  An exception is thrown at runtime.



Q11.     Given:



 public class Key {

 private long id1;

 private long id2;

 // class Key methods

 }



A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap. Which two methods should be overridden to assure that Key works correctly as a key? (Choose two.)



A.   public int hashCode()

B.   public boolean equals(Key k)

C.   public int compareTo(Object o)

D.   public boolean equals(Object o)

E.   public boolean compareTo(Key k)



Q12      Given a pre-generics implementation of a method:



11.   public static int sum(List list) {

12.        int sum = 0;

13.        for ( Iterator iter = list.iterator(); iter.hasNext(); ) {

14.                    int i = ((Integer)iter.next()).intValue();

15.                    sum += i;

16.        }

17.        return sum;

18. }



Which three changes must be made to the method sum to use generics? (Choose three.)



A.   remove line 14

B.   replace line 14 with "int i = iter.next();"

C.   replace line 13 with "for (int i : intList) {"

D.   replace line 13 with "for (Iterator iter : intList) {"

E.   replace the method declaration with "sum(List<int> intList)"

F.   replace the method declaration with "sum(List<Integer> intList)"



Q13.     What are major enhancements in 1.4 version of collection frame work?



A.     LinkedHashSet     B.  LinkedHashMap     C.  IdentityHashMap   D.  WeakHashSet



Q14.     We are planning to do an indexed search in a list of objects. Which of the 2 Java collections should you use



A.     ArrayList                B.  LinkedList



Q15.     Which of the following are true about List interface?



A.    List interface is a child interface of Collections interface

B.    Duplicates are allowed

C.    Insertion order is preserved

D.    List is a sub interface of Collection class



Q16.     Which of the following are true about Set interface?



A.    Set is a child interface of Collection class.

B.    It can be used to represent a group of individual objects as a single entity

C.    Duplicate objects are not allowed.

D.    Insertion order is not preserved



Q17.     Which of the following are true about NavigableSet?



A.    It is child interface of SortedSet and provides several utility methods for navigation purposes

B.    It allows duplicates

C.    Insertion order is not preserved

D.    It is introduced in 1.6 version



Q18.     Which of the following are true about Map interface?



A.    Remember it is not a child Interface of Collection Interface and hence Map and Collection Interfaces doesn’t have any relationship.

B.    It cannot be used for representing a group of Objects as key, value pairs.

C.    Both keys and values need not be objects

D.    Keys can’t be duplicated but values can be duplicated.

E.    It has  introduced in 1.2 version



Q19.     Which of the following are true about ArrayList class?



A.    The underlying data structure is resizable or growable array.

B.    Duplicates are not allowed

C.    Heterogeneous objects are not allowed

D.    null insertion is not possible

E.    This class implements RandomAccess, Serializable, Cloneable interfaces



Q20.     Which of the following are true about LinkedList class?



A.    Underlying data Structure is DoubleLinkedList

B.    Allows duplicates objects

C.    Insertion order is not preserved

D.    Does not allow heterogeneous objects

E.    null insertion is possible

F.     This class implements RandomAccess, Serializable, Cloneable interfaces



Q21.     Which of the following are true about Vector class?



A.    The underlying data structure is resizable or growable array.

B.    Duplicates are not allowed

C.    Heterogeneous objects are not allowed

D.    It is a implemented class for List interface

E.     null insertion is possible

F.     Vector class object is not thread safe.



Q22.     Which of the following are true Iterator and ListIterator?



A.    ListIterator is not the child interface of the Iterator

B.    Iterator is the single direction cursor where as ListIterator is bidirectional cursor.

C.    While iterating the elements by Iterator we can perform only read and remove operations. But by using ListIterator we can perform read, removal, replace and addition of new objects also.

D.    Iterator is applicable for every Collecton implemented class object but ListIterator is applicable only for List implemented class objects.

E.     ListIterator cannot be get by using listIterator() method of List interface



Q23.     Which of the following are true about HashSet class?



A.    Insertion order is based on hashcode of the object hence insertion order is not preserved

B.    Best  suitable if frequent operation is  search operations

C.    HashSet class implements Serializable and Cloneable

D.    It is implementation class for List interface

E.     Heterogeneous objects are not allowed



Q24.     Which of the following are true about TreeMap?



A.    The underlying data structure is RED-BLACK Tree

B.    Duplicates keys are allowed but values can be duplicated.

C.    Insertion order is preserved because insertion is based on some sorting order

D.    For non empty TreeMap if we are trying to insert null keys we will get NullPointerException

E.     There are no restrictions for null values.



 Q25.    Which of the following are true about Hashtable



A.    Duplicates keys are allowed but duplicate values are allowed

B.    null insertion is possible for both keys and values

C.    all methods are synchronized

D.    insertion order is preserved because it is  based on hashcode  of keys

E.    heterogeneous Objects are allowed for both keys and values



Q26.     A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of      add(0, object), but does NOT need to support quick random access. What supports these requirements?



            A.   java.util.Queue

            B.   java.util.ArrayList

            C.   java.util.LinearList

            D.   java.util.LinkedList



Q27.     5.         import java.util.Date;

            6.         import java.text.DateFormat;

            21.        DateFormat df;

            22.        Date date = new Date();

            23.        // insert code here

            24.        String s = df.format(date);

           

            Which code fragment, inserted at line 23, allows the code to compile?

           

            A.   df = new DateFormat();

            B.   df = Date.getFormat();

            C.   df = date.getFormat();

            D.   df = DateFormat.getFormat();

            E.   df = DateFormat.getInstance();



Q28.     Given:

            10.  interface A { void x(); }

            11.  class B implements A { public void x() {} public void y() {} }

            12.  class C extends B { public void x() {} }

            And:

            20.  java.util.List<A> list = new java.util.ArrayList<A>();

            21.  list.add(new B());

            22.  list.add(new C());

            23.  for (A a : list) {

            24.        a.x();

            25.        a.y();

            26.  }



            What is the result?



            A.   The code runs with no output.

            B.   An exception is thrown at runtime.

            C.   Compilation fails because of an error in line 20.

            D.   Compilation fails because of an error in line 21.

            E.   Compilation fails because of an error in line 23.

            F.   Compilation fails because of an error in line 25.



Q29.     import java.util.*;



            public class Mapit {



                        public static void main(String[] args) {



                                    Set<Integer> set = new HashSet<Integer>();

                                    Integer i1 = 45;

                                    Integer i2 = 46;



                                    set.add(i1);

                                    set.add(i1);

                                    set.add(i2);

                                    System.out.print(set.size() + " ");

                                   

                                    set.remove(i1);

                                    System.out.print(set.size() + " ");

           

                                    i2 = 47;

                                    set.remove(i2);

                                    System.out.print(set.size() + " ");

                        }

            }


            What is the result?



            A. 2 1 0                  B. 2 1 1                   C. 3 2 1                  D. 3 2 2                E. Compilation fails.

            F. An exception is thrown at runtime.



Q30.     Given a class whose instances, when found in a collection of objects, are sorted by using the compareTo()   method, which two statements are true? (Choose two.)



            A.   The class implements java.lang.Comparable.

            B.   The class implements java.util.Comparator.

            C.   The interface used to implement sorting allows this class to define only one sort sequence.

            D.   The interface used to implement sorting allows this class to define many different sort sequences.


0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju