pogramming quiz 5

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the output of the following code? def m(n): n += 1 def main(): n = 1 m(n) print(n) main()

1

What will be displayed by the following code? m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]print(m[0][0])

1

What will be displayed by the following program? values = [[3, 4, 5, 1], [33, 6, 1, 2]] v = values[0][0]for lst in values: for element in lst: if v > element: v = element print(v)

1

What will be displayed by the following code? myList = [1, 5, 5, 5, 5, 1] max = myList[0] indexOfMax = 0 for i in range(1, len(myList)): if myList[i] > max: max = myList[i] indexOfMax = iprint(indexOfMax)

1 indexOfMax is the index of the first max element in the list. So, it is 1.

what is the output of the following code? def m(i, lst): i = 9 lst[0] = 9 def main(): x = [1, 2] i = 1 m(i, x) print(i, x[0], end = '') main()

1 9

How many elements are in m = [[x, y] for x in range(0, 4) for y in range(0, 4)]?

16 (4x4)

Suppose m = [[4, 5, 6], [2, 1, 0]], what is m[1][0]?

2

What is k after running the following code? def nPrint(message, n): while n > 0:print(message) n -= 1k = 2n Print("A message", k)

2

What is the output of following code? def m(n): n += 1 print(n, end = '') def main(): m(1) main()

2

What is k after running the following code? def nPrint(message, n): while n > 0:print(message) n -= 1k = 2n Print(n = k, message = "A message")

2 Hint: Note that if the argument is a number or a string, the argument is not affected, regardless of the changes made to the parameter inside the function. In this case, k is changed inside the function. But its original value is used after the function call.

Suppose list1 = [1, 2, 3], what is the output of the following code? for i in list1: print(i * 2, end = '')

246

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is max(list1)?

25

Suppose list1 is [1, 3, 2], what is sum(list1)?

6

Given the following function header, which of the following is correct to invoke it? def f(p1, p2, p3, p4)

A. f(1, 2, 3, 4) D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) E. f(1, 2, 3, p4 = 4)

What are the benefits of using a function?

At least three benefits: (1) Reuse code; (2) Reduce complexity; (3) Easy to maintain.

When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

B. pass by value

What happens when your program attempts to access a list element with an invalid index?

Causes a run time error (index out of range)

True or false? A call to a function with a None return type is always a statement itself, but a call to a value-returning function cannot be a statement by itself.

True: a call to a function with a None return type is always a statement itself. False: a call to a value-returning function is always a component of an expression.

Compare positional arguments and keyword arguments.

Using positional arguments requires the arguments be passed in the same order as their respective parameters in the function header. keyword arguments, passing each argument in the form name=value.

Given the following function, what will be displayed by the call nPrint('a', 4)? def nPrint(message, n): while n > 0: print(message, end = '') n -= 1

aaaa

suppose a function header is as follows: def f(p1, p2, p3, p4): Which of the following calls are correct? f(1, p2 = 3, p3 = 4, p4 = 4) f(1, p2 = 3, 4, p4 = 4) f(p1 = 1, p2 = 3, 4, p4 = 4) f(p1 = 1, p2 = 3, p3 = 4, p4 = 4) f(p4 = 1, p2 = 3, p3 = 4, p1 = 4)

f(1, p2 = 3, p3 = 4, p4 = 4) # Correct f(1, p2 = 3, 4, p4 = 4) # Not Correct f(p1 = 1, p2 = 3, 4, p4 = 4) # Not Correct f(p1 = 1, p2 = 3, p3 = 4, p4 = 4) # Correct f(p4 = 1, p2 = 3, p3 = 4, p1 = 4) # Correct

list1 = [11, 2, 23] and list2 = [11, 2, 2], list1 < list2 is ________

false

list1 = [11, 2, 23] and list2 = [2, 11, 23], list1 == list2 is ________

false

Given the following function, what will be displayed by the call nPrint('a', 4)? def nPrint(message, n): while n > 0: print(message, end = '') n -= 1

infinite loop n -= 1. this part was not indented

Consider the following incomplete code: def f(number): # Missing function body # return the value number print(f(5)) The missing function body should be ________.

return number

Show the output of the following two programs: (a) def main(): list1 = m(1) print(list1) list2 = m(1) print(list2) def m(x, lst = [1, 1, 2, 3]): if x in lst: lst.remove(x) return lst main() (b) def main(): list1 = m(1) print(list1) list2 = m(1) print(list2) def m(x, lst = None): if lst == None: lst = [1, 1, 2, 3] if x in lst: lst.remove(x) return lst main()

(a) [1, 2, 3] [2, 3] (b) [1, 2, 3] [1, 2, 3]

If a key is not in the list, the binarySearch function returns _________.

-(insertion point + 1)

Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], What is list1[-1]?

0

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is min(list1)?

1

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.index(5)?

2 Returns lowest index where the element appears.

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?

2 there are 2 #5 in the index

What will be displayed by the following code? myList = [1, 2, 3, 4, 5, 6] for i in range(1, 6):myList[i - 1] = myList[i] for i in range(0, 6): print(myList[i], end = " ")

2 3 4 5 6 6 The elements in myList are shifted one space left.

What will be displayed by the following code? matrix = [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] for i in range(0, 4): print(matrix[i][1], end = " ")

2 5 9 13

Assume x = [[1, 2], [3, 4, 5], [5, 6, 5, 9]], what are len(x[0]), len(x[1]), and len(x[2])?

2, 3, and 4

Use the selectionSort function presented in this section to answer this question. Assume lst is [3.1, 3.1, 2.5, 6.4, 2.1], what is the content of list after the first iteration of the outer loop in the function?

2.1, 3.1, 2.5, 6.4, 3.1 2.1 (smallest) is swapped with 3.1. So, after the first iteration, the list is e.

Assume m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], what are len(m)?

3

What will be displayed by the following code? def f(value, values): v = 1 values[0] = 44 t = 3 v = [1, 2, 3] f(t, v) print(t, v[0])

3 44

Assume x = [[1, 2], [3, 4, 5], [5, 6, 5, 9]], what are len(x) are len(x[0])?

3 and 2

What will be displayed by the following program? values = [[3, 4, 5, 1], [33, 6, 1, 2]] v = values[0][0] for row in range(0, len(values)): for column in range(0, len(values[row])): if v < values[row][column]: v = values[row][column] print(v)

33

Assume m = [[1, 2, 3, 4], [4, 5, 6, 5], [1, 7, 8, 9]], what are len(m[0])?

4

What will be displayed by the following code? matrix = [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] for i in range(0, 4): print(matrix[1][i], end = " ")

4 5 6 7

What will be displayed the following code? data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] def ttt(m): v = m[0][0] for row in m: for element in row: if v < element: v = element return v print(ttt(data[0]))

4. (data[0] is [[1, 2], [3, 4]]. ttt(m) is to return the largest number in m)

Suppose m = [[4, 5, 6], [2, 1, 0]], what is m[0][1]?

5

What will be displayed by the following program? def m(list): v = list[0] for e in list: if v < e: v = return values = [[3, 4, 5, 1], [33, 6, 1, 2]] for row in values: print(m(row), end = " ")

5 33

What will be displayed by the following code? data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] print(data[1][0][0])

5. each multidimension set is a set of 2.

Suppose m = [[4, 5, 6], [2, 1, 0]], what is m[0][2]?

6

What is the output of the following code? def f(m): result = 0 for i in range(0, len(m)): for j in range(0, len(m[i])): result += m[i][j] return result def main(): x = [[1, 2, 1], [1, 1, 1]] print(f(x)) main()

7 (add)

What is the output of the following code? def m(m): result = m[0][1] for i in range(0, len(m)): for j in range(0, len(m[i])): if result < m[i][j]: result = m[i][j] return result def main(): x = [[2, 1, 1], [1, 7, 1]] print(m(x)) main()

7 (the greatest #)

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is len(list1)?

8

What is the output of the following code? def m(m): result = 0 for i in range(0, len(m)): result += m[i] return result def main(): x = [[2, 1], [1, 7, 1]] print(m(x[1])) main()

9 (add second row)

Which of the following should be defined as a None function?

A. Write a function that prints integers from 1 to 100.

__________ creates a list.

A. list1 = list() B. list1 = [ ] C. list1 = list([12, 4, 4]) D. list1 = [12, 4, 4] E. list1 = [1, "3", "red"]

Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], Which of the following is correct?

A. print(list1[0]) B. print(list1[:2]) C. print(list1[:-2]) D. print(list1[4:6])

Indicate true or false for the following statements: a. Every element in a list must have the same type. b. A list's size is fixed after it is created. c. A list can have duplicate elements. d. The elements in a list can be accessed via an index operator.

Indicate true or false for the following statements: (a) Every element in a list must have the same type. (False) (b) The list size is fixed after it is created. (False) (c) The list can have duplicated elements. (True) (d) The elements in a list can be accessed via an indexer. (True)

True or false? When a list is passed to a function, a new list is created and passed to the function.

No. The reference of the list is passed to the parameter in the function. No new list is created.

Will the program pick four random cards if you replace lines 14-17 in LiveExample 7.3 DeckOfCards.py with the following code? for i in range(4): cardNumber = random.randint(0, 51) suit = suits[cardNumber // 13] rank = ranks[cardNumber % 13] print("Card number", cardNumber, "is the", rank, "of", suit)

No. You may get two same cards.

How do you define a function? How do you invoke a function?

See the Sections 6.2 and 6.3 on how to define and invoke functions.

What will be displayed by the following program? values = [[3, 4, 5, 1 ], [33, 6, 1, 2]] for row in values: row.sort()for element in row: print(element, end = " ") print()

The program prints 1 3 4 5 followed by 1 2 6 33

Given lst = [30, 1, 12, 14, 10, 0], how many elements are in lst? What is the index of the first element in lst? What is the index of the last element in lst? What is lst[2]? What is lst[-2]?

There are 6 elements in the list. The index of the first element is 0. The index of the last element is 5. lst[2] is 12. list[-2] is list[6-2], which is 10.

"Welcome to Python".split() is ________

["Welcome", "to", "Python"]

What is list("a#b#c#d".split('#')?

['a', 'b', 'c', 'd']

What is list("abcd")?

['a', 'b', 'c', 'd']

What is the output of the following code? lst = [1, 2, 3, 4, 5, 6] for i in range(1, 6): lst[i] = lst[i - 1] print(lst)

[1, 1, 1, 1, 1, 1] right to left if it was lst[i] = lst[i - 5] then it would be [1, 3, 4, 5, 6, 1]

What will be displayed by the following code? def f(i, values = [ ]): values.append(i) return values f(1) f(2) v = f(3) print(v)

[1, 2, 3]

Suppose list1 is [1, 3, 2], What is list1 * 2?

[1, 3, 2, 1, 3, 2]

Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], What is list1[:-1]?

[1, 3, 2, 4, 5, 2, 1]

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.sort()?

[1, 3, 3, 4, 5, 5, 20, 25]

What is the output of the following code? list1 = list(range(1, 10, 2)) list2 = [ ] + list1 list1[0] = 111 print(list1) print(list2)

[111, 3, 5, 7, 9] [1, 3, 5, 7, 9]

What is the output of the following code? list1 = list(range(1, 10, 2)) list2 = list1 list1[0] = 111 print(list1) print(list2)

[111, 3, 5, 7, 9] [111, 3, 5, 7, 9]

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?

[3, 1, 25, 5, 20, 5, 4, 3]

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.extend([34, 5])?

[3, 4, 5, 20, 5, 25, 1, 3, 34, 5] add [34,5] to the end of the list

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.pop()?

[3, 4, 5, 20, 5, 25, 1] If it is not specified, list.pop() removes and returns the last element in the list. removes 3 at the end of the list

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.pop(1)?

[3, 5, 20, 5, 25, 1, 3] Removes the element at the given index and return it. removes 4 in the list

What will be displayed by the following code? list1 = [1, 3] list2 = list1 list1[0] = 4 print(list2)

[4, 3] list2 = list1 assigns the reference for list1 to list2. list2 and list1 now points to the same list.

What will be displayed by the following code? def f(values): values[0] = 44 v = [1, 2, 3] f(v) print(v)

[44, 2, 3]

For m = [[x, x + 1, x + 2] for x in range(0, 3)], m is _______.

[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

What will be displayed by the following code? points = [[1, 2], [3, 1.5], [0.5, 0.5]] points.sort() print(points)

[[0.5, 0.5], [1, 2], [3, 1.5]]

For m = [[x, x + 1, x + 2] for x in range(1, 9, 3)], m is _______.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

What is the output of the following code? matrix = [ ] matrix.append([1, 2, 3]) matrix.append([4, 5]) matrix.append([6, 7, 8, 9]) print(matrix)

[[1, 2, 3], [4, 5], [6, 7, 8, 9]]

What is the output of the following code? matrix = [ ] matrix.append(3 * [1]) matrix.append(3 * [1]) matrix.append(3 * [1]) matrix[0][0] = 2 print(matrix)

[[2, 1, 1], [1, 1, 1], [1, 1, 1]]

What is the output of the following code? matrix = [ ] matrix.append([3 * [1]]) matrix.append([3 * [1]]) matrix.append([3 * [1]]) print(matrix) matrix[0] = 3 print(matrix)

[[[1, 1, 1]], [[1, 1, 1]], [[1, 1, 1]]] [3, [[1, 1, 1]], [[1, 1, 1]]]

Given list1 = [30, 1, 2, 1, 0], what is the return value of each of the following statements? [x for x in list1 if x > 1] [x for x in range(0, 10, 2)] [x for x in range(10, 0, -2)]

[x for x in list1 if x > 1] => [30, 2] [x for x in range(0, 10, 2)] => [0, 2, 4, 6, 8] [x for x in range(10, 0, -2)] => [10, 8, 6, 4, 2]

Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.

a stack

Write statements to do the following: a. Create a list with 100 Boolean False values. b. Assign the value 5.5 to the last element in the list. c. Display the sum of the first two elements. d. Compute the sum of the first five elements in the list. e. Find the minimum element in the list. f. Randomly generate an index and display the element of this index in the list.

a. lst = 100 * [False] b. lst.append(5.5) c. print(lst[0] + lst[1]) d. total = sum(lst[0:5]) e. minimum = min(lst) f. print(list[random.randint(0, len(lst) - 1)])

If the binary search function returns -4, where should the key be inserted if you wish to insert the key into the list?

at index 3

How do you create an empty list and a list with the three integers 1, 32, and 2?

emptyList = [] lst = [1, 32, 2]

What is the return value from invoking p(27)? def p(n): for i in range(2, n): if n % i == 0: return True return False

false

How do you obtain a list from a string? Suppose s1 is welcome, what is s1.split('o')?

list(s) s1.split('o') is ['Welc', 'me']

Given list1 = [30, 1, 2, 1, 0] and list2 = [1, 21, 13], what is the return value of each of the following statements? list1 + list2 2 * list2 list2 * 2 list1[1 : 3] list1[3]

list1 + list2 => [30, 1, 2, 1, 0, 1, 21, 13] 2 * list2 => [1, 21, 13, 1, 21, 13] list2 * 2 => [1, 21, 13, 1, 21, 13] list1[1 : 3] => [1, 2] list1[3] => 1

Given list1 = [30, 1, 2, 1, 0] and list2 = [1, 21, 13], what is the return value of each of the following statements? list1 < list2 list1 <= list2 list1 == list2 list1 != list2 list1 > list2 list1 >= list2

list1 < list2 => False list1 <= list2 => False list1 == list2 => False list1 != list2 => True list1 > list2 => True list1 >= list2 => True

Use the selectionSort function presented in this section to answer this question. What is list1 after executing the following statements? list1 = [3.1, 3.1, 2.5, 6.4]selectionSort(list1)

list1 is 2.5 3.1, 3.1, 6.4

What are list1 and list2 after the following lines of code? list1 = [1, 43] list2 = [x for x in list1] list1[0] = 22

list1 is [22, 43] list2 is [1, 43]

What are list1 and list2 after the following lines of code? list1 = [1, 43] list2 = list1 list1[0] = 22

list1 is [22, 43] and list2 is [22, 43].

To add 5 to the end of list1, use _______.

list1.append(5)

To insert 5 to the third position in list1, use _______.

list1.insert(2, 5). The first position is 0. So, the third position is 2.

To remove string "red" from list1, use _______.

list1.remove("red")

For the binarySearch function in Section 7.9.2, what is low and high after the first iteration of the while loop when invoking binarySearch([1, 4, 6, 8, 10, 15, 20], 11)?

low is 4 and high is 6

Given lst = [30, 1, 2, 1, 0], what is the return value of each of the following statements? lst.index(1) lst.count(1) len(lst) max(lst) min(lst) sum(lst)

lst.index(1) => 1 lst.count(1) => 2 len(list) => 5 max(list) => 30 min(list) => 0 sum(list) => 34

Create a two-dimensional list named m with two rows. The first row has values 1 and 2, and the second row has values 3 and 4.

m = [[1, 2], [3, 4]]

Write a statement that appends value 5 into the first row in a two-dimensional list m.

m[0].append(5)

How do you create a list for a two-dimensional data with three rows and four columns with values 0?

matrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

A function _________.

may have no parameters

Function Header: begins with the def keyword, followed by the function's name and parameters, and ends with a colon. Formal Parameter: refers to the variables defined in the function header. Actual Parameter: refers to the value passed to the function when invoking the function. None Function: is a function that does not return a value.

n/a

Does the function call main() in the following code cause syntax errors? import math def main(): return math.sin(math.pi) main()

no

A function with no return statement returns ______.

none

Arguments to functions always appear within __________.

parentheses

To shuffle list1, use _______.

random.shuffle(list1)

How do you simplify the max function in LiveExample 6.1 using the conditional expression?

return num1 if (num1 > num2) else num2

What is the return value from invoking p(77)? def p(n): for i in range(2, n): if n % i == 0: return True return False

true

Can you create a list for two-dimensional data with a different number of elements in a row?

yes

Can you have a return statement in a None function? Does the return statement in the following function cause syntax errors? def xFunction(x, y): print(x + y) return

yes. A syntax error occurs if a return statement is not used to return a value in a value-returning function. You can have a return statement in a None function, which simply exits the function. So, this function is OK, although the return statement is not needed at all.


Ensembles d'études connexes

Test 2 Questions, Bus 189 (Chapter 23 & 24), Bus189 Ch7 FINAL, CH 6: Corporate Strategy, Ch.5 Review Questions and Vocab, Test 2 BUS 401 Ch.4

View Set

Exam 4: Powerpoint & Reading Questions

View Set

IFS3109 Exam 2 Ch. 9 Professional Sport

View Set

C Programming Chapter 4 & 5 Functions and Decision Making

View Set

Pharmacology - Chapter 25: Muscle Relaxants

View Set

Comparative Vertebrate Anatomy Exam 2

View Set

Chapter 05: The Working Cell Dynamic Study Module

View Set

CCNA 1 (v5.1 + v6.0) Chapter 3 Exam Answers 2018

View Set

CPSC 317 Internet Computing: Module 4 (Socket programming with TCP)

View Set

ACE Personal Trainer Manual: Chapter 5

View Set

Econ 401 Exam 4- Marc Herold UNH 2019

View Set