Exam Python Final Exam 1Your Name2Exam Name(Required) First Last What is true about the following code segment:(Required)if x == 5 : print('Is 5') print('Is Still 5') print('Third 5') Depending on the value of x, either all three of the print statements will execute or none of the statements will execute Only two of the three print statements will print out if the value of x is less than zero. The string 'Is 5' will never print out regardless of the value for x. The string 'Is 5' will always print out regardless of the value for x. What is a good description of the following bit of Python code?(Required)zork = 0 for thing in [9, 41, 12, 3, 74, 15] : zork = zork + thing print('After', zork) Sum all the elements of a list Count all of the elements in a list Compute the average of the elements in a list Find the smallest item in a list Which of these operators is not a comparison / logical operator?(Required) = != == <= Which of the following is not a Python reserved word?(Required) else if for speed What is the most important benefit of writing your own functions?(Required) Following the rule that whenever a program is more than 10 lines you must use a function Following the rule that no function can have more than 10 statements in it Avoiding writing the same non-trivial code more than once in your program To avoid having more than 10 lines of sequential code without an indent or de-indent What will be the value of x when the following statement is executed:(Required)x = int(98.6) 98 6 99 100 What does the following Python program print out?(Required)x = '40' y = int(x) + 2 print(y) 42 402 int402 x2 What is the value of the following expression(Required)42 % 10 2 10 0.42 4210 What does the following code print out?(Required)print("123" + "abc") 123abc This is a syntax error because you cannot add strings hello world 123+abc What is wrong with this Python loop:(Required)n = 5 while n > 0 : print(n) print('All done') This loop will run forever The print('All done') statement should be indented four spaces There should be no colon on the while statement while is not a Python reserved word Which line of the following Python program will never execute?(Required)def stuff(): print('Hello') return print('World') stuff() print ('World') def stuff(): return print ('World') What will the following Python program print out?(Required)def greet(lang): if lang == 'es': return 'Hola' elif lang == 'fr': return 'Bonjour' else: return 'Hello' print(greet('fr'),'Michael') Bonjour Michael Hola Bonjour Hello Hello Michael def Michael Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do?(Required)x = x + 2 Retrieve the current value for x, add two to it, and put the sum back into x Increase the speed of the program by a factor of 2 Exit the program This would fail as it is a syntax error In the following code (numbers added) - which will be the last line to execute successfully?(Required)(1) astr = 'Hello Bob' (2) istr = int(astr) (3) print('First', istr) (4) astr = '123' (5) istr = int(astr) (6) print('Second', istr) 1 2 4 5 In Python, how do you indicate the end of the block of code that makes up the function?(Required) You de-indent a line of code to the same indent level as the def keyword You put the colon character (:) in the first column of a line You add a line that has at least 10 dashes You add the matching curly brace that was used to start the function } For the following code, What value of 'x' will cause 'Something else' to print out?(Required)if x < 2 : print('Below 2') elif x >= 2 : print('Two or more') else : print('Something else') This code will never print 'Something else' regardless of the value for 'x' x = 2.0 x = -2 x = 2 In the following Python code, which of the following is an "argument" to a function?(Required)x = 'banana' y = max(x) print(y) x print max 'banana' Which Python keyword indicates the start of a function definition?(Required) def help continue sweet It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason?(Required)if x == 6 : print('Is 6') print('Is Still 6') print('Third 6') You have mixed tabs and spaces in the file In order to make humans feel inadequate, Python randomly emits 'Indentation Errors' on perfectly good code - after about an hour the error will just go away without any changes to your program Python thinks 'Still' is a mis-spelled word in the string Python has reached its limit on the largest Python program that can be run What is the iteration variable in the following Python code:(Required)friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print('Happy New Year:', friend) print('Done!') friend Sally Glenn Joseph What is the best way to think about a "Syntax Error" while programming?(Required) The computer has used GPS to find your location and hates everyone from your town The computer needs to have its software upgraded The computer is overheating and just wants you to stop to let it cool down The computer did not understand the statement that you entered How would you use the index operator [] to print out the letter q from the following string?(Required)x = 'From marquard@uct.ac.za' print(x[8]) print(x[7]) print(x[9]) print(x[q]) What will the following Python code print out?(Required)def func(x) : print(x) func(10) func(20) 10 20 x 10 x 20 def x func func x 20 What will the following program print out:(Required)>>> x = 15 >>> x = x + 5 >>> print(x) 20 15 5 x+5 How many times will the body of the following loop be executed?(Required)n = 0 while n > 0 : print('Lather') print('Rinse') print('Dry off!') 0 1 5 This is an infinite loop How would you use string slicing [:] to print out 'uct' from the following string?(Required)x = 'From marquard@uct.ac.za' print(x[14:17]) print(x[14/17]) print(x[14+17]) print(x[15:18]) What does the following Python program print out?(Required)tot = 0 for i in [5, 4, 3, 2, 1] : tot = tot + 1 print(tot) 5 10 15 0 Which of the following is a comment in Python?(Required) // This is a test # This is a test /* This is a test */ * This is a test What does the following Python code print out?(Required)print(len('banana')*7) 42 banana banana banana banana banana banana banana -1 banana7 What will the following code print out?(Required)x = 0 if x < 2 : print('Small') elif x < 10 : print('Medium') else : print('LARGE') print('All done') Small All done Small Small Medium LARGE All done LARGE All done What will be the output of the following Python code?(Required)i = 1 while True: if i%3 == 0: break print(i) i + = 1 1 2 3 1 2 None of the mentioned error What does the break statement do?(Required) Jumps to the "top" of the loop and starts the next iteration Resets the iteration variable to its initial value Exits the currently executing loop Exits the program What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?(Required) Indent the line below the if statement Start the statement with a "#" character Underline all of the conditional code Begin the statement with a curly brace { What will the following code print out?(Required)smallest_so_far = -1 for the_num in [9, 41, 12, 3, 74, 15] : if the_num < smallest_so_far : smallest_so_far = the_num print(smallest_so_far) -1 74 42 3 Which of the following elements of a mathematical expression in Python is evaluated first?(Required) Subtraction - Multiplication * Parentheses ( ) Addition + What is a good statement to describe the is operator as used in the following if statement:(Required)if smallest is None : smallest = value matches both type and value Is true if the smallest variable has a value of -1 Looks up 'None' in the smallest variable if it is a string The if statement is a syntax error When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you?(Required) What is your favourite color? What would you like to do? What is the next machine language instruction to run? What Python script would you like me to run? What will be the value of x after the following statement executes:(Required)x = 1 + 2 * 3 - 8 / 4 5.0 4 2.0 8 When you have multiple lines in an if block, how do you indicate the end of the if block?(Required) You capitalize the first letter of the line following the end of the if block You use a curly brace { after the last line of the if block You de-indent the next line past the if block to the same level of indent as the original if statement You omit the semicolon ; on the last line of the if block Which of the following is the correct extension of the Python file?(Required) .python .pl .py .p What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?(Required) break except iterate else What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully).(Required)def addtwo(a, b): added = a + b return a x = addtwo(2, 7) print(x) 2 9 14 7 In Python what is the print() feature best described as?(Required) A built-in function The central processing unit A user-defined function A reserved word What does the following code print out?(Required)def thing(): print('Hello') print('There') There Hello def thing thing Hello There Which of these words is a reserved word in Python ?(Required) tobe names isit while What will the following Python code print out?(Required)data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008' pos = data.find('.') print(data[pos:pos+3]) .ma uct 09:14 mar