Functions - Practice Quiz

 

You can find the solutions to these questions at the bottom.

 

PART 1 – MULTIPLE CHOICE QUESTIONS

#

CODE

ANSWER

1

What is the name of the function below?

 

def coco(low, high):

    for x in range(low, high):

        print(x)

a) range

b) low

c) def

d) coco

e) x

2

What would be a better name for the following function?

 

def fun(num1, num2):
    return num1 + num2

 

a) store

b) add

c) multiply

d) display

e) chicken_wing

3

What would be a better name for the following function?

 

def fin(m1, m2, m3, m4):
    return (m1 + m2 + m3 + m4) / 4

 

a) divide

b) add

c) multiply

d) average

e) yoda

4

What will the function below do?

 

def coco(a, b):

    for x in range(a, b):

        print(x)

a) Output x a few times

b) Output numbers

c) Output “Hello”

d) Nothing

e) Drink coco

5

Which of the following function calls is correct?

 

def greet(n1, n2):

    print("Hello", n1, " ", n2)

a) greet("Pat","Campo")

b) g("Wayne","Gretz")

c) hello("Pat Campo")

d) g("Wayne Gretz")

e) greet("Pat Campeau")

6

Which of the function calls will return a value of 8 for the function below?

 

def fun(num1, num2):
    return num1 + num2

a) fun (2, 2, 2)

b) fun (4, 2)

c) fun (6, 2)

d) fun (8, 8)

e) This is not fun

7

What will the following function do?

 

def m(a,b,c):

    if a > b and a > c:

        return a

    elif b > a & b > c:

        return b

    else:

        return c

a) Output the max of a, b and c

b) Return the max of a, b and c

c) Return the sum of a, b and c

d) Return the average of a, b and c

e) Output the value of c

8

What will the following function do?

 

def hyp(a, b):

    return (a**2 + b**2)**0.5

a) Return a + b

b) Return a2 + b2

c) Return a * b

d) Return hypotenuse of right triangle with sides a and b

 

PART 2 – WHAT WILL BE OUTPUTTED?

 

1

 

def disp(option):

    if option == 1:

        print("Hi")

    elif option == 2:

        print("Hello")

    else:

        print("Yo")

       

print("Hey")

 

 

2

 

def disp(option):

    if option == 1:

        print("Hi")

    elif option == 2:

        print("Hello")

    else:

        print("Yo")

 

disp(1)

 

 

3

 

def disp(option):

    if option == 1:

        print("Hi")

    elif option == 2:

        print("Hello")

    else:

        print("Yo")

 

disp("Word up")

 

 

4

 

def fun(a, b):

    return b - a

 

print(fun(10,4))

print(fun(4,10))

 

 

5


def dif(a, b):

    return b - a

 

val = dif(4, dif(2, 9))

print(val)

 

 

6

 

def first(name):

    return name[0]

 

def ini(fname, lname):

    return first(fname) + first(lname)

 

print(ini("Pat", "Campo"))

 

7


def add(a, b):

    return a + b

 

val = add(add(1, 3), add(4, 5))

print(val)

 

8


def grade(mark):

    if mark >= 80:

        return "A"

    elif mark >= 70:

        return "B"

    elif mark >= 60:

        return "C"

    elif mark >= 50:

        return "D"

    else:

        return "F"

 

print(grade(75))

 

9

def funky(name="Mrs. Pacman"):

    print("Hello", name)

 

funky("Pacman")

 

10

def funky(name="Mrs. Pacman"):

    print("Hello", name)

 

funky()

 

 

Click here for the solutions.