Quiz 2 Study Guide

The quiz will be administered on Gradescope. It will be available at 9pm on Thursday, October 8 and must be completed by 10am Friday, October 9. You will have 35 minutes to complete the quiz once you start working on it. You will be asked to affirm the same integrity policy as you did last time.

Python concepts and functions

You are responsible for all material needed for Lab 1, 2, and 3, which includes for loops, if statements, boolean operators, and relational operators.

In addition, this quiz will cover while loops and functions. We will not be asking you to write your own while loops and functions on this quiz; however, you should be able to read and understand programs with these in them.

Study guides are not graded. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on Slack, or with meetings with faculty and staff. We do not provide full solutions to the study guides.

In addition to all concepts from Quiz 1, you should understand the following:

  • the Boolean data type

  • if, if/else, if/elif/else

  • the relational Boolean operators <, <=, ==, >, >=, !=

  • the logical Boolean operators and, or, and not

  • nested statements (e.g., nested if statements, if/else in a loop, etc.)

  • definite for loop vs indefinite while loop

  • function definitions and function calls

  • function parameters and function arguments

Sample practice problems

  1. Given these assignments:

    x = 5
    y = 13
    S = "we love computer science"
    Done = False

    Show the value and type of each expression below:

    x < 10 and y < 10
    x < 10 or y < 10
    x < 10 and x > 0
    x > 10 or x < 0
    (5/x) > 7.0
    len(S) >= 10
    S[0] < S[1]
    not Done
  2. The code fragment below asks the user for two input values. Provide 5 different sets of input values for number_one and number_two such that each branch of the if statement is followed.

    number_one = int(input("Enter the first number: "))
    number_two = int(input("Enter the second number: "))
    if number_one < 10:
        print("Option 1")
    elif number_two < 10:
        print("Option 2")
    elif number_one < number_two:
        print("Option 3")
    elif number_two > 20:
        print("Option 4")
    else:
        print("Option 5")
  3. Show the output from the following code fragments:

    x = 0
    while x < 6:
          print(x, x**2)
          x = x + 1
    print("done!")
    text = "strings!"
    i = 0
    found = False
    while not found:
        if text[i] in 'aeiou':
            found = True
        else:
            print(text[i])
            i = i + 1
    print("found at position", i)
  4. Consider the following program. Note that calling int on a float argument always rounds down to the nearest integer. For example, int(7.9) == 7.

    x = 3
    i = 0
    print("%d  %d" % (0, x))
    while x != 1:
        if x % 2 == 0:
            x = int(x / 2)
        else:
            x = int(( 3*x + 1 ) / 2)
        print("%d  %d" % (i, x))
        i = i + 1
    1. Trace the program and show its output.

    2. Create a table having columns for i, and x and show how the values change as the loop executes.

  5. Given the program below:

    def find_min(a, b):
        min_value = a
        if b < a:
            min_value = b
        return min_value
    
    def main():
        number_one = int(input("Enter the first number: "))
        number_two = int(input("Enter the second number: "))
        smaller = find_min(number_one, number_two)
        print("The smaller value is %d" % (smaller))
    
    main()
    1. What are the function definitions?

    2. What are the function calls for functions defined within this program?

    3. Which functions are called that aren’t defined within this program?

    4. Give an example of a parameter and give an example of an argument.

    5. What would this program print if the user entered the following two numbers:

      • 4 and 5

      • 5 and 5

      • 9 and 2