CS 272 Chapter 5 Quiz
Only type the code that replaces "// your solution goes here". Do not retype the statements that are given. Before you begin typing your answer, change the "Paragraph" setting on the toolbar to "Preformatted". Complete the following program so that it will do the following: 1. Create a stack of Strings (using Java's Stack class). 2. Using a sentinel loop, input words from the user. (The user will type one word per line.) 3. Push only words that start with 'a' or 'A' onto the stack. 4. Continue inputting words until the user types "quit" (case doesn't matter). import java.util.*; public class Ch5_Quiz { public static void main (String args[ ]) { // your solution goes here } // end main } // end class
A sentinel loop is a while loop that continues the loop operations indefinitely until an exit statement or exit value is entered, such as the user typing "quit". You cannot use standard equals operators (==, !=, <=, >=, <, >) to compare strings, you have to use .equals() or .equalIgnoreCase()
Which of the following correctly declares and instantiates a stack of Strings, using Java's Stack class? A) Stack<String> myStack = new Stack<String> ( ); B) Stack<String> myStack = new Stack( ); C) Stack<String> = new Stack<String> ( ); D) Stack String myStack;
A) Stack<String> myStack = new Stack<String> ( );
Given a stack named myData that currently has two items: 34, 78 (top is 34), what is the output after the following operations? Peek(myData) Push(myData, 2) Push(myData, 15) Pop(myData) Pop(myData) print(IsEmpty(myData)) A) 34 true B) false C) 78 false D) true
B) false
Given a stack named myData that currently has 5 items: 34, 56, 78, 12, 66 (top is 34) what is the output after the following operations? Push(myData 43) Pop(myData) Pop(myData) print(Peek(myData)) Pop(myData) print(Peek(myData)) A) 34 56 B) 43 34 C) 56 78 D) 12 66
C) 56 78
Given a stack named myData that currently has two items: Tom, Sam (top is Tom), what is the output after the following operations? Push(myData, Hal) Pop(myData) Pop(myData) Pop(myData) print(Pop(myData)) A) Hal B) Tom C) null D) Sam
C) null
Given a stack named myData that currently has two items: Tom, Sam (top is Tom), what is the output after the following operations? Push(myData, Hal) Pop(myData) Pop(myData) Pop(myData) print(Pop(myData)) A) Sam B) Tom C) null D) Hal
C) null
Because a stack only allows items to be added and removed at the top, a stack is called a ______ ADT.
Last-in first-out (LIFO)
Given an empty stack named menuItems, what will be the on the stack after the following operations? StackPush(menuItems , Pizza) StackPush(menuItems , Chips) StackPush(menuItems , Nachos) StackPush(menuItems , Tea) print(StackPop(menuItems)) A) Pizza B) Nachos Chips Pizza C) Pizza Chips Nachos Tea D) Tea Nachos Chips Pizza
Nachos Chips Pizza
List the 5 Stack ADT operations that are implemented in Java's Stack class.(different from PA5 stack class implementation)
push(); pop(); peek(); search(); empty();