Programming Quiz on Loops and Data Types
40 Questions
0 Views

Programming Quiz on Loops and Data Types

Created by
@LucidQuatrain

Podcast Beta

Play an AI-generated podcast conversation about this lesson

Questions and Answers

How many times will the word 'exam' be printed by the given code fragment?

  • Infinite Loop
  • 9
  • 11 (correct)
  • 10
  • Which of the following correctly matches the input to its data type?

  • [3.1, 4.2] -> tuple
  • 'hello' -> string (correct)
  • (2, 3) -> list
  • [1, 2, 3] -> tuple
  • What will the recursive function return if lst = [1,2,3]?

  • 4.0
  • 32.0
  • 1
  • 64.0 (correct)
  • What is the output of the function call 'whatisthis(3)' based on the definitions provided?

    <p>6</p> Signup and view all the answers

    In the for loop, what is the value of x after the first iteration when using 'for x in range(20,9,-1)'?

    <p>20</p> Signup and view all the answers

    What type of data structure is the output of foo2(2) in the foo1 function?

    <p>Integer</p> Signup and view all the answers

    What value does the variable 'a' hold in the function after the first recursive call with lst = [1,2,3]?

    <p>1</p> Signup and view all the answers

    Which of the following will NOT result in an error when run in Python?

    <p>print('Hello World')</p> Signup and view all the answers

    What will be the output of the expression f1(5)?

    <p>False</p> Signup and view all the answers

    What is the output of whatisthis(2) + foo2(2)?

    <p>Error</p> Signup and view all the answers

    What would be a more meaningful name for the function f1?

    <p>isTrue</p> Signup and view all the answers

    How does function f2 relate to function f1?

    <p>It creates a circular dependency with f1.</p> Signup and view all the answers

    Which of the following statements correctly completes the function isprime(x)?

    <p>c = 2; return True;</p> Signup and view all the answers

    Which statement correctly completes the return condition in isprime(x)?

    <p>return True;</p> Signup and view all the answers

    What would be the result of calling foo2(3) based on the provided functions?

    <p>4</p> Signup and view all the answers

    What is a potential flaw in the implementation of isprime(x)?

    <p>It does not handle negative inputs.</p> Signup and view all the answers

    What will the function 'counting_clouds' return if provided with an empty list?

    <p>0</p> Signup and view all the answers

    Which option correctly completes the 'counting_clouds' function to count clouds wider than 5?

    <p>return 1 + counting_clouds(clouds[1:])</p> Signup and view all the answers

    In which implementation of the Fibonacci series does the function perform the slowest?

    <p>Using naive recursion</p> Signup and view all the answers

    What does the 'stopTime' function return when evaluated as stopTime(100)?

    <p>An infinite loop</p> Signup and view all the answers

    The condition checked in 'stopTime' for stopping is based on which variable?

    <p>Time</p> Signup and view all the answers

    What will be the outcome if a width smaller than or equal to 5 is passed to 'counting_clouds'?

    <p>The function will skip it and continue counting.</p> Signup and view all the answers

    In the context of list slicing, what does 'clouds[1:]' represent?

    <p>A new list without the first element</p> Signup and view all the answers

    What is the main purpose of the while loop inside 'stopTime' function?

    <p>To increment both time and running until they are equal</p> Signup and view all the answers

    What will the function return when called with lst = [1,2,3,4,5] in ForFunction3?

    <p>10</p> Signup and view all the answers

    What will be the output of pass_grade(50) when using the given function?

    <p>Error</p> Signup and view all the answers

    What will mylst[:4] return based on the provided mylst?

    <p>['COMP1126', [0,1,2,3]]</p> Signup and view all the answers

    What does mylst[-1] evaluate to?

    <p>[1,2,3]</p> Signup and view all the answers

    What will mylst[2:-1:2] return given the definition of mylst?

    <p>[0,2,4]</p> Signup and view all the answers

    What will be the output of the following expression: mylst?

    <p>['COMP1126', [0,1,2,3,4,5,6,7], [], [[1,2,3]]]</p> Signup and view all the answers

    In the context of getTime(X,Y), what should be returned if no path is found?

    <p>-999</p> Signup and view all the answers

    What does the expression number // 4 calculate?

    <p>Integer quotient of number divided by 4</p> Signup and view all the answers

    What will be the output of getTime('Gym','School') based on the given map?

    <p>4</p> Signup and view all the answers

    Which code should replace Code_A to correctly check for the path from X to Y?

    <p>path</p> Signup and view all the answers

    What is the purpose of Code_B in the getTime function?

    <p>to check if the paths are not equal</p> Signup and view all the answers

    What will happen if Code_C is replaced incorrectly in the getTime function?

    <p>Returns the entire map</p> Signup and view all the answers

    What will be the result of evaluating whatif(5,6,7) as defined in the provided function?

    <p>Error</p> Signup and view all the answers

    Which condition in the whatif function is invalid due to the use of the assignment operator?

    <p>all conditions are valid</p> Signup and view all the answers

    Which of the following options can appropriately serve as a base case in a recursive function?

    <p>Return a fixed value</p> Signup and view all the answers

    What is the desired outcome of completing the recursive function if no base case is provided?

    <p>An infinite recursion occurs</p> Signup and view all the answers

    Study Notes

    Question 22

    • The code fragment uses a for loop with a range of 20 to 9, stepping by -1
    • This means the loop will iterate 11 times (20, 19, 18, ... 10, 9)
    • The loop prints "exam" in each iteration
    • Therefore, the word "exam" will be printed 11 times

    Question 23

    • ["hello", "again"] is classified as a list data type
    • (2,3) is classified as a tuple data type
    • "hello" is classified as a string data type

    ###‌ Question 24

    • The function function(lst) is recursive, calling itself with a smaller sublist
    • The base case is lst == [] (an empty list), which returns 1
    • When lst is not empty, the function recursively calls itself with lst[1:] (removing the first element)
    • It then calculates a * function(lst[1:]) * math.sqrt(16)
    • After three recursive calls, the function returns 1 * 4 * 4 * 4 which equals 64
    • In this case, the math.sqrt(16) is irrelevant since it's constant

    Question 25

    • foo2(2) returns 1 + 2 = 3, which is used in foo1()
    • foo1() returns 3
    • whatisthis(2) returns 3 + 2 = 5
    • foo2(2) returns 1 + 2 = 3
    • Therefore, whatisthis(2) + foo2(2) evaluates to 5 + 3 = 8

    Question 26

    • f1(x) calls f2(x-1) unless x is 0, which returns True
    • f2(x) calls f1(x-1) unless x is 0, which returns False
    • f1(5) calls f2(4), which calls f1(3), which calls f2(2), which calls f1(1), which calls f2(0)
    • f2(0) returns False and propagates back through the function calls until f1(5) returns False

    Question 27

    • The purpose of the code is to check if a number x is a prime number (divisible by 1 and itself only)
    • CODE_A should be x % c (the remainder of x divided by c) to check for divisibility
    • CODE_B should be False to return False if x is divisible by any number other than 1 and x
    • CODE_C should be True to return True if x is not divisible by any number within the loop

    Question 4

    • The function should count the number of clouds in the clouds list that have a width greater than 5
    • The base case is an empty list (clouds == []), which returns 0
    • When clouds is not empty, it checks if the width of the first cloud is greater than 5
    • If yes, it should increment a counter and call itself recursively with the rest of the list (clouds[1:])
    • If no, it directly calls itself recursively with the rest of the list (clouds[1:])

    Question 5

    • The augmenting recursion version took the longest time to run for fibonacci series

    Question 6

    • The function has a recursive call within the while loop
    • The base case is time == endOftime-1, which returns -1
    • In the code above, endOftime is set to 999, and time starts at 0
    • The while loop keeps executing until time reaches 998 (endOftime - 1)
    • When time reaches 998, the base case is hit, and -1 is returned

    Question 7

    • The function iterates through the list elements with a for loop
    • The function returns the length of the list after the loop
    • The if conditional (el == 0) is never satisfied since the list is [1,2,3,4,5]
    • The loop does nothing, and len(lst) returns 5

    Question 8

    • The function returns w = 50
    • This is incorrect because the = operator is used for assignment, not comparison
    • The code will assign w to 50 and then return w, but it won't check if w equals 50.
    • Therefore, evaluating pass_grade(50) will return 50, not a boolean value

    Question 9

    • The function calculates the number of digits in a number
    • The count(number, total) function uses recursion to check each digit
    • The base case is when number is less than 10. In this case, the function returns total directly
    • Otherwise, it calls count recursively with number // 10 and total + 1, iterating through the digits
    • The digits(number) function initializes the total to 0 and calls count to begin the digit counting process

    Question 10

    • mylst[:4] returns a slice of the list from the beginning up to, but not including, index 4, which is ["COMP1126",[0,1,2,3,4,5,6,7], [], [[1,2,3]]]
    • mylst returns the entire list ["COMP1126",[0,1,2,3,4,5,6,7], [], [[1,2,3]]]
    • mylst[-1] returns the last element of the list, which is [[1,2,3]]
    • mylst[2:-1:2] returns a slice of the list starting at index 2 up to, but not including, the second-last element, taking every other element, which is [ [], [[1,2,3]] ]

    Question 11

    • CODE_A should be path[0] (first element of path) which represents the starting location X
    • CODE_B should be and to check if both starting and ending locations match the X and Y of the path
    • CODE_C should be path[2] (third element of path) to return the time between the locations

    Question 12

    • The function has a logical error
    • if (a = c) ... is not a valid comparison in Python, it assigns a to c
    • Since a is being assigned in the if condition, a will always be equal to c, leading to an infinite loop and the function never returning a value
    • The or condition also has the same error, which is why the function never returns a value

    Question 13

    • There is no code in the Question to provide a base case
    • The code uses base cases to prevent infinite recursion in the if statements (e.g. if x == 0 )

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    Test your knowledge of Python programming concepts with this quiz focused on loops, data types, and recursion. Determine how various data structures are used and how functions behave under certain conditions. Challenge yourself and see how well you understand these key concepts!

    More Like This

    Use Quizgecko on...
    Browser
    Browser