Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
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 Overview, Java Collection Interface, List 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) synchronizedQ2) Vector class is?
a) Thread-safe
b) Non-Thread safe
c) Both (a) and (b)
d) None of these
View Answer
Answer:- a) Thread-safeAt 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 theseStack 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 orderThe 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 100Initially 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 falseQ8) 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 RuntimeThere 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 KotlinThe 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 -1If 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 do you find anything incorrect? Let us know in the comments. Thank you!