Computer Science Midterm
Which of the following properly expresses the precedence of operators (using parentheses) in the following expression: 5*3 > 10 and 4+6==11
A. ((5*3) > 10) and ((4+6) == 11)
What is the value of the following expression: 16 - 2 * 5 // 3 + 1
A. 14
Consider the following Python code. Note that line numbers are included on the left. def pow(b, p): y = b ** p return y def square(x): a = pow(x, 2) return a n = 5 result = square(n) print(result) What does this function print?
A. 25
What value is printed when the following statement executes? print(18 / 4)
A. 4.5
What is the value of the following expression: 2 ** 2 ** 3 * 3
A. 768
What is printed when the following statements execute? n = input("Please enter your age: ") # user types in 18 print ( type(n) )
A. <class 'str'>
In Python a module is:
A. A file containing Python definitions and statements intended for use in other Python programs.
What is a Boolean function?
A. A function that returns True or False.
What is a function in Python?
A. A named sequence of statements.
The difference between programming and debugging is:
A. A programming is the process of writing and gradually debugging a program until it does what you want.
What is the difference between a tab ('\t') and a sequence of spaces?
A. A tab will line up items in a second column, regardless of how many characters were in the first column, while spaces will not.
What is a local variable?
A. A temporary variable that is only used inside a function.
Which of the following is a run-time error?
A. Attempting to divide by 0.
If you have a pixel whose RGB value is (50, 0, 0), what color will this pixel appear to be?
A. Dark red
To find out information on the standard modules available with Python you should:
A. Go to the Python Documentation site.
What happens if you give range only one argument? For example: range(4)
A. It will generate a sequence starting at 0, with every number included up to but not including the argument it was passed.
Will the following code cause an error? x = -10 if x < 0: print("The negative number ", x, " is not valid here.") else: if x > 0: print(x, " is a positive number") else: print(x," is 0")
A. No
What will the following function return? def addEm(x, y, z): print(x + y + z)
A. None
In the command range(3, 10, 2), what does the second argument (10) specify?
A. Range should generate a sequence that stops before 10 (including 9).
In the random walk program in this section, what does the isInScreen function do?
A. Returns True if the turtle is still on the screen and False if the turtle is no longer on the screen.
Source code is another name for
A. The instructions in a program, stored in a file.
Who or what typically finds semantic errors?
A. The programmer.
Consider the following code: def square(x): runningtotal = 0 for counter in range(x): runningtotal = runningtotal + x return runningtotal What happens if you put the initialization of runningtotal (the line runningtotal = 0) inside the for loop as the first instruction in the loop?
A. The square function will return x instead of x * x
How does python know what statements are contained in the loop body?
A. They are indented to the same degree from the loop header.
Debugging is:
A. Tracking down programming errors and correcting them.
True or False: A Turtle object can have any name that follows the naming rules from Chapter 2.
A. True
True or False: You can rewrite any for-loop as a while-loop.
A. True
True or false: A function can be called several times by placing a function call in the body of a loop.
A. True
Which of the following is a Boolean expression? Select all that apply.
A. True B. 3 == 4 D. 3 + 4 == 7
Is the following statement legal in a Python function (assuming x, y and z are defined to be numbers)?
A. Yes
Which type of loop can be used to perform the following iteration: You choose a positive integer at random and then print the numbers from 1 up to and including the selected integer.
A. a for-loop or a while-loop.
A program is:
A. a sequence of instructions that specifies how to perform a computation.
Which of the following is a valid function header (first line of a function definition)?
A. def drawCircle(t):
Which statement allows you to use the math module in your program?
A. import math
Which of the following is the correct way to reference the value pi within the math module. Assume you have already imported the math module.
A. math.pi
The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate? n = 10 answer = 1 while n > 0: answer = answer + n n = n + 1 print(answer)
A. n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive
The correct code to generate a random number between 1 and 100 (inclusive) is:
A. prob = random.randrange(1, 101)
The activecode interpreter allows you to...
A. save programs and reload saved programs. B. type in Python source code. C. execute Python code right in the text itself within the web browser.
Pick the best replacements for 1 and 2 in the following sentence: When comparing compilers and interpreters, a compiler is like 1 while an interpreter is like 2.
B. 1 = translating an entire book, 2 = translating a line at a time
Using the previous ActiveCode example, select the answer that is closest to the RGB values of the pixel at row 100, column 30? The values may be off by one or two due to differences in browsers.
B. 183 179 170
In the following code, what is the value of number the second time Python executes the loop? for number in [5, 4, 3, 2, 1, 0]: print("I have", number, "cookies. I'm going to eat one.")
B. 4
What value is printed when the following statement executes? print(int(53.785))
B. 53
An algorithm is
B. A step by step list of instructions that if followed exactly will solve the problem under consideration.
One reason that lotteries don't use computers to generate random numbers is:
B. Because computers don't really generate random numbers, they generate pseudo-random numbers.
Who or what typically finds run-time errors?
B. Compiler and interpreter.
Who or what typically finds syntax errors?
B. Compiler and interpreter.
The print function:
B. Displays a value on the screen.
What does the following code print (choose from output a, b, c or nothing)?
B. FALSE
True / False: All standard Python modules will work in activecode.
B. False
True or False: Reading a program is like reading other kinds of text.
B. False
True or False: You can only have one active turtle at a time. If you create a second one, you will no longer be able to access or use the first.
B. False
True or False: the following is a legal variable name in Python: A_good_grade_is_A+
B. False
What are comments for?
B. For the people who are reading your code to know, it natural language, what the program is doing.
Which of the following is a syntax error?
B. Forgetting a colon at the end of a statement where one is required.
Consider the following code: import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) alex.left(90) alex.forward(75)
B. It defines the module turtle which will allow you to create a Turtle object and draw with it.
What does the following code print? x = -10 if x < 0: print("The negative number ", x, " is not valid here.") print("This is always printed") a. This is always printed b. The negative number -10 is not valid here This is always printed c. The negative number -10 is not valid here
B. Output b
What is a variable's scope?
B. The range of statements in the code where a variable can be accessed.
What is one main purpose of a function?
B. To help the programmer organize programs into chunks that match how they think about the solution to the problem.
How can you determine the type of a variable?
B. Use the type function.
Will the following code cause an error? x = -10 if x < 0: print("The negative number ", x, " is not valid here.") else: print(x, " is a positive number") else: print("This is always printed")
B. Yes
Can you use the same name for a local variable as a global variable?
B. Yes, but it is considered bad form.
What is wrong with the following function definition: def addEm(x, y, z): return x + y + z print('the answer is', x + y + z)
B. You should not have any statements in a function after the return statement. Once the function gets to the return statement it will immediately stop executing the function.
The differences between natural and formal languages include:
B. ambiguity, redundancy, and literalness
Codelens allows you to...
B. control the step by step execution of a program. D. execute the Python code that is in codelens.
What is the name of the following function? def drawSquare(t, sz): for i in range(4): t.forward(sz) t.left(90)
B. drawSquare
Which range function call will produce the sequence 20, 15, 10, 5?
B. range(20, 3, -5)
Which of the following explains why wait_time_int = int(wait_time_int) is an error?
B. wait_time_int does not have a value so it cannot be used on the right hand side.
What is printed when the following statements execute? x = 12 x = x - 1 print(x)
C. 11
What could the second parameter (12) in range(2, 12, 4) be replaced with and generate exactly the same sequence?
C. 11, 13, or 14
What value is printed when the following statement executes? print(18 // 4)
C. 4
In the following code, how many lines does this code print? for number in [5, 4, 3, 2, 1, 0]: print("I have", number, "cookies. I'm going to eat one.")
C. 6
What is printed by this code? n = 1 x = 2 while n < 5: n = n + 1 x = x + 1 n = n + 2 x = x + n print(n, x)
C. 7 15
Consider the following code: for aColor in ["yellow", "red", "green", "blue"]: alex.forward(50) alex.left(90)
C. Draw one side of a square
Which direction does the Turtle face when it is created?
C. East
Which of the follow is a semantic error?
C. Forgetting to divide by 100 when printing a percentage amount.
What is the difference between a high-level programming language and a low-level programming language?
C. It is high-level if the program must be processed before it can run, and low-level if the computer can execute it without additional processing.
Consider the code that prints the 3n+1 sequence in ActiveCode box 6. Will the while loop in this code always terminate for any positive integer value of n?
C. No one knows.
How many statements can appear in each block (the if and the else) in a conditional statement?
C. One or more.
Which of the following best describes what is wrong with the previous example?
C. Python is doing string concatenation, not integer addition.
Why do we type turtle.Turtle() to get a new Turtle object?
C. The first "turtle" (before the period) tells Python that we are referring to the turtle module, which is where the object "Turtle" is found.
What is the most important skill for a computer scientist?
C. To be able to solve problems.
What will the following code print if x = 3, y = 5, and z = 2? if x < y and x < z: print("a") elif y < x and y < z: print("b") else: print("c")
C. c
What command correctly generates the sequence 2, 5, 8?
C. range(2, 10, 3)
What are the parameters of the following function? def drawSquare(t, sz): for i in range(4): t.forward(sz) t.left(90)
C. t, sz
What is a correct Python expression for checking to see if a number stored in a variable x is between 0 and 5?
C. x > 0 and x < 5
What is printed when the following statements execute? day = "Thursday" day = 32.5 day = 19 print (day)
D. 19
What value is printed when the following statement executes? print(18 % 4)
D. 2
What is the data type of 'this what kind of data'?
D. String.
After the following statements, what are the values of x and y? x = 15 y = x x = 22
D. x is 22 and y is 15
Consider the following Python code. Note that line numbers are included on the left. def pow(b, p): y = b ** p return y def square(x): a = pow(x, 2) return a n = 5 result = square(n) print(result) Which of the following best reflects the order in which these lines of code are processed in Python?
E. 1, 5, 9, 10, 5, 6, 1, 2, 3, 6, 7, 10, 11
Considering the function below, which of the following statements correctly invokes, or calls, this function (i.e., causes it to run)? Assume we already have a turtle named alex. def drawSquare(t, sz): for i in range(4): t.forward(sz) t.left(90)
E. drawSquare(alex, 10)
What is printed when the following statements execute? x = 12 x = x - 3 x = x + 5 x = x + 1 print(x)
c. 15
Which module would you most likely use if you were writing a function to simulate rolling dice?
the random module