Python Midterm Exam
operator used to perform string concatenation in Python
+
if the variable num contains a positive integer, what are the only two possible results of the expression num % 2
0 and 1; two either divides the number evenly or there is one left over
if the current value of the variable num1 is 2 and the current value of num2 is 3, what value will be stored in num2 after the following line of code is executed num2 /= num1 * num2
0.5; 2*3 = 6 3/6 = 0.5
Which value of num will cause the print statement in the following code to be executed? if num < 15: if num + 7 >= 20: print('There you go!')
13
If a variable called address refers to the string '123 Main Street', what is the result of the expression address.find('3')
2
what output is produced by the following code: print(15 + 30)
45
Which of the following is a relational operator
<=
what output is produced by the following code: print('Jan', 'Feb', 'Mar', sep='-')
Jan-Feb-Mar
What output would be produced when the following code is executed? state = 'Mississippi' print(state.replace('is', 'was'))
Mwasswassippi
output
The data produced by an algorithm
Granualarity
The level at which an instruction is expressed.
if a variable called jedi refers to the string 'Yoda', what is the result of the expression jedi * 3
YodaYodaYoda
Which of the following expressions could be used to determine if a is not less than b
a>=b
print( 'apple', 'banana' )
apple banana print function separates its output by a space by default
what is a docstring
character string used as a comment in a program
if a variable called word refers to the string 'committee', what is the result of the expression word.strip('m')
committee strip only strips these letters if they're at the end or beginning
an if statement cannot have both an elif and an else clause
false
true or false: python 3 is backwards compatible to python 2
false; Python 3 broke backward compatibility, meaning some code that was valid in Python 2 code does not run on Python 3 platforms
true or false: Python variables that represent integers are NOT object reference variables
false; all python variables are object references
pass statement is usless
false; it is very useful
true or false: python was named after a snake
false; named after a monty python comedy group
true or false: the origin point (0, 0) of the turtle coordinate system is in the upper left corner of the graphic screen
false; origin point is in the center of the screen
The arithmetic operators have a lower precedence than the relational operators
false; other way around
A Python function must have at least one parameter
false; some functions have no parameters
The less than operator (<) should not be used to compare floating point values
false; there is no danger in doing this
true or false: the stroke color of a filled turtle shape must be the same as the fill color
false; they can be different
true or false: a turtle draws a filled circle using the fill_circle command
false; this command doesn't exist
true or false: in dynamically typed languages, variables are declared to store a specific type of data
false; variables do not have to be declared in dynamically typed languages
true or false: the effect of the setheading depends on the current heading of the turtle
false; when setting the heading explicitly, the current heading doesn't matter
true or false: running a program that contains errors will cause thonny to terminate
false; your program will terminate but thonny will not
of the options given, what values of the variables height, size, and width would cause the following code to set the variable weight to 100? if height < size: weight = 50 if width < 20: print('short') else: if width > 100: weight = 100 println('tall')
height = 15 size = 10 width = 110
thonny is best described as
integrated development environment
if a variable called user_id refers to the string 'AEinstein12', what is the result of the expression user_id.isalpha()
is alpha only works if all the characters in the string are alphabetic
What output is printed by the following code if it is executed when appetizer is 3 and entree is 12? if appetizer > 1: if entree < 7: print('ketchup') else: print('mustard') else: if appetizer < 0: print('mayonnaise') else: print(relish)
mustard
text that requests user input is called
prompt
if a variable called greeting refers to the string 'hello', what does the expression greeting.upper() accomplish
returns a new string containing the characters "HELLO"
syntax coloring
showing certain elements of program code in different colors
Which of the following statements is true?
statements in the body of an if statement must be indented
Which of the following is NOT true regarding the return statement?
the last statement in a function must be the return statement this is often the case, but not a requirement
What will happen if the variable total has the value 5 when the following code is executed? if total > 8: print('collywobbles')
the word collywobbles is not printed and processing continues
A character string can be used as the condition of an if statement
true
A function parameter ceases to exist when the function returns to the caller
true
In Python, the relational operators can be used to put character strings in alphabetical order
true
The relational operators all return boolean results
true
The result of a relational operator can be assigned to a variable
true
a function can return a boolean result
true
pass statement can be used as the body of an if statement, for loop, function definition, or class definition
true
true or false: full documentation of the python standard library can be found online
true
true or false: python is a general purpose programming language, appropriate for solving problems in many areas of computing
true
true or false: setting the turtle's speed to 0 turns off the animation of the turtle movement completely
true
true or false: thonny displays code using syntax highlighting
true
variables declared inside a function cannot be accessed from outside that function
true
The pass statement has no effect on the program state
true, doesn't compute anything
Which way would the turtle be facing after executing the command turtle.setheading(135)
up and left (northwest)
the turtle circle command can be used to draw a regular polygon
true; the optional parameter steps
true or false: you can store a value in a variable in the python shell
true; the state of variables is maintained from one command to the next
true or false: built in functions of the python standard library can be used without being imported
true; they are essentially part of the python language
principles embraced by python programming language
1. beautiful is better than ugly 2. simple is better than complex 3. explicit is better than implicit 4. complex is better than complicated 5. readability counts
Python embodies elements of which programming paradigm
1. procedural programming 2. object-oriented programming 3. functional programming
what is the result of the expression math.floor(12.843)
12 returns nearest integer less than x
result of expression max(6, 17)
17
if a variable called pioneer refers to the string 'Grace Murray Hopper', what is the result of the expression len(pioneer)
19 len refers to characters in a string
Which of the following is NOT a valid Python identifier first_place place1 1stplace FIRST_PLACE
1stplace an identifier cannot begin with a digit
what is the result of the expression math.pow(3, 3)
27; 3^3
15 % 6
3; because 6 goes into 15 two times with 3 left over
what is the result of the expression 7 // 2
3; the fractional part is truncated
what is the result of the expression 43 % 5
3; there is 3 leftover after the division is performed
if the current value of size is 2, what value will be stored in size after the following line of code is executed size *= 3
6 2*3=6
if the current value of the variable num is 4, what value will be stored in num after the following line of code is executed num += 2
6 4+2=6
What value is assigned to the variable num by the following statement if the current value of num is 4 num = num * 2
8
if the current value of the variable count is 2, what value will be stored in count after the following line of code is executed count += count * 3
8 2*3 =6 + 2 = 8
flowchart
A graphical representation of the logic of an algorithm
pseudocode
A language that is independent of any particular programming language
Algorithm
A step-by-step procedure for solving a problem
who developed python programming language?
Guido van Rossum
what output is produced by the following code: print('Ready', end=' ') print('Set', end='') print('Go')
Ready SetGo the end argument is a space in the first call but empty in second call
a print statement is
a call to a function
what is the python standard library
a collection of programming components that can be used in any python program
what issue must be addressed when printing a long character string
a regular string literal cannot span across multiple lines
what is the python shell
a window in which python statements and expressions are executed immediately
if a variable called business refers to the string 'Time Warner', what is the result of the expression business[6]
a; string indexes start at 0
which of the following does the input function NOT do when it is called
convert the user input to an integer but it does: return the value typed by the user wait for the user to type information and press enter print a message if provided
input
data needed by an algorithm to accomplish its task
what line of code is equivalent to the following depth += 50 * offset
depth = depth + (50 * offset)
Which way would the turtle be facing after executing the following code turtle.setheading(270) turtle.right(20) turtle.left(65)
down and right (southeast)
true or false: using components from the python standard library is a rare occurrence
false; any useful python program will rely on the standard library
true or false: the assignment statement is also an expression
false; assignment operator does not return a value
A Python block comment begins with a double slash (//)
false; begins with a #
The input function will produce an error if the value read is not numeric
false; int and float produce an error
A Python comment cannot appear on a line that contains an executable statement
false; it can be put after a line of code to explain that code
true or false: the goto command moves the turtle without drawing a line, even if the turtle's pen is down
false; it will draw a line if the turtle's pen is lowered
true or false: if both operands to the remainder operator (%) are positive, a divisor of n will produce a result in the range 1 to n
false; it will produce a result in the range 0 to n-1
true or false: a python program must be compiled into an executable form before it can be run
false; python programs are interpreted, executing the code line by line
true or false: a python program must be enclosed in curly brackets {} to be executed
false; python statements do not have to be enclosed by anything. A single statement is a complete program
true or false: You must use a print statement to display the result of an expression in the Python shell
false; the expression is evaluated and the result is displayed immediately
true or false: the expression x ^ y raises the value x to the power y
false; the python exponentiation operator is **
true or false: all mathematical functions in Python are part of the math module
false; there are several math functions that are built in
true or false: value assigned to a variable must be numeric
false; value can be one of several non-numeric types, such as character strings
suppose the integer variable hours contains a value that represents a number of hours. which of the following expressions will compute how many 24-hour periods are represented by that value, without worrying about any leftover hour
hours // 24 integer division throws the amount leftover away
natural language
language that humans use to communicate, such as French or English
the assignment operator has a _______ precedence than all the arithmetic operators
lower
what output is produced by the following code: print('oak', 'elm', 'pine', end='!', sep='#')
oak#elm#pine!
if a variable called son refers to the string 'Justin', what is the result of the expression son[-3]
t
what does case sensitive mean
the difference between uppercase and lowercase letters matters
print('Total: ' + 100 + 20)
this line would produce an error; you can't concatenate a string and integer in python
Which of the following identifiers follows the convention for naming Python variables: totalValue Total_Value total_value TOTAL_VALUE
total_value should use undercase letters and have an underscore to separate words
The input, int, and float functions are all built-in functions
true
a comment in a Python program is ignored by the interpreter
true
python comments begin with a hash mark (#) and extend to the end of the line
true
the input function always returns the read data as a character string
true
true or false: the math.pi constant represents pi to 15 decimal places
true
true or false: the pencolor and size determines the color and width of lines drawn
true
true or false: the position of the turtle depends on the current heading of the turtle
true
true or false: the python shell has an integrated help system
true
true or false: the python turtle graphics module is based on the programming language Logo developed in the 1960s
true
true or false: thonny has a package manager that lets you install and update external python packages
true
true or false: python variables are created using an assignment statement
true; a variable is created the first time it is assigned to a value
true or false: the math module contains a constant that represents the value pi to several digits
true; also has a constant for e
true or false: if either or both operands to the division operator (//) are floating point values, then the result will be a floating point value
true; and if both are integers, it performs integer division
true or false: the value assigned to a variable could be a floating point number
true; both integers and floating point numbers can be stored in variables floating point number ex: 4.2
true or false: if a filled shape drawn by a turtle is not fully enclosed, Python fills the shape as if the starting point is connected to the end point.
true; but the stroke will not be shown for the connecting line
true or false: the python shell is great for exploring python langauge features
true; it's a quick way to try a newly learned statement, operator or function
you cannot change the contents of Python character string once it has been created.
true; python strings are immutable
true or false: the value of a variable can change throughout the program
true; that's why it's called a variable, its value can vary
true or false: output of a program run in thonny appears in the shell window
true; the command to run the program along with any text output produced by the program will appear in the shell window
if the turtle is currently facing up (north), which way would it be facing after executing the command turtle.right(45)
up and right (northeast)