2214 Data Structures and Alogrithms- Test 2

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

For the following code, what is returned(assume that ArrayQueue is a correctly implemented class)? { String s; Queue<String> queue = new ArrayQueue<String>(); queue.add("a"); queue.add("b"); queue.remove( ); queue.add("c"); queue.add("d"); queue.remove( ); s = queue.remove( ); }

"b"

The add(int, Object) operation in an ordered list with a linked implementation has what level of complexity?

0(n)

What would be the complexity of the size() method for the linked implementation of a list, if there were no count variable?

0(n)

What is the difference between these two class headers, in terms of how to compareTo method is written? 1. public class TShirt implements Comparable {....} 2. public class TShirt implements Comparable<TShirt> {....}

1 allows TShirt to be compared against anything, 2 requires only TShirt objects to be passed in as arguments to the compareTo method

What is the minimum number of assertions would you need to test the following method? public int myMethod(int x) { int y = 0; if(x > 25) { y = x -25; } else { y = x -1; } return y; }

2

Given the following code using a queue X: X.enqueue(new Integer(4)); X.enqueue(new Integer(3)); Integer y = X.dequeue(); X.enqueue(new Integer(7)); X.enqueue(new Integer(2)); X.enqueue(y); X.enqueue(new Integer(9)); y = X.dequeue(); X.enqueue(new Integer(3)); X.enqueue(new Integer(8));

7

For two instances of a Student class implements the Comparable interface (assume it compares the value of id), if student1.id < student2.id, then student1.compareTo(student2) will return an int that is

< 0

What assert calls do you need to test thatt a dequeue() operation works on a queue of String objects? Assume result is what the dequeue operation returns, expResult is what you think you should get, and instance is the collection. You have created the instance, and just added one element to it, then you removed it. A) AssertEquals(result, expResult); AssertEquals(instance.size(), 0); B) AssertEquals(result, expResult); C) AssertEquals(result, expResult); AssertEquals(instance.size(), 1); D) AssertEquals(instance.size(), 0);

A), but when writing test cases the it goes assertEquals(expResult, result);

What methods from the following make sense for the ADT of a sorted list 1) add(object) 2)addFirst(object) 3)addLast(object) 4)add(index, object) 5)remove(object) 6)removeFirst() 7)removeLast() 8)first() 9)last() 10)size() 11)isEmpty()

All expect 2, 3, 4

A queue processes elements in a _____ manner

FIFO, First In First Out

Binary search requires what to be true of the collection that is being searched? It can't be in an array It can only run on collections of objects, not primitives It must be sorted The collection must be a power of 2

It must be sorted

Assuming you have an initial list L = [a -> -> c], what is the result of the following code? (Assume that a, b, c, are objects and -> is a link between nodes.) L.addLast(d); L.remove(b); L.set(3 ,e);

It will throw an exception -the index in set is beyond the end of the list

For the following code, what is returned? public String testQueue( ) throws EmptyCollectionExpception { String s; Queue<String> queue = new ArrayQueue<Integer>(); queue.add("a"); queue.add("b"); queue.remove( ); queue.remove( ); queue.remove( ); queue.add("c"); queue.add("d"); queue.remove( ); s = queue.remove( ); return s; }

Nothing, an exception is thrown

For the following code, what is returned? public String testQueue( ) throws EmptyCollectionExpception { String s; Queue<String> queue = new ArrayQueue<Integer>(); queue.add("a"); queue.add("b"); queue.remove( ); queue.add("c"); queue.add("d"); queue.remove( ); s = queue.remove( ); return s; }

Nothing, the code won't compile -Line 2, Integer can not be converted to String

The addLast( ) operation in an ordered list implemented with a array has Big O complexity of:

O(1)

The addLast(Object) operation in an ordered list with a linked implementation has what level of complexity?

O(1)

Linear search has a worst case complexity of

O(n)

The add(Object) operation in a sorted list with a linked implementation has what level of complexity? Assume a linear search algorithm is being used.

O(n)

The addToFront() operation in an ordered list with an array implementation has what level of complexity

O(n)

The complexity of a set(index, Object) operation using a linked list implementation of an ordered list is:

O(n)

The worst-case complexity of insertion sort is

O(n^2)

What is the contents of an ArrayList called list, after the following code is run? list.add("a"); list.add("c"); list.add("b"); list.add("e"); list.remove(0); list.remove(2);

["c" "b"]

Assuming you have initial list L = [a -> b -> c], what is the result of the following code? (Assume that a, b, c are nodes contain and -> is a link between nodes.) L.addLast(d); L.remove(2);

[a -> b -> d]

Which of the following test asserts pass when evaluated against a queue instance with 1 item in it? assertFalse(!instance.isEmpty()); assertEquals(instance.size(), 1); assertTrue(instance.isEmpty()); assertNull(instance);

assertEquals(instance.size(), 1);

A queue is appropriate for which of the following domains: contact list maze navigation call center call processing undo tracking

call center call processing

What kinds of operations should matter in evaluating the efficiency of a sort method? assignments only method calls comparisons and assignments comparisons only

comparisons and assignments

Linked nodes have this advantage over arrays for implementing collections:

faster access to elements in the collection

After the smallest item in the unsorted portion of a list is identified during a selection sort, what happens next? it is added to the end of the sorted portion it is swapped with the last item in the unsorted portion it is added to the front of the sorted portion it is swapped with the first item in the unsorted portion

it is swapped with the first item in the unsorted portion

Assume myIntQueue is constructed as follows: LinkedListQueue<Integer> my IntQueue = new LinkedListQueue<>(); Then, myIntQueue.enqueue(56); is equivalent to: myIntQueue.enqueue("56"); myIntQueue.enqueue(new Integer(56)); myIntQueue.enqueue((int)56); myIntQueue.(56);

myIntQueue.enqueue(new Integer(56));

The first() operation on a queue does what?

returns the element from the front of the queue


Set pelajaran terkait

Early Stream of Consciousness and Feminism in Fiction Instruction & Assignment

View Set

AH2 - Exam 1 - Lippincott 11ed - Test 4 - The Client with Pneumonia

View Set

ATI - PN Fundamentals Online Practice 2020 A

View Set

Interpersonal Communication Chapter 2

View Set

NET260.30 LINUX ADMINISTRATION Chapter 11

View Set

Информатика 4 класс Горячев

View Set

Chapter 32: Drug Therapy to Decrease Histamine Effects and Allergic Response

View Set