programming exam 4-6

Ace your homework & exams now with Quizwiz!

Which input value causes "Goodbye" to be output next? x = int(input()) while x >= 0: # Do something x = int(input()) print('Goodbye') a. -1 b. 0 c. 1 d. No such value

a. -1

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

What should XXX and YYY be so that the final output shows how many negative values are input? n = 0 val = Get next input While val is not 0 If XXX YYY val = Get next input put n to output a. XXX: val < 0, YYY: n = n + 1 b. XXX: val < 0, YYY: n = n + val c. XXX: val > 0, YYY: n = n + 1 d. XXX: val < 0, YYY: val = val + 1

a. XXX: val < 0, YYY: n = n + 1

Which has an error? Assume x = 10 and y = 20. a. if x = y: b. if x < y: c. if x <= y: d. if x != y:

a. if x = y

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? age_type = '' if YYY: age_type = age_type + "Young" if ZZZ: age_type = age_type + " but not too young" print(age_type) 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 > 20ZZZ: user_age > 10

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

Excess indentation must be removed from which lines to make the code correct? 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, 3, 4 d. 2, 4, 5

c. 2,3,4

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

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

Which statement is equivalent to the following? if x == 1: t = 'minute' else: t = 'minutes' a. t = 'minutes' if x == 1 else 'minute' b. t = 'minute' if x != 1 else 'minutes' c. t = 'minute' if x == 1 else 'minutes' d. t = 'minute' + ('s' if x == 1 else '')

c. t = 'minute' if x == 1 else 'minutes'

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

d. ((not) and (y == a)) and b

What is the value of x after the following code is executed? x = 17 if x * 2 <= 34: x = 0 else: x = x + 1 x = x + 1 a. 1 b. 18 c. 19 d. 35

a. 1

How many times will the body of the loop execute? my_list = [6, 2, 8, -1, 12, 15, -7] x = Get first my_list value While x is not negative: put "Positive number!" to output x = Get next my_list value Put "Done" to output a. 3 b. 4 c. 5 d. 7

a. 3

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

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

Which determines if user_unit is in the list accepted_units? accepted_units = [ 'in', 'cm', 'mm', 'km', 'miles' ] a. if user_unit in accepted_units: b. if accepted_units in user_unit: c. if user_unit == (accepted_units): d. if user_unit == x in accepted_units:

a. if user_unit in accepted_units

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

Which expression can be used to decide if x is not between 10 and 20? a. not (10 < x < 20) b. not (x < 10 and x < 20) c. not (x < 10 or x < 20) d. not (x > 10 or x < 20)

a. not (10 < x < 20)

Which operator is evaluated last in an expression? a. or b. and c. == d. +

a. or

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

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

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

With the logic block shown below, what is 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

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

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)

b. prints 'loop' three times, then 'final value of count:3'

What is the value of x after the following code is executed? x = 7 If x < 7 x = x + 1 x = x + 2 a. 7 b. 8 c. 9 d. 10

c. 9

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'

Which is true of the badly formatted code? x = input() if x == 'a': print('first') print('second') a. Both print() statements must be indented. b. Neither print() statement has to be indented. c. The first print() statement must be indented. d. The second print() statement can't be indented.

c. The first print() statement must be indented

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

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

If x = 10 and y = 20, which expression is True? a. x == y b. y <= x c. y >= x d. y != 2 * x

c. y >= x

Which operator is evaluated first: x + y < y - z * 2 ? a. + b. < c. - d. *

d. *

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

What is output when the following code is executed? score = 65 group = '' if score <= 60: group = group + 'A' if score <= 70: group = group + 'B' if score <= 80: group = group + 'C' else: group = group + 'D' print(group) a. C b. D c. AB d. BC

d. BC


Related study sets

Chapter 27. Measuring Domestic Output and National Income

View Set

Module11 Security in Network Design

View Set

Earthquakes and Earth's Interior

View Set

Life and Health Simulation Exam Missed Questions

View Set

CHFI - Chapter 6 (Operating System Forensics)

View Set

A&P Chapter 14: Brain and Cranial Nerves

View Set

Prep U's - Chapter 1 - Professional Nursing Practice

View Set

Chapter 23: perioperative nursing

View Set

Epithelial Tissue: Structure and Function

View Set