CS1100 Python: Unit 2 - Milestone 2 Questions and Answers
Suppose you have a list defined in the following way: myList = [[12, 11, 10, 9, 8, 7], [6, 5, 4, 3, 2, 1]] What are the correct dimensions of this list?
2 x 6
In the update to the tic-tac-toe game, why was a while loop chosen to check for valid player position inputs?
A while loop can be used to wait indefinitely for a player to input valid positions.
A user that inputs 2 and 1 into the following code will result in what output? board = {"-", "-", "-", "-", "-", "-", "-", "-", "-"} col = int(input("X player, select a column: ")) row = int(input("X player, select a row: ")) col -= 1 row -= 1 board[row][col] = "X" print(board[0]) print(board[1]) print(board[2])
An error when board[row][col] = "X" is executed.
What is the result of this code? my_first_list = ['a', 'b', 'c'] my_second_list = ['d', 'e', 'f', 'g'] my_third_list = my_first_list * 2 my_third_list.upper() print (my_third_list)
An error, because upper is not a method available for lists.
What do dictionaries and sets have in common?
Both can be created using curly brackets { }.
Which of the following options is a property of Python lists?
Lists are dynamic.
What is the return result of the dB2 function from the following code? import math def dB2(param1, param2): dB_calc = 20 * math.log10(param1/param2) if dB_calc > 6: return "High SNR" elif dB_calc > 0: return "Low SNR" return "Noise" print(dB2(7,5))
Low SNR
When would you use a break statement in a for loop?
To exit the loop.
When would you use a break statement in a while loop?
To exit the loop.
What is the result of this code? my_list = [[9, 8], [7, 6], [5, 4], [3, 2]] my_list.sort() print(my_list)
[[3,2],[5,4],[7,6],[9,8]]
What is the output of the print statement for the following code: multiples = [] for threeD in range(1,3): multiples.append([]) for twoD in range(1,3): multiples[threeD-1].append([]) for oneD in range(1,3): multiples[threeD-1][twoD-1].append(oneD) print(multiples)
[[[1,2], [1,2]], [[1,2], [1,2]]]
A recursive formula can be implemented in Python and then used to provide the nth term in a series. Which of the following recursive programs implements the recursive formula below and prints out the value of its 5th term (31): base: a(1) = -5 recursion: a(n) = a(n -1) + 9
def recursive_2(num): if num == 1: return -5 return recursive_2(num - 1) + 9 print('rec_2 ',recursive_2(5))
Functions can be used to replace multiple code statements. This occurs in the Tic-Tac-Toe game program within the tutorial 2.3.5 Finishing the Tic-Tac-Toe Program; see the printBoard function created there as an example. What if we wanted to replace the following lines in the Tic-Tac-Toe game: while (row < 1 or row > 3): row = int(input(playerTurn + " player, select a row 1-3: ")) if (row < 1 or row > 3): print("The row must be between 1 and 3.") with a function called turn. For the call to this function, we could use: row = turn("row", playerTurn) where we input into the function that we want to select a row ("row") and which player is making this selection (playerTurn). Which code segment properly implements this function?
def turn(col_row, player): good_selection = False while (good_selection == False): col_row_picked = int(input(playerTurn + " player, select a {} 1-3: ".format(col_row))) if (col_row_picked < 1 or col_row_picked > 3): print("The {} must be between 1 and 3..format(col_row)) else: good_selection = True return col_row_picked
Parameters are used __________ a function.
internally by
Scope defines where variables can be accessed. What variables are within the scope of the outer2 function?
j, k, i, q
Where is the optimal place to insert an indented print statement in the for loop of the following code to debug it? 1 Max = None 2 3 for primes in [3, 5, 31, 29, 17, 7]: 4 5 if Max is None or primes > Max : 6 7 Max = primes 8
line 8