coding

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the purpose of exceptions? 1)To indicate that something has gone wrong, and the program does not know how to continue 2)To point out when the programmer has written exceptionally good code 3)To tell the user that he/she has given bad data, such as a negative number instead of a positive number 4)To allow the programmer to write bad code, but have it still work

1

In which namespace is the variable 'x' defined in the following program? x = 10 def add_nums(): y = 2 z = x + y print z 1)Function namespace 2)Global namespace 3)Object namespace 4)Class namespace

2

What will be printed when the program below is run? def add_two(x): return x + 2 def multiply_by_three(x): return x * 3 def my_function(x): return add_two(x) + multiply_by_three(x) print my_function(12) 1)12 2)24 3)50 4)66

3

Which of the following functions successfully returns the number 10? 1)def my_function(): 10 2)def my_function(10): return 3)def my_function(): return 10 4)def my_function(): print 10

3

What is the correct way to call a function named 'print_sum'? 1)def print_sum(): 2)print_sum 3)print_sum(): 4)print_sum()

4

What is the output of this program? Assume the user enters "kipp52", then "baseball45". # Checks if the user entered the correct username def validate_user(username): if username != "coder43" and username != "goodHacker90" and username != "baseball45": return False else: return True # Gets the user's username def get_username(): while True: username = input("Enter your username: ") if validate_user(username): print "Welcome!" return username else: print "That's not a valid username. Try again" print "Welcome to CodeHS!" get_username() 1)Welcome to CodeHS! Enter your username: kipp52 That's not a valid username. Try again Enter your username: baseball45 Welcome! 2)Welcome to CodeHS! Enter your username: kipp52 Enter your username: baseball45 Welcome! 3)Welcome to CodeHS! Enter your username: kipp52 That's not a valid username. Try again Enter your username: baseball45 4)Welcome to CodeHS! Enter your username: kipp52 That's not a valid username. Try again Enter your username: baseball45 That's not a valid username. Try again Welcome!

1

What line of code should be written after the function definition to add two single digit values and print the sum? def get_single_digit(): while True: try: num = int(input("Enter a single digit positive number: ")) if num > 9: print "Number must be less than 10" elif num < 0: print "Number must be positive." else: return num except ValueError: print "That isn't a number!" 1)print get_single_digit() + get_single_digit() 2)return get_single_digit() + get_single_digit() 3)print get_single_digit(4) + get_single_digit(5) 4)print get_single_digit()++

1

Which of the following code samples correctly creates a function that takes a single parameter and prints it? 1)def print_number(x): print x 2)def print_number(): print parameter 3)def print_number(): print x 4)def print(x)

1

Which of the following code samples correctly passes the number 10 as an argument to the function print_number? 1)print_number(10) 2)print_number() print 10 3)10(print_number) 4)print print_number print 10

1

Which of the following functions successfully returns double the variable 'number'? 1)def my_function(number): return number*2 2)def my_function(number*2): return 3)def my_function(): return number 4)def my_function(number): number*2

1

Which of the following keywords are relevant to exceptions in Python? I. def II. except III. try 1)II and III 2)II only 3)I and II

1

Which of the programs below will print the following output? I would like a green balloon. 1)def balloon_choice(color): print 'I would like a ' + color + ' balloon.' balloon_choice('green') 2)def balloon_choice(color): print 'I would like a ' + color + ' balloon.' 3)def balloon_choice(): print 'I would like a ' + color + ' balloon.' balloon_choice('green') 4)def balloon_choice(): print 'I would like a green balloon.' balloon_choice('green')

1

What will be the output of the following program? name = "Tracy" def print_hello(name): name = "Turtle" print "hello " + name print_hello(name) 1)Tracy 2)hello Turtle 3)Turtle 4)hello Tracy

2

Which of the following inputs to the program below would return a print statement containing Your number: try: my_number = int(input("Enter an integer: ")) print "Your number: " + str(my_number) except ValueError: print "That wasn't an integer!" 1)Four 2)5 3)3.0 4)*

2

Which of the following options is NOT a valid Python function definition? 1)# computes the surface area of a cube of side length 10 # def surface_area(): return 2*(100 + 100 + 100) 2)# computes the surface area of a rectangular prism def surface_area(length, width=10, height): return 2*(length * width + width * height + height * length) 3)# computes the surface area of a rectangular prism def surface_area(length, width, height): return 2*(length * width + width * height + height * length) 4# computes the surface area of a rectangular prism def surface_area(length, width, height=10): return 2*(length * width + width * height + height * length)

2

Consider the following functions. def fn_a(num): if num > 3: print "Fizz" def fn_b(num): if num > 2 and num < 4: print "Fizz" Which for loop would print: Fizz Fizz 1)for i in range(6): fn_b(i+1) 2)for i in range(6): fn_a(i) fn_b(i) 3)for i in range(6): fn_a(i) 4)for i in range(6): fn_b(i)

3

If added to the program below, which of the following additional functions would not cause an error? x = 10 def add_nums(): y = 2 z = x + y print z 1)def subtract_nums(): z = x - y print z 2)def subtract_nums(): z = z - x print z 3)def subtract_nums(): y = 5 z = x - y print z 4)def subtract_nums(): z = y z = x - y print z

3

Which of the following programs will NOT cause an error? 1)say_hi('Margaret') def say_hi(name): print 'hi ' + name 2)function say_hi(name): print 'hi ' + name 3)def say_hi(first_name='John', last_name='Doe'): print 'hi ' + first_name + ' ' + last_name say_hi('Polly Pocket') 4)def say_hi(first_name='John', last_name): print 'hi ' + first_name + ' ' + last_name say_hi('Polly', 'Pocket')

3

Why do programmers use try and except statements when their code might throw an exception? 1)So that the programmer can disregard the exception and do what he/she wants anyways 2)Because Python requires it 3)To handle exceptions gracefully and either handle the error or print out a useful error message. 4)Exceptions only are thrown when there are try and except statements. Otherwise, the program will just fail and the programmer will have no idea why it failed.

3

What will be printed to the screen if the inputs given to the program below are 1.2, -4, hi, 5 ? def retrieve_positive_number(): while True: try: number = int(input("Enter a positive number: ")) if number > 0: return number print "The number must be positive!" except ValueError: print "That wasn't a number!" print retrieve_positive_number() 1)Enter a positive number: 1.2 The number must be positive! Enter a positive number: -4 The number must be positive! Enter a positive number: hi That wasn't a number! Enter a positive number: 5 5 2)Enter a positive number: 1.2 That wasn't a number! Enter a positive number: -4 That wasn't a number! Enter a positive number: hi That wasn't a number! Enter a positive number: 5 5 3)Enter a positive number: 1.2 1.2 4)Enter a positive number: 1.2 That wasn't a number! Enter a positive number: -4 The number must be positive! Enter a positive number: hi That wasn't a number! Enter a positive number: 5 5

4

What will be the output of the following program? a = 4 def print_something(): a = "code" print a * 3 print_something() 1)12 2)code 3)4 4)codecodecode

4

What would this program print? def mystery(num, pattern): result = "" for i in range(num): result = result + pattern return result print mystery(3, "<>") 1)It doesn't print anything 2)<> 3)<><><><> 4)<><><>

4

What would this program print? x_pos = 16 y_pos = 20 # Computes the midpoint between (0,0) and the point def compute_distance(x_pos, y_pos): print "Original Point: " + str(x_pos) + ", " + str(y_pos) print "Midpoint from Origin: " + str(x_pos/2) + ", " + str(y_pos/2) compute_distance(12, 14) print "Point: " + str(x_pos) + ", " + str(y_pos) 1)Original Point: 16, 20 Midpoint from Origin: 8, 10 Point: 16, 20 2)Original Point: 12, 14 Midpoint from Origin: 6, 7 Point: 12, 14 3)Original Point: 16, 20 Midpoint from Origin: 8, 10 Point: 12, 14 4)Original Point: 12, 14 Midpoint from Origin: 6, 7 Point: 16, 20

4

Which of the following is the correct way to declare a function in Python? 1)print_hello: print "hello" 2)function print_hello(): print "hello" 3)print_hello(): print "hello" 4)def print_hello(): print "hello"

4


Kaugnay na mga set ng pag-aaral

Chapter 27 upper respiratory problems

View Set

The Vegetable Industry #8: ID Plant, Fruit, and Seeds (AEST Ag Systems)

View Set

14c. Schizophrenia; Dissociative, Personality, and Eating Disorders; Neurodevelopmental Disorders

View Set

APUSH Semester 1 Final Study Guide

View Set

MKT: Chapter 2: Marketing foundations: Global, ethical, sustainable

View Set

English I Honors FInal Study Guide

View Set

Genetics, Biology - Semester 2 Unit 1

View Set