Top Posters
Since Sunday
5
o
5
4
m
4
b
4
x
4
a
4
l
4
t
4
S
4
m
3
s
3
A free membership is required to access uploaded content. Login or Register.

ch04

Uploaded: 6 years ago
Contributor: bio_man
Category: Computer Science
Type: Test / Midterm / Exam
Rating: N/A
Helpful
Unhelpful
Filename:   ch04.docx (55.02 kB)
Page Count: 10
Credit Cost: 1
Views: 219
Last Download: N/A
Transcript
CHAPTER 4 TEST BANK QUESTIONS Section 4.1 List Structures True/False Questions True or False? A list is an ordered, linear data structure in which each element in the list is accessed by its location in the list. True or False? Zero-based indexing means that the index value of a list always starts with 0. True or False? The typical list operations retrieve, update, insert, delete and append each need to be provided with an index value. True or False? List traversal is the process of accessing, one-by-one, each element in a list. True or False? List traversal always begins with the first element of a list. Multiple Choice Questions What would be the range of index values for a list of 10 elements? 0 – 9 1 – 10 0 – 10 Which of the following typical list operations does not need to be provided an index value? append insert delete The process of accessing each element of a list, one-by-one, is referred to as list stepping list traversal linear access Matching Match the following list operations with their description. (a) retrieve __d__ removes the item in a list a the specified index value (b) update __c__ adds at new value in a list at the specified index value (c) insert __a__ gets the value in the list at the specified index value (d) delete __e__ adds a provided value to the end of the list (e) append __b__ replaces the value at the specified index location and a provided value Fill-in-the-Blank Questions A linear data structure is simply referred to as a _______. ANSWER: list Lists in which the first index value is always 0 is referred to as _______________________. ANSWER: zero-based indexing Five basic operations performed on lists are __________, ___________, ___________, ______________, and _____________. ANSWER: retrieve, update, insert, delete, append The process of accessing each element of a list, one-by-one, is referred to as ___________________. ANSWER: list traversal Section 4.2 Lists (Sequences) in Python True/False Questions True or False? Lists must contain at least one element in Python. True or False? Lists in Python use zero-based indexing. True or False? Lists in Python as mutable, which means that the contents of the list may be altered. True or False? An empty list in Python is denote as ['']. True or False? In Python, square brackets are used to both create a list and access its individual elements. True or False? Tuples are the same as lists in Python, with the exception that they are immutable. True or False? An empty tuple is denote as ( ) in Python. True or False? Of the basic list operations of retrieve, update, insert, delete and append, only the append operation can be used on tuples. True or False? The + operator is used to concatenate two lists or two strings into one. True or False? Because lists can contain elements of any type, they can also contain lists as their elements, allowing for the creation of arbitrarily complex data structures. True or False? Strings, tuples and strings are collectively referred to as sequences in Python, and therefore the elements of each type can be accessed by index value. True or False? The slice operation removes an index range of values from a list. Multiple Choice Questions Which of the following is not a characteristic of lists in Python? (a) mutable (b) variable length (c) may contain mixed-type elements (d) nonlinear Which of the following is the proper way to denote an empty list in Python? (a) [''] (b) [ ] (c) ['empty'] (d) all of the above Which of the following is the correct way to access the fourth element of a list named lst? (a) lst[4] (b) lst[3] (c) lst['3'] (d) lst['4'] Square brackets are used with lists in Python to (a) access a particular element of a list (b) to create a list with specified elements (c) to create an empty list (d) all of the above Which of the following is not a valid list in Python? (a) [85, 'sunny', 'June 5'] (b) [10; 20; 30] (c) ['one', 'two', 'three'] (c) all of the above For lst = [10, 20, 30, 40, 50], what value is accessed by list[3]? (a) 30 (b) 40 (c) '30' (d) '40' What should the value of k be for lst[k] to access the tenth element of list lst? (a) 9 (b) 9.0 (c) '9' (d) All of the above For lst = [10, 20, 30, 40, 50], what value is the value of lst after del lst[3] is performed? (a) [10, 20, 30] (b) [30, 40, 50] (c) [10, 20, 40, 50] (d) [10, 20, 30, 50] For lst = [10, 20, 30, 40, 50], what value is the value of lst after lst.append(0) is performed? (a) [0, 10, 20, 30, 40, 50] (b) [10, 20, 30, 40, 50, 0] (c) [0] (d) [ ] For lst = [10, 20, 30, 40, 50], what is the value of lst after lst.insert(3, 0) is performed? (a) [10, 20, 0, 30, 0, 40, 50] (b) [10, 20, 30, 0, 40, 50] (c) [10, 20, 30, 40, 0, 50] (d) [10, 20, 30, 40, 50, 0] For lst = [10, 20, 30, 40, 50], what is the resulting value of lst[2:4]? (a) [20, 30] (b) [20, 30, 40] (c) [30, 40] (d) [30, 40, 50] For some list lst, what will be the result of the execution of the instruction lst.insert(1, 3)? insert the value 1 at the location of the third element in the list insert the value 1 at the location of the fourth element in the list replace the third element in the list with the value 1 replace the fourth element in the list with the value 1 Which of the following is the correct notation for a tuple of only one element? (a) (10,) (b) (10) (c) (,10,) (d) All of the above Which of the following operations cannot be used with tuples? (a) len (b) slice (c) del (d) concatenation For nested list lst = [ [10, 20, 30], [40, 50, 60] ], which of the following is the correct means of assessing the element with the value 40? (a) lst[0][0] (b) lst[0][1] (c) lst[1][0] (d) lst[2][1] Fill-in-the-Blank Questions In Python, all lists are indexed starting with an index value of 0. This is referred to as _______________________. ANSWER: zero-based indexing In Python, ____________ are the same as lists, except that their contents cannot be altered. ANSWER: tuples The ___ operator symbol is used to represent the concatenation of two lists or strings. ANSWER: + An operator symbol (such as +) that is used to denote to different possible operations (arithmetic addition and concatenation) is referred to as an ________________ operator. ANSWER: overloaded operator Lists, tuples and strings are collectively referred to as _______________ in Python. ANSWER: sequences The ______________ operation in Python can be used to obtain a sublist (ubstring) or a given list (string). ANSWER: slice Open Response Questions Give Python code that replaces the value 25 in list lst = [10, 20, 25, 40] with the value 40. ANSWER: lst[2] = 30 Give Python code that inserts the value 35 in lst = [10, 20, 25, 40] so that the values in the list remain in numerical order. ANSWER: lst.insert(3, 35) Give Python code that adds to the (empty) list lst = [ ] the values 10, 20, 30 such that lst = [10, 20, 30] ANSWER: lst.append(10) lst.append(20) lst.append(30) Give Python code to determine if two lists, lstA and lstB, both of length 4, have the same values (e.g., are equal), setting Boolean value True or False to equal_lists, accordingly. ANSWER: if lstA[0]== lstB[0] and lstA[1] == lstB[1] and lstA[2] == lstB[2] and lstA[3] == lstB[3]: equal_lists = True else: equal_lists = False Give Python code that prompts the user for a series of positive numbers, and stores them in a list named num_list in the order that they are entered. ANSWER: num_list = [] print('Enter a series of positive numbers (-1 to quit): ') num = int(input(': ')) while num != -1: num_list.append(num) num = int(input(': ')) Section 4.3 Iterating Over Lists (Sequences) in Python True/False Questions True or False? For statements are used for the construction of definite loops. True or False? For loops can be used with all sequence types, except for strings. True or False? The built-in range function in Python is used for generating a sequence of integers. True or False? When iterating over a list, a for loop may be used to either iterate over the list values, or the index values of the list. True or False? In for loops, the loop variable always serves as an index variable. True or False? The for loop Is always the best iterative control structure to use when traversing over indexed data structures in Python. True or False? Although while loops can be nested, for loops cannot because of their use of a loop variable. Multiple Choice Questions Which of the following best describes the kind of control flow that a for loop provides? (a) definite loop (b) indefinite loop (c) terminating loop (d) finite loop Examine the following Python code, numList = [1, 4, 6, 8, 10, 3, 12] for k in range(0, len(numList)): print(numList[k]) How many times does the above loop iterate? (a) 0 (b) 6 (c) 7 (d) 8 Examine the following Python code, numList = [1, 4, 6, 8, 10, 3, 12] for k in numList: print(k) How many times does the above loop iterate? (a) 0 (b) 6 (c) 7 (d) 8 Examine the following Python code, (a) for k in [1, 4, 6, 8, 10, 3, 12]: (b) numList = [1, 4, 6, 8, 10, 3, 12] (c) numList = [1, 4, 6, 8, 10, 3, 12] print(k) for k in numList: for k in range(0,len(numList)): print(k) print(numList[k]) Which of the above loops prints all the values in the list [1, 4, 6, 8, 10, 3, 12] ? (a) the first only (b) the first and second only (c) the second and third only (d) each of the loops Fill-in-the-Blank Questions The for statement is used to construct __________ loops. ANSWER: definite For statement can be applied both lists and tuples, as well as _____________. ANSWER: strings The built-in ________ function in Python can be used for generating a sequence of integers. ANSWER: range When iterating over a list by use of a for loop, the loop variable may range over the ____________ of the list, or the ___________ values. ANSWER: elements / index Open Response Questions Give Python code to add up all the elements in a list of integers named nums, and displays the result. ANSWER: total = 0 for k in nums: total = total + k print(total) Give Python code that displays how many even elements there are in a list of integers named nums. ANSWER: num_even = 0 for k in nums: if k % 2 == 0: num_even = num_even + 1 print(num_even) Give Python code that determines the average of only the positive integers in a list of positive and negative integers named nums. ANSWER: total = 0 num_values = 0 for k in nums: if k >= 0: num_values = num_values + 1 total = total + k print(total / num_values) For fruit = ' banana', what is displayed by the following Python code? for k in range(0, len(fruit), 2): print(fruit[k], end='') ANSWER: bnn Give Python code that, for two lists of integers (of the same length) named nums1 and num2, creates a third list named nums3 containing the sum of the corresponding numbers in each list, as demonstrated below. num1 [1, 2, 3, 4] num2[10, 20, 30, 40] num3[11, 22, 33, 44] ANSWER: for k in range(0, len(nums1)): nums3.append(nums1[k] + nums2[k]) Section 4.4 More on Python Lists True/False Questions True or False? When a variable is assigned to another variable holding a list, each variable ends up referring to the same instance of the list in memory. True or False? List comprehensions are an alternate means of generating the same sequences that can be generated using the built-in range function in Python. Multiple Choice Questions Examine the following lines of Python code: lst1 = [1, 2, 3, 4] lst2 = [1, 2, 3, 4] lst1 == lst2 What will the last line return? (a) True (b) False (c) an error Examine the following lines of Python code: lst1 = [1, 2, 3, 4] lst2 = lst1 lst1 == lst2 What will the last line return? (a) True (b) False (c) an error Examine the following lines of Python code: lst1 = [1, 2, 3, 4] lst2 = lst1 lst1 [0] = 10 lst1 == lst2 What will the last line return? (a) True (b) False (c) an error Examine the following lines of Python code: list1 = [1, 2, 3, 4, ] list2 = list1 list1[0] = 6 list2[0] == 6 What will the last line return? (a) True (b) False (c) an error Regarding list comprehensions, which of the following correctly generates a list containing all the even numbers between 2 and 100, inclusive? (a) [x % 2 == 0 for x in [1,2,3,4,5,6,7,8,9]] (b) [x for x in [1,2,3,4,5,6,7,8,9] if x % 2 == 0] (c) [x for x in range(2, 100) if x % 2 == 0] (d) [x for x in range(2, 102) if x % 2 == 0] Open Response Questions Write a function named displayGrades that is passed a list of grades in the range 0-100, and displays the grades from highest to lowest, each grade on a separate line. Note that the original list argument passed should be not be altered. You may use the built-in sort method in the function. ANSWER: def displaySorted(lst): save_lst = list(lst) save_lst.sort() for grade in lst: print(grade) Write a function named returnSquares that returns as a function the squares of all the numbers of a list argument passed to it in sorted order from lowest to highest. Your code must make use of list comprehensions. ANSWER: def returnSquares(lst): return_list = [x ** 2 for x in lst] return_list.sort() return return_list Write a function named returnEvens that returns as a function value only the even numbers of a list argument passed to it. Your code must make use of list comprehensions. ANSWER: def returnEvens(lst): return_lst = list(lst) return_lst = [x for x in lst if x % 2 == 0] return return_lst

Related Downloads
Explore
Post your homework questions and get free online help from our incredible volunteers
  937 People Browsing
 112 Signed Up Today
Your Opinion
Which of the following is the best resource to supplement your studies:
Votes: 249