Java Vector and Stack MCQ

Java Vector and Stack MCQ | In Java, collection framework classes are used to store elements in value or key-value format without size limitation. We have listed some Java Vector and Stack MCQ and programming questions, answer them. See:- Java Collections Quiz-1

Also see- Java Collection Framework OverviewJava Collection InterfaceList Interface in Java

Q1) Methods of vector class is?

a) synchronized
b) non-synchronized
c) Both (a) and (b)
d) None of these

View Answer Answer:- a) synchronized

Q2) Vector class is?

a) Thread-safe
b) Non-Thread safe
c) Both (a) and (b)
d) None of these

View Answer Answer:- a) Thread-safe

At a time only one thread is allowed to operate on vector object & hence it is thread-safe.

Q3) Stack class implements?

a) Cloneable, Serializable
b) AutoCloseable, Cloneable, Serializable
c) Cloneable
d) None of these

View Answer Answer:- d) None of these

Stack class doesn’t implement any interface.

Q4) Stack class is designed for?

a) LIFO order
b) FIFO order
c) Both (a) and (b)
d) None of these

View Answer Answer:- a) LIFO order

The stack class is designed for LIFO (Last in first out) order.

Q5) Find the output of the below Java Stack program?

import java.util.Stack;

public class Test {
   public static void main(String[] args) {

      Stack<Integer> st = new Stack<Integer>();
      st.add(100);
      st.push(50);
      st.add(200);

      st.pop();

      st.add(220);
      st.push(50);

      System.out.println(st);
   }
}

a) [50]
b) [100, 50, 200, 220, 50]
c) [100, 50, 220, 50]
d) [220, 50]

View Answer Answer:- c) [100, 50, 220, 50]

Stack is child class of Vector therefore we can use all the methods of Vector class, List(I), and Collection(I). The push() method is also used to insert elements. The pop() method is used to remove the topmost element.

Before calling pop() the stack was [100, 50, 200]. On calling pop() the topmost element 200 is removed, now the stack is [100, 50]. After that 220 and 50 are inserted into the stack therefore stack contains [100, 50, 220, 50].

Q6) Find the output of the below Java Stack program?

import java.util.Stack;

public class Test {
   public static void main(String[] args) {

      Stack<Integer> st = new Stack<Integer>();
      st.add(100);
      st.push(50);

      System.out.println(st.pop() 
                 + " " + st.pop());
   }
}

a) 100 50
b) 50 100
c) 50 50
d) None of these

View Answer Answer:- b) 50 100

Initially before calling pop() the stack contains [100, 50]. On calling pop() the topmost element 50 is removed and returned. Now, the stack contains [100], again pop() is called which removes the 100. The stack becomes empty.

Q7) Find the output of the below Java Stack program?

import java.util.Stack;

public class Test {
   public static void main(String[] args) {

      Stack<String> st = new Stack<String>();
      System.out.print(st.isEmpty() + " ");
      st.push("Java");
      st.push("Kotlin");
      System.out.println(st.isEmpty());
   }
}

a) true false
b) false true
c) true true
d) false false

View Answer Answer:- a) true false

Q8) Find the output of the below Java Stack program?

import java.util.Stack;

public class Test {
   public static void main(String[] args) {

      Stack<String> st = new Stack<String>();
      st.push("Java");
      st.push("Kotlin");
      System.out.println( st.pop() 
                  + " " + st.pop() 
                  + " " + st.pop() ); 
   }
}

a) Kotlin Java
b) Kotlin Java null
c) Compile time error
d) Exception raised at Runtime

View Answer Answer:- d) Exception raised at Runtime

There are only two elements in the stack. After calling pop() 2 times, the stack becomes empty. Now, on calling pop() we get EmptyStackException because the stack is empty.

Q9) Find the output of the below Java Stack program?

import java.util.Stack;

public class Test {
   public static void main(String[] args) {

      Stack<String> st = new Stack<String>();
      st.push("Java");
      st.push("Kotlin");
      System.out.println( st.peek() 
                  + " " + st.peek() 
                  + " " + st.peek() );
   }
}

a) Kotlin Java
b) Kotlin Java null
c) Kotlin Kotlin Kotlin
d) EmptyStackException

View Answer Answer:- d) Kotlin Kotlin Kotlin

The peek() method fetches the last element but it doesn’t remove the element. Initially, the stack contains [Java, Kotlin]. On calling peek() the stack remains [Java, Kotlin].

Q10) Find the output of the below Java Stack program?

import java.util.Stack;

public class Test {
   public static void main(String[] args) {

      Stack<String> st = new Stack<String>();
      st.push("Java");
      st.push("Kotlin");
      st.push("Python");
      System.out.println(st.search("Java") 
                 + " " + st.search("SQL"));
   }
}

a) 1 0
b) 3 -1
c) 0 -1
d) 3 0

View Answer Answer:- b) 3 -1

If the stack doesn’t contain the given element then the search() method returns -1. But if the stack contains the given element then the search() method returns offset (position from the topmost element, starting from 1). Here “Python” is at offset=1, “Kotlin” is at offset=2, and “Java” is at offset=3.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *