CS 170 CH 5-9
Which of the following is used to format a floating point number?
%g
Given a tuple t = (1, 2, 3), which of the following is the result of t * 2?
(1, 2, 3, 1, 2, 3)
Given a tuple t = (1, 2, 3, 4), which of the following is the result of t[2:-1]?
(3)
What's the output of the following code? def inc(x): x = x + 1 print(x) cnt = 10 inc(cnt) print(cnt)
11 10
What is the output of the following code? def mystery(num1, num2): if (num1 < num2): print(num1, 'is the smallest') elif (num1 == num2): print(num1, 'is equal to', num2) else: print(num2, 'is the smallest') mystery(13, 49)
13 is the smallest
Given a sorted list with 15 elements, what is the maximum number of comparisons(with the elements in the list) needed to search a value not in the list using linear search?
15
Given list1 = [1, 5, 9], what is sum(list1)?
15
What's the output of the following program? def test(x): x = x + 5 print(x) test(10)
15
Given a string s = "Welcome", what is the value of s.count('e')?
2
Given m = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] What's the value of len(m)
3
What's the output of the following code? x = 1 def f1(): y = x + 2 print(y) f1() print(x)
3 1
What's the output of the following program? x = 4 def f1(): global x x = 6 x = x - 3 print(x) f1() print(x)
3 3
What's the value of "{:.3f}".format(3)? No enclosing quotes.
3.000
Given a sorted list with 1,000,000,000 elements, how many comparisons are needed to find an element in the worst case?
30
Given a sorted list with 15 elements, what is the maximum number of comparisons(with the elements in the list) needed to search a value not in the list using binary search?
4
What is the value of len('It\'s')?
4
What is the output of the following code? def mystery(aList): aList.append(5) list1 = [1, 2, 3, 4] print(len(list1)) mystery(list1) print(len(list1))
4 5
What's the output of the following code? sList = [1, 2, 3, 4, 5] print(sList[-2]) print(sList[-4:-1])
4 [2, 3, 4]
To draw an Entry and extract its value, you have to write:
4-5 steps
According to some studies, visual aids in the classroom improve learning by up to ___________________.
400%
Given m = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] What's the value of len(m[1])
5
What's the output of the following code segment? aList = [14, 55, 4, 34, 2, -3, 76] index = 0 for i in range(1, len(aList)): if ( aList[i] < aList[index] ): index = i print(index)
5
What is the output of the following code? x = 50 def main(x): print(x) x = 2 print(x) main(x) print(x)
50 2 50
What is the output when following code is executed ? listA = [-8, 3, 45, 22, 12, 85] for i in range (1, 6): listA[i - 1] = listA[i] *2 for i in range(0, 6): print(listA[i], end = " ")
6 90 44 24 170 85
What is the output of the below program? def cube(num): return num **3 value1 = cube(4) print(value1)
64
What's the output of the following code? x = 5 def f1(): x = 7 print(x) f1() print(x)
7 5
Given m = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] What's the value of m[1][2]
8
What is the output of the following code? list1 = [1, 2, 3, 4] list2 = [5, 6, 7, 8] print(len(list1 + list2))
8
What is a variable defined outside a function referred to as?
A global variable
What is a variable defined inside a function referred to as?
A local variable
What is a local variable?
A variable that is only used and visible inside a function.
The list [40, 10, 20, 30] is sorted by insertion sort with three calls to the insert method. How many of those calls need to modify the location of at least one element in the list and why?
All calls to insert modify an element's location - since all elements to be inserted are smaller than 40.
What are the two main types of functions?
Built-in functions & User-defined functions.
Which of the following is true regarding functions in python? A. Functions will make a program run faster. B. You can't create your own functions. C. Functions are reusable pieces of program. D. A user-defined function must start with a lower case letter.
C. Functions are reusable pieces of program.
To sort a list in descending order, what changes are needed in the insert function?
Change the condition for the while to: i >= 0 and elements[i] < target.
Suppose that the string x is as follows: x = "This is a test" what is the value of x.isupper()?
False
What is the output of the following code? studentsNames = ['Alex', 'Smith', 'William', 'Tom'] if ('Smith' in studentsNames): print ("Found") else: print ("Not found")
Found
What is the output of the following code? def display(message, n = 1): while (n > 0): print (message [0] + message[-1] ) n -= 1 num = 5 display("Hello", num)
Ho Ho Ho Ho Ho
What is printed by the following statements: s = "Ball" r = "" for item in s: r = item.upper() + r print(r)
LLAB
The list [20, 30, 40, 10] is sorted by insertion sort with 3 calls to the insert method. How many of those calls need to modify the location of at least one element in the list and why?
One call - only the last call to insert modifies the location of an element since it starts with [<20, 30, 40> ,10]
Suppose s is "python", what is the value of s.upper()?
PYTHON
What's the value of "Payment: {:>8.2f}".format(743.123)? No enclosing quotes.
Payment:
What is the value of "Python is fun"[:2]?
Py
Suppose s is "\t\tPython Programming\n", what is the value of s.strip()?
Python Programming
Which graphics class is the best for drawing a square?
Rectangle
Any color we make using the color_rgb function is a mix between the following colors?
Red, green and blue
What is the output of the following code? num = 1 def incrNum(): num = num + 3 print(num) incrNum() print(num)
The program has a runtime error because num is not initialized on line 3.
What is the scope of a variable?
The range of statements in the code where a variable can be accessed.
Given the following code: string1 = "hi" string2 = "hi" which of the following statements about string1 and string2 are true?
The values of both strings are the same. The values of the id function of both strings are the same.
Strings are immutable. This means that once defined, they cannot be modified.
True
Many studies found that most beginners are _________________________.
Visual Learners
Which of the following should be defined as a function that returns None?
Write a function that displays integers from 1 to 100.
The difference between the following two lines win.getMouse() p1 = win.getMouse()
You can know the coordinates of the point you clicked in Line2 but not Line 1.
What is the value stored in res when the following code is executed ? x= "Welcome to Python" res = x.split()
["Welcome", "to", "Python"]
What's the output of the following code? a = [1, 2, 3, 4] b = [10, 20, 30, 40] c = a + b print(c)
[1, 2, 3, 4, 10, 20, 30, 40]
What's the output of the following program? def addOne(alist): for singleItem in alist: singleItem = singleItem + 1 list1 = [1, 2, 3] addOne(list1) print(list1)
[1, 2, 3]
What's the output of the following program? def mystery(lis): lis = lis * 2 list1 = [1, 2, 3] mystery(list1) print(list1)
[1, 2, 3]
Given list1 is [1, 3, 2], what is the result of list1 * 2?
[1, 3, 2, 1, 3, 2]
Suppose list1 is [13, 18, 59, 21, 75, 44], what are the elements of list1 after executing list1.pop()?
[13, 18, 59, 21, 75]
What's the output of the following program? def addOne(alist): for i in range(0, len(alist)): alist[i] = alist[i] + 1 list1 = [1, 2, 3] addOne(list1) print(list1)
[2, 3, 4]
What is printed by the following statements? a = [4, 2, 8, 6, 5] b = a * 2 b[3] = 999 print(a)
[4, 2, 8, 6, 5]
What is printed by the following statements? a = [4, 2, 8, 6, 5] b = [a] * 2 c = a * 2 a[3] = 999 print(a) print(b) print(c)
[4, 2, 8, 999, 5] [[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]] [4, 2, 8, 6, 5, 4, 2, 8, 6, 5]
Suppose that list1 is [99,16, 85, 43], What is the result of list1[:-1]?
[99,16, 85]
Given the list [6, 3, 8, 9, 4, 2, 7], which of the following is an arrangement of the numbers after four insert operations?
[<3, 4, 6, 8, 9>, 2, 7]
Which of the following is not a valid escape sequence?
\%
Which of the following represents a newline character?
\n
What's the output of the following code? def printChar(word = 'b', n = 3): while (n > 0): print(word) n -= 1 printChar(n = 5, word = 'a')
a a a a a
The format function returns _______.
a str
What's the value of "{:>5}".format("abc")?
abc
What's the value of "{:^5}".format("abc")?
abc
Which of the following computes the horizontal distance between points p1 and p2?
abs( p1.getX() - p2.getX())
Which of the following statement is to fill a circle named circle1 with a green color?
circle1.setFill(color_rgb(0,255,0))
A function header consists of ______________.
function name and parameter list
It is a good practice to avoid using __________.
global variables
A well designed software module should have ________________.
high cohesion and low coupling
Which of the following statements create empty lists?
li = [] li = list()
Which of the following statement inserts 7 to the second position in list1?
list1.insert(1, 7)
Which of the following statement removes the third element from list1?
list1.pop(2)
Which of the following statement removes string "python" from list1?
list1.remove("python")
Given a list1 with the following value: list1 = [ [9, 7, 4], [0, 99, 8], [74, 33, 89]] which of the following expression has the value of 99.
list1[1][1]
A function _________.
may have no parameters
Which operations may be performed using the % operator? Check all that apply.
modulus (remainder) format
Which of the following creates a Text object centered at the point(50,50) that says "All is well"?
point3 = Point (50,50) textObjectName = Text (point3, "All is well")
Given a tuple t = (3, 6, 2, 4), which of the following will result in errors?
print(t[4]) t[0] = 5
To check whether or not string s1 contains string s2, use _________.
s2 in s1 s1.find(s2) >= 0
To retrieve the third character from string s, use _________.
s[2]
Given the following statement in a code from random import * which of the following is a correct statement to shuffle list list1?
shuffle(list1)
We may use _______ delimiters for string literals.
single quote double quote
If no parameter is passed to the split() function what is the string split on?
white space
Which of the following is the correct statement to change the background color of a window called window1 to red color?
window1.setBackground(color_rgb(255, 0, 0))
Which of the following is correct about the following statement? x = 1, 2, 3, 4
x is a tuple
What is the output of the following code? x = 10 def f1(y): global x print (x) x = 6 print (x) y = y + x return y x = f1(x) print(x)
10 6 16
What's the output of the following code? def inc(x): x = x + 1 print(x) return x cnt = 10 cnt = inc(cnt) print(cnt)
11 11
Assume that you have the following two python files: multi.py def prod(x, y): return x * y def main(): print(3, 4) print(2, 4) main() test.py import multi print(prod(4, 4)) what's the output when we run the test.py file?
12 8 Error: Name Error
What is the output of the following code? list1 = [2, 3, 21, 14, 25] print(list1[-1])
25
What's the output of the following code? 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)
25
What is the output of the following program? i = 27 def triple(i): i = i * 3 return i triple(20) print(i)
27
What is the output of the following code? x = 1 def f1(): x = 3 print(x) f1() print(x)
3 1
What's the output of the following code? def test(x, y): print(x, y) y = 3 x = 4 test(y, x)
3 4
What is the output of the following code? def main(a = 2, b = 5): b = a + b a += 1 print(a, b) main()
3 7
______________ is a measure of the degree of interdependence between modules.
Coupling
What type of object can be used to get input from the keyboard into a graphics window?
Entry
A graphics window always has a width of 500 and a height of 500.
False
Every element in a list must be of the same type
False
The graphics library has a special class to draw an octagon called Octagon.
False
The statement myShape.move(10, 20) moves myShape to the point(10, 20).
False
Traditionally, the bottom-left corner of a graphics window has coordinates (0, 0).
False
True or False. In real-world applications, programmers rarely define their own functions.
False
True or False: The line: win = GraphWin(my first example, 100, 100) has no error.
False
True or False: A graphics window always has the title "Graphics Window."
False
True or False: Graphics do not help students better understand the basic elements of programming (e.g., selections, iterations, functions, lists, files)
False
True or False: There is no need for a special graphics module (library) to handle graphics projects.
False
True or False: Using graphics.py allows graphics to be drawn into a Python shell window
False
True or False? The method (operation) that is used in the graphics library to get a mouse click is readMouse.
False
What's the output of the following code? greeting = "Hi" def greeting(first, last): print( first, last) greeting("Joe","Doe")
Joe Doe
Which statement creates a line from (2,3) to (4,5)?
Line(Point(2, 3), Point(4, 5))
A function with no return statement returns _____.
None
What will the following function return? def add(x, y, z): print(x + y + z)
None
What are the advantages of functions in Python?
Reducing duplication of code. Decomposing complex problems into simpler pieces. Improving clarity of the code.
What type of objects can be used to show/print/display a message in a graphics window?
Text
What color is color_rgb(255,255,255)?
White
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[4:])
[[], 3.14, False]
What's the output of the following code? def printChar(word = 'b', n = 3): while (n > 0): print(word) n -= 1 printChar('a')
a a a
What will be displayed by the following code? def printChar(word, n): while (n > 0): print(word) n -= 1 printChar('a', 5)
a a a a a
The memory area that stores the function's parameters and its local variables in a function call is called ___________.
a stack
Which of the following refer to the last element in a list names? Check all that apply.
names[-1] names[len(names) - 1]
Which of the following code lines does not create a Text object centered at point (50 ,50 ) that says "All is well"?
optimistMessage = T((50,50) , "All is well")
In a function call, passing the value of the argument to the parameter is referred to as _________.
pass by value
Which command is used to draw the graphics object shape into the graphics window win?
shape.draw(win)
Which of the following statement will make a shape called shape1 disappear?
shape1.undraw()
To know the x-coodinate value of the point you clicked in the following line: clickedPoint = window.getMouse() You have to use
xCoord= clickedPoint.getX()
What is the output of the below program? x = 10 y = 10 def change(): global y x = 45 y = 56 #call the function change() print(x) print(y)
10 56
What is the output of the following code? def print_multi(num, str): for i in range(0, num): print(str, end="") def print_triangle(num): for n in range(1, num+1): print_multi(n, "*") print("") def main(): print_triangle(5) main()
* ** *** **** *****
What is the output of the following code? list1 = [3, 5, 25, 1, 3] print(min(list1))
1
What is the output when the following code is executed? myList = [-85, 34, 23, 90, 123] maximumValue = myList[0] indexOfMax = 0 for i in range(1, len(myList)): if (myList[i] > maximumValue): maximumValue = myList[i] indexOfMax = i print(indexOfMax)
4
What's the output of the following program? x = 3 def test(): x = 4 print(x) test() print(x)
4 3
What's the output of the following code? import math print(sqrt(9))
There is a NameError.
Given the following function, and show the output after invoking it. def display(message = "a", n): while (n > 0): print (message [0] + message[-1] ) n -= 1 num = 5 display("Hello", num)
There is a SyntaxError on line 1.
Analyze the following code nums = [3, 7, 2, 1] print(len(nums)) print(nums[-1]) print(nums[len(nums)]) which of the following statements are true?
There is an IndexError on line 4.
Analyze the following code list1 = ['CS', 'Math', 'EE'] major1, major2 = list1 which of the following statements are true? Check all that apply.
There is an error because the number of variables on the left-hand side is different from the size of the list.
What is wrong with the following function definition: def add(x, y, z): return x + y + z print(x + y + z)
There should not be any statements in a function after the return statement. Once the function gets to the return statement, it will immediately stop executing the function.
A list may have duplicates.
True
A single point on a graphics screen is called a pixel.
True
Which keyword is used to define a function?
def
Which of the following statement changes the size of the text inside the object text1 to 24?
text1.setSize(24)