Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.

Unit 2 MCQs Conditionals and Iterations; Functions and Recursion

ECE181 — Introduction To Python 60 questions. Pick an option to check it.

Read the notes Subjective questions

0 of 60 answered 0 right 0 wrong , 60 left

  1. Question 1 of 60 if statement Easy

    Which keyword is used to begin a conditional statement in Python?

  2. Question 2 of 60 two way if - else Easy

    In a two-way if-else statement, when does the else block execute?

  3. Question 3 of 60 nested if and multi-way if-elif-else statement Easy

    Which keyword is used for a multi-way branch to test additional conditions in Python?

  4. Question 4 of 60 for loop Easy

    What does the range(5) function generate when used in a for loop?

  5. Question 5 of 60 while loop Easy

    A while loop in Python continues to execute as long as its condition is:

  6. Question 6 of 60 break and continue Easy

    What does the break statement do inside a loop?

  7. Question 7 of 60 break and continue Easy

    What does the continue statement do inside a loop?

  8. Question 8 of 60 nested loops Easy

    What is a nested loop?

  9. Question 9 of 60 random numbers Easy

    Which module in Python is commonly used to generate random numbers?

  10. Question 10 of 60 random numbers Easy

    Which function returns a random integer between two given values (inclusive)?

  11. Question 11 of 60 function calls Easy

    How do you call a function named greet in Python?

  12. Question 12 of 60 type conversion and coercion Easy

    Which function converts a string "25" into an integer?

  13. Question 13 of 60 type conversion and coercion Easy

    What is the result of float(7) in Python?

  14. Question 14 of 60 math functions Easy

    Which function from the math module returns the square root of a number?

  15. Question 15 of 60 math functions Easy

    What does math.ceil(4.2) return?

  16. Question 16 of 60 adding new function Easy

    Which keyword is used to define a new function in Python?

  17. Question 17 of 60 parameters and argument Easy

    In the function definition def add(x, y):, what are x and y called?

  18. Question 18 of 60 recursion and its use Easy

    What is recursion in programming?

  19. Question 19 of 60 recursion and its use Easy

    What is required in every recursive function to prevent infinite recursion?

  20. Question 20 of 60 conditional expressions Easy

    What is the value of the conditional expression "Yes" if 5 > 3 else "No"?

  21. Question 21 of 60 conditional expressions Medium

    What is the value assigned to result after executing the following code?

    Python
    x = 7
    result = "even" if x % 2 == 0 else "odd"

  22. Question 22 of 60 if statement Medium

    How many times will "Hi" be printed?

    Python
    n = 10
    if n > 5:
        if n < 20:
            print("Hi")

  23. Question 23 of 60 two way if - else Medium

    What does the following code output?

    Python
    age = 18
    if age >= 18:
        print("Adult")
    else:
        print("Minor")

  24. Question 24 of 60 nested if and multi-way if-elif-else statement Medium

    What is printed by this code?

    Python
    marks = 75
    if marks >= 90:
        print("A")
    elif marks >= 70:
        print("B")
    elif marks >= 50:
        print("C")
    else:
        print("F")

  25. Question 25 of 60 for loop Medium

    What is the output of the following loop?

    Python
    total = 0
    for i in range(1, 5):
        total += i
    print(total)

  26. Question 26 of 60 while loop Medium

    How many times does the while loop body execute?

    Python
    x = 16
    count = 0
    while x > 1:
        x = x // 2
        count += 1
    print(count)

  27. Question 27 of 60 nested loops Medium

    What is the output of the following nested loops?

    Python
    count = 0
    for i in range(3):
        for j in range(2):
            count += 1
    print(count)

  28. Question 28 of 60 break and continue Medium

    What does this code print?

    Python
    for i in range(1, 6):
        if i == 3:
            continue
        if i == 5:
            break
        print(i, end=" ")

  29. Question 29 of 60 random numbers Medium

    Which function call returns a random integer that could be any value in the set ?

  30. Question 30 of 60 function calls Medium

    What is printed by the following code?

    Python
    def square(n):
        return n * n
    
    print(square(square(2)))

  31. Question 31 of 60 type conversion and coercion Medium

    What is the result of the following expression?

    Python
    int("12") + int(3.9)

  32. Question 32 of 60 type conversion and coercion Medium

    What is the output of the following code?

    Python
    print(3 + True + 2.0)

  33. Question 33 of 60 math functions Medium

    What does the following code print?

    Python
    import math
    print(math.ceil(4.1) + math.floor(4.9))

  34. Question 34 of 60 math functions Medium

    What is the value of the following expression?

    Python
    import math
    math.sqrt(math.pow(3, 2) + math.pow(4, 2))

  35. Question 35 of 60 adding new function Medium

    What does the following program print?

    Python
    def greet(name):
        print("Hello", name)
    
    x = greet("Sam")
    print(x)

  36. Question 36 of 60 parameters and argument Medium

    What is the output of this code using default and keyword arguments?

    Python
    def power(base, exp=2):
        return base ** exp
    
    print(power(3), power(2, exp=3))

  37. Question 37 of 60 parameters and argument Medium

    What does the following code print?

    Python
    def info(a, b, c):
        return a - b - c
    
    print(info(c=1, a=10, b=3))

  38. Question 38 of 60 recursion and its use Medium

    What value does fact(4) return?

    Python
    def fact(n):
        if n <= 1:
            return 1
        return n * fact(n - 1)

  39. Question 39 of 60 recursion and its use Medium

    What is printed by the following recursive function call mystery(3)?

    Python
    def mystery(n):
        if n == 0:
            return
        print(n, end=" ")
        mystery(n - 1)

  40. Question 40 of 60 for loop Medium

    What is the output of the following code that uses range with a step?

    Python
    result = []
    for i in range(10, 0, -3):
        result.append(i)
    print(result)

  41. Question 41 of 60 conditional expressions Hard

    What is the output of the following code?

    Python
    x = 0
    y = 5
    result = "A" if x else "B" if y else "C"
    print(result)

  42. Question 42 of 60 if statement Hard

    Consider:

    Python
    values = [0, '', None, [], 'False', 0.0]
    count = 0
    for v in values:
        if v:
            count += 1
    print(count)



    What is printed?

  43. Question 43 of 60 two way if - else Hard

    What does this function return for f(-4)?

    Python
    def f(n):
        if n % 2 == 0:
            return n // 2
        else:
            return 3 * n + 1

  44. Question 44 of 60 nested if and multi-way if-elif-else statement Hard

    What is the output?

    Python
    score = 85
    if score >= 90:
        grade = 'A'
    elif score >= 80:
        if score >= 85:
            grade = 'B+'
        else:
            grade = 'B'
    elif score >= 70:
        grade = 'C'
    else:
        grade = 'F'
    print(grade)

  45. Question 45 of 60 for loop Hard

    What is the final value of total?

    Python
    total = 0
    for i in range(2, 20, 3):
        total += i
    print(total)

  46. Question 46 of 60 while loop Hard

    How many times does the loop body execute?

    Python
    n = 100
    count = 0
    while n > 1:
        n = n // 2
        count += 1
    print(count)

  47. Question 47 of 60 nested loops Hard

    What is printed?

    Python
    count = 0
    for i in range(4):
        for j in range(i):
            count += 1
    print(count)

  48. Question 48 of 60 break and continue Hard

    What is the output?

    Python
    result = []
    for i in range(1, 10):
        if i % 2 == 0:
            continue
        if i > 6:
            break
        result.append(i)
    print(result)

  49. Question 49 of 60 break and continue Hard

    What does this print?

    Python
    for i in range(3):
        for j in range(3):
            if j == 1:
                break
            print(i, j)

  50. Question 50 of 60 random numbers Hard

    Given import random; random.seed(0), which statement is true about random.randint(1, 6)?

  51. Question 51 of 60 random numbers Hard

    What is the possible range of values produced by random.randrange(2, 20, 4)?

  52. Question 52 of 60 function calls Hard

    What is the output?

    Python
    def g(x):
        return x * 2
    
    def h(x):
        return x + 3
    
    print(g(h(g(2))))

  53. Question 53 of 60 type conversion and coercion Hard

    What is the result of the following?

    Python
    print(int("10", 2) + int("10", 8) + int("10", 16))

  54. Question 54 of 60 type conversion and coercion Hard

    What is the output?

    Python
    print(int(True) + float("3.5") + int("7"))

  55. Question 55 of 60 math functions Hard

    What does the following evaluate to?

    Python
    import math
    print(math.floor(math.sqrt(50)) + math.ceil(math.log2(10)))

  56. Question 56 of 60 math functions Hard

    What is the output?

    Python
    import math
    print(math.gcd(48, 36), math.factorial(4) % 7)

  57. Question 57 of 60 adding new function Hard

    What is printed?

    Python
    def make_counter():
        count = 0
        def inc():
            nonlocal count
            count += 1
            return count
        return inc
    
    c = make_counter()
    print(c(), c(), c())

  58. Question 58 of 60 parameters and argument Hard

    What is the output?

    Python
    def f(a, b=[]):
        b.append(a)
        return b
    
    print(f(1))
    print(f(2))
    print(f(3, []))

  59. Question 59 of 60 recursion and its use Hard

    What does mystery(4) return?

    Python
    def mystery(n):
        if n <= 1:
            return 1
        return n * mystery(n - 1)

  60. Question 60 of 60 recursion and its use Hard

    How many times is fib called (including the initial call) when computing fib(4)?

    Python
    def fib(n):
        if n < 2:
            return n
        return fib(n - 1) + fib(n - 2)

That's unit 2

Answer a few questions and your score shows up here.