Computer Programming Final Notes
Assume that info = {'name':'Sandy','age':17}. Then the expression info.get('name') evaluates to:
'Sandy'
The following are tuple methods:
'count', 'index'
You distinguish a tuple from a list by enclosing the elements in ____ instead of ____.
(), []
What is the output when the following code is executed, with the recursive function is called with an argument of 4? def example(n): if n > 0: print(n,end=' ') example(n-1) example(4) print()
4 3 2 1
def example(n): if n > 0: print(n) example(n) else: example(n-1) example(4) print()
4 3 2 1
What value is printed when the following statement executes? print(18 / 4)
4.5
What is printed by the following statements? total = 0 mydict = {'cat':12, 'dog':6, 'elephant':23, 'bear':20} for akey in mydict: if len(akey) > 3: total = total + mydict[akey] print(total)
43
To find out information on the standard modules available with Python, you should:
Go to the Python Documentation site.
What is the purpose of the docstring immediately following the function header? def functionName (parameter): ''' (type) -> (type) description goes here '''
Helper code for people using the function to know, in natural language, what the function is doing and what type of argument is expected to be passed to the functon's parameters.
What is printed by the following statements? alist = [1,3,5] blist = [2,4,6] print(alist + blist)
[1,3,5,2,4,6]
If the variable data refers to the list [10,20,30], then the expression data[1:3] evaluates to:
[20, 30]
What is printed by the following statements? alist = [4,2,8,6,5] temp = alist.pop(2) temp = alist.pop() print(alist)
[4, 2, 6]
An interpreter is
a program that line by line translates instructions written in a high-level language into the equivalent machine language and does what it says. Alternately reading and carrying out commands.
Choose the best description of Polymorphism:
allows objects of different types, each with their own specific behaviors to be treated as the same general type.
What will the following code print, if x = 3, y = 5, and z = 2? elif y < x and y < z: print ('b') else: print ('c')
c
What is used to begin an end-of-line comment?
# symbol
Which of these is a comment?
#I'm a comment!
Which of the following lists of operators is ordered by deceasing precedence?
**, *, +
What is this hexadecimal number B3D converted to binary? (Type only 1s & 0s do not include any extra spaces)
011001111110
What is printed when the following statements execute? x = 12 x = x - 3 x = x + 5 x = x + 1 print (x)
15
re are the function design steps in alphabetical order. Which option puts the step numbers in the order in which they should be completed according to the design recipe. 1 Description - includes all parameters and return 2 Examples 3 Header: def function_name (parameter) 4 Test your function 5 Type contract - parameter types and return type 6 Write the function body
2 5 3 1 6 4
temperature refers to [18, 20, 22.5, 24] what does temperature[1] refer to:
20
Which of these is a NOT a valid variable name?
2timesbase
What value is printed when the following statement executes? print(18 // 4)
4
def circle_area(radius): area = radius * radius * math.pi return area circle_area(5)
5
The expression 2 ** 3 ** 2 evaluates to which of the following values?
512
What value is printed when the following statement executes? print( int(53.785) )
53
What is printed by the following statements? mydict = {'cat':12, 'dog':6, 'elephant':23} print(mydict['dog'])
6
What is printed by the following statements? alist = [3, 67, 'cat', [56, 57, 'dog'], [ ], 3.14, False] print(len(alist))
8
Assume that the variable name has the value 47. What is the value of name after the assignment statement name = name * 2 executes?
94
Choose the most accurate description of a method: def getName(self):
A function that is defined inside a class definition and is invoked on instances of that class.
An algorithm is
A general process or steps for solving a category of problems
A function is
A named sequence of instructions that performs a specific useful task or operation.
What must a programmer use to test a program?
A reasonable set of legitimate inputs
This code should print a greeting message. Determine what is wrong with this code. Re-type this line of code correctly. print("hello)
Answer: print(hello)
Which of the following is a run-time error?
Attempting to divide by 0.
Choose the best description of Encapsulation:
Bundling together instance variables and methods to to form a type. Also restricting access to certain class members - hides the details.
What is printed by the following statements? myname = 'Edgar Allan Poe' namelist = myname.split() init = '' for aname in namelist: init = init + aname[0] print(init)
EAP
An advantage of functions is that they:
Eliminate repetitive code.
What are comments for?
For the people who are reading your code to know, in natural language, what the program is doing.
current_time = input('what is the current time (in hours 0--23)? ') wait_time = input('How many hours do you want to wait? ') print(current_time) print(wait_time) final_time = current_time + wait_time print(final_time) What best describes what is wrong?
Python is doing string concatenation, not integer addition
When the function range receives two arguments, what does the second argument specify?
The last value of a sequence of integers up to but not including the value of the arguement.
Which of the following outputs data in a Python program?
The print statement
Consider the code: import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) alex.left(90) alex.forward(75)
This line allows the module called turtle, which has all the built in functions for drawing on the screen with the Turtle object, be used by this program.
What is printed when the following statements execute? day = "Thursday" day = 32.5 day = 19 print(day)
Thursday
The lifetime (how long it stays in memory) of a parameter within a function is:
While the function is executing.
What is printed by the following statements? alist = [3, 67, 'cat', [56, 57, 'dog'], [ ], 3.14, False] print(alist[4:])
[ [ ], 3.14, False]
Assume that info = {'name':'Sandy','age':17}. Then the expression list(info.keys()) evaluates to:
['name','age']
What is printed by the following statements? alist = [4,2,8,6,5] blist = alist * 2 blist[3] = 999 print(alist)
[4,2,8,6,5]
What will grades refer to after this code is executed? grades = [80, 70, 60, 90] grades.sort() grades.insert(1, 95)
[60, 95, 70, 80, 90]
What is printed by the following statements? alist = [4,2,8,6,5] blist = [ ] for item in alist: blist.append(item+5) print(blist)
[9,7,13,11,10]
What is source code?
a program written in a high-level computer langua
Type the Python statement which would create a variable called city, which stores the text "Minneapolis". Do not use any extraneous characters or spaces.
city="Minneapolis" print(city)
Which of the following is a valid function header (first line of a function definition)?
def drawCircle(t):
What is printed by the following statements? mydict = {'cat':12, 'dog':6, 'elephant':23, 'bear':20} keylist = list(mydict.keys()) keylist.sort() print(keylist[3])
elephant
length = 6 What type of variable is length?
integer
A variable
is assigned a set value
Choose the best description of Inheritance:
is the ability of a subclass (also called 'derived class' or 'child class') to acquire the members of a superclass (also called 'base class' or 'parent class') as part of its own definition
Choose the most accurate description of the following: def __init__(self):
is the initializer method or constructor for this class. This method is automatically called when a new instance of this class is created.
temperature refers to [18, 20, 22.5, 24] select the statement(s) that refer to 24.
temperature [3:4] temperature [3:]
Two approaches to program design are _________ and stepwise refinement.
top-down design
Debugging is:
tracking down programming errors and correcting them.
A dictionary is a(n) ___________ collection of _______ .
unordered, key-value pairs
Consider the following Python code: current_time_str = input('what is the current time (in hours 0-23)? ') wait_time_str = input('How many hours do you want to wait? ') current_time_int = int(current_time_str) wait_time_int = int(wait_time_int) final_time_int = current_time_int + wait_time_int print(final_time_int) Why is wait_time_int = int(wait_time_int) flagged as an error?
wait_time_int has not yet been assigned a value so it cannot be converted using int.
What is the correct Python expression for checking to see if a number stored in a variable x is between 0 and 5? Choose all correct answers.
x > 0 and < 5, 0 < x < 5
What is printed by the following statements? mydict = {'cat':12, 'dog':6, 'elephant':23} mydict['mouse'] = mydict['cat'] + mydict['dog'] print(mydict['mouse'])
18
What is printed by the following statements? mydict = {'cat':12, 'dog':6, 'elephant':23, 'bear':20} answer = mydict.get('cat')//mydict.get('dog') print(answer)
2
What value is printed when the following statement executes? print(18 % 4)
2
An IDE
Integrated Development Environment (IDE): contains many programs that are useful in creating your program. Interface used to write, compile and run your progra
Choose the most accurate description of the following: class Shape(object):
Is a user defined compound data type. It can also be thought of as a template for the objects that are instances of it.
import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) alex.left(90) alex.forward(75)
It creates a new turtle object that can be used for drawing.
In the command range(3, 10, 2), what does the second argument (10) specify?
Range should generate a list that stops at 9 (including 9).
What does s refer to after this code is executed? s = "" names = ["Sally", "Dick", "Jane"] for name in names: s = s + name
SallyDickJane
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?
The square function will return x instead of x*x
Choose the most accurate description of a instantiation: cap = coffee_drink("Cappuccino", 4.50)
To create an instance of a class, and to run its initializer.
What is one main purpose of a function?
To help the programmer organize programs into chunks that each performs some useful operation leading to the solution of the problem.
What is the best way to determine the type of a variable?
Use the type function.
A recursive function:
Usually runs more slowly than the equivalent loop.
What is the output of the following code containing the recursive function call with'hello' and 0 passed as arguments? def example(aString,index): if index == len(aString): return "" else: return aString[index]+example(aString, index+1) print(example('hello',0))
hello
You write Python code using high-level, human-like language. What does the Python code have to be converted into before it can be executed by the computer's CPU?
machine code
Select the expression(s) that produce 4
min(10, 8, 4) len(["math"]) len([1, 2, 3, 4])
Suppose you are going to write a recusive function to calculate the factorial of a number. fact(n) returns n * n-1 * n-2 * ... Where the factorial of zero is definded to be 1. What would be the most appropriate base case?
n<=1
What is the output of the following code containing the recursive function call with'hello' and 0 passed as arguments? def example(aString,index): if index < len(aString): example(aString,index+1) print(aString[index],end='') example('hello',0) print()
olleh
The method to remove and return an entry from a dictionary is named:
pop
he correct code to generate a random number between 1 and 100 (inclusive) is:
prob = random.randrange(1, 101)
In the following code identify the parameter: def circle_area(radius): area = radius * radius * math.pi return area circle_area(5)
radius
What command correctly generates the list [2, 5, 8]?
range(2, 10, 3)
A for loop is convenient for
running a set of statements a predictable number of times
When a recursive function is called, the values of its arguments and its return address are placed in a:
stack frame