Download Questions and Answers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Questions and Answers
Q.1) Output:
$ javac vector.java
$ java vector
4
e.
co
m
A. [3, 2, 6]
B. [3, 2, 8]
C. [3, 2, 6, 8]
D. [3, 2, 8, 6]
in
ANSWER : [3, 2, 8, 6]
SOLUTION :
Output:
$ javac vector.java
$ java vector
[3, 2, 8, 6]
tio
A. Java.util.Map
B. Java.util.List
C. Java.util.HashTable
D. Java.util.Collection
nl
Q.2) Which interface does java.util.Hashtable implement?
w
.a
p
ANSWER : Java.util.Map
SOLUTION :
Hash table based implementation of the Map interface.
Q.3) asses is not part of Java's collection framework?Explanation:
w
A. Maps
B. Array
C. Stack
D. Queue
w
ANSWER : Queue
SOLUTION :
Queue is not a part of collection framework.
Q.4) Which of these methods can be used to delete the last element in a LinkedList
object?
A. remove()
B. delete()
C. removeLast()
D. deleteLast()
ANSWER : removeLast()
SOLUTION :
removeLast() and removeFirst() methods are used to remove elements in end and
beginning of a linked list.
Q.5) Which of these method can be used to increase the capacity of ArrayList object
manually?
A. Capacity()
B. increaseCapacity()
C. increasecapacity()
D. ensureCapacity()
Page 1
Questions and Answers
e.
co
m
ANSWER : ensureCapacity()
SOLUTION :
When we add an element, the capacity of ArrayList object increases automatically, but
we can increase it manually to specified length x by using function ensureCapacity(x);
Q.6) Which of the following statements about the hashcode() method are incorrect?
1. The value returned by hashcode() is used in some collection classes to help locate
objects.
2. The hashcode() method is required to return a positive int value.
3. The hashcode() method in the String class is the one inherited from Object.
4. Two new empty String objects will produce identical hashcodes.
in
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 4
tio
nl
ANSWER : 2 and 3
SOLUTION :
(2) is an incorrect statement because there is no such requirement.
(3) is an incorrect statement and therefore a correct answer because the hashcode for a
string is computed from the characters in the string.
w
.a
p
Q.7) What is the output of this program?
w
w
import java.util.*;
class Output {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.ensureCapacity(3);
System.out.println(obj.size());
}
}
A. 1
B. 2
C. 3
D. 4
ANSWER : 1
SOLUTION :
Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the
value is stored only at index 0, therefore obj.size() returns the total number of elements
stored in the obj i:e 1, it has nothing to do with ensureCapacity().
Output:
$ javac Output.java
$ java Output
1
Q.8) class Test1
{
public int value;
public int hashCode() { return 42; }
}
class Test2
{
Page 2
Questions and Answers
public int value;
public int hashcode() { return (int)(value^5); }
}
which statement is true?
e.
co
m
A. class Test1 will not compile.
B. The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
C. The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
D. class Test2 will not compile.
in
ANSWER : The Test1 hashCode() method is less efficient than the Test2 hashCode()
method.
SOLUTION :
The so-called "hashing algorithm" implemented by class Test1 will always return the
same value, 42, which is legal but which will place all of the hash table entries into a
single bucket, the most inefficient setup possible.
nl
Option A and D are incorrect because these classes are legal.
Option B is incorrect based on the logic described above.
w
.a
p
A. Set
B. List
C. Comparator
D. Collection
tio
Q.9) Which of these interface handle sequences?
ANSWER : List
SOLUTION :
A List is an ordered Collection (sometimes called a sequence). Lists may contain
duplicate elements. In addition to the operations inherited from Collection, the List
interface includes operations for the following:
w
w
Positional access : manipulates elements based on their numerical position in the list.
This includes methods such as get, set, add, addAll, and remove.
Search : searches for a specified object in the list and returns its numerical position.
Search methods include indexOf and lastIndexOf.
Iteration : extends Iterator semantics to take advantage of the list's sequential nature.
The listIterator methods provide this behavior.
Range-view : The sublist method performs arbitrary range operations on the list.
Q.10) Which of these interface must contain a unique element?
A. Set
B. List
C. Array
D. Collection
ANSWER : Set
SOLUTION :
Set interface extends collection interface to handle sets, which must contain unique
elements.
Q.11) What is the output of this program?
import java.util.*;
class Maps {
public static void main(String args[]) {
HashMap obj = new HashMap();
Page 3
Questions and Answers
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj);
}
e.
co
m
}
ANSWER : {A=1, B=2, C=3}
SOLUTION :
Output:
$ javac Maps.java
$ java Maps
{A=1, B=2, C=3}
nl
Q.12) What is the output of this program?
in
A. {A 1, B 1, C 1}
B. {A, B, C}
C. {A-1, B-1, C-1}
D. {A=1, B=2, C=3}
w
.a
p
tio
import java.util.*;
class vector {
public static void main(String args[]) {
Vector obj = new Vector(4,2);
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(5));
obj.removeAll(obj);
System.out.println(obj.isEmpty());
}
}
w
w
A. 0
B. 1
C. true
D. false
ANSWER : true
SOLUTION :
firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is
executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns
true.
Output:
$ javac vector.java
$ java vector
true
Q.13) What is the output of this program?
import java.util.*;
class Collection_Algos {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
while(i.hasNext())
System.out.print(i.next() + " ");
}
Page 4
Questions and Answers
}
e.
co
m
A. 2 8 5 1
B. 1 5 8 2
C. 2
D. 2 1 8 5
ANSWER : 2 8 5 1
SOLUTION :
$ javac Collection_Algos.java
$ java Collection_Algos
2851
w
.a
p
tio
nl
import java.util.*;
class Output {
public static void main(String args[]) {
TreeSet t = new TreeSet();
t.add("3");
t.add("9");
t.add("1");
t.add("4");
t.add("8");
System.out.println(t);
}
}
in
Q.14) What is the output of this program?
A. [1, 3, 5, 8, 9]
B. [3, 4, 1, 8, 9]
C. [9, 8, 4, 3, 1]
D. [1, 3, 4, 8, 9]
w
w
ANSWER : [1, 3, 4, 8, 9]
SOLUTION :
TreeSet class uses set to store the values added by function add in ascending order
using tree for storage
Output:
$ javac Output.java
$ java Output
[1, 3, 4, 8, 9]
Q.15) Which of these method is used add an element and corresponding key to a map?
A. put()
B. set()
C. redo()
D. add()
ANSWER : put()
SOLUTION :
Maps revolve around two basic operations, get() and put(). to put a value into a map,
use put(), specifying the key and the value. To obtain a value, call get() , passing the
key as an argument. The value is returned,
Q.16) What is the output of this program?
import java.util.*;
class Maps {
public static void main(String args[]) {
Page 5
Questions and Answers
HashMap obj = new HashMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj.keySet());
}
e.
co
m
}
A. [A, B, C]
B. {A, B, C}
C. {1, 2, 3}
D. [1, 2, 3]
nl
in
ANSWER : [A, B, C]
SOLUTION :
keySet() method returns a set containing all the keys used in the invoking map. Here
keys are characters A, B & C. 1, 2, 3 are the values given to these keys.
Output:
$ javac Maps.java
$ java Maps
[A, B, C]
tio
Q.17) What is the output of this program?
w
w
.a
p
import java.util.*;
class Maps {
public static void main(String args[]) {
TreeMap obj = new TreeMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj.entrySet());
}
}
w
A. [A, B, C]
B. [1, 2, 3]
C. {A=1, B=2, C=3}
D. [A=1, B=2, C=3]
ANSWER : [A=1, B=2, C=3]
SOLUTION :
obj.entrySet() method is used to obtain a set that contains the entries in the map. This
method provides set view of the invoking map.
Output:
$ javac Maps.java
$ java Maps
[A=1, B=2, C=3]
Q.18) Which of these is true about unmodifiableCollection() method?
A. unmodifiableCollection() returns a collection that cannot be modified.
B. unmodifiableCollection() method is available only for List and Set.
C. unmodifiableCollection() is defined in Collection class.
D. None of the mentioned.
ANSWER : unmodifiableCollection() method is available only for List and Set.
SOLUTION :
unmodifiableCollection() is available for al collections, Set, Map, List etc.
Page 6
Questions and Answers
Q.19) What is the output of this program?
e.
co
m
import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5 - i] = i;
Arrays.sort(array);
System.out.print(Arrays.binarySearch(array, 4));
}
}
nl
tio
ANSWER : 3
SOLUTION :
Output:
$ javac Array.java
$ java Array
3
in
A. 2
B. 3
C. 4
D. 5
w
.a
p
Q.20) Which of these class object can be used to form a dynamic array?
A. ArrayList
B. Map
C. Vector
D. Both a & b
w
w
ANSWER : Both a & b
SOLUTION :
Vectors are dynamic arrays, it contains many legacy methods that are not part of
collection framework, and hence these methods are not present in ArrayList. But both
are used to form dynamic arrays.
Q.21) What is the output of this program?
import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5-i] = i;
Arrays.fill(array, 1, 4, 8);
for (int i = 0; i < 5 ; i++)
System.out.print(array[i]);
}
}
A. 12885
B. 12845
C. 58881
D. 54881
ANSWER : 58881
SOLUTION :
array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills
the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
Page 7
Questions and Answers
Output:
$ javac Array.java
$ java Array
58881
in
tio
w
.a
p
A. 2 8 5 1
B. 1 5 8 2
C. 2
D. 2 1 8 5
nl
import java.util.*;
class Collection_Algos {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
e.
co
m
Q.22) What is the output of this program?
w
ANSWER : 1 5 8 2
SOLUTION :
Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after reversing it
became 1->5->8->2.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
1582
w
Q.23) Which collection class allows you to grow or shrink its size and provides indexed
access to its elements, but whose methods are not synchronized?
A. java.util.HashSet
B. java.util.LinkedHashSet
C. java.util.List
D. java.util.ArrayList
ANSWER : java.util.ArrayList
SOLUTION :
All of the collection classes allow you to grow or shrink the size of your collection.
ArrayList provides an index to its elements. The newer collection classes tend not to
have synchronized methods. Vector is an older implementation of ArrayList functionality
and has synchronized methods; it is slower than ArrayList.
Q.24) What is the output of this program?
import java.util.*;
class vector {
public static void main(String args[]) {
Vector obj = new Vector(4,2);
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(5));
System.out.println(obj.elementAt(1));
Page 8
Questions and Answers
}
}
e.
co
m
A. 0
B. 3
C. 2
D. 5
Q.25) What is the output of this program?
w
.a
p
tio
nl
import java.util.*;
class Maps {
public static void main(String args[]) {
TreeMap obj = new TreeMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj.entrySet());
}
}
in
ANSWER : 2
SOLUTION :
obj.elementAt(1) returns the value stored at index 1, which is 2.
Output:
$ javac vector.java
$ java vector
2
A. [A, B, C]
B. [1, 2, 3]
C. {A=1, B=2, C=3}
D. [A=1, B=2, C=3]
w
w
ANSWER : [A=1, B=2, C=3]
SOLUTION :
obj.entrySet() method is used to obtain a set that contains the entries in the map. This
method provides set view of the invoking map.
Output:
$ javac Maps.java
$ java Maps
[A=1, B=2, C=3]
Q.26) Which of these methods can be used to obtain set of all keys in a map?
A. getAll()
B. getKeys()
C. keyall()
D. keySet()
ANSWER : keySet()
SOLUTION :
keySet() methods is used to get a set containing all the keys used in a map. This
method provides set view of the keys in the invoking map.
Q.27) Which of these return type of hasNext() method of an iterator?
A. Integer
B. Double
C. Boolean
Page 9
Questions and Answers
D. Collections Object
e.
co
m
ANSWER : Boolean
SOLUTION :
hasNext() returns boolean values true or false.
Q.28) Which of these method is used to change an element in a LinkedList Object?
A. change()
B. set()
C. redo()
D. add()
nl
A. A group of objects
B. A group of classes
C. A group of interfaces
D. None of the mentioned
tio
Q.29) What is Collection in Java?
in
ANSWER : redo()
SOLUTION :
An element in a LinkedList object can be changed by first using get() to obtain the index
or location of that object and the passing that location to method set() along with its new
value.
w
.a
p
ANSWER : A group of objects
SOLUTION :
A collection is a group of objects, it is similar to String Template Library (STL) of C++
programming language.
Q.30) What will be the output of the program?
w
w
public class Test
{
public static void main (String args[])
{
String str = NULL;
System.out.println(str);
}
}
A. NULL
B. Compile Error
C. Code runs but no output
D. Runtime Exception
ANSWER : Compile Error
SOLUTION :
Option B is correct because to set the value of a String variable to null you must use
"null" and not "NULL".
Q.31) Which of these interface is not a part of Java's collection framework?
A. List
B. Set
C. SortedMap
D. SortedList
ANSWER : SortedList
SOLUTION :
Page 10
Questions and Answers
SortedList is not a part of collection framework.
Q.32) Which statement is true for the class java.util.ArrayList?
e.
co
m
A. The elements in the collection are ordered.
B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. The elements in the collection are accessed using a unique key.
ANSWER : The elements in the collection are ordered.
SOLUTION :
Yes, always the elements in the collection are ordered.
Q.33) Which of the following are Java reserved words?
nl
tio
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 2 and 4
in
1. run
2. import
3. default
4. implement
w
.a
p
ANSWER : 2 and 3
SOLUTION :
(2) - This is a Java keyword
(3) - This is a Java keyword
(1) - Is incorrect because although it is a method of Thread/Runnable it is not a keyword
w
(4) - This is not a Java keyword the keyword is implements
w
Q.34) What will be the output of the program?
public class Test
{
public static void main (String[] args)
{
String foo = args[1];
String bar = args[2];
String baz = args[3];
System.out.println("baz = " + baz); /* Line 8 */
}
}
And the command line invocation:
> java Test red green blue
A. baz =
B. baz = null
C. baz = blue
D. Runtime Exception
ANSWER : Runtime Exception
SOLUTION :
When running the program you entered 3 arguments "red", "green" and "blue". When
Page 11
Questions and Answers
dealing with arrays in java you must remember ALL ARRAYS IN JAVA ARE ZERO
BASED therefore args[0] becomes "red", args[1] becomes "green" and args[2] becomes
"blue".
e.
co
m
When the program entcounters line 8 above at runtime it looks for args[3] which has
never been created therefore you get an
ArrayIndexOutOfBoundsException at runtime.
Q.35) Which of these classes implements Set interface?
in
A. ArrayList
B. HashSet
C. LinkedList
D. DynamicList
nl
ANSWER : HashSet
SOLUTION :
HashSet and TreeSet implements Set interface where as LinkedList and ArrayList
implements List interface.
w
.a
p
A. change()
B. set()
C. redo()
D. add()
tio
Q.36) Which of these method is used to change an element in a LinkedList Object?
ANSWER : redo()
SOLUTION :
An element in a LinkedList object can be changed by first using get() to obtain the index
or location of that object and the passing that location to method set() along with its new
value.
w
w
Q.37) x = 0;
if (x1.hashCode() != x2.hashCode() ) x = x + 1;
if (x3.equals(x4) ) x = x + 10;
if (!x5.equals(x6) ) x = x + 100;
if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
System.out.println("x = " + x);
and assuming that the equals() and hashCode() methods are property implemented, if
the output is "x = 1111", which of the following statements will always be true?
A. x2.equals(x1)
B. x3.hashCode() == x4.hashCode()
C. x5.hashCode() != x6.hashCode()
D. x8.equals(x7)
ANSWER : x3.hashCode() == x4.hashCode()
SOLUTION :
By contract, if two objects are equivalent according to the equals() method, then the
hashCode() method must evaluate them to be ==.
Option A is incorrect because if the hashCode() values are not equal, the two objects
must not be equal.
Option C is incorrect because if equals() is not true there is no guarantee of any result
from hashCode().
Option D is incorrect because hashCode() will often return == even if the two objects do
Page 12
Questions and Answers
not evaluate to equals() being true.
Q.38) Which of these is a method of ListIterator used to obtain index of previous
element?
e.
co
m
A. previous()
B. previousIndex()
C. back()
D. goBack()
ANSWER : previousIndex()
SOLUTION :
previousIndex() returns index of previous element. if there is no previous element then
-1 is returned.
in
Q.39) Which of these classes provide implementation of map interface?
nl
A. ArrayList
B. HashMap
C. LinkedList
D. DynamicList
w
.a
p
tio
ANSWER : HashMap
SOLUTION :
AbstractMap, WeakHashMap, HashMap and TreeMap provide implementation of map
interface.
Q.40) Which of these interface is not a part of Java's collection framework?
A. List
B. Set
C. SortedMap
D. SortedList
w
w
ANSWER : SortedList
SOLUTION :
SortedList is not a part of collection framework.
Q.41) What is the output of this program?
import java.util.*;
class stack {
public static void main(String args[]) {
Stack obj = new Stack();
obj.push(new Integer(3));
obj.push(new Integer(2));
obj.pop();
obj.push(new Integer(5));
System.out.println(obj);
}
}
A. [3, 5]
B. [3, 2]
C. [3, 2, 5]
D. [3, 5, 2]
ANSWER : [3, 5]
SOLUTION :
push() and pop() are standard functions of the class stack, push() inserts in the stack
and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which
Page 13
Questions and Answers
e.
co
m
removes 2 from the stack then again push is used to insert 5 hence stack contains
elements 3 & 5.
Output:
$ javac stack.java
$ java stack
[3, 5]
Q.42) What will be the output of the program?
nl
tio
A. 0
B. null
C. Compile Error
D. NullPointerException at runtime
in
public class Test
{
private static int[] x;
public static void main(String[] args)
{
System.out.println(x[0]);
}
}
w
.a
p
ANSWER : NullPointerException at runtime
SOLUTION :
In the above code the array reference variable x has been declared but it has not been
instantiated i.e. the new statement is missing, for example:
private static int[]x = new int[5];
private static int[x] declares a static i.e. class level array.
the "new" keyword is the word that actually creates said array.
w
w
int[5] in association with the new sets the size of the array. so since the above code
contains no new or size decalarations when you try and access x[0] you are trying to
access a member of an array that has been declared but not intialized hence you get a
NullPointerException at runtime.
Q.43) Which of these interface declares core method that all collections will have?
A. set
B. EventListner
C. Comparator
D. Collection
ANSWER : Collection
SOLUTION :
Collection interfaces defines core methods that all the collections like set, map, arrays
etc will have.
Q.44) Which collection class allows you to associate its elements with key values, and
allows you to retrieve objects in FIFO (first-in, first-out) sequence?
A. java.util.ArrayList
B. java.util.LinkedHashMap
C. java.util.HashMap
D. java.util.TreeMap
ANSWER : java.util.LinkedHashMap
SOLUTION :
Page 14
Questions and Answers
LinkedHashMap is the collection class used for caching purposes. FIFO is another way
to indicate caching behavior. To retrieve LinkedHashMap elements in cached order, use
the values() method and iterate over the resultant collection.
e.
co
m
Q.45) Which of these methods is used to obtain an iterator to the start of collection?
A. start()
B. begin()
C. iteratorSet()
D. iterator()
ANSWER : iterator()
SOLUTION :
To obtain an iterator to the start of the start of the collection we use iterator() method.
in
Q.46) Which of these are legacy classes?
nl
A. Stack
B. Hashtable
C. Vector
D. All of the mentioned
tio
ANSWER : All of the mentioned
SOLUTION :
Stack, Hashtable, Vector, Properties and Dictionary are legacy classes.
w
.a
p
Q.47) What will be the output of the program?
w
w
public static void main(String[] args)
{
Object obj = new Object()
{
public int hashCode()
{
return 42;
}
};
System.out.println(obj.hashCode());
}
A. 42
B. Runtime Exception
C. Compile Error at line 2
D. Compile Error at line 5
ANSWER : 42
SOLUTION :
This code is an example of an anonymous inner class. They can be declared to extend
another class or implement a single interface. Since they have no name you can not use
the "new" keyword on them.
In this case the annoynous class is extending the Object class. Within the {} you place
the methods you want for that class. After this class has been declared its methods can
be used by that object in the usual way e.g. objectname.annoymousClassMethod()
Q.48) What will be the output of the program?
package foo;
import java.util.Vector; /* Line 2 */
private class MyVector extends Vector
{
Page 15
Questions and Answers
nl
tio
A. Compilation will succeed.
B. Compilation will fail at line 3.
C. Compilation will fail at line 5.
D. Compilation will fail at line 15.
in
}
public class MyNewVector extends MyVector
{
public MyNewVector ()
{
i = 4; /* Line 15 */
}
public static void main (String args [])
{
MyVector v = new MyNewVector(); /* Line 19 */
}
}
e.
co
m
int i = 1; /* Line 5 */
public MyVector()
{
i = 2;
}
w
.a
p
ANSWER : Compilation will fail at line 3.
SOLUTION :
Option B is correct. The compiler complains with the error "modifier private not allowed
here". The class is created private and is being used by another class on line 19.
Q.49) What will be the output of the program?
w
w
import java.util.*;
class H
{
public static void main (String[] args)
{
Object x = new Vector().elements();
System.out.print((x instanceof Enumeration)+",");
System.out.print((x instanceof Iterator)+",");
System.out.print(x instanceof ListIterator);
}
}
A. Prints: false,false,false
B. Prints: false,false,true
C. Prints: false,true,false
D. Prints: true,false,false
ANSWER : Prints: true,false,false
SOLUTION :
The Vector.elements method returns an Enumeration over the elements of the vector.
Vector implements the List interface and extends AbstractList so it is also possible to get
an Iterator over a Vector by invoking the iterator or listIterator method.
Q.50) What will be the output of the program?
public class Test
{
private static float[] f = new float[2];
public static void main (String[] args)
{
System.out.println("f[0] = " + f[0]);
Page 16
Questions and Answers
}
}
e.
co
m
A. f[0] = 0
B. f[0] = 0.0
C. Compile Error
D. Runtime Exception
ANSWER : f[0] = 0.0
SOLUTION :
The choices are between Option A and B, what this question is really testing is your
knowledge of default values of an initialized array. This is an array type float i.e. it is a
type that uses decimal point numbers therefore its initial value will be 0.0 and not 0
in
Q.51) Which class does not override the equals() and hashCode() methods, inheriting
them directly from class Object?
nl
A. java.lang.String
B. java.lang.Double
C. java.lang.StringBuffer
D. java.lang.Character
w
.a
p
tio
ANSWER : java.lang.StringBuffer
SOLUTION :
java.lang.StringBuffer is the only class in the list that uses the default methods provided
by class Object.
Q.52) Which of these methods is used to add elements in vector at specific location?
A. add()
B. set()
C. AddElement()
D. addElement()
w
w
ANSWER : addElement()
SOLUTION :
addElement() is used to add data in the vector, to obtain the data we use elementAt()
and to first and last element we use firstElement() and lastElement() respectively.
Q.53) Which of these standard collection classes implements a dynamic array?
A. AbstractList
B. LinkedList
C. ArrayList
D. AbstractSet
ANSWER : ArrayList
SOLUTION :
ArrayList class implements a dynamic array by extending AbstractList class.
Q.54) What is the output of this program?
import java.util.*;
class Collection_iterators {
public static void main(String args[]) {
ListIterator a = list.listIterator();
if(a.previousIndex()! = -1)
while(a.hasNext())
System.out.print(a.next() + " ");
else
System.out.print("EMPTY");
Page 17
Questions and Answers
}
}
e.
co
m
A. 0
B. 1
C. -1
D. EMPTY
Q.55) Which is valid declaration of a float?
nl
A. float f = 1F;
B. float f = 1.0;
C. float f = "1";
D. float f = 1.0d;
in
ANSWER : EMPTY
SOLUTION :
Output:
$ javac Collection_iterators.java
$ java Collection_iterators
EMPTY
w
.a
p
tio
ANSWER : float f = 1F;
SOLUTION :
Option B is incorrect because any literal number with a decimal point u declare the
computer will implicitly cast to double unless you include "F or f"
Option C is incorrect because it is a String.
Option D is incorrect because "d" tells the computer it is a double so therefore you are
trying to put a double value into a float variable i.e there might be a loss of precision.
Q.56) What is the output of this program?
w
w
import java.util.*;
class Arraylist {
public static void main(String args[]) {
ArrayList obj1 = new ArrayList();
ArrayList obj2 = new ArrayList();
obj1.add("A");
obj1.add("B");
obj2.add("A");
obj2.add(1, "B");
System.out.println(obj1.equals(obj2));
}
}
A. 0
B. 1
C. true
D. false
ANSWER : true
SOLUTION :
obj1 and obj2 are an object of class ArrayList hence it is a dynamic array which can
increase and decrease its size. obj.add(";X";) adds to the array element X and
obj.add(1,";X";) adds element x at index position 1 in the list, Both the objects obj1 and
obj2 contain same elements i:e A & B thus obj1.equals(obj2) method returns true.
Output:
$ javac Arraylist.java
$ java Arraylist
Page 18
Questions and Answers
true
Q.57) What is the output of this program?
nl
in
A. 1
B. 2
C. 3
D. null
e.
co
m
import java.util.*;
class Maps {
public static void main(String args[]) {
HashMap obj = new HashMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj.get("B"));
}
}
w
.a
p
tio
ANSWER : 2
SOLUTION :
obj.get(";B";) method is used to obtain the value associated with key ";B";, which is 2.
Output:
$ javac Maps.java
$ java Maps
2
Q.58) What is the output of this program?
w
w
import java.util.*;
class Collection_iterators {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
Collections.shuffle(list);
i.next();
i.remove();
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
A. 2 8 5
B. 2 1 8
C. 2 5 8
D. 8 5 1
ANSWER : 2 1 8
SOLUTION :
i.next() returns the next element in the iteration. i.remove() removes from the underlying
collection the last element returned by this iterator (optional operation). This method can
be called only once per call to next(). The behavior of an iterator is unspecified if the
underlying collection is modified while the iteration is in progress in any way other than
by calling this method.
Output:
Page 19
Questions and Answers
$ javac Collection_iterators.java
$ java Collection_iterators
218
(output will be different on your system)
e.
co
m
Q.59) Which of these methods can be used to search an element in a list?
A. find()
B. get()
C. sort()
D. binaryserach()
Q.60) What is the output of this program?
w
.a
p
tio
nl
import java.util.*;
class Linkedlist {
public static void main(String args[]) {
LinkedList obj = new LinkedList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.addFirst("D");
System.out.println(obj);
}
}
in
ANSWER : binaryserach()
SOLUTION :
binaryserach() method uses binary search to find a specified value. This method must
be applied to sorted arrays.
w
A. [A, B, C]
B. [D, B, C]
C. [A, B, C, D]
D. [D, A, B, C]
w
ANSWER : [D, A, B, C]
SOLUTION :
obj.addFirst('D') method is used to add 'D' to the start of a LinkedList object obj.
Output:
$ javac Linkedlist.java
$ java Linkedlist
[D, A, B, C]
Q.61) What is the output of this program?
import java.util.*;
class Collection_Algos {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
Collections.shuffle(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
Page 20
Questions and Answers
e.
co
m
A. 2 8 5 1
B. 1 5 8 2
C. 1 2 5 8
D. Any random order
ANSWER : Any random order
SOLUTION :
shuffle : randomizes all the elements in a list.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
1528
(output will be different on your system)
in
Q.62) Which of these classes implements Set interface?
nl
A. ArrayList
B. HashSet
C. LinkedList
D. DynamicList
w
.a
p
tio
ANSWER : HashSet
SOLUTION :
HashSet and TreeSet implements Set interface where as LinkedList and ArrayList
implements List interface.
Q.63) What is the output of this program?
w
w
import java.util.*;
class Collection_Algos {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
Collections.sort(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
A. 2 8 5 1
B. 1 5 8 2
C. 1 2 5 8
D. 2 1 8 5
ANSWER : 1 2 5 8
SOLUTION :
Explanation: Collections.sort(list) sorts the given list, the list was 2->8->5->1 after sorting
it became 1->2->5->8.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
1582
Q.64) What is the output of this program?
Page 21
Questions and Answers
e.
co
m
import java.util.*;
class Bitset {
public static void main(String args[]) {
BitSet obj = new BitSet(5);
for (int i = 0; i < 5; ++i)
obj.set(i);
obj.clear(2);
System.out.print(obj);
}
}
in
A. {0, 1, 3, 4}
B. {0, 1, 2, 4}
C. {0, 1, 2, 3, 4}
D. {0, 0, 0, 3, 4}
tio
nl
ANSWER : {0, 1, 3, 4}
SOLUTION :
Output:
$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}
Q.65) Which statement is true for the class java.util.HashSet?
w
.a
p
A. The elements in the collection are ordered.
B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. The elements in the collection are accessed using a unique key.
w
ANSWER : The elements in the collection are guaranteed to be unique.
SOLUTION :
Option C is correct. HashSet implements the Set interface and the Set interface
specifies collection that contains no duplicate elements.
w
Option A is wrong. HashSet makes no guarantees as to the iteration order of the set; in
particular, it does not guarantee that the order will remain constant over time.
Option B is wrong. The set can be modified.
Option D is wrong. This is a Set and not a Map.
Q.66) What is the output of this program?
import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5-i] = i;
Arrays.fill(array, 1, 4, 8);
for (int i = 0; i < 5 ; i++)
System.out.print(array[i]);
}
}
A. 12885
B. 12845
C. 58881
Page 22
Questions and Answers
D. 54881
e.
co
m
ANSWER : 58881
SOLUTION :
array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills
the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
Output:
$ javac Array.java
$ java Array
58881
w
.a
p
A. 0
B. 1
C. true
D. false
tio
nl
import java.util.*;
class vector {
public static void main(String args[]) {
Vector obj = new Vector(4,2);
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(5));
obj.removeAll(obj);
System.out.println(obj.isEmpty());
}
}
in
Q.67) What is the output of this program?
w
w
ANSWER : true
SOLUTION :
firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is
executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns
true.
Output:
$ javac vector.java
$ java vector
true
Q.68) Which of these is Basic interface that all other interface inherits?
A. Set
B. Array
C. List
D. Collection
ANSWER : Collection
SOLUTION :
Collection interface is inherited by all other interfaces like Set, Array, Map etc. It defines
core methods that all the collections like set, map, arrays etc will have
Q.69) What is the numerical range of char?
A. 0 to 32767
B. 0 to 65535
C. -256 to 255
D. -32768 to 32767
ANSWER : 0 to 65535
SOLUTION :
Page 23
Questions and Answers
The char type is integral but unsigned. The range of a variable of type char is from 0 to
216-1 or 0 to 65535. Java characters are Unicode, which is a 16-bit encoding capable of
representing a wide range of international characters. If the most significant nine bits of
a char are 0, then the encoding is the same as seven-bit ASCII.
w
.a
p
ANSWER : 4
SOLUTION :
Output:
$ javac vector.java
$ java vector
4
tio
nl
A. 2
B. 3
C. 4
D. 6
in
import java.util.*;
class vector {
public static void main(String args[]) {
Vector obj = new Vector(4,2);
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(5));
System.out.println(obj.capacity());
}
}
e.
co
m
Q.70) What is the output of this program?
w
Q.71) Suppose that you would like to create an instance of a new Map that has an
iteration order that is the same as the iteration order of an existing instance of a Map.
Which concrete implementation of the Map interface should be used for the new
instance?
w
A. TreeMap
B. HashMap
C. LinkedHashMap
D. The answer depends on the implementation of the existing instance.
ANSWER : LinkedHashMap
SOLUTION :
The iteration order of a Collection is the order in which an iterator moves through the
elements of the Collection. The iteration order of a LinkedHashMap is determined by the
order in which elements are inserted.
When a new LinkedHashMap is created by passing a reference to an existing Collection
to the constructor of a LinkedHashMap the Collection.addAll method will ultimately be
invoked.
The addAll method uses an iterator to the existing Collection to iterate through the
elements of the existing Collection and add each to the instance of the new
LinkedHashMap.
Since the iteration order of the LinkedHashMap is determined by the order of insertion,
the iteration order of the new LinkedHashMap must be the same as the interation order
of the old Collection.
Q.72) What is the output of this program?
Page 24
Questions and Answers
in
A. 0
B. 1
C. true
D. false
e.
co
m
import java.util.*;
class vector {
public static void main(String args[]) {
Vector obj = new Vector(4,2);
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(5));
obj.removeAll(obj);
System.out.println(obj.isEmpty());
}
}
w
.a
p
tio
nl
ANSWER : true
SOLUTION :
firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is
executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns
true.
Output:
$ javac vector.java
$ java vector
true
Q.73) Which of these is an incorrect form of using method max() to obtain maximum
element?
w
A. max(Collection c)
B. max(Collection c, Comparator comp)
C. max(Comparator comp)
D. max(List c)
w
ANSWER : max(Comparator comp)
SOLUTION :
Explanation: It's illegal to call max() only with comparator, we need to give the collection
to be serched into.
Q.74) What is the output of this program?
import java.util.*;
class Output {
public static void main(String args[]) {
HashSet obj = new HashSet();
obj.add("A");
obj.add("B");
obj.add("C");
System.out.println(obj + " " + obj.size());
}
}
A. ABC 3
B. [A, B, C] 3
C. ABC 2
D. [A, B, C] 2
ANSWER : [A, B, C] 3
SOLUTION :
Page 25
Questions and Answers
e.
co
m
HashSet obj creates an hash object which implements Set interface, obj.size() gives the
number of elements stored in the object obj which in this case is 3.
Output:
$ javac Output.java
$ java Output
[A, B, C] 3
tio
A. [3, 5]
B. [3, 2]
C. [3, 2, 5]
D. [3, 5, 2]
nl
import java.util.*;
class stack {
public static void main(String args[]) {
Stack obj = new Stack();
obj.push(new Integer(3));
obj.push(new Integer(2));
obj.pop();
obj.push(new Integer(5));
System.out.println(obj);
}
}
in
Q.75) What is the output of this program?
w
w
.a
p
ANSWER : [3, 5]
SOLUTION :
push() and pop() are standard functions of the class stack, push() inserts in the stack
and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which
removes 2 from the stack then again push is used to insert 5 hence stack contains
elements 3 & 5.
Output:
$ javac stack.java
$ java stack
[3, 5]
w
Q.76) Which two statements are true about comparing two instances of the same class,
given that the equals() and hashCode() methods have been properly overridden?
1. If the equals() method returns true, the hashCode() comparison == must return true.
2. If the equals() method returns false, the hashCode() comparison != must return true.
3. If the hashCode() comparison == returns true, the equals() method must return true.
4. If the hashCode() comparison == returns true, the equals() method might return true.
A. 1 and 4
B. 2 and 3
C. 3 and 4
D. 1 and 3
ANSWER : 1 and 4
SOLUTION :
(1) is a restatement of the equals() and hashCode() contract. (4) is true because if the
hashCode() comparison returns ==, the two objects might or might not be equal.
(2) and (3) are incorrect because the hashCode() method is very flexible in its return
values, and often two dissimilar objects can return the same hash code value.
Q.77) Which of these methods can randomize all elements in a list?
Page 26
Questions and Answers
A. rand()
B. randomize()
C. ambigous()
D. shuffle()
e.
co
m
ANSWER : shuffle()
SOLUTION :
shuffle : randomizes all the elements in a list.
Q.78) Which interface provides the capability to store objects using a key-value pair?
A. Java.util.Map
B. Java.util.Set
C. Java.util.List
D. Java.util.Collection
nl
in
ANSWER : Java.util.Map
SOLUTION :
An object that maps keys to values. A map cannot contain duplicate keys; each key can
map to at most one value.
w
.a
p
A. trim()
B. trimSize()
C. trimTosize()
D. trimToSize()
tio
Q.79) Which of these method is used to reduce the capacity of an ArrayList object?
ANSWER : trimToSize()
SOLUTION :
trimTosize() is used to reduce the size of the array that underlines an ArrayList object.
Q.80) Which of the following are true statements?
w
w
1. The Iterator interface declares only three methods: hasNext, next and remove.
2. The ListIterator interface extends both the List and Iterator interfaces.
3. The ListIterator interface provides forward and backward iteration capabilities.
4. The ListIterator interface provides the ability to modify the List during iteration.
5. The ListIterator interface provides the ability to determine its position in the List.
A. 2, 3, 4 and 5
B. 1, 3, 4 and 5
C. 3, 4 and 5
D. 1, 2 and 3
ANSWER : 1, 3, 4 and 5
SOLUTION :
The ListIterator interface extends the Iterator interface and declares additional methods
to provide forward and backward iteration capabilities, List modification capabilities, and
the ability to determine the position of the iterator in the List.
Q.81) Which of these methods deletes all the elements from invoking collection?
A. clear()
B. reset()
C. delete()
D. refresh()
ANSWER : clear()
SOLUTION :
clear() method removes all the elements from invoking collection.
Page 27
Questions and Answers
Q.82) Which of these method is used to make all elements of an equal to specified
value?
e.
co
m
A. add()
B. fill()
C. all()
D. set()
ANSWER : fill()
SOLUTION :
fill() method assigns a value to all the elements in an array, in other words it fills the
array with specified value.
w
.a
p
tio
nl
import java.util.*;
class Linkedlist {
public static void main(String args[]) {
LinkedList obj = new LinkedList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.removeFirst();
System.out.println(obj);
}
}
in
Q.83) What is the output of this program?
A. [A, B]
B. [B, C]
C. [A, B, C, D]
D. [A, B, C]
w
w
ANSWER : [B, C]
SOLUTION :
Output:
$ javac Linkedlist.java
$ java Linkedlist
[B, C]
Q.84) What is the output of this program?
class Output {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("D");
obj.ensureCapacity(3);
obj.trimToSize();
System.out.println(obj.size());
}
}
A. 1
B. 2
C. 3
D. 4
ANSWER : 2
SOLUTION :
trimTosize() is used to reduce the size of the array that underlines an ArrayList object.
Output:
Page 28
Questions and Answers
$ javac Output.java
$ java Output
2
e.
co
m
Q.85) Which of these methods can convert an object into a List?
A. SetList()
B. ConvertList()
C. singletonList()
D. CopyList()
ANSWER : singletonList()
SOLUTION :
singletonList() returns the object as an immutable List. This is a easy way to convert a
single object into a list. This was added by Java 2.0.
w
.a
p
tio
nl
in
Q.86) /* Missing Statement ? */
public class foo
{
public static void main(String[]args)throws Exception
{
java.io.PrintWriter out = new java.io.PrintWriter();
new java.io.OutputStreamWriter(System.out,true);
out.println("Hello");
}
}
What line of code should replace the missing statement to make this program compile?
A. No statement required.
B. import java.io.*;
C. include java.io.*;
D. import java.io.PrintWriter;
w
w
ANSWER : No statement required.
SOLUTION :
The usual method for using/importing the java packages/classes is by using an import
statement at the top of your code. However it is possible to explicitly import the specific
class that you want to use as you use it which is shown in the code above. The
disadvantage of this however is that every time you create a new object you will have to
use the class path in the case "java.io" then the class name in the long run leading to a
lot more typing
Q.87) What is the output of this program?
import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5 - i] = i;
Arrays.sort(array);
for (int i = 0; i < 5; ++i)
System.out.print(array[i]);;
}
}
A. 12345
B. 54321
C. 1234
D. 5432
Page 29
Questions and Answers
e.
co
m
ANSWER : 54321
SOLUTION :
Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:
$ javac Array.java
$ java Array
12345
nl
w
.a
p
A. one two three four
B. four three two one
C. four one three two
D. one two three four one
tio
TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
System.out.print( it.next() + " " );
}
in
Q.88) What will be the output of the program?
ANSWER : four one three two
SOLUTION :
TreeSet assures no duplicate entries; also, when it is accessed it will return elements in
natural order, which typically means alphabetical.
w
w
Q.89) What is the output of this program?
import java.util.*;
class hashtable {
public static void main(String args[]) {
Hashtable obj = new Hashtable();
obj.put("A", new Integer(3));
obj.put("B", new Integer(2));
obj.put("C", new Integer(8));
obj.remove(new String("A"));
System.out.print(obj);
}
}
A. {C=8, B=2}
B. [C=8, B=2]
C. {A=3, C=8, B=2}
D. [A=3, C=8, B=2]
ANSWER : [C=8, B=2]
SOLUTION :
Output:
$ javac hashtable.java
$ java hashtable
{C=8, B=2}
Q.90) What is the output of this program?
Page 30
Questions and Answers
in
A. 12345
B. 54321
C. 1234
D. 5432
e.
co
m
import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5 - i] = i;
Arrays.sort(array);
for (int i = 0; i < 5; ++i)
System.out.print(array[i]);;
}
}
w
w
w
.a
p
tio
nl
ANSWER : 54321
SOLUTION :
Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:
$ javac Array.java
$ java Array
12345
Page 31