Question One
public class StackTest{
public static void main(String[] args){
Stack<Integer> stack = new Stack<Integer>();
for(int i = 0; i < 10; i++)
stack.push(i);
stack.pop();
stack.pop();
stack.pop();
}
}
What is the value returned when stack.peek() is called?
back
top
next
Question Two
public class StackTest{
public static void main(String[] args){
Stack<Integer> stackOne = new Stack<Integer>();
Stack<Integer> stackTwo = new Stack<Integer>();
for(int i =0; i<5; i++)
stackOne.push(i);
stackTwo.push(stackOne.pop());
stackTwo.push(stackOne.pop());
stackTwo.push(stackOne.pop());
}
}
What are the values returned by peeking on each stack?
back
top
next
Question Three
public class Test{
public static void main(String[] args){
Stack<Integer> stack = new Stack<Integer>();
int n = 12
while (n > 0){
stack.push(n%2);
n = n/2;
}
String result = "";
while (!stack.isEmpty())
results += stack.pop();
System.out.println(result)
}
}
What is printed out?
back
top
next
Question Four
public class Test{
public static void main(String[] args){
Stack<Integer> stack = new Stack<Integer>();
Scanner scanner = new Scanner(System.in)
while (true){
char ch = scanner.next();
if(ch == '(')
stack.push(1);
else if (ch == ')' && !stack.isEmpty())
stack.pop();
else
System.println("unbalanced");
}
}
}
back
top
next
Question Five
public class Test{
public static void main(String[] args){
Stack<Integer> stackOne = new Stack<Integer>();
Stack<Integer> stackTwo = new Stack<Integer>();
for(int i = 0; i < 100; i++)
stackOne.push(i);
for(int i = 0; i < 100; i++)
if(i%4==0)
stackTwo.push(stackOne.peek());
}
}
How many elements are in each Stack?
back
top
next