Advertisement

Python Quiz - 2

 1.    Which Python keyword indicates the start of a function definition?


A.    def

B.    return

C.    rad

D.    continue


Ans :    def                                    


2.    In Python, how do you indicate the end of the block of code that makes up the function?


A.    You de-indent a line of code to the same indent level as the def keyword

B.    You put the colon character (:) in the first column of a line

C.    You add the matching curly brace that was used to start the function }

D.    You add a line that has at least 10 dashes


Ans :    You de-indent a line of code to the same indent level as the def keyword           


3.    In Python what is the input() feature best described as?


A.    A user-defined function

B.    A reserved word

C.    A built-in function

D.    A data structure that can hold multiple values using strings as keys


Ans :    A built-in function                            

    

4.    What does the following code print out?

def thing():          

    print('Hello')   

print('There')      


A.    There

B.    Hello

C.    def, thing

D.    thing, Hello


Ans :    There                                               


5.    In the following Python code, which of the following is an "argument" to a function?

x = 'banana'   

y = max(x)      

z = y * 2           


A.    x

B.    max

C.    'banana'

D.    y


Ans :    x                                             


6.    What will the following Python code print out?

def func(x) :  

    print(x)      

func(10)         

func(20)         


A.    def, x, func, func

B.    x, 20

C.    x, x

D.   10, 20


Ans : 10, 20                                         


7.    Which line of the following Python program will never execute?

def stuff():                

    print('Hello')        

    return                    

    print('World')      

stuff()                        


A.    print ('World')

B.    print('Hello')

C.    def stuff():

D.    return


Ans :    print('Hello')                          


8.    What will the following Python program print out?

def greet(lang):                          

    if lang == 'es':                        

        return 'Hola'                      

    elif lang == 'fr':                     

        return 'Bonjour'               

    else:                                        

        return 'Hello'                    

print(greet('fr'),'Michael')       


A.    Bonjour Michael

B.    Hola Michael

C.    Hello Michael

D.    def, Hola, Bonjour, Hello, Michael


Ans :    Bonjour Michael                             


9.    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).

def addtwo(a, b):     

    added = a + b       

    return a                

x = addtwo(2, 7)       

print(x)                     

        

A.    2

B.    14

C.    7

D.    9


Ans :    2                                           


10.   What is the most important benefit of writing your own functions?


A.    Following the rule that no function can have more than 10 statements in it

B.    To avoid having more than 10 lines of sequential code without an indent or de-indent

C.    Following the rule that whenever a program is more than 10 lines you must use a function

D.    Avoiding writing the same non-trivial code more than once in your program


Ans :    Avoiding writing the same non-trivial code more than once in your program                

        

11.   What is wrong with this Python loop:

n = 5                           

while n > 0 :               

    print(n)                  

print('All done')        


A.    This loop will run forever

B.    The print('All done') statement should be indented four spaces

C.    There should be no colon on the while statement

D.    while is not a Python reserved word


Ans :    This loop will run forever               


12.    What does the break statement do?


A.    Resets the iteration variable to its initial value

B.    Exits the currently executing loop

C.    Jumps to the "top" of the loop and starts the next iteration

D.    Exits the program


Ans :    Exits the currently executing loop            


13.   What does the continue statement do?


A.    Exits the currently executing loop

B.    Jumps to the "top" of the loop and starts the next iteration

C.    Exits the program

D.    Resets the iteration variable to its initial value


Ans :    Jumps to the "top" of the loop and starts the next iteration       


14.   What does the following Python program print out?

tot = 0                                 

for i in [5, 4, 3, 2, 1] :        

    tot = tot + 1                    

print(tot)                            


A.    5

B.    0

C.    15

D.    10


Ans :    5                                                  

 

15.   What is the iteration variable in the following Python code:

friends = ['Joseph', 'Glenn', 'Sally']      

for friend in friends :                              

     print('Happy New Year:',  friend)    

print('Done!')                                           


A.    friend

B.    Sally

C.    Glenn

D.    Joseph


Ans :    friend                                     


16.   What is a good description of the following bit of Python code?

zork = 0                                             

for thing in [9, 41, 12, 3, 74, 15] :    

    zork = zork + thing                      

print('After', zork)                          


A.    Compute the average of the elements in a list

B.    Sum all the elements of a list

C.    Count all of the elements in a list

D.    Find the smallest item in a list


Ans :    Sum all the elements of a list                   


17.   What will the following code print out?

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)                             


A.    -1

B.    74

C.    42

D.    3


Ans :    -1                                            


18.    What is a good statement to describe the is operator as used in the following if statement:

if smallest is None :       

     smallest = value        


A.    Looks up 'None' in the smallest variable if it is a string

B.    matches both type and value

C.    Is true if the smallest variable has a value of -1

D.    The if statement is a syntax error


Ans :    matches both type and value             


9.    Which reserved word indicates the start of an "indefinite" loop in Python?


A.    while

B.    def

C.    for

D.    break


Ans :    While                                        


20.    How many times will the body of the following loop be executed?

n = 0                            

while n > 0 :                

    print('Lather')        

    print('Rinse')          

print('Dry off!')          


A.    1

B.    This is an infinite loop

C.    0

D.    5


Ans :    0                                          


Post a Comment

1 Comments