Java Chapter 21

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A queue based on a linked list uses the following code class Node{ String element; Node next; Node (String el, Node n) { element = el; next = n; } } Node front = null, rear = null; What is the right code for the boolean empty() method? A) return front == null; B) if (rear == front) return true; else return false; C) return front == rear; D) if (front == null) throw new RunTimeException("Empty"); else return false; return true;

A) return front == null;

A stack based on a linked list is based on the following code class Node{ String element; Node next; Node(String el, Node n) { element = el; next = n; } } Node top = null; The code for testing whether the stack is empty is A) return top == null; B) if (top == 0) return true; else return false; C) return top = null; D) if (top == null) return true; else throw new RunTimeException();

A) return top == null;

The stack peek operation A) returns the item at the top of the stack, but does not remove it B) checks a stack to see if there are any elements in it C) adds a single item to the stack D) removes and returns an item from the stack

A) returns the item at the top of the stack, but does not remove it

In a list implementation of a queue, the end of the list at which elements are added is called A) the rear of the queue B) the head of the queue C) the bottom of the queue D) the front of the queue

A) the rear of the queue

In an implementation of a stack based on a singly-linked list, it is most efficient for the push() method to add a new item A) so that the items in the stack stay sorted in ascending order B) at the beginning of the list C) that is not duplicated by any other item already in the stack D) at the end of the list

B) at the beginning of the list

The stack class provided by the Java Collections Framework A) is not efficient so its use is not recommended B) cannot be used to instantiate a stack of int, or of any primitive type C) can be used to create stacks of int D) can be used to hold values of any type

B) cannot be used to instantiate a stack of int, or of any primitive type

Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = 0; a method that implements the String pop() operation can be written as A) top--; return s[top]; B) if (top == 0) throw new RuntimeException("Underflow"); top--; String temp = s[top]; s[top] = null; return temp; C) if (top == 0) throw new RunTimeException("Underflow"); return s[top-1]; top--; s[top] = null; D) if (top == 0) throw new RunTimeException("Underflow"); String temp = s[top]; top--; s[top] = null; return temp;

B) if (top == 0) throw new RuntimeException("Underflow"); top--; String temp = s[top]; s[top] = null; return temp;

Which of the following operations is not a stack operation? A) remove and return an item with a specified value B) set the element at the bottom of the stack to a specified value C) All of these: that is, none of the above are stack operations

C) All of these: that is, none of the above are stack operations

The operation for adding an item to a queue is called A) requeue B) dequeue C) enqueue D) proqueue

C) enqueue

The stack pop operation A) removes from the stack the number of elements specified by its integer parameter B) removes all items currently on the stack C) extracts one element from the stack and returns it D) does not exist: There is no such stack operation

C) extracts one element from the stack and returns it

A stack based on a linked list is based on the following code class Node{ String element; Node next; Node(String el, Node n) { element = el; next = n; } } Node top = null; The code for implementing the String peek() operation is A) String temp = top.element; if (temp != null) return temp; else throw new RunTimeException("Empty Stack"); B) if (top != null) return top; C) if (top != null) return top.element else throw new RuntimeException("Empty Stack"); D) if (top != null) return top.element; else return top.next;

C) if (top != null) return top.element else throw new RuntimeException("Empty Stack");

Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = 0; a method that implements the String peek() operation can be written as A) if (top == 0) throw new RunTimeException("Underflow"); B) return s[top-1]; C) if (top == 0) throw new RuntimeException("Underflow"); else return s[top-1]; D) return s[top];

C) if (top == 0) throw new RuntimeException("Underflow"); else return s[top-1];

Consider a class that uses the following variables to implement an array-based stack: String [ ] s = new String[100]; int top = -1; // Note top == -1 indicates stack is empty a method that implements a String peek() operation can be written as A) if (top == 0) throw new RunTimeException("Empty Stack"); else { top--; return s[top]; } B) top--; if (top == -1) throw new RunTimeException("Empty Stack"); else return s[top]; C) if (top > -1) return s[top]; else throw new RuntimeException("Empty Stack"); D) if (top == -1) throw new RunTimeException("Empty Stack"); else return s[top -1];

C) if (top > -1) return s[top]; else throw new RuntimeException("Empty Stack");

A stream of cars going through a toll booth is an example of a A) Stack B) hash set C) queue D) priority queue

C) queue

The stack pull operation A) brings two stacks together and combines their elements B) increases the capacity of a stack that is about to fill up C) extracts one element from the stack and returns it D) does not exist: There is no such stack operation

D) does not exist: There is no such stack operation

A queue is a container that allows elements to be stored and removed A) quickly and efficiently B) in a first-in-last-out fashion C) in a last-in-first-out fashion D) in a first-in-first-out fashion

D) in a first-in-first-out fashion

In a queue implementation that uses an array of fixed size, A) the array must be made so larger that the queue will never run out of space B) the queue must implement the List interface C) the array must be created from an ArrayList object D) it is necessary to use the Array as a circular buffer

D) it is necessary to use the Array as a circular buffer

In an array-based implementation of a stack, an operation that needs to add a new element to the stack may not be able to complete because the array is full. In this case, the failed operation should A) alert the user before continuing execution B) print an error message and return from the method C) print an error message and exit D) throw some appropriately defined exception

D) throw some appropriately defined exception

A stack based on a linked list is based on the following code class Node{ String element; Node next; Node(String el, Node n) { element = el; next = n; } } Node top = null; The code for implementing the operation void push(String x) can be written as A) top.add(x); B) top = add(Node x); C) if (top == full) throw new RunTimeException("Overflow"); else top = new Node(x, top); D) top = new Node(x, top);

D) top = new Node(x, top);


Kaugnay na mga set ng pag-aaral

Module 4.1: Hypothesis Testing and Null Hypothesis Significance Testing

View Set

Abnormal Psych Chapter 1 Questions

View Set

Common Fractions with Decimal and Percent Equivalents

View Set

Electrical - J2 - UNVERIFIED QUESTIONS

View Set

KNR 380, Human Resources, Exam 1 Study Guide

View Set