Quiz 2 Study Guide

Quiz Study Guides are provided as a courtesy. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on piazza, or during meetings with faculty and staff. We do not provide full solutions to the quiz guides.

You are responsible for all material covered through the end of Week 3.

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

Terms

  • data types: integer, float, string, Boolean

  • branching and looping

  • the accumulator design pattern

  • string indexing

Python concepts and functions

  • basic for loops

  • range()

  • basic arithmetic expressions using +, -, *, /, //, **, %

  • conversion between the int, float, and str types

  • the empty string ""

  • string concatenation with +, string repetition with *

  • string indexing with []

  • for loops over strings

  • len()

  • tracing a program (show the output)

  • 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.)

  • string/print formatting

  • accumulator pattern using string, float, or integer variables

Sample practice problems

  1. Show the output from the following code fragments:

    for x in range(6):
          print(x, x**2)
    print("done!")
    text = "hello!"
    for i in range(len(text)):
          print("-" + text[i] + "-")
  2. 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
  3. Consider the following program

    x = 3
    print("%d  %d" % (0, x))
    for i in range(5):
        if x % 2 == 0:
            x = x // 2
        else:
            x = ( 3*x + 1 ) // 2
        print("%d  %d" % (i, x))
    .. Trace the program and show its output.
    .. Create a table having columns for `i`, and `x` and show
    how the values change as the loop executes.
  4. Write a program that asks the user how many grades they want to average, prompts the user for those grades, and prints the average.

    Enter number of grades to average: 5
    
    Enter grade: 96
    Enter grade: 86
    Enter grade: 76
    Enter grade: 66
    Enter grade: 56
    
    Average is: 76.0
  5. Write a program that asks the user for a word, and then uses an accumulator to print out the word with all lowercase "e"s changed to uppercase "E"s.

    Enter a word: repetitive
    
    rEpEtitivE