Python Final
What is the output of this Python code? >>> spam = 'Hello world!' >>> spam = spam.upper() >>> spam
'HELLO WORLD!'
if x < 2 : print 'Less than 2' elif x < 20 : print 'Less than 20' elif x < 10 : print 'Less than 10' else : print 'Invalid number' Refer to the code shown above, for all values of x, which of the following line in the code will never print?
'Less than 2' 'Less than 20' 'Less than 10' ?
What does the following expression evaluate to? >>> 'Remember, remember, the fifth of November. ' .split()
'Remember', 'remember', 'the', 'fifth', 'of', 'November'.
Which of the following is a string?
('8.7')
What is the difference between the equal to operator (==) and the assignment operator (=)?
(==) is used for comparison (=) is used for assignments ?
If you were writing your source code in the Python programming language, what file extension would you use to save your file?
.py
What will the following program print? def mult_inv(num): return 1/num result=mult_inv(3) print(result)
0.333333333
Which of the following is an output of a random function?
0.596946964
What is the output of this Python for loop code? total = 0 for num in range(5) : total = total + num print total
10, the code sums up all the numbers between 0 and 4
What is the outcome of this Python code? >>> general = '100' >>> account = '150' >>> print general + account
100150 ?
What is the value of var? def addTwo(x) : return x + 2 def multiply(a, b) : return a * b var = multiply(addTwo(2), 3)
12 ?
if x == y: print 'x and y are equal' else: if x < y: print 'x is less that y' else: print 'x is greater that y' Which of the following flowcharts is a CORRECT representation of the Python code shown above?
1st ?
Which of the following shapes is used to represent branching points (decisions) on a flowchart?
2 or 3 oval or triangle ?
What is the output of the expression shown below? >>> 23 // 7
3
Which of the following is the correct output of the Python function shown below? >>> float(32)
32.0
x = 34 x = x + 2 print x
36
Which expression evaluates to 2?
5 % 3
Which of the following expressions will generate a False Boolean value?
>>> 'hello' == 'Hello'
Which of the following variable type is an example of a float?
>>> pi = 3.1415926535897931
What slice of string will this Python code print? >>> name = 'Johnny Jacobs' >>> print name[0:5]
?
Which statement is true regarding loops?
A for loop is based on iterating through a sequence.
What will the following Python program print out? def fred(): print "Zap" def jane(): print "ABC" jane() fred() jane()
ABC Zap ABC
A ____________ expression is an expression that is neither true or false.
Boolean
How would you execute a certain statement ONLY if a loop exits normally?
By placing this statement inside an else clause after the loop block
The Python interpreter is written in a high-level language called _________.
C
What keys do you press if your program is stuck in an infinite loop?
Ctrl+C
Which of the following is a comparison operator?
Double Equal to (==)
Boolean expressions are the same as logical operators.
False
If more than one condition of an if statement is true, all true branches will execute.
False
In Python programing, blank lines are executed but they are not effected by indentation.
False
Python treats all numbers the same, regardless of numerical value.
False
Source code is written in rich text.
False
The code that is contained inside a function is called the function's legs.
False
Updating a variable by adding 1 is called decrement.
False
What Boolean outcome does this expression evaluate to? not ( (5 > 4) or (3 == 5) )
False
spam = 0 spam = 0 if spam < 5 : while spam < 5 : print('Hello, world.') print('Hello, world.') spam = spam + 1 spam = spam + 1 Refer to the if statement and the while loop above: Which of the following is correct?
For the if statement, the output is "Hello, world.", but for the while statement, the output is "Hello, world." repeated five times!
Which of the following is NOT a benefit of functions?
Functions eliminate crashes in written code
What is the implication of the highest portion of the script shown above? percentage = (minute * 100 / 60 # percentage of an hour
It is a comment; it has no effect on the program.
What does the additional control 'continue' do?
It shortcuts the loop.
What is the function of the len function?
It states how many items are in the argument of a function.
Which of the following is the main function of the computer?
It translates the high-level source code of an entire program into machine language and puts it into a file for later execution.
first_name = "Jeff" first_Name = "Sara" print(first_name) What is the output of this Python code?
Jeff
x = 104 Where in the computer is a variable such as "X" stored after the following Python line finishes?
Memory
Will the print statement be reached in this code? for number in range(1, 100) : if number % 10 == 0 : break else: print('Let\'s print something out!')
No
What is the return value of a call to a function that does not have a return statement?
None
Which of the following is a correct representation of sequential executions?
Performs statements one after another in the order they are encountered in the script.
What is the function of the secondary memory in a computer?
Store information for the long term - even beyond a power cycle
Which of the following Python errors is an indication of a violation of Python's grammatical rules?
Syntax errors
What is the output of this program? num = 16 if num % 3 == 0: print(num, "is divisible by 3") print("The end")
The end
What does the void keyword mean when it is specified before a function definition?
The function does not return a value
Which of the following is a correct description of the operation of the while loop function?
The while loop statement checks at the start of each iteration, if the condition is True, then the clause is executed, and afterward, skipped the clause if the condition is found to be False.
>>> print 'Hello world!' File "<stdin>", line 1 primt 'Hello world!' SyntaxError: invalid syntax >>> What is wrong with the code displayed above?
There is a grammatical error. 'Print' is spelled wrong. It should be the following: >>> print ("Hello, world!")
_________________ convert programs to machine language for actual execution by the CPU.
Translators
Comments on a line are ignored with respect to indentation.
True
In programming, a crash is when your program stops early or freezes because something unexpected happened.
True
Python programming requires strings to be enclosed in quotation marks.
True
Python's reversed words cannot be used as a variable.
True
The code in a while clause will be executed as long as the while statement's condition is True.
True
The if-else statements will always execute one of the code blocks depending on the result of the condition test.
True
What is the output of this Python code? >>> True != False
True
What is the output of this Python code? >>> not not not not True
True
Which of the following statements is INCORRECT?
Variable names can start with a number.
Why is there no output for this program? def goodbye ( ) : print("Bye")
We never called the goodbye function.
What is the output of this Python code? >>> fruit = 'banana' >>> letter = fruit[1] >>> print letter
a
Which variable name is valid in Python?
account_info
A loop in which the terminating condition is never satisfied or for which there is no terminating condition is called _____________ loop.
an infinite
___________________ is a value or variable that is being passed into a function as input to the function.
argument
Multiple statements grouped together are called a ________.
block
Handling an exception with a try statement is called ________________ an exception.
catching
def mystery(x) : if (x % 2 == 0) : return("yes") else: return("no") What can the function shown above be used for?
check whether a number is even or odd
All if conditional statement usually end with a ____________.
colon(:)
To join two operands together, end to end is known as _________________.
concatenation
Which of the following is NOT a valid Python variable? (Choose all that apply).
current balance $currentBalance current-balance
Which keyword doe Python use to define a function?
def
What is the outcome of this expression? >>> fruit = 'banana' >>> fruit[3:3]
empty string
What operator do you use to assign a value to a variable?
equals (=)
What type of value is a number with a decimal point?
float
The order in which statements are executed within a function is called the ___________________________.
flow of execution
What is the proper code for creating a loop that will execute 9 times starting at the number 6?
for i in range(6, 15): ...
Which of the following statement loops through a set of things?
for statement
A ________________ is a named sequence of statements that performs a computation.
function
The first line of the function definition is called the _________________.
header
While loops are called ___________________ loops because they keep going until a logical condition becomes False.
indefinite
An error that occurs due to irregular order of the statements or how the statements relates to one another is known as __________________
logic error
The Central Processing Unit of a computer system understands __________________
machine language
>>> name = input ('What is your last name?\n') What is the implication of the highlighted portion of the syntax shown above?
newline
Which is-else statement will not produce this output? is divisible by 3
num = 16 if num % 3 == 0: print("is divisible by 3") else: print("is not divisible by 3")
In this function definition, the variable 'name' is a _______________.
parameter
Which of the following is equivalent of telling Python to speak?
How would you write the Hello, world! program in Python?
print ("Hello, world!")
Which of these will NOT output: I'm learning a lot!
print('I'm learning a lot!')
A sequence of Python statements that have been crafted to perform an action or solve a problem is known as a ____________
program
Which of the following is the proper syntax to exit Python?
quit()
Python instructions that are written into a file using a text editor is called a _________
script
When the evaluation of a logical expression stops because the overall value is already known, it is called ___________________ the evaluation.
short-circuiting
Which of the following is NOT a Python built-in function? (Choose all that apply)
size(): call():
A _________________ is a unit of code that the Python interpreter can execute.
statement
What type is the variable 'a' in the statement a ='12'
str
What will be printed by the following loop? for i in range(10) : print(i)
the numbers 0 to 9
A ______________ is a name that refers to a value.
variable
