Java MCQs on Vectors & Stack of Java Programming Language.
1. Which of these class object can be used to form a dynamic array? Answer: d 2. Which of these are legacy classes? Answer: d 3. Which of these is the interface of legacy? Answer: b 4. What is the name of a data member of class Vector which is used to store a number of elements in the vector? Answer: c 5. Which of these methods is used to add elements in vector at specific location? Answer: d 6. What will be the output of the following Java code? a) 0 7. What will be the output of the following Java code? a) 2 8. What will be the output of the following Java code? a) [3, 2, 6] 9. What will be the output of the following Java code? a) 0 10. What will be the output of the following Java code? a) [3, 5]
a) ArrayList
b) Map
c) Vector
d) ArrayList & Vector
Clarification: 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.
a) Stack
b) Hashtable
c) Vector
d) All of the mentioned
Clarification: Stack, Hashtable, Vector, Properties and Dictionary are legacy classes.
a) Map
b) Enumeration
c) HashMap
d) Hashtable
Clarification: None.
a) length
b) elements
c) elementCount
d) capacity
Clarification: None.
a) add()
b) set()
c) AddElement()
d) addElement()
Clarification: 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.
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));
}
}
b) 3
c) 2
d) 5
Clarification: obj.elementAt(1) returns the value stored at index 1, which is 2.
Output:
$ javac vector.java
$ java vector
2
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());
}
}
b) 3
c) 4
d) 6
Clarification: None.
Output:
$ javac vector.java
$ java vector
4
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(6));
obj.insertElementAt(new Integer(8), 2);
System.out.println(obj);
}
}
b) [3, 2, 8]
c) [3, 2, 6, 8]
d) [3, 2, 8, 6]
Clarification: None.
Output:
$ javac vector.java
$ java vector
[3, 2, 8, 6].
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());
}
}
b) 1
c) true
d) false
Clarification: 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
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);
}
}
b) [3, 2]
c) [3, 2, 5]
d) [3, 5, 2]
Clarification: 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].