Python Final
Is this sentence true or false? "Once your program has been compiled to byte code, the next time you run your program, Python will have to recompile again even if there is nothing changes"
False
Which one of the operations below is the exponentiation operator?
**
a = input("Enter the value of a: ") b = input("Enter the value of b: ") print(a + b) When this code segment is run the user enters 1 at the first prompt and 5 at the second prompt. The output displayed is:
15
sum([1, 2, 3, 4, 5])
15
A loop inside another loop is called:
A nested loop
What is true regarding subclasses?
A subclass may inherit methods and instance variables from its superclass, and may also implement its own methods and declare its own instance variables
Parameter variables should not be changed within the body of a function because
It is confusing because it mixes the concept of a parameter with that of a variable
Sequences of instructions?
The program flows from one step to the next in strict sequence
What must a subclass do to modify a private superclass instance variable?
The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable
What is the purpose of a method?
To access the instance variables of the object on which it acts
Definition of dictionary
a dictionary keeps associations between keys and values
What is true regarding subclasses?
a subclass inherits methods and instance variables from its superclass
What is supplied to a function when it is called?
arguments
What statement is used to implement a decision?
if
Which operator tests to see if a key exists in a dict?
in
How do you remove item in dictionary?
pop method
Which of the following statements causes Python to report an error?
x = 17 + "18.4"
Which of the following statements sets x so that it refers to no object at all?
x = None
What is displayed by the following code segment? print("\"Hello World!\"")
"Hello World!"
Which operator below will perform a floor division?
//
What is the value of x after the following code segment? x = len("Hello World!")
12
Consider the following function call round(3.14159, 3) what is the return value ?
3.142
What must be included in the subclass constructor as the first statement?
A call to the superclass constructor
What are the two parts of an if statement?
A condition and a body
Select all options that apply to Python
Byte code is the lower-level form of your program after Python compiles it. Python automatically stores byte code in files with a .pyc extension. Source code is the statements you write for your program — it consists of text in text files that normally end with a .py extension. The Python interpreter is a program that runs the Python programs you write.
Who invented Structured programming?
Edsger Dijkstra
Who invented the first high level language?
Grace Hopper
Loops?
In this construct the program steps are repeated continuously until some test condition is reached, at which point control then flows past the loop into the next piece of program logic.
Python disadvantage:
Performance
Python's advantages:
Python code is less to type, less to debug and less to maintain compared to other languages Python is Object Oriented and Functional Python is portable
Which of the following is NOT true about objects?
The == and != operators test whether two variables are aliases
Branches?
The program reaches a decision point and if the result of the test is true then the program performs the instructions in Path 1, and if false it performs the actions in Path 2
What is the purpose of using an inheritance hierarchy?
To share common code among the classes
Is this sentence true or false? "In Python, there is no need to precompile and link before execution may begin."
True
What is true regarding inheritance?
You can always use a subclass object in place of a superclass object
Which of the following statements is not legal?
["Hello", "World"].lower()
What is the name of the instance variable in the following code segment? class Fruit : def get_color(self) : return self._color
_color
A class name inside parentheses in the class header indicated a
class inherits from a superclass
What is it called when you put two strings together in Python?
concatenation
Which statement correctly saves the price in the variable cost? userInput = input("Please enter the price:")
cost = float(userInput)
x = Fruit() y = Fruit("Banana", "Yellow") Which constructor header will construct both objects successfully?
def __init__(self, name="", color="") :
Which of the following syntax statements correctly define a function?
def functionName(parameterName1, parameterName2,. . .) :
How to search a dict using a key value, providing a default value in case it is not found
food = favorite_foods.get("Joe", "seafood")
Which of the following for loops is illegal?
for i in range(0) :
What is the name of the method in the following code segment? class Fruit : def get_color(self) : return self._color
get_color
Which statement checks to see if the key 'Apple' is in the dict 'fruit'?
if "Apple" in fruit:
A set is a subset of another set if..
if every element of the first set is also an element of the 2nd set
If an integer value can be changed to a string, what functions convert strings to numbers?
int, float
A ___________________________ is a collection of programming instructions that can be applied to an object?
method
To override a superclass method in a subclass, the subclass method...
must use the same method name and the same parameters
Given the definition of a list: names = [] which statement correctly adds "Harry" to the list?
names.append("Harry")
How many return statements can be included in a function?
none, one, or infinite
What is printed by the following code snippet: street = " Main Street" address = 123 + street print(address)
nothing is printed, this code snippet causes an error
Which statement gets the length of the list values?
numValues = len(values)
class Vehicle : ... what is Vehicle's superclass?
object
The line of code that displays the floating point number stored in the variable x using 3 decimal places is:
print("%.3f" % x)
fruit = {"Apple" : "Green", "Banana" : "Yellow", "Plum" : "Purple"} What statement prints color of Banana?
print(fruit["Banana"])
somelist = ["a", "b", ..., "z"] Which one of the following options is a valid line of code for displaying the element at the twenty-second position in the list?
print(somelist[22])
Naming conventions for Python dictate that instance variables should start with an underscore to represent:
private visibility
A(n) __________________ is used to tell the user which input is expected?
prompt
In what order are the elements of a set visited when the set is traversed using a for loop?
random order
What Python statement exits the function and gives the result to the caller?
return
What is the default data type of input from the keyboard?
string
What is wrong with the following code snippet? mystery(10, 2) def mystery(num1, num2) : result = num1 ** num2 return result
the function must be defined before the statement that calls it
Which one(s) of the names below are valid variable names in Python?
timeInMs1 _time_in_ms_1
The differences between tuples and lists
tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
A set is a container that stores a collection of
unique values
How to print sorted order of elements in a set?
use the sorted method
Which statement correctly creates a list that contains four elements?
values = [1, 2, 3, 4]
what is the value of x? x = {1, 2, 3} x.discard(4)
{1, 2, 3}