CMPSC8 practice quizzes!

.pdf

School

University of California, Santa Barbara *

*We aren’t endorsed by this school

Course

8

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

43

Uploaded by JudgeDinosaur3878

Report
What is the correct syntax to make a global variable (ex_global_var) available inside a function definition? a. global ex_global_var b. ex_global_var c. global(ex_global_var) X d. public ex_global_var How should the following code be modified to also multiply by c the value provided by: return a + b a. a + b * c b. return (a + b) * c c. (return a + b) * c X d. return a + b * c What is returned by the following? result = '5' * 3 print (result) a. error X b. 15 c. 535353 d. 555 What is the output of the following code? def math(x,y): z = x + y return z math(3,5) print(z) a. 5 b. 8 X c. 3 d. error What is the output of the following code?
def print_red(): print("red", end = ", ") def print_orange(): print("orange") def print_yellow(): print("yellow", end = ", ") print_orange() def print_green(): print_red() print("green", end = ", ") print_yellow() print_green() a. red, green, yellow, orange b. red, green, yellow c. red, green d. Red, green, orange, yellow Practice Short Answer Quiz: Given a variable age, write the code that correctly outputs the following. If the age is less zero or greater than 150 output "invalid age", if the age is less than four or greater than eighty output "free", if the age is between four and twelve (inclusive) output "child discount", otherwise output "regular pricing". if age < 0 or age > 150: print(“invalid age”) elif age < 4 or age > 80: print(“free”) elif 4 <= age <= 12: print(“child discount”)
else: print(“regular pricing”) Given this grading scheme (80-100] A , (60-80] B, [0-60] C, and less than 0 and greater than 100 is invalid. Given the variable numerical_grade, output the corresponding letter grade. (Note: (80-100] means 100 is an A but 80 is a B so ( means exclusive and ] means inclusive. Ex: if numerical_grade is 80 the output should be B if the numerical_grade is -1 the output should be invalid) def letter_grade(grade): “““ this function takes a numerical grade as an input and outputs the letter grade ””” if 0 <= grade <= 60: letter = ‘C’ elif 60 < grade <= 80: letter = ‘B’ elif 80 < grade <= 100: letter = ‘A’ else: letter = ‘invalid’ return letter Define a function called triangle_area with parameters base and height that returns the area of a triangle. Relevant geometry equations: area = 1/2 x base x height def triange_area(base, height(: area = ½ * base * height Return area
Define a function add_three_to_me that takes in an integer as a parameter. The function first checks if the type of the parameter is an integer and if it is not it prints "incorrect type", otherwise it returns 3 plus the value of the input. Define three functions hours_to_seconds, hours_to_minutes, and minutes_to_seconds with appropriate parameters. hours_to_minutes will convert hours to minutes, minutes_to_seconds will convert minutes to seconds, and hours_to_seconds will just call the other two functions. def hours_to_minutes(hours): “ “ “ This function takes hours as input and outputs minutes ” ” ” minutes = hours * 60 return minutes def minutes_to_seconds(minutes): “ “ “ This function takes minutes as input and outputs seconds ” ” ” seconds = minutes * 60 return seconds def hours_to_seconds(hours): “ “ “ This function takes hours as input and outputs seconds ” ” ” se What code replaces XXX to make the program output the number 10? multiplier = 1 def do_multiplication(x): return x * multiplier def set_multiplier(x): XXX multiplier = x user_value = 5 set_multiplier(2) print(do_multiplication(user_value)) a. set_multiplier(2) b. global multiplier
c. do_multiplication(x) d. global x What is the output? def print_message1(): print('Message #1') def print_message2(): print('Message #2') print_message1 = print_message2 print_message2 = print_message1 print_message1() print_message2() a. Message #1 Message #2 b. Message #1 Message #1 c. Message #2 Message #1 d. Message #2 Message #2 Which of the following is true? a. A function can have any number of return statements, or no return statement at all. b. A function must have exactly one return statement, or no return statement at all. c. A function can only return strings and numbers, not lists or dictionaries. d.
A function must always have at least one return statement. Which of the following types of constructs should be called when the programmer wants to stop program execution? a. Print a "FIXME" message and return -1 b. return None c. Use a pass statement d . Raise NotImplementedError What is the result when the program is executed? def add(x, y): return x + y print('Begin test') s = add('hello', 5) print(s) print('End test') 2/7 :
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help