Programming Midterm

Ace your homework & exams now with Quizwiz!

47) Which expression calculates the average of first_num andsecond_num?first_num = input('Enter the first number: ') second_num = input('Enter the second number: ') a. (float(first_num) + float(second_num)) / 2 b. float((first_num + second_num) / 2) c. (first_num + second_num) / 2 d. float(first_num / 2) + float(second_num / 2)

a. (float(first_num) + float(second_num)) / 2

35) What is the ending value of a when b is assigned with the value 5? a = 7 a = b + 5 if b > 5 else 0 a. 0 b. 5 c. 7 d. 10

a. 0

39) What is the output? my_list = [3, 7, 0, 2, -1, 8] index = 0 while my_list[index] > 0: print(my_list[index], end=' ') index += 1 a. 3 7 b. 3 7 0 c. 3 7 0 2 d. 3 7 0 2 -1

a. 3 7

46) What is the result of the expression: int('1750.0')? a. An error: the string does not represent an integer value b. The value 1750 as an int c. The value 1750.0 as a float d. The value '1750' as a string

a. An error: the string does not represent an integer value

31) Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first. (x == 5) or (y == 2) and (z == 5) a. False OR (True AND False) --> False OR False --> False b. False OR (True AND False) --> False OR True --> True c. (False OR True) AND False --> True AND False --> False d. (False OR True) AND False --> True AND False --> True

a. False OR (True AND False) --> False OR False --> False

12) Which of the following statements about my_list is false? my_list = ['JFK', 'LAX', 'MIA'] a. The element at index 1 is 'JFK' b. The list has a length of 3 c. The list elements are all strings d. The index of the last item in the list is 2

a. The element at index 1 is 'JFK'

25) What conditions have to be true to make the following code display "B"? if color == 'red': if style < 3: print('A') elif style < 5: print('B') else: print('C') elif color == 'blue': print('D') a. color is 'red' and style is 4 b. color is 'red' and style is 5 c. color is 'red' and style is 6 d. color is 'blue' and style is 3

a. color is 'red' and style is 4

4) What is the name of the data type used for floating point numbers? a. float b. decimal c. non_integer d. floating_point

a. float

3) Objects like integers and strings that can't be modified are called _____ . a. immutable b. mutable c. frozen d. set

a. immutable

2) Which of the following identifiers is valid? a. max_age b. 32area c. transfer$ d. True

a. max_age

29) What condition should replace ZZZ to output "Same name" only if the values of two variables are the same? my_name = input("Enter my name: ") your_name = input("Enter your name: ") if ZZZ: print("Same name") a. my_name == your_name b. my_name = your_name c. my_name is your_name d. id(my_name) == id(your_name)

a. my_name == your_name

20) Which print statement displays: 'Tokyo had 9.273000 million people in 2015'? a. print('{:s} had {:f} million people in {:d}'.format('Tokyo',9.273, 2015)) b. print('{:s} had {:f} million people in {:d}', ('Tokyo', 9.273,2015)) c. print('{:s} had {:d} people in {:d}.format('Tokyo', 9273000,2015)) d. print({:s} + ' had ' + {:d} + ' million people in ' + {:d},('Tokyo', 9.273, 2015))

a. print('{:s} had {:f} million people in {:d}'.format('Tokyo',9.273, 2015))

34) Which expression is equivalent to the following code? if age < 18: x = x + 5 else: x = x + 1 a. x = x + 5 if age < 18 else x + 1 b. x = x + 5 if age >= 18 else x + 1 c. if age < 18 x = x + 5 else x = x + 1 d. x = x + 1 else if age < 18 x + 5

a. x = x + 5 if age < 18 else x + 1

22) With the logic block shown below, what will be the output when grade is assigned with the value 75? If grade < 50 Put "F" to output Else If grade < 60 Put "D" to output Else If grade < 75 Put "C" to output Else If grade < 85 Put "B" to output Else If grade <= 100 Put "A" to output Else Put "Invalid grade" to output a. "A" b. "B" c. "C" d. "Invalid grade"

b. "B"

23) What is the value of test_val after the following code is executed? a = 12 test_val = 6 if a * 2 == test_val: a = a + 7 else: test_val = 2 * a test_val = a + 1 a. 7 b. 13 c. 24 d. 25

b. 13

26) What is x's final value? x = 10 y = 20 if y <= 2 * x: x = x + 5 else: x = x * 2 a. 10 b. 15 c. 20 d. 25

b. 15

14) Which statement correctly explains a difference between lists and tuples? a. The built-in function len() works with lists but not with tuples. b. List items can be changed, while tuple items can't be changed. c. List items can be of any type, while tuple types can only be numbers. d. List items use [ ] operators to access items by index, while tuples use ( ) operators to access items by index.

b. List items can be changed, while tuple items can't be changed.

48) Which expressions for YYY and ZZZ will output "Young" when user_age is less than 20 and "Young but not too young" when user_age is between 10 and 20? if YYY: print("Young") if ZZZ: print(" but not too young") a. YYY: user_age < 20 ZZZ: user_age < 10 b. YYY: user_age < 20 ZZZ: user_age > 10 c. YYY: user_age > 20 ZZZ: user_age < 10 d. YYY: user_age > 20 ZZZ: user_age > 10

b. YYY: user_age < 20 ZZZ: user_age > 10

50) A child is required to use a booster seat in a car until the child is 9 years old, unless the child reaches the height of 59 inches before age 9. Which expression can be used to decide if a child requires a car seat or not? a. if age < 9 or height < 59: b. if age >= 9 or height >= 59: c. if age >= 9 and height >= 59: d. if age <= 9 and height <=59:

b. if age >= 9 or height >= 59:

10) What does print("one\\two\\\\three") display? a. one\\two\\\three b. one\two\\three c. one twothree d. one\\\\two\\\\\\\\three

b. one\two\\three

44) What is displayed when the following code is executed? empty_string = ' ' print(len(empty_string)) a. "empty" b. 1 c. 0 d. "0"

c. 0

1) What is the value of y after the following code is executed? Note that the question asks for y, not x. x = 10 y = x + 2 x = 12 a. 8 b. 10 c. 12 d. 14

c. 12

43) What is the ending value of x? x = 0 i = 5 while i > 1: x = x + i i = i - 1 a. 0 b. 12 c. 14 d. 15

c. 14

13) What is the output? my_list = [2, 8, 3, 1, 18, 5] print(my_list[3] + my_list[1] * 2) a. 7 b. 10 c. 17 d. 18

c. 17

49) What is the output? x = 18 while x % 3 == 0: print(x, end=' ') x = x // 3 a. 6 b. 6 2 c. 18 6 d. 18 6 2

c. 18 6

33) Which line numbers are indented incorrectly? 1. print('start') 2. if x > 10: 3. print('large') 4. else: 5. print('small') 6. print('done') a. 1, 6 b. 1, 2, 3 c. 2, 4, 5 d. 4, 5, 6

c. 2, 4, 5

28) What is the final value of z? grades = { 'A': 90, 'B': 80, 'C': 70, 'D': 60 } my_grade = 70 if my_grade not in grades: z = 1 else: z = 2 if 'F' in grades: z = z + 10 else: z = z + 20 a. 11 b. 12 c. 21 d. 22

c. 21

37) What is the ending value of count? my_list = [3, -4, 0, -1, 2, 1, 8] n = 0 count = 0 While n < length of my_list: If my_list[n] > 0 count = count + 1 n = n + 1 a. 1 b. 3 c. 4 d. 5

c. 4

45) How many times will the body of the loop be executed? number = 70 guess = 55 while number != guess: if number > guess: guess = guess + 10 else: guess = guess - 1 print('The number is:', guess) a. 2 b. 3 c. 7 d. 15

c. 7

38) Which input for variable c causes "Done" to be output next? c = 'y' while c == 'y': # Do something print('Enter y to continue, n to quit: ', end=' ') c = input() print('Done'); a. 'y' only b. 'n' only c. Any value other than 'y' d. No such value (infinite loop)

c. Any value other than 'y'

27) A company wants to send a reminder email to users who have not logged in for more than 10 days, but less than 20 days. Which expression can be used to decide if a user should get an email or not? a. if days_since_login > 10: b. if days_since_login > 10 or days_since_login < 20: c. if days_since_login > 10 and days_since_login < 20: d. if days_since_login > 10 and not days_since_login < 20:

c. if days_since_login > 10 and days_since_login < 20:

21) Which branch structure is necessary if a program needs to output "Yes" if a variable's value is positive, or "No" otherwise? a. if b. else c. if-else d. if-elseif-else

c. if-else

8) Which statement makes the code in the math module available? a. use math b. allow math c. import math d. include math

c. import math

9) Assume a and b are variables that hold the base and height of a right triangle. The length of the long side (hypotenuse) is calculated as the square root of a^2 + b^2. Which expression calculates the length of the hypotenuse? a. math.square_root(a * a + b * b) b. math.sqrt(math.pow(a * a), math.pow(b * b)) c. math.sqrt(math.pow(a, 2)) + math.pow(b, 2))) d. math.pow(math.sqrt(a), 2) + math.pow(math.sqrt(b), 2)

c. math.sqrt(math.pow(a, 2)) + math.pow(b, 2)))

40) Which expression replaces ZZZ to make the loop ask for names until 'quit' is entered? name = input("What is your name ('quit' to exit)? ") while ZZZ: print('Hello, ', name) name = input("What is your name ('quit' to exit)? ") a. name == 'quit' b. name is not 'quit' c. name != 'quit' d. 'quit' is False

c. name != 'quit'

30) Which expression is equivalent to: not x and y == a and b? a. (not (x and y)) == (a and b) b. ((not x) and y) and (a and b) c. not ((x and (y == a)) and b) d. ((not x) and (y == a)) and b

c. not ((x and (y == a)) and b)

17) The variable emails_dict is assigned with a dictionary that associates student idswith email addresses. Which statement prints the email address associated with the student id "C2104"? a. print(value of emails_dict("C2104")) b. print(key of emails_dict("C2104")) c. print(emails_dict["C2104"]) d. print(emails_dict["[email protected]"])

c. print(emails_dict["C2104"])

18) Which data type is the correct choice to store the names of all the hockey players who have scored 3 or more goals in a single game in no specific order? a. int b. tuple c. set d. list

c. set

36) For the given pseudocode, which XXX and YYY will output the sum of the input integers (stopping when -1 is input)? Choices are in the form XXX / YYY. val = Get next input XXX While val is not -1 YYY val = Get next input Put sum to output a. sum = val / sum = val b. sum = val / sum = sum + val c. sum = 0 / sum = sum + val d. sum = 0 / sum = val

c. sum = 0 / sum = sum + val

6) Which statement is equivalent to the following assignment? x -= 2 + y a. x = 2 + y - x b. x = -(2 + y) c. x = x - (2 + y) d. x = x - 2 + y

c. x = x - (2 + y)

11) If text_line = 'one fish two fish', what is the value of text_line[6]? a. ' ' b. 'h' c. 'i' d. 's'

d. 's'

24) What is displayed when the following code is executed? day = 23 if day % 10 == 1: ending = "st" elif day % 10 == 2: ending = "nd" elif day % 10 == 3: ending = "rd" else: ending = "th" print(str(day) + ending) a. 23th b. 23st c. 23nd d. 23rd

d. 23rd

42) How many times does the following loop iterate? i = 0 while i <= 100: print(i) i = i + 2 a. 0 b. 49 c. 50 d. 51

d. 51

32) What is the output? count = 0 while count < 3: print('loop') count = count + 1 print('final value of count:', count) a. Prints 'loop' once, then 'final value of count: 1' b. Prints 'loop' three times, then 'final value of count: 3' c. Prints 'loop' three times, then 'final value of count: 4' d. Prints 'loop' forever (infinite loop)

d. Prints 'loop' forever (infinite loop)

15) Which of the following statements removes the value 'Google' from the set,companies? companies = { 'Apple', 'Microsoft', 'Google', 'Amazon' } a. companies.pop(2) b. companies.pop('Google') c. companies.remove(2) d. companies.remove('Google')

d. companies.remove('Google')

41) Fill in the blank so that the loop displays all odd numbers from 1 to 100. i = 1 while i <= 100: print(i) i = _____ a. 1 b. i + 1 c. 2 d. i + 2

d. i + 2

19) Which line in the following program causes a runtime error? sales = { "apples": 0, "lemonade": 0 } sales["apples"] = sales["apples"] + 1 del sales["lemonade"] print(len(sales["apples"])) a. sales = { "apples": 0, "lemonade": 0 } b. sales["apples"] = sales["apples"] + 1 c. del sales["lemonade"] d. print(len(sales["apples"]))

d. print(len(sales["apples"]))

7) Which expression gives the number of whole minutes that corresponds to some number of seconds? a. seconds % 60 b. seconds / 60 c. seconds * 60 d. seconds // 60

d. seconds // 60

5) Which of the following statements produces an error? Assume string_1 = 'abc' and string_2 = '123'. a. string_2 = string_1 b. string_1 = string_2 + "456" c. print(string_1 + string_2) d. string_1[1] = 'B'

d. string_1[1] = 'B'

16) What values are in result_set after the following code is run? my_set = {1, 2, 3, 4, 5, 6} other_set = {2, 4, 6} result_set = my_set.union(other_set) a. { } b. {1, 3, 5} c. {2, 4, 6} d. {1, 2, 3, 4, 5, 6}

d. {1, 2, 3, 4, 5, 6}


Related study sets

Project Management - Ch. 9 MCQ only

View Set

Astronomy 105: Chapter 12: Saturn

View Set

Unit 2B Nature and Functions of Product Market (Surplus, Elasticity, and Utility)

View Set

4720 Project management Post quiz study

View Set

Chapter 3: The Gallbladder (Penny)

View Set