CS 2114 Exam 2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

If client code is traversing a list by using an iterator. The efficiency of traversal is:

O(n)

What is the worst-case time complexity for searching a linked-based bag ADT for a particular entry?

O(n)

If client code is traversing a list by repeatedly using a get method and the underlying structure is a linked chain. The efficiency of traversal is:

O(n2)

The time complexity of selection sort is:

O(n2)

The worst-case time complexity of insertion sort is:

O(n2)

Chris implements a standard sorting algorithm that sorts the numbers 64, 25, 12, 22, 11 like so: 64 25 12 22 11 11 25 12 22 64 11 12 25 22 64 11 12 22 25 64 11 12 22 25 64 Which sorting algorithm is this?

Selection sort

To efficiently remove a node at the end of a linked chain implementation of a queue requires a

extra reference in the node pointing to the previous node

public class RecursiveMath ... public int fib (int a) { if (a < 2) return a; else return fib(a-1) + fib(a-2); } ... } What is the base case for fib in the above definition?

fib(a) for a < 2

The dequeue operation:

removes and returns the item at the front of a queue

What would be the contents of a deque that started empty after the following code executes: addBack( "Mazda3"); addFront("Chevrolet Sonic"); addBack("Ford Fiesta"); addBack("Honda Fit");

"Chevrolet Sonic", "Mazda3", "Ford Fiesta", "Honda Fit"

After the following statements execute, what item is at the front of the queue? QueueInterface zooDelivery = new LinkedQueue();zooDelivery.enqueue("lion"); zooDelivery.enqueue("tiger"); zooDelivery.enqueue("cheetah"); String next = zooDelivery.dequeue(); next = zooDelivery.dequeue(); zooDelivery.enqueue("jaguar");

"cheetah"

Consider the following method: public int examMethod(int n) { if (n == 1) return 1; else return (n + this.examMethod(n-1)); } Which of the following inputs will cause a non-terminating recursion?

0

Given the following code: public static int powerOf5(int exponent){ if (exponent == 0) return 1; else return 5 * powerOf5(exponent -1); } what is powerOf5(3)?

125

Consider the following recursive method: public int examMethod(int n) { if (n == 1) return 1; else return (n + this.examMethod(n-1)); } What value is returned by the call examMethod(16)?

136

Given the following code: With the call displayArray3 ({4,5,6,7,8,9,10,11},0,7), how many recursive calls would be made?

14

Given the following code: public static void displayArray(int[] array, int first, int last) { System.out.print(array[first] + " "); if (first < last) displayArray(array, first + 1, last); } With the call displayArray3 ({4,5,6,7,8,9,10,11},0,7), how many recursive calls would be made?

7

Consider the software design for a Vending Machine that both loads and dispenses from the front. With respect to the data structure design decision, which statement is true?

A Stack would be an appropriate data structure for this application

What does the compare method of a comparator object return when the first parameter is less than the second?

Less than 0

With respect to Cybersecurity the term ______ refers to mechanisms which attempt to confirm That users are who they claim to be The "believed" source of data matches the "actual" source of data.

Authenticity

Efficient of list operations Which of the following could be a fourth column for a doubly linked

Choice B

Given fields: private Node firstNode; // Reference to first node private int numberOfEntries; And the following code: private void displayChain(Node curr) { if (curr != null) { System.out.println(curr.getData()); displayChain(curr.getNext()); } } Indicate how each private helper member method would print a non-empty chain

In order

Given fields: private Node firstNode; // Reference to first node private int numberOfEntries; And the following code: private void displayChain(Node curr) { if (curr != null) { displayChain(curr.getNext()); System.out.println(curr.getData()); } } Indicate how each private helper member method would print a non-empty chain

In reverse order

Which cache policy is more susceptible to exploitation by a malicious user (hacker) who knows the cache policy used by a given system?

Least Recently Used

When adding a node newNode to a linked list referenced by head, what code would be the branch for the case that the list is empty?

Head = newNode;

Given fields: private Node firstNode; // Reference to first node private int numberOfEntries; And the following code: private void displayChain(Node curr) { if (curr.getNext() != null) displayChain(curr.getNext()); System.out.println(curr.getData()); } Indicate how each private helper member method would print a non-empty chain

In reverse order

Given the following code: public static int powerOf5(int exponent){ if (exponent == 0) return 1; else return 5 * powerOf5(exponent -1); } what is powerOf5(-2)?

Infinite Recursion

Which of the following is the best option for classes for the Vending Machine? Carefully consider the options presented, remember that some nouns are better suited for fields not classes.

ProjectRunner, Controller, GUI, Product, Payment

Imagine you have an empty stack of strings named stack. List the contents of the stack from top to bottom after the following operations have executed. (Assume that the pop method returns the element that waspopped.) stack.push("K");stack.pop();stack.push("P");stack.push("G");stack.push("R");String str = stack.pop();stack.push("V");stack.push(str);

R V G P

Based on the deque class shown in the videos. What does this code most likely do: firstNode = firstNode.getNext(); if (firstNode == null) lastNode = null; else firstNode.setPreviousNode(null);

Removes the first node from a doubly linked chain

Consider the Vending Machine software design. With respect to a relatively loosely coupled design which incorporates MVC, which of the following would be the preferred arrangement?

The View would include a field reference to the Controller, the reference would be passed via the View's constructor

Given that 0 <= k < number of entries in the list minus 1, after a call to remove(k), getEntry(k) should return

The item formerly stored in position k+1

In cybersecurity the term ______ is used to refer to a weakness in an information system, system security procedures, internal controls, or implementation that could be exploited or triggered by a threat source

Vulnerability

You've got a class that holds two ints and that can be compared with other IntPair objects: Let's denote new IntPair(5, 7) as [5 7]. You've got a list of IntPairs: [3 7], [4 6], [3 4] You sort them using IntPair.compareTo. What is their sorted order?

[3 7], [3 4], [4 6]

Which of the following best describes the 2 key components of a recursive method:

a base case and a recursive case

Suppose you are trying to choose between an array and a singly linked list to store the data in your program. Which data structure must be traversed one element at a time to reach a particular point?

a linked list

The enqueue operation:

adds a new item at the end of the queue

Given a list: bicep curls, burpees, push-ups, sit-ups, squats, tricep dips With an iterator between burpees and push-ups. What would be the state of the list after the following sequence of iterator calls: next(), remove(), next(), remove()?

bicep curls, burpees, squats, tricep dips

This sorting algorithm repeatedly examines neighboring elements, swapping those pairs that are out of order.

bubble sort

Select the best choice below to fill in the blanks: When a __________ implements Comparator, the __________ named ____________ must be implemented.

class, method, compare

Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: frontIndex is 0 and backIndex is -1

invalid

Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: frontIndex is 0 and backIndex is 0

one element in the queue

Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: frontIndex is 2 and backIndex is 2

one element in the queue

Which of the following is a possible sequence of calls to get from: barbells, step, weights, mat with an iterator before barbells to: barbells, weights, mat with an iterator between barbells and weights

next(), next(), remove()

Which of the following methods requires the creation of a new node?

public void add(T newEntry); public void add(int newPosition, T newEntry);

Which of the following methods could throw an IndexOutOfBoundsException?

public void add(int newPosition, T newEntry); public T remove(int givenPosition);

Given a list: bicep curls, burpees, push-ups, sit-ups, squats, tricep dips With an iterator between burpees and push-ups. What would a call to next() return?

push-ups

Based on the deque class shown in the videos. Given the following member method: public T removeBack(){ T back = getBack(); lastNode = lastNode.getPrev(); if (lastNode == null) firstNode = null; else lastNode.setNext(null); return back; } What is the case that is covered when the expression (lastNode == null) is true?

removing from a deque with one item

This sorting algorithm starts by finding the smallest value in the entire array. Then it finds the second smallest value, which is the smallest value among the remaining elements. Then it finds the third smallest value, the fourth, and so on

selection sort

In the demonstrated linked-chain implementation of a StackADT, when a node is popped from the stack

the original first node will no longer be referenced

An example of something that could be built using a QueueADT is a structure that models:

the customers waiting to talk to an online helpdesk

public class RecursiveMath ... public int fib (int a) { if (a < 2) return a; else return fib(a-1) + fib(a-2); } ... } Given the above definition, what is the result of executing the following? RecursiveMath bill = new RecursiveMath(); int x = bill.fib(-1);

x is set to -1

Consider a system with a cache capacity of 3 which uses the FIFO Cache Eviction Policy. If the cache was initially empty, which items would still be in the cache after an application requests files in the following order: File 7, File 55, File 32, File 3, File 44, File 32, File 44, File 55?

File 3, File 44, File 55

Select the most accurate steps to remove a node that contains a designated element.

Find the node that contains the designated element, get a reference to the node before it, reset the next reference of the node before it to be the node after it

The advantage of a hard drive versus cache is that

Hard drives hold more data

public class RecursiveMath ... public int fib (int a) { if (a == 1) return 1; else return fib(a-1) + fib(a-2); } ... } Given the above definition, what is the result of executing the following? RecursiveMath bill = new RecursiveMath(); int x = bill.fib(-1);

The code does not terminate

Both ListInterface and SortedListInterface include the method definition, contains(anEntry). Consider the example implementation of the List contains(anEntry) method presented below. public boolean contains(T anEntry) { boolean found = false;int index = 0;while (!found && (index < numberOfEntries)) { if (anEntry.equals(list[index])){ found = true; } index++;} // end while return found;} // end contains How could the contains(anEntry) method implementation used for List be improved upon when implementing the contains(anEntry) method for a SortedList?

The contains method could be written so that it stops searching the List for anEntry when it passes the location where anEntry should be found if stored within a SortedList.

What would be the contents of a deque that started empty after the following code executes: addFront( "Mazda3"); addFront("Chevrolet Sonic"); addFront("Ford Fiesta"); removeBack(); addBack("Honda Fit");

"Ford Fiesta", "Chevrolet Sonic", "Honda Fit"

Suppose q is an instance of a queue that can store Strings, and I execute the following statements starting with q empty: 1. q.enqueue("Sweden"); 2. q.enqueue("is"); 3. q.enqueue("my"); 4. String w = q.dequeue(); 5. String x = q.peek(); 6. q.enqueue("neighbor"); 7. String y = q.dequeue(); 8. String z = q.dequeue(); What is the value of z after executing these expressions in order?

"my"

Which of the following would correctly complete the isEmpty() method for a linked implementation of a deque in all cases Public boolean isEmpty() { return < fill in code here >; }

(firstNode == null) && (lastNode == null) (firstNode == null) (lastNode == null)

How many times will F be called if n = 5, ignoring F(5) itself? public static long F(int n) {if (n == 0)return 0;if (n == 1) return 1;return F(n-1) + F(n-2);}

14

Suppose you are trying to choose between an array and a singly linked list to store the data in your Java program. Which arguments would correctly support one side or the other?

A and B only Linked lists are better suited to a program where the amount of data can change unpredictably. Arrays provide more efficient access to individual elements.

Given a list: bicep curls, burpees, push-ups, sit-ups, squats, tricep dips With an iterator between burpees and push-ups. What would the state of the iterator be after a call to next() ?

Between push-ups and sit-ups.

Insertion sort on an array is requires (select all that apply):

Comparison of each unsorted item to enough currently sorted items to find its insertion point Shifting of sorted items above each unsorted item's insertion point

With respect to Cybersecurity the term ______ means the protection of data from being disclosed to, viewed, or used by unauthorized users.

Confidentiality

When using the runner technique on a singly linked chain to insert an item in order, both prev and curr nodes are used in order to (select all that apply):

Insert an item before the node that curr references Loop through the items in the chain to find the insertion location

What are the merits of insertion sort compared to bubble sort and selection sort?

It is faster for already sorted data.

What is the primary risk of implementing a SortedList by inheriting from the List class?

Methods like add(newPosition, newEntry) and replace(givenPosition, newEntry) would be inherited, client code can use these methods in a manner that affects the sorted order

Given fields: private Node firstNode; // Reference to first node private int numberOfEntries; And the following code: private void displayChain(Node curr) { if (curr == null) System.out.println(curr.getData()); else displayChainBackwards(curr.getNext()); } Indicate how each private helper member method would print a non-empty chain

Neither

Based on the deque class shown in the videos. Given the following member method: public void addToFront(T newEntry) { DLNode newNode = new DLNode(null,newEntry,firstNode); if (isEmpty()) { lastNode = newNode; } else { firstNode.setPrev(newNode); } firstNode = newNode; } If "Jeep Wrangler" is passed as a parameter to newEntry. What will the prev field of the node containing "Jeep Wrangler" reference?

Null

If an algorithm requires 7 basic operations for an algorithm with a problem size of n, the algorithmic complexity is

O(1)

Suppose QueueADT is implemented using a singly linked list. What is the lowest Big-Oh time complexity that can be achieved for an enqueue method?:

O(1)

What is the time complexity for adding an entry to a fixed-size array-based bag ADT?

O(1)

What is the time complexity for adding an entry to a linked-based bag ADT?

O(1)

What is the main benefit of implementing a SortedList by inheriting from a revised List Base class instead of inheriting from the standard List Base class?

The revised List Base class can be set up so that unwanted methods - like add(newPosition, newEntry) and replace(givenPosition, newEntry) - cannot be inherited by subclasses

You have been hired to produce a high-level software design for a Vending Machine application. This design must take the form of a UML class diagram. Your client has asked you to use good examples of actual vending machines as inspiration for your software design. With respect to other requirements your client has indicated that the physical vending machine would be similar in form, behavior, and features to the machine depicted in the images below.

The system shall allow users to select a product to purchase The system shall dispense a product The system shall process a payment The system shall notify users when a payment has been processed The system shall update product quantity after successfully dispensing a product

Give the following code: public void countDown(int leadTime, String message) { System.out.println(leadTime + "..."); if (leadTime > 0) countDown(leadTime - 1, message); else System.out.println(message); } If client code calls countDown(5,"GAMEOVER"), what is the order of the subsequent method calls?

countDown(4,"GAMEOVER"), countDown(3,"GAMEOVER"), countDown(2,"GAMEOVER"), countDown(1,"GAMEOVER"), countDown(0,"GAMEOVER")

Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: frontIndex is 0 and backIndex is 4

five elements in the queue

Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: frontIndex is 1 and backIndex is 4

four elements in the queue

Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: frontIndex is 3 and backIndex is 0

four elements in the queue

Suppose we have a Queue implemented with a circular array. The capacity is 10 and the size is 5. Which are legal values for front and rear?

front: 5 rear: 9

A 'Cache Hit' occurs when ______

the data item wanted by an application was found within the cache, thereby removing the need for the system to retrieve the data item from the hard drive (or some other storage medium).

In the demoed array-based implementation of a Stack ADT, the entry peek returns may be found at

the last occupied location in the array

Consider the following code: public int examMethod(int n) { if (n == 1) return 1; else if (n > 1) return (n + this.examMethod(n-1)); } What is the purpose of examMethod?

to compute the sum of the positive integers from 1 to n


Set pelajaran terkait

FAA Written - ADS-B surveillance systems

View Set

Organizational behavior chapters 1-13

View Set

EEL 4806 - Final (Ch. 8 - 14) (Only contains 8-11)

View Set

Intro to Psychology Chapter 9 (Psychology in Every Day Life)

View Set