COP3530 CHP 01 - CHP 06

Ace your homework & exams now with Quizwiz!

The keywords True and False are floating point values. Answers: True False

Answer : False

In computer science, collections are also called objective data types (ODTs). Answers: True False

Answer: False

Indenting code that should be executed when an if statement evaluates as true makes your program easier to read, but the indentation is not necessary. Answers: True False

Answer: False

Python programs require two or more modules. Answers: True False

Answer: False

The use of collections is a Python programming requirement, but they are not very useful for working with real-world objects. Answers: True False

Answer: False

To make your program more secure, use obscure variable names such as xz14dEEa. Answers: True False

Answer: False

A dictionary object contains zero or more entries where each entry associates a unique key with a value. Answers: True False

Answer: True

To traverse a collection's items, use the for loop. Answers: True False

Answer: True

A bubble sort always requires approximately n2 comparisons. Answers: True False

Answer: False

A line of customers waiting to get into a movie is an example of a hierarchical collection. Answers: True False

Answer: False

A linked structure can have items that have a maximum of one link to another item. Answers: True False

Answer: False

An algorithm that uses the exact same Python code run on two different machines should have the same performance data. Answers: True False

Answer: False

If you clone an object using the = operator (as in myList=list(yourList)), the is operator returns True while the == operator returns False. Selected Answer: True False

Answer: False

In a selection sort, the inner loop is executed n - 2 times for every pass through the outer loop. Answers: True False

Answer: False

It's easier to get to an item's predecessor in a singly linked structure than in a doubly linked structure. Answers: True False

Answer: False

Similar to an array, linked structures support random access. Answers: True False

Answer: False

The list is the primary implementing structure in Python collections. Answers: True False

Answer: False

The time() function returns the amount of time a program has been running in seconds. Answers: True False

Answer: False

When a list's append method results in memory wasted beyond a threshold, the size of the underlying array is decreased. Answers: True False

Answer: False

Benchmarking is the process of using the computer's clock to obtain run time information for an algorithm. Answers: True False

Answer: True

In the first step of the quicksort, you begin by selecting the item at the list's midpoint. Answers: True False

Answer: True

On a linked structure, index-based operations must be emulated by manipulating links within the structure. Answers: True False

Answer: True

Python's in operator is implemented as a method named __contains__ in the list class. Answers: True False

Answer: True

The bubble sort has a complexity of O( n 2). Answers: True False

Answer: True

The first item in a singly linked structure is accessed using a head link. Answers: True False

Answer: True

Time performance is improved if you double the size of the array when you need to increase its size. True False

Answer: True

Using a binary search, a list of 1 million items requires only 20 comparisons. Answers: True False

Answer: True

When an array object is traversed in a for loop, the object's __iter__ method is called. Answers: True False

Answer: True

What are the values of variables x, y, and z after the following code is run? y = 0z = 0for x in range(5,7):if y > z:z, y = y, zy = y + x; Answers: a. x == 6, y == 6, z == 5 b. x == 7, y == 12, z == 5 c. x == 7, y == 11, z == 6 d. x = 6, y == 5, z == 6

Answer: a. x == 6, y == 6, z == 5

Which real-world item best represents a linear collection? Answers: a. stack of bricks b. organizational chart c. file system d. table of contents

Answer: a. stack of bricks

What is the best case performance of a sequential search? Answers: a. O(1) b. linear c. quadratic d. On

Answer: a. O(1)

How does the class definition of a doubly linked structure differ from a singly linked structure? Answers: a. by adding a previous pointer b. by adding an extra data field c. by removing the tail pointer d. by adding another head pointer

Answer: a. by adding a previous pointer

What type of algorithm is list indexing an example of? Answers: a. constant-time b. linear-time c. logarithmic-time d. exponential-time

Answer: a. constant-time

What keyword is used to make a multiway if statement? Answers: a. elif b. then c. else d. elseif

Answer: a. elif

What is the name of the function that contains the starting point of program execution? Answers: a. main b. begin c. start d. enter

Answer: a. main

What process is required to avoid wasting memory if successive calls to the pop method on a list are made? Answers: a. shrink the array b. grow the array c. reset the array d. delete the array

Answer: a. shrink the array

Select the answer that should be used to replace the missing code in the following statements. ​ myList = list()fileObj = open("myfile.dat", "rb")while True:try:item = pickle.load(fileObj)myList.append(item)<missing code> fileObj.close()breakprint(myList) Answers: a. if EOF: b. except EOFError: c. elif Error: d. else while TRUE:

Answer: b. except EOFError:

In terms of complexity, what is the worst-case scenario for an insertion sort? Answers: a. O(log2n) b. O(n2) c. O(n) d. O2

Answer: b. O(n2)

An analysis of an algorithm's complexity divides its behavior into what three types of cases? Answers: a. sequential case, exponential case, binary case b. best case, worst case, average case c. minimum case, maximum case, average case d. linear case, quadratic case, logarithmic case

Answer: b. best case, worst case, average case

On average, what is the performance of a sequential search on a singly linked structure? Answers: a. random b. linear c. logarithmic d. exponential

Answer: b. linear

What should the missing code be in the following insertion sort algorithm? ​ i = 1 while i < len(myList): itemToInsert = myList[i] j = i - 1 while j >= 0: if itemToInsert < myList[j]: myList[j + 1] = myList[j] j -= 1 else: break <missing code> i += 1 ​ Answers: a. myList[i] = itemToInsert b. myList[j + 1] = itemToInsert c. myList[j] = itemToInsert d. myList[i + 1] = itemToInsert

Answer: b. myList[j + 1] = itemToInsert

What type of algorithm is the following code? i = 0while i < len(myList) - 1:minIndex = ij = i + 1while j < len(myList):if myList[j] < myList[minIndex]:minIndex = jj += 1if minIndex != i:swap(myList, minIndex, i)i += 1 Answers:a. bubble sort b. selection sort c. binary search d. sequential search

Answer: b. selection sort

The following code sums all the values in the two-dimensional array. What is the missing code? sum = 0for row in range(grid.getHeight()):for column in range(grid.getWidth()):<missing code> Answers: a. sum += grid[column][row] b. sum += grid[row][column] c. sum += grid[column+1][row+1] d. sum += grid[row-1][column-1]

Answer: b. sum += grid[row][column]

In a binary search of an ascending order list, where does the search algorithm begin the search? Answers: a. the beginning of the list b. the middle of the list c. a random spot in the list d. the end of the list

Answer: b. the middle of the list

Which of the following is true about Python keywords? Answers: a. they are written in uppercase b. they can begin with a number, letter, or hyphen c. they are case sensitive d. they can be a maximum of 6 characters long

Answer: c. they are case sensitive

Which statement is true about Python syntax? Answers: a. each code statement must end with a semicolon b. white space is ignored c. blocks of code are indicated by indentation d. a code block must begin with a left brace

Answer: c. blocks of code are indicated by indentation

What type of algorithm is the following code? n = len(myList)while n > 1:i = 1while i < n:if myList[i] < myList[i - 1]:swap(myList, i, i - 1)i += 1n -= 1 Answers: a. linear sort b. selection sort c. bubble sort d. insertion sort

Answer: c. bubble sort

What can a programmer do to prevent side effects from cloning mutable objects? Answers: a. create a shallow copy using a type b. create a deep copy using a type conversion c. create a deep copy using a for loop d. use the assignment operator in stead of a type conversion

Answer: c. create a deep copy using a for loop

The code for the __iter__ method is shown below. What is the missing code? def __iter__(self):<missing code> while cursor < len(self):yield self.items[cursor]cursor += 1 Answers: a. cursor = self.size b. cursor = len(self) c. cursor = 0 d. cursor = 1

Answer: c. cursor = 0

Which Python function returns the total number of items in a collection? Answers: a. total b. sizeof c. len d. count

Answer: c. len

In the Array class defined in Chapter 4, how do you instantiate an array object that can hold 10 values? Answers: a. Array(10) myArray b. myArray(10) = Array c. myArray = Array(10) d. Array myArray, 10

Answer: c. myArray = Array(10)

The following code searches a linked structure. What is the missing code? probe = headwhile probe != None and targetItem != probe.data:<missing code>if probe != None:print("Target item found!")else:print("Target item not found!") Answers: a. probe.next = targetItem.data b. probe.head = probe.next c. probe = probe.next d. probe.data = next.data

Answer: c. probe = probe.next

What should the missing code be in the following algorithm if the goal is to determine how many seconds the algorithm runs? start = time.time()myCount = 1for x in range(100):myCount *= xelapsed = time.time() - <missing code> Answers: a. myCount b. elapsed c. start d. x

Answer: c. start

Why are the insertion and removal of the first node special cases of the insert and remove ith node on singly linked structures? Answers: a. the last item must be deleted b. the tail pointer must be reset c. the head pointer must be reset d. the first item must be deleted

Answer: c. the head pointer must be reset

In a class definition, what type of method uses the values of the object's instance variables without changing them? Answers: a. constructor b. instance c. mutator d. accessor

Answer: d. accessor

What can you use a docstring for? Answers: a. to hold the name of a document file b. to create a large string variable c. to hold data of type string d. to create a multiline comment

Answer: d. to create a multiline comment

How can the following algorithm be described? left = 0right = len(sortedLyst) - 1while left <= right:midpoint = (left + right) // 2if target == sortedLyst[midpoint]:return midpointelif target < sortedLyst[midpoint]:right = midpoint - 1else:left = midpoint + 1return -1 Answers: a. sequential search b. bubble sort c. selection sort d. binary search

Answer: d. binary search

What are almost all operations on arrays based upon? Answers: a. hashes b. keys c. links d. indexes

Answer: d. indexes

In a class definition, what type of method uses the values of the object's instance variables without changing them? Answers: a. accessor b. mutator c. instance d. constructor

Answer:a. accessor

Which operator is used to obtain a string representation of a collection? Answers: a. char b. in c. str d. len

Answer:c. str

What is the value of aList after the following code is executed? aList = [10, 20, 30]aList.pop() Answers: a. [20, 30] b. [10, 20, 30] c. [] d. [10,20]

Answer:d. [10,20]

Which of the following is an unordered collection? Answers: a. queue b. stack c. string d. dictionary

Answer:d. dictionary

A literal representation of a list is made using parentheses to enclose items separated by commas.

False

If a programmer calls the next function on an iterator object and there is no current item in the sequence, the next function returns the Null item. True False

False

In the remove method for LinkedBag, if probe points to the node at the head of the linked structure, trailer will point to the tail. True False

False

Strings are mutable objects, which means you can replace their contents.

False

The __init__ method in the parent class is automatically called by a subclass of the parent class. True False

False

The implementations of the __str__ and __eq__ methods in the AbstractCollection class can be used in both unordered and linear collections. True False

False

The logical size of an array-based bag will always be the same as the array's capacity. Answers: True False

False

The statement in the ArrayBag constructor to copy an item from a source collection to a new ArrayBag object is self.init(item). True False

False

To distinguish a method in the parent class from a method with the same name in a subclass, prefix the method name with self. True False

False

To ensure that inheritance occurs, you need to place the name of the subclass in parentheses of the class header. True False

False

When looking at a hierarchy of abstract classes, the classes trend from the more specific to the more general going from the top to the bottom of the hierarchy. True False

False

When the LinkedBag structure is not empty, self.items refers to the last node in the linked structure. True False

False

A concrete class is often a subclass of an abstract class and is used to create objects in client applications. True False

True

A precondition is a statement of what must be true for a method to perform its actions correctly. True False

True

An abstract class captures the common features and behavior of a set of related classes. True False

True

An interface's documentation gives you enough information to know how to use or call a method and what to expect it to accomplish and return. True False

True

Programming languages such as Java include a collection framework more extensive than that of Python. True False

True

Software bags can grow as more items are added, or they can shrink as items are removed. True False

True

Standard functions and Python's library functions check the types of their arguments when the function is called.

True

The clear method empties a bag. True False

True

Use the comparison operator != to check if one value is not equal to another value.

True

When Python sees the in operator used with a collection, it runs the __contains__ method in the collection's class. True False

True

When a programmer calls the iter function on a collection, the collection's iterator object is returned. True False

True

Which of the following is NOT a step in using inheritance to create a subclass? Answers: a. duplicate redundant instance variables b. modify the code for methods that change c. add any new methods d. delete the methods that do not change

a. duplicate redundant instance variables

In the case of the AbstractCollection class, which of the following methods should NOT be included in this class? Answers: a. isEmpty b. add c. count d. __len__

b. add

Which of the following is a visual aid to help catalog resources in your software toolbox? Answers: a. unit modeler b. class diagram c. docstring d. method mapper

b. class diagram

When you finish writing the abstract class, what must the subclasses do to use it? Answers: a. nothing, the abstract class is automatically included in the subclass b. import the abstract class c. reference the abstract class in their __init__ method. d. copy the code to the subclass file

b. import the abstract class

In the constructor for the ArrayBag class shown below, what is the missing code? def __init__(self, sourceCollection = None):<missing code> AbstractBag.__init__(self, sourceCollection) Answers: a. items.add(self) b. self.items = Array(ArrayBag.DEFAULT_CAPACITY) c. add.items(self, sourceCollection) d. self.items = ArrayBag.add(sourceCollection)

b. self.items = Array(ArrayBag.DEFAULT_CAPACITY)

The code for the add method for the ArrayBag class is shown below. What is the missing code? def add(self, item):self.items[len(self)] = item<missing code> Answers: a. self.items +=1 b. self.size += 1 c. self.len = self.items d. self = self + 1

b. self.size += 1

In the ArrayBag class, what must the __init__ method do if a source collection is specified? Answers: a. the source collection must be initialized with the clear method b. the data in the source collection must be copied c. the data in the source collection must be deleted d. the new instantiation of the bag must be copied to the source collection.

b. the data in the source collection must be copied

What would be the purpose of creating a more general abstract class called AbstractCollection? Answers: a. to hide the details of the methods used in your abstract classes b. to group methods that can be used for other types of collections c. to group methods used by a single subclass d. to create objects from a client application

b. to group methods that can be used for other types of collections

What is the output of the following code? sum = 0 for value in range(1,4): sum += valueprint(sum) a. 5 b. 4 c. 6 d. 10

c. 6

When implementing the __init__ method in the AbstractBag class, what task should you leave out because it is the responsibility of the subclasses? Answers: a. checking if a source collection exists b. adding items from the source collection c. initializing the self.items variable d. setting the self.size variable to 0

c. initializing the self.items variable

What action does the following code perform assuming the Node class defined in Chapter 4? ​ head = Node(newItem, head) ​ Answers: a. appending an item to the end of a linked list b. replacing an item at the beginning of a linked list c. insertion of an item at the beginning of a linked list d. deletion of an item in a linked li

c. insertion of an item at the beginning of a linked list

What word is best defined as the use of common names for operations on different types of collections? Answers: a. concatenation b. modularization c. polymorphism d. anachronism

c. polymorphism

A linked structure can have items that have a maximum of one link to another item. Answers: True False

d Answer: False

When creating the AbstractCollection class, which methods must you modify to provide default behavior? Answers: a. isEmpty and add b. __init__ and __len__ c. __len__ and count d. __str__ and __eq__

d. __str__ and __eq__

Which is true about a bag collection? Answers: a. bags are of a fixed size b. bags can contain only numeric objects c. it is a standard collection type in Python d. it is an unordered collection

d. it is an unordered collection

On average, what is the performance of a sequential search on a singly linked structure? Answers: a. random b. exponential c. logarithmic d. linear

d. linear

What testing tool can be used to help ensure that a resource meets its requirements? Answers: a. unitcheck b. pytest c. unittest d. pyunit

d. pyunit

In the following code for the __add__ method in the ArraySortedBag class, what is the missing code? def __add__(self, other):result = ArraySortedBag(self)for item in other:<missing code> return result Answers: a. result = result + 1 b. add.item(result) c. item.add(result) d. result.add(item)

d. result.add(item)

When considering the ArrayBag class and the ArraySortedBag class, which of the following is true? Answers: a. the ArrayBag class is a subclass of ArraySortedBag b. the ArraySortedBag class is the parent of the ArrayBag class c. the ArrayBag class is the child class of ArraySortedBag d. the ArraySortedBag class is a subclass of ArrayBag

d. the ArraySortedBag class is a subclass of ArrayBag

Which of the following is one of the hallmarks of well-designed software? Answers: a. a sophisticated interface with a high learning curve b. a random grouping of interfaces to ensure secure code c. an interface that is tightly coupled to the implementation d. the clean separation of interfaces from implementations

d. the clean separation of interfaces from implementations

Which of the following is true about the sorted bag collection compared to the regular bag collection? Answers: a. the add method is identical in both b. the sorted bag's in operator runs in linear time c. the __init__ method is identical in both d. the items in a sorted bag must be of the same type

d. the items in a sorted bag must be of the same type


Related study sets

Chapter 47: Caring for Clients with Disorders of the Liver, Gallbladder, or Pancreas

View Set

Assignment 9 - Life insurance Policy Provisions Exam

View Set

Chapter 12: Developing New Products

View Set

Chapter 18 - Non-Infectious Disease

View Set

Psych Flash Cards Ch. 13 (Early Adulthood)

View Set

Chapter 9 - Social Media and Data Analytics

View Set

Inheritance and Composition Chapter 11

View Set