Week 6


Week 6 quiz questions

Question 1 Functions

Which of the following is NOT a good reason for using functions in Python?

(a) to make complex scripts more easy to read.

(b) to make complex scripts more modular.

(c) to make complex scripts more self-explanatory.

(d) to make complex scripts more compliant to client requirements.

Answer: A


Question 2 Keywords

Which of the following is TRUE in Python?

(a) a function definition begins with the keyword function. 

(b) a function can send output results back to the script the function was called from with the keyword print.

(c) a function must contain the keyword return.

(d) a function with no return keyword returns nothing.

Answer: D


Question 3 Functions

Consider the Python function

def double(x):
    calc = x * x
    return calc

and assume that this function is used by the following script

x = 3
y = double((double(x)))
print(x, y)

Which of the following is TRUE?

(a) the script will print out 3, 3.

(b) the script will print out 3, 6.

(c) the script will print out 3, 12.

(d) the script will print out nothing.

Answer: The script would output 3, 81 I think.


Question 4 Functions

Consider the Python function

def MulDiv(x, y):
    u = x * y
    v = x / y
    return u, v

Assuming that u and v are variables to store the product and the division of two numbers. What is a correct way of calling the function MulDiv to calculate the product and the division of 20 and 10 in a Python script?

(a) u, v = MulDiv (20, 10)

(b) (u, v) = MulDiv (20, 10)

(c) MulDiv (20, 10) = (u, v)

(d) MulDiv (20, 10) = v, u

Answer: A


Question 5 Dictionaries

Consider the following dictionary D in Python:

D = {'key1' : 1, 'key2' : 2, 'key3' : 3}

Which of the following would be the correct way for adding a new key to this dictionary?

(a) D['key4'] = 4

(b) D = key4: 4

(c) D{key4} == 4

(d) key4 = {D:4}

Answer: A

Question 6 Functions

Below is an incomplete definition for a function called smallest that takes two integers as input parameters and returns the smallest of them.

def smallest(x, y):
    if ??? return x
    else: return y

So, for example if print(smallest(10, 20)) is executed the result will be 10. What do we need to replace ??? with for the function to work correctly?

(a) x >= y:

(b) x < y

(c) x > y

(d) x < y:

Answer:  D

Question 7 For Loops

Consider the following Python script snippet

target = 0
for i in range(2,6):
    target = target + i*i
    print ( i )
print (target )

which of the following is TRUE?

(a) it will output 2, 3, 4, 5, 0

(b) it will output 1, 2, 3, 4, 5, 6

(c) it will output 1, 2, 3, 36

(d) it will output 2, 3, 4, 5, 54

HINT: calling the range function with two inputs means to generate numbers up to, but not including the second input, so range(10, 13) will generate the numbers 10, 11 and 12.

Answer: D

Question 8 Lists

Given that t is a list of strings, what is the functionality of the following Python snippet?

def process_all(t):
    res = []
    for s in t:
        if s.isupper():
            res.append(s)
    return res

(a) to select the uppercase strings from a list of strings and append them into a new list

(b) to remove the uppercase strings from a list of strings

(c) no functionality, as there is a syntax error

HINT: the method isupper() checks whether all the letters of a string are uppercase, while the method append() is used in Python to append an item into an existing list.

Answer: A

Question 9 Booleans

In the Python snippet below what will the output be?

x = 20
y = 10
if x > 15 and y >= 30:
    print (“x > 15 and y >= 30!!”)
elif x > 15 and y < 30:
    print (“x > 15 and y < 30!!”)
else:
    print (“Must be another number!”)

(a) x > 15 and y >= 30!!

(b) x > 15 and y < 30!!

(c) Must be another number!

(d) Nothing, as there is a syntax error!

Answer: B

Question 10 Arithmetic Operations

In the Python snippet below what will the output be?

x = 1
y = '1'
print (x + y)

(a) 2

(b) 11

(c) 50

(d) Nothing, as there is an error!

Answer: D

Question 11 Comments

Which example below is a correct way to write comments in Python scripts?

(a) //I am a comment

(b) #I am a comment

(c) "I am a comment"

(d) ^&I am a comment

Answer: B

Question 12 Functions

Given the definition of function test as below, what will be printed out?

def test(x):
    print (x*2)
test(10 * 5 + 1 * 2)

(a) 104

(b) 204

(c) 240

Answer: A

Question 13 Functions

Assuming the definition for the function truetest as below, what will be returned by executing the print statement?

def truetest(x,z,y):
    return y + z - x
print (truetest(10,20,30))

(a) 10

(b) 20

(c) 30

(d) 40

(e) nothing, as a function cannot use more than two inputs!

Answer: D

Question 14 Functions

Assuming the definitions of functions ex1 and ex2 as below, what will be returned if print(ex1(12)) is executed?

def ex1(x):
    return ex2(x + 2)
def ex2(y):
    return y / 2

(a) 7

(b) 12

(c) 14

(d) 28

Answer: A

Question 15 While Loops

a = 5
while a < 20:
    print (a)
    a +=2

What will the output be?

(a) 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19

(b) 5, 7, 9, 11, 13, 15, 17, 19

(c) 5, 7, 9, 11, 13, 15, 17, 19, 21

(d) 5, 7, 11, 13, 17

Answer: C

Question 16 Comments

Which of the following statements is false?

(a) comments make scripts easier to understand

(b) comments make scripts easier to debug

(c) comments make scripts run faster

(d) comments make scripts comply with common industry standards

Answer: C

Question 17 Variables

What values can be stored in a variable in Python?

(a) fixed integer values only

(b) character or string values only

(c) floats only

(d) any data value that can change during run-time

Answer: D

Question 18 Lists

Given the script below, what will be printed?

myList = [3, "food", 33,["anotherList", "cat", "dog", 8/4], 80.3, [4,2,0]]
print (myList[3][3])

(a) "ood"

(b) 420

(c) 2

(d) Nothing, there's a syntax error

HINT: When counting a list's elements using an index we always start from 0, e.g. if L = [2, 3, 4], then L[0] is the element 2, L[1] is the element 3 and so on. Note that in the example above some of the elements of the given list myList are lists themesleves.

Answer: C

Question 19 break

What is the purpose of the statement break in Python?

(a) to allow scripters to build loops

(b) to exit a loop and resume execution at the next statement

(c) to allow scripters to build functions

(d) to return control to the beginning of a loop

Answer: B

Question 20 Integer Division

Given the Python script below, in Python 3 what will be printed?

a = 2
b = 3
print(a/b*b*10)

(a) 0

(b) 20

(c) 19.9999...

(d) nothing, not valid syntax

Answer: D

Question 21 Integer Division

Given the Python script below, in Python 2 what will be printed? (Compare with your answer to Question 20)

a = 2
b = 3
print(a/b*b*10)

(a) 0

(b) 20

(c) 19.9999...

(d) nothing, not valid syntax

Answer: 0

Leave a comment

Log in with itch.io to leave a comment.