Quizzes

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

What is the output of the following: x = [1, 2, 3] y = x print(id(x) == id(y)

True

What is the result of the snippet of code shown below if x = 1? x << 3

8 **** Bitwise operator << The left operands value is moved left by the number of bits specified by the right operand.

Which of the following statements are the same? a) x -= x +4 b) x = x + 4 -x c) x = x - (x - 4)

A & C are the same

15.1 Which of the following statements are true? A. Every recursive function must have a base case or a stopping condition. B. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. C. Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case. D. Every recursive function must have a return value. E. A recursive function is invoked differently from a non-recursive function.

A B C

5.21 Will the following program terminate? balance = 10 while True: if balance < 9: break balance = balance - 9 A. Yes B. No

A. Yes

10.35 If the binary search function returns -4, where should the key be inserted if you wish to insert the key into the list? A. at index 3 B. at index 4 C. at index 5 D. at index 6

A. at index 3 ****refer to previous question(107)

10.16 To insert 5 to the third position in list1, use _______. A. list1.insert(3, 5) B. list1.insert(2, 5) C. list1.add(3, 5) D. list1.append(3, 5)

A. list1.insert(3,5)

11.13 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 = " ") A. 1 2 3 4 B. 4 5 6 7 C. 1 3 8 12 D. 2 5 9 13 E. 3 6 10 14

B

15.3 What are the base cases in the following recursive function? def xfunction(n): if n > 0: print(n % 10) xfunction(n // 10) A. n > 0 B. n <= 0 C. no base cases D. n < 0

B

7.16 Which of the following statement is most accurate? A. A reference variable is an object. B. A reference variable refers to an object. C. An object may contain other objects. D. An object may contain the references of other objects.

B & D A reference variable refers to an object. An object may contain the references of other objects.

11.4 For m = [[x, x + 1, x + 2] for x in range(0, 3)], m is _______. A. [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B. [[0, 1, 2], [1, 2, 3], [2, 3, 4]] C. [1, 2, 3, 4, 5, 6, 7, 8, 9] D. [0, 1, 2, 1, 2, 3, 2, 3, 4]

B.

10.13 list1 = [11, 2, 23] and list2 = [11, 2, 2], list1 < list2 is ________ A. True B. False

B. False

5.26 What will be displayed by after the following loop terminates? number = 25 isPrime = True for i in range(2, number): if number % i == 0: isPrime = False break print("i is", i, "isPrime is", isPrime) A. i is 5 isPrime is True B. i is 5 isPrime is False C. i is 6 isPrime is True D. i is 6 isPrime is False

B. i is 5 isPrime is False

5.14 What is the output for y? y = 0 for i in range(0, 10, 2): y += i print(y) A. 9 B. 10 C. 11 D. 20

D. 20 0+0=0 0+2=2 2+4=6 6+6=12 12+8=20

6.27 What will be displayed by the following code? def f1(x = 1, y = 2): return x + y, x - y x, y = f1(y = 2, x = 1) print(x, y) A. 1 3 B. 3 1 C. The program has a runtime error because the function returns the multiple values D. 3 -1 E. -1 3

D. 3 -1

10.31 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]) A. 1 1 B. 1 44 C. 3 1 D. 3 44

D. 3 44

10.33 For the binarySearch function in Section 10.10.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)? A. low is 0 and high is 6 B. low is 0 and high is 3 C. low is 3 and high is 6 D. low is 4 and high is 6 E. low is 0 and high is 5

D. low is 4 and high is 6

11.9 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) A. 1 B. 3 C. 5 D. 6 E. 33

E

10.36 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? A. 3.1, 3.1, 2.5, 6.4, 2.1 B. 2.5, 3.1, 3.1, 6.4, 2.1 C. 2.1, 2.5, 3.1, 3.1, 6.4 D. 3.1, 3.1, 2.5, 2.1, 6.4 E. 2.1, 3.1, 2.5, 6.4, 3.1

E.

True of False: A variable that hold a value is actually a reference to an object for the value.

True

True or False: Each object has a unique id.

True

What is the output of the code shown below? print(not(3 > 4), not(5 & 5))

True False

What is list("a#b#c#d".split('#')? Answers: a. ['a', 'b', 'c', 'd'] b. ['a b c d'] c. ['a#b#c#d'] d. ['abcd']

a. ['a', 'b', 'c', 'd'] ***** The split( ) splits specified value from the string.

What is "Programming is fun"[4: 6]? Answers: a. ram b. ra c. r d. pr e. pro

b. ra **** starts 4 up to but not including 6

What Python statement will add 25 to the end of list x? Answers: a. x.add(25) b. x.append(25) c. x.addLast(25) d. x.addEnd(25)

b. x.append(25)

Suppose x is a char variable with a value 'b'. What will be displayed by the statement: print(chr(ord(x) + 1))? a. a b. b c. c d. d

c. c

Which of the following commands will create a list? a. list1 = list( ) b. list1 = [ ] c. list1 = list( [1, 2, 3] ) d. ALL OF THE ABOVE

d

What is len("Good")? Answers: a. 1 b. 2 c. 3 d. 4 e. -1

d. 4

6.15 When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as _________. A. function invocation B. pass by value C. pass by reference D. pass by name

B. pass by value

Why do computers use zeros and ones?

Binary (0's & 1's) are used by computers because digital devices have two stable states and it is natural to use one state for 0 and the other for 1.

How can a derived class use a parent class's data field.

By calling the parent's constructor method. Pic example: A.__init__(self, i) Or Super.__init__( )

10.11 Suppose list1 is [1, 3, 2], What is list1 * 2? A. [2, 6, 4] B. [1, 3, 2, 1, 3] C. [1, 3, 2, 1, 3, 2] D. [1, 3, 2, 3, 2, 1]

C. ******** multiplying a list will add the elements into the list (in this case) twice.

10.34 If a key is not in the list, the binarySearch function returns _________. A. insertion point B. insertion point - 1 C. -(insertion point + 1) D. -insertion point

C. -(insertion point + 1) ******Book return -low -1 if key is not found. This indicates not only that the key is not in the list, but also where the key would be inserted.

count = 0 while count < 10: print("Welcome to Python") count += 1 A. 8 B. 9 C. 10 D. 11 E. 0

C. 10 ***** prints on count 0 - 9 therefore it prints 10 times.

11.6 How many elements are in m = [[x, y] for x in range(0, 4) for y in range(0, 4)]? A. 8 B. 12 C. 16 D. 32

C. 16

6.13 Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What is k after invoking nPrint("A message", k)? k = 2 nPrint("A message", k) A. 0 B. 1 C. 2 D. 3

C. 2

5.15 What is the output for y? y = 0 for i in range(10, 1, -2): y += i print(y) A. 10 B. 40 C. 30 D. 20

C. 30 ************ 0+10=10 10+8=18 18+6=24 24+4=28 28+2=30

6.21 What will be displayed by the following code? x = 1 def f1(): x = x + 2 print(x) f1() print(x) A. 1 3 B. 3 1 C. The program has a runtime error because x is not defined. D. 1 1 E. 3 3

C. The program has a runtime error because x is not defined (in the function).

10.32 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) A. [1] [2] [3] B. [1] [1, 2] [1, 2, 3] C. [1, 2, 3] D. 1 2 3

C. [1, 2, 3]

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

C. [44, 2, 3]

11.12 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 = " ") A. 1 2 3 4 B. 4 5 6 7 C. 1 3 8 12 D. 2 5 9 13 E. 3 6 10 14

D

What is the output of the code shown? print( [ 'Mozzarella', 'Gouda' ] [bool('Brie') ] )

Gouda

_______ is the physical aspect of the computer that can be seen.

Hardware

Python syntax is case-sensitive.

True

True or False: An identifier cannot be a keyword.

True

You can place the line continuation symbol____ at the end of a line to tell the interpreter that statement is continued on the next line.

a) / b) \ #correct c) # d) *

What is the result of 45 // 4? a) 10 b) 11 c) 11.25 d) 12

b) 11

What is the output of the code shown below? print(not(3 > 4), not(5 & 5)) a) True True b) True False c) False True d) False False

b) True False

What data type is the object L below? L = [ 'Gouda', 1, 23, 'hello', 1, 'Brie' ]

list

To start Python from the command prompt, use the command __________.

python

A Python line comment begins with ________.

# (""" """) is the multi-line comment

What is the value of i printed? j = i = 1 i += j + j * 5 print("What is i?", i)

7

11.5 For m = [[x, x + 1, x + 2] for x in range(1, 9, 3)], m is _______. A. [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B. [[0, 1, 2], [1, 2, 3], [2, 3, 4]] C. [1, 2, 3, 4, 5, 6, 7, 8, 9] D. [0, 1, 2, 1, 2, 3, 2, 3, 4]

A

Python features include: 1. Interactive mode 2. portable 3. easy to add modules

1, 2, and 3

Which of the following are true about Python: 1. Python is interpreted 2. Python is Object Oriented 3. Python is interactive

1, 2, and 3

2 * 3**2 evaluates to:

18

6.10 Given the following function header: def f(p1, p2, p3, p4) Which of the following is correct to invoke it? A. f(1, 2, 3, 4) B. f(p1 = 1, 2, 3, 4) C. f(p1 = 1, p2 = 2, p3 = 3, 4) D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) E. f(1, 2, 3, p4 = 4)

A, D, E **** Cannot have f(p1 = 1, 2, 3, 4) Declaring a variable has to be last or atleast behind any single variables.

11.10 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) A. 1 B. 3 C. 5 D. 6 E. 33

A. 1

Which of the following statement prints smith\exam1\test.txt? Answers: a. print("smith\exam1\test.txt") b. print("smith\\exam1\\test.txt") c. print("smith\"exam1\"test.txt") d. print("smith"\exam1"\test.txt")

b. print("smith\\exam1\\test.txt")

In order to store values in terms of key & values we use what core data type?

dictionary

What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1()

e. 3 3 **** prints x & y since x is defined in the parameter of the class it too is changed

In Python, a syntax error is detected by the _______ at _______.

interpreter at runtime

6.12 Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What will be displayed by the call nPrint('a', 4)? A. aaaaa B. aaaa C. aaa D. invalid call E. infinite loop

E. infinite loop **** n never gets decremented inside the while statement.

Which of the following is a valid identifier? a) $343 b) #Mile c) 9x d) 8+9 e) max_radius

e) max_radius **** A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Here are naming conventions for Python identifiers − Class names start with an uppercase letter. All other identifiers start with a lowercase letter. Starting an identifier with a double leading underscore indicates that the identifier is private. Starting an identifier with two leading underscores indicates a strongly private identifier. If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

What will be displayed by the following code? x = 1 def f1(): global x x = x + 2 print(x) f1() print(x)

e. 3 3 ***** global makes the x inside the class global therefore both x's are changed e.g. 3 3

Analyze the following recursive function. def factorial(n): return n * factorial(n-1)

e. The function runs infinitely and causes a StackOverflowError ****** This is because there is no "base case" to stop the recursion and the stack is overflowing/at maximum capacity for its memory.

Suppose s = {1, 2, 4, 3}, what happens when invoking s.add(4)? Answers: a. There is no add method for a set object. b. This method is executed fine and 4 is added to the set. c. Since 4 is already in the set, Python raises a KeyError exception. d. You cannot add an element from a set. e. This method is executed fine and 4 is not added to the set since 4 is already in the set.

e. This method is executed fine and 4 is not added to the set since 4 is already in the set. **** Selected Answer: Incorrect b. This method is executed fine and 4 is added to the set. *When run in Python, I got {1, 2, 3, 4} which means 4 was not added but moved to the last position.

6.7 Which of the following should be defined as a None function? A. Write a function that prints integers from 1 to 100. B. Write a function that returns a random integer from 1 to 100. C. Write a function that checks whether a number is from 1 to 100. D. Write a function that converts an uppercase letter to lowercase.

A. Write a function that prints integers from 1 to 100. **** Does not need any parameters/arguments

6.29 __________ is a simple but incomplete version of a function. A. A stub B. A function C. A function developed using botton-up approach D. A function developed using top-down approach

A. a stub

10.10 Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], What is list1[:-1]? A. 0 B. [1, 3, 2, 4, 5, 2, 1] C. [1, 3, 2, 4, 5, 2] D. [1, 3, 2, 4, 5, 2, 1, 0]

B. ***** Start: beginning End: (up to but not including) -1

sum = 0 for d in range(0, 10, 0.1): sum += sum + d A. The program has a syntax error because the range function cannot have three arguments. B. The program has a syntax error because the arguments in the range must be integers. C. The program runs in an infinite loop. D. The program runs fine.

B. The program has a syntax error b/c the arguments in the range must be integers.

6.28 __________ is to implement one function in the structure chart at a time from the top to the bottom. A. Bottom-up approach B. Top-down approach C. Bottom-up and top-down approach D. Stepwise refinement

B. Top-down approach

7.17 What is the value of times displayed? def main(): myCount = Count() times = 0 for i in range(0, 100): increment(myCount, times) print("myCount.count =", myCount.count, "times =", times) def increment(c, times): c.count += 1 times += 1 class Count: def __init__(self): self.count = 0 main() A. count is 101 times is 0 B. count is 100 times is 0 C. count is 100 times is 100 D. count is 101 times is 101

B. count is 100 times is 0 ****** times is being set to 0 every time we enter the loop.

5.28 What is the number of iterations in the following loop: for i in range(1, n + 1): # iteration A. 2*n B. n C. n - 1 D. n + 1

B. n

5.17 Analyze the following fragment: sum = d = 0 while d != 10.0: d += 0.1 sum += sum + d A. The program does not run because sum and d are not initialized correctly. B. The program never stops because d is always 0.1 inside the loop. C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

C. The program may not stop b/c of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

6.6 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. a heap B. storage area C. a stack D. an array

C. a stack

5.27 What is the number of iterations in the following loop: for i in range(1, n): # iteration A. 2*n B. n C. n - 1 D. n + 1

C. n-1

10.7 To shuffle list1, use _______. A. list1.shuffle() B. shuffle(list1) C. random.shuffle(list1) D. random.shuffleList(list1)

C. random.shuffle(list1)

5.25 What will be displayed by after the following loop terminates? number = 25 isPrime = True i = 2 while i < number and isPrime: if number % i == 0: isPrime = False i += 1 print("i is", i, "isPrime is", isPrime) A. i is 5 isPrime is True B. i is 5 isPrime is False C. i is 6 isPrime is True D. i is 6 isPrime is False

D. i is 6 isPrime is False ******* i being 5 turns isPrime to False. However, i+= 1 turns i to 6 (but doesn't change isPrime)

6.22 What will be displayed by the following code? x = 1 def f1(): global x x = x + 2 print(x) f1() print(x) A. 1 3 B. 3 1 C. The program has a runtime error because x is not defined. D. 1 1 E. 3 3

E. 3 3 **** The use of the "global" referrers back to x and changes its reference value to 3.

5.13 What is the output for y? y = 0 for i in range(0, 10): y += i print(y) A. 10 B. 11 C. 12 D. 13 E. 45

E. 45 ****** 0 + 0 = 0 0 + 1 = 1 ................ 36 + 9 = 45

If you enter 1 2 3 in three separate lines, when you run this program, what will be displayed? print("Enter three numbers: ") number1 = eval(input()) number2 = eval(input()) number3 = eval(input()) #Compute average average = (number1 + number2 + number3)/3 #Display result print(average)

a) 1.0 b) 2.0 #correct c) 3.0 d) 4.0

Show the output of the following code: def f2(n, result): if n == 0: return 0 else: return f2(n - 1, n + result) print(f2(2, 0))

a. 0 **** n = 2 - 1 n = 1 - 1 n = 0 returns 0 *****only returns 0 b/c the recursive function continues until it reaches 0.

What will be displayed by print(ord('z') - ord('a'))? Answers: a. 25 b. 26 c. a d. z

a. 25 ****Unicode ord('a') = 97 ord('z') = 122 ord('A') = 65 ord('Z') = 90

What is the result of the code shown below if x = 1? x << 3

a. 8 *Correct b. 1 c. 2 d. 4 ****Bitwise operators:

If a function does not return a value, by default, it returns _______________. a. None b. int c. double d. public e. null

a. None

What is "Programming is fun"[:2]? Answers: a. Pr b. P c. Pro d. Programming e. Programming is

a. Pr

In the following function, what is the base case? def xfunction(n): if n ==1: return 1 else: return n + xfunction(n - 1)

a. n is 1 ***** The base case is: if n == 1

What should we use to insert 5 to the third position in list x? Answers: a. x.insert(3, 5) b. x.insert(2, 5) c. x.add(3, 5) d. x.append(3, 5)

a. x.insert(3, 5) ***** Selected Answer: Incorrect b. x.insert(2, 5) b is incorrect because the keyword is "position" & not "index"

What will be displayed by the following code? x = 1 def f1(): y = x + 2 print(y) f1() print(x)

b. 3 1 **** prints y then x e.g. 3 1

What is chr(ord( 'B' ))? a. A b. B c. C d. D

b. B

Suppose x = [1, 3, 2, 4, 5, 2, 1, 0], What is x[:-1]? Answers: a. 0 b. [1, 3, 2, 4, 5, 2, 1] c. [1, 3, 2, 4, 5, 2] d. [1, 3, 2, 4, 5, 2, 1, 0]

b. [1, 3, 2, 4, 5, 2, 1] ***** Start: beginning End: up to but not including the last element

What will be displayed by the following code? list_1 = [1, 3] list_2 = list_1 list_1[0] = 4; print(list_2) Answers: a. [1, 3] b. [4, 3] c. [1, 4] d. [1, 3, 4]

b. [4, 3] ****** The pointer object (list_2) points at (list_1)'s object and thus, changes when list_1 does.

What will be displayed by the call nPrint('a', 4) for the function defined below: def nPrint(message, n): while n > 0: print(message) n -= 1

b. aaaa **** prints the message 4 times. e.g. a a a a

Arguments to functions always appear within _______.

b. parentheses ***** so do their parameters

What is the return value for xfunction(4) after calling the following function? def xfunction(n): if n == 1: return 1: else: return n + xfunction( n - 1)

c. 10 ******* In this function 4 is added to 3, 2, 1 for every entry to the recursive call the (n) is subtracted by 1. Therefore, 4 + 3 + 2 + 1 = 10

Suppose x = [ 0.5 * y for y in range(0, 4) ]. What is x? Answers: a. [0, 1, 2, 3] b. [0, 1, 2, 3, 4] c. [0.0, 0.5, 1.0, 1.5] d. [0.0, 0.5, 1.0, 1.5, 2.0]

c. [0.0, 0.5, 1.0, 1.5]

Suppose t = (1, 2, 4, 3, 8, 9). What is the result of the following? x = [ t[i] for i in range(0, len(t), 2) ] Answers: a. [2, 3, 9] b. [1, 2, 4, 3, 8, 9] c. [1, 4, 8] d. (1, 4, 8) e. (2, 3, 9)

c. [1, 4, 8] **** Start: position 0 every 2 elements continue until length

Suppose x = [3, 4, 5, 20, 5, 25, 1, 3]. What is x after x.pop(1)? Answers: a. [3, 4, 5, 20, 5, 25, 1, 3] b. [1, 3, 3, 4, 5, 5, 20, 25] c. [3, 5, 20, 5, 25, 1, 3] d. [1, 3, 4, 5, 20, 5, 25] e. [3, 1, 25, 5, 20, 5, 4]

c. [3, 5, 20, 5, 25, 1, 3] ****** The pop( ) pops off specified index, & if no index is not specified then the last element/index is popped off.

Given a string s = "Mozzarella", which of the following code is incorrect? Answers: a. print(s[0]) b. print(s.lower()) c. s[1] = 'r' d. print(s.strip())

c. s[ 1 ] = 'r' ***** Strings are immutable (cannot change in-place) must create a new object.

Which of the following expression results in a value 1? a. 2 % 1 b. 15 % 4 c. 25 % 5 d. 37 % 6

d **** a = 0 b = 3 c = 0 d = 1

What will be displayed by the following code? x = 1 x = 2 * x + 1 print(x) a) 0 b) 1 c) 2 d) 3 e) 4

d) 3

Which of the following expression results in a value 1? a) 2 % 1 b) 15 % 4 c) 25 % 5 d) 37 % 6

d) 37 % 6 **** The modulo operator % gets the remainder of two numbers divided by each other.

Suppose x = [3, 4, 5, 20, 5, 25, 1, 3]. What is x.index(5)? Answers: a. 0 b. 4 c. 1 d. 2

d. 2 **** looking for element/int 5 at what index?

What is "Programming is fun".find('ram')? Answers: a. 1 b. 2 c. 3 d. 4 e. -1

d. 4

Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i? Answers: a. print(i) b. print(str(i)) c. print(int(i)) print(chr(i))

d. print(chr(i))


Set pelajaran terkait

Zoology Ch. 7, 8, & 9 Study Guide

View Set

Porth Patho Chapter 39: Disorders of the Male Genitourinary System

View Set

Emission Control System Fundamentals

View Set

CH 20 BLOOD VESSELS QUIZ UPDATED 3-2-22

View Set

MGT491 - Chapter 12 Practice Quiz

View Set

Chapter 05: Health Promotion for the Developing Child

View Set

Solve Trigonometric Equations cc

View Set

QUIZ Ch. 1-2, Quiz Ch 3-5, Quiz Ch. 7-8, Quiz Ch. 8-10, Quiz Ch. 11 - 12, Quiz Ch. 13 & 14, Quiz Ch. 15 - 16, Quiz Ch. 17 - 18

View Set

5.3: Reducing Stereotypes, Prejudice, and discrimination

View Set

Session 33: Vehicle emergencies pt. 1

View Set