Python Function MCQ : Set 3

Python Function MCQ

1). What is the purpose of the given Python function?

def add(a,b):
    total = a+b
    print(“Total: “,total)

num1 = int(input(“Enter number 1: “))
num2 = int(input(“Enter number 2: “))

add(num1,num2)

a) It multiplies two numbers and prints the result.
b) It divides two numbers and prints the result.
c) It subtracts two numbers and prints the result.
d) It adds two numbers and prints the result.

Correct answer is: d) It adds two numbers and prints the result.
Explanation: The function add(a, b) takes two parameters a and b, adds them together, and stores the result in the variable total. Then it prints the result using the print statement. In the provided code, num1 is set to 10 and num2 is set to 20. When add(num1, num2) is called, it adds 10 and 20, resulting in a total of 30, which is printed as “Total: 30”.

2). What is the output of the following code?

def String(str1):
    print(str1*10)

string = “Python”
String(string)

a) PythonPythonPythonPythonPythonPythonPythonPythonPythonPython
b) 10Python
c) 10
d) Python10

Correct answer is: a) PythonPythonPythonPythonPythonPythonPythonPythonPythonPython
Explanation: The given code defines a function called `String(str1)` that takes a string as input and prints it ten times. It then creates a variable `string` with the value “Python” and calls the `String()` function, passing the `string` variable as an argument. When the function is called with `String(string)`, the function takes the input string “Python” and multiplies it by 10.

3). What is the purpose of the following code snippet?

def table(num):
    a = 0
    for i in range(1, 11):
        a = i * num
    print(i, “*”, num, “=”, a)

n = 5
table(n)

a) It calculates the factorial of a given number.
b) It generates a multiplication table for a given number.
c) It prints the squares of numbers from 1 to 10.
d) It calculates the sum of a given number and its multiples.

Correct answer is: b) It generates a multiplication table for a given number.
Explanation: The code defines a function named `table` that takes a parameter `num`. Within the function, a for loop iterates over the range from 1 to 11 (exclusive). In each iteration, the variable `a` is assigned the result of multiplying `i` by `num`. Then, it prints the equation `i * num = a`. The variable `n` is set to 5, and the `table` function is called with `n` as the argument, generating a multiplication table for 5.

4). What is the output of the following code?

def largest(a,b,c):
    if a>b:
        if a>c:
            print(f”{a} is the greatest number”)
    elif b>a:
        if b>c:
             print(f”{b} is the greatest number”)
    else:
        print(f”{c} is the greatest number”)

num1 = 50
num2 = 60
num3 = 44

largest(num1,num2,num3)

a) 50 is the greatest number
b) 60 is the greatest number
c) 44 is the greatest number
d) No output is printed

Correct answer is: b) 60 is the greatest number
Explanation: The function largest is called with three numbers: 50, 60, and 44. Among these numbers, 60 is the greatest. Therefore, the output of the function call is “60 is the greatest number”, which will be printed to the console.

5). What is the output of the following Python code?

def total(list1):
    t = 0
    for val in list1:
        t += val
    print(“Sum of given list: “, t)

l = [6, 9, 4, 5, 3]
total(l)

a) Sum of given list: 27
b) Sum of given list: 27 [6, 9, 4, 5, 3]
c) 27
d) None

Correct answer is: a) Sum of given list: 27
Explanation: The function `total` takes a list as input and iterates through each element, adding them to the variable `t`. In this case, the list `l` contains the elements [6, 9, 4, 5, 3], and their sum is 27. The function prints “Sum of given list: 27” as the output. The print statement is inside the function, and it displays the sum directly to the console.

6). What is the output of the following code?

def mul(list1):
    t = 1
    for val in list1:
        t *= val
    print(“Product of elements in the given list: “,t)

l = [-8, 6, 1, 9, 2]
mul(l)

a) 0
b) 1
c) 864
d) -864

Correct answer is: d) -864
Explanation: The function “mul(l)” is called with the list “[-8, 6, 1, 9, 2]”. The function calculates the product of all elements in the list: (-8) * 6 * 1 * 9 * 2 = -864. The product (-864) is then printed as the output.

7). What is the purpose of the following code snippet?

def rev(str1):
    a = str1[::-1]
    print(“Reverse of the given string: “, a)

string = “Python1234”
rev(string)

a) It reverses the order of characters in a given string.
b) It checks if the given string is a palindrome.
c) It prints the given string without any modifications.
d) It generates an error due to an undefined function.

Correct answer is: a) It reverses the order of characters in a given string.
Explanation: The function `rev` takes a string `str1` as input and assigns the reverse of the string to the variable `a` using slicing with a step of -1. The `[::-1]` notation is used to reverse the order of characters in the string. Finally, the function prints the reversed string as “Reverse of the given string: ” followed by the value of `a`. In the given code, the string “Python1234” is passed as an argument to the function `rev`, so the output will be “Reverse of the given string: 4321nohtyP”.

8). What is the output of the following code?

def check(num):
    if num in range(2, 20):
        print(“True”)
    else:
        print(“False”)
num1 = 15
check(num1)

a) True
b) False
c) Error
d) None

Correct answer is: a) True
Explanation: The given code defines a function `check(num)` that takes a parameter `num`. Inside the function, it checks if the value of `num` is in the range from 2 (inclusive) to 20 (exclusive) using the `in` keyword with the `range()` function. If `num` is within this range, it prints “True”, otherwise, it prints “False”.

9). What is the output of the following code?

def unique(list1):
    print(list(set(list1)))

l = [2, 2, 3, 1, 4, 4, 4, 4, 4, 6]
unique(l)

a) [2, 3, 1, 4, 6]
b) [2, 3, 1, 4]
c) [2, 3, 1, 4, 4, 4, 4, 4, 6]
d) [2, 3, 1, 4, 4, 6]

Correct answer is: a) [2, 3, 1, 4, 6]
Explanation: The function `unique` takes a list as an argument and uses the `set()` function to remove duplicate elements from the list. The `set()` function creates an unordered collection of unique elements. When the `set()` is converted back to a list using the `list()` function, the order of elements may differ.

10). What is the output of the following code?

def prime(num):
    count = 0

    for i in range(2, num):
        if num % i == 0:
            count += 1

    if count > 0:
        print(“It is not a prime number”)
    else:
        print(“It is a prime number”)

num1 = 7
prime(num1)

a) It is not a prime number
b) It is a prime number
c) Error
d) None

Correct answer is: b) It is a prime number
Explanation: The function `prime` takes an input number `num` and checks if it is a prime number. It iterates over the numbers from 2 to `num-1` (exclusive) and checks if `num` is divisible by any of these numbers. If it is divisible by any number, the count is incremented. If the count is greater than 0, it means the number has divisors other than 1 and itself, and thus, it is not a prime number. Otherwise, if the count remains 0, it means the number has no divisors other than 1 and itself, making it a prime number.

11). What is the output of the following code?

def even(list1):
    even_list = []
    for val in list1:
        if val % 2 == 0:
    even_list.append(val)
    print(even_list)

l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even(l)

a) [1, 2, 3, 4, 5, 6, 7, 8, 9]
b) [2, 4, 6, 8]
c) [1, 3, 5, 7, 9]
d) [1, 2, 3, 4, 5, 6, 7, 8]

Correct answer is: b) [2, 4, 6, 8]
Explanation: The function `even()` takes a list as input and creates an empty list called `even_list`. It then iterates through each value in the input list. If the value is divisible by 2 (i.e., it is even), it appends it to `even_list`. Finally, it prints `even_list`. In this case, the input list `l` contains numbers from 1 to 9. The even numbers in this list are 2, 4, 6, and 8. Therefore, the output is `[2, 4, 6, 8]`.

12). What is the output of the following code?

def square():
    for i in range(1, 11):
        print(i, “:”, i ** 2)

square()

a) 1 : 1, 2 : 4, 3 : 9, 4 : 16, 5 : 25, 6 : 36, 7 : 49, 8 : 64, 9 : 81, 10 : 100
b) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
c) 1 : 1, 4 : 16, 9 : 81
d) None of the above

Correct answer is: a) 1 : 1, 2 : 4, 3 : 9, 4 : 16, 5 : 25, 6 : 36, 7 : 49, 8 : 64, 9 : 81, 10 : 100
Explanation: The function `square()` uses a for loop to iterate over the numbers from 1 to 10 (inclusive) using the `range(1, 11)` expression. Inside the loop, it prints the current number (`i`) followed by a colon (`:`) and the square of `i` (`i ** 2`).

13). What is the output of the following code?

mycode = ‘print(“Python”)’
code = “””
def multiply(x, y):
    return x * y

print(‘sqatools’)
“””
exec(mycode)
exec(code)

a) Python
b) sqatools
c) Python\nsqatools
d) sqatools\nPython

Correct answer is: c) Python\nsqatools
Explanation: The `exec()` function in Python is used to execute dynamically generated Python code. In this code, `mycode` variable contains the string `’print(“Python”)’`, which is executed using `exec(mycode)`. This will print the string “Python” to the console. The `code` variable contains a multi-line string that defines a function called `multiply(x, y)` and prints the string “sqatools” to the console. When `exec(code)` is executed, it will execute the code within the string.

14). What is the output of the following code?

def lcm(num1, num2):
    if num1 > num2:
        greater = num1
    else:
        greater = num2

    while True:
        if (greater % num1 == 0) and (greater % num2 == 0):
            lcm = greater
            break
        greater += 1

    print(f”L.C.M of {num1} and {num2}: {lcm}”)

num1 = 12
num2 = 20
lcm(num1, num2)

a) L.C.M of 12 and 20: 60
b) L.C.M of 12 and 20: 12
c) L.C.M of 12 and 20: 20
d) L.C.M of 12 and 20: 240

Correct answer is: a) L.C.M of 12 and 20: 60
Explanation: The function `lcm()` calculates the least common multiple (LCM) of two numbers, `num1` and `num2`. In this case, `num1` is 12 and `num2` is 20. The code first compares the values of `num1` and `num2` and assigns the larger value to the variable `greater`. Next, a `while` loop is initiated. It continues until the condition `True` is met. Within the loop, it checks if the `greater` number is divisible by both `num1` and `num2` using the modulo operator `%`. If it is, it means that `greater` is the LCM, and the value is stored in the variable `lcm`. The loop is then terminated using the `break` statement. Finally, the function prints the result using an f-string, displaying the LCM of `num1` and `num2`.

15). What is the output of the following code?

def total():
    t = 0
    for i in range(1, 11):
        t += i
    print(“Total: “, t)

total()

a) Total: 55
b) Total: 45
c) Total: 10
d) Total: 15

Correct answer is: a) Total: 55
Explanation: The code defines a function named `total()`. Inside the function, a variable `t` is initialized to 0. Then, a loop is executed using the `range()` function from 1 to 10 (excluding 11). In each iteration, the loop variable `i` is added to `t`. After the loop finishes, the total value of `t` is printed with the message “Total: “. The loop iterates over the numbers 1 to 10, and each number is added to the total `t`. Therefore, the final value of `t` is the sum of all numbers from 1 to 10, which is 55. Thus, the output of the code is “Total: 55”.

16). What is the output of the following code?

def func(*args):
    for num in args:
        print(num**3, end=” “)

func(5, 6, 8, 7)

a) 125 216 512 343
b) 5 6 8 7
c) 15 18 24 21
d) 125 216 512 343 5 6 8 7

Correct answer is: a) 125 216 512 343
Explanation: The function `func` accepts a variable number of arguments using the `*args` syntax, which allows it to receive multiple arguments as a tuple. Inside the function, a loop iterates over each element in `args` and prints the cube of each number.

17). What is the output of the following code?

def fact(n):
    fact = 1
    while n > 0:
        fact *= n
        n -= 1
    print(f”Factorial of {num}: {fact}”)

num = 5
fact(num)

a) Factorial of 5: 5
b) Factorial of 5: 120
c) Factorial of 5: 25
d) Factorial of 5: 1

Correct answer is: b) Factorial of 5: 120
Explanation: The code defines a function `fact(n)` to calculate the factorial of a given number. It initializes the variable `fact` to 1 and enters a while loop that iterates while `n` is greater than 0. In each iteration, it multiplies `fact` by `n` and decreases `n` by 1.

18). What is the output of the following code?

def fibo():
    num1 = 0
    num2 = 1
    count = 0

    while count < 10:
        print(num1, end=” “)
        n2 = num1 + num2
        num1 = num2
        num2 = n2
        count += 1

fibo()

a) 0 1 1 2 3 5 8 13 21 34
b) 1 1 2 3 5 8 13 21 34 55
c) 0 1 2 3 4 5 6 7 8 9
d) 0 1 1 3 5 8 13 21 34 55

Correct answer is: a) 0 1 1 2 3 5 8 13 21 34
Explanation: The code defines a function `fibo()` that prints the Fibonacci sequence up to the 10th term. The initial values `num1` and `num2` are set to 0 and 1, respectively. The `count` variable keeps track of the number of terms printed. Inside the while loop, `num1` is printed, and then `num1` is updated to the value of `num2`, and `num2` is updated to the sum of the previous `num1` and `num2`. This process continues until the count reaches 10.

19). What is the output of the following code?

def unique(l):
    distinct = set(l)
    print(list(distinct))

unique([4, 6, 1, 7, 6, 1, 5])

a) [4, 6, 1, 7, 6, 1, 5]
b) [4, 6, 1, 7, 5]
c) [4, 6, 1, 7]
d) [4, 6, 1, 7, 5, 6, 1]

Correct answer is: b) [4, 6, 1, 7, 5]
Explanation: The function `unique` takes a list `l` as input. It creates a set `distinct` from the elements of the list, which removes duplicate values. Then, the set is converted back to a list using the `list()` function. The `print()` statement displays the resulting list.

20). What is the output of the following code?

def dupli(string):
    list1 = []
    for char in string:
        if string.count(char) > 1:
            list1.append(char)
    print(set(list1))

dupli(‘Programming’)

a) {‘r’, ‘g’}
b) {‘o’, ‘m’}
c) {‘r’, ‘m’, ‘g’}
d) {‘r’, ‘o’, ‘m’, ‘g’}

Correct answer is: c) {‘r’, ‘m’, ‘g’}
Explanation: The function `dupli` takes a string as input and checks each character in the string. If the count of the character in the string is greater than 1, it is considered a duplicate and added to the `list1` list. In this case, the string is ‘Programming’, and the characters ‘r’, ‘o’, and ‘g’ occur more than once. The `list1` list will contain these duplicate characters. Finally, the `set()` function is used to convert the `list1` list into a set, which removes duplicate elements. The set containing the duplicate characters is then printed, resulting in {‘r’, ‘m’, ‘g’}.

Leave a Comment