Java Questions & Answers – Java.util – Vectors & Stack

This section of our 1000+ Java MCQs focuses on Vectors & Stack of Java Programming Language.

1. Which of these class object can be used to form a dynamic array?
a) ArrayList
b) Map
c) Vector
d) ArrayList & Vector
View Answer

Answer: d
Explanation: 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.

2. Which of these are legacy classes?
a) Stack
b) Hashtable
c) Vector
d) All of the mentioned
View Answer

Answer: d
Explanation: Stack, Hashtable, Vector, Properties and Dictionary are legacy classes.

3. Which of these is the interface of legacy?
a) Map
b) Enumeration
c) HashMap
d) Hashtable
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

4. What is the name of a data member of class Vector which is used to store a number of elements in the vector?
a) length
b) elements
c) elementCount
d) capacity
View Answer

Answer: c
Explanation: None.

5. Which of these methods is used to add elements in vector at specific location?
a) add()
b) set()
c) AddElement()
d) addElement()
View Answer

Answer: d
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             System.out.println(obj.elementAt(1));
  11.         }
  12.     }

a) 0
b) 3
c) 2
d) 5
View Answer

Answer: c
Explanation: obj.elementAt(1) returns the value stored at index 1, which is 2.
Output:

advertisement
$ javac vector.java
$ java vector
2

7. What will be the output of the following Java code?

advertisement
  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             System.out.println(obj.capacity());
  11.         }
  12.     }

a) 2
b) 3
c) 4
d) 6
View Answer

Answer: c
Explanation: None.
Output:

$ javac vector.java
$ java vector
4

8. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(6));
  10.             obj.insertElementAt(new Integer(8), 2);
  11.             System.out.println(obj);
  12.         }
  13.     }

a) [3, 2, 6]
b) [3, 2, 8]
c) [3, 2, 6, 8]
d) [3, 2, 8, 6]
View Answer

Answer: d
Explanation: None.
Output:

$ javac vector.java
$ java vector
[3, 2, 8, 6].

9. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             obj.removeAll(obj);
  11.             System.out.println(obj.isEmpty());
  12.         }
  13.     }

a) 0
b) 1
c) true
d) false
View Answer

Answer: c
Explanation: 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

10. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class stack 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Stack obj = new Stack();
  7.             obj.push(new Integer(3));
  8.             obj.push(new Integer(2));
  9.             obj.pop();
  10.             obj.push(new Integer(5));
  11.      	    System.out.println(obj);
  12.         }
  13.     }

a) [3, 5]
b) [3, 2]
c) [3, 2, 5]
d) [3, 5, 2]
View Answer

Answer: a
Explanation: 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].

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.