Program Questions 2-6
Which of the following statements are True? A. (x > 0 and x < 10) is same as (x > 0 and x < 10) B. (x > 0 or x < 10) is same as (0 < x < 10) C. (x > 0 or x < 10 and y < 0) is same as (x > 0 or (x < 10 and y < 0)) D. (x > 0 or x < 10 and y < 0) is same as ((x > 0 or x < 10) and y < 0)
A. (x > 0 and x < 10) is same as (x > 0 and x < 10) C. (x > 0 or x < 10 and y < 0) is same as (x > 0 or (x < 10 and y < 0))
Which of the following function is incorrect? A. range(0, 3.5) B. range(10, 4, -1) C. range(1, 3, 1) D. range(2.5, 4.5) E. range(1, 2.5, 4.5)
A. range(0, 3.5) D. range(2.5, 4.5) E. range(1, 2.5, 4.5)
Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A: sum = 0 for i in range(1, 99): sum += i / (i + 1) print("Sum is", sum) B: sum = 0 for i in range(1, 100): sum += i / (i + 1) print("Sum is", sum) C: sum = 0 for i in range(1.0, 99.0): sum += i / (i + 1) print("Sum is", sum) D: sum = 0 for i in range(1.0, 100.0): sum += i / (i + 1) print("Sum is", sum)
B
Which of the following is a valid identifier? A. $343 B. mile C. 9X D. 8+9 E. max_radius A. import B. mile1 C. MILE D. (red) E. "red"
B, E B, C
Analyze the following code: Code 1: if number % 2 == 0: even = True else: even = False Code 2: even = number % 2 == 0
Both Code 1 and Code 2 are correct, but Code 2 is better.
C3 STARTS HERE
C3 STARTS HERE
CHAPTER 4 START HERE
CHAPTER 4 START HERE
CHAPTER 5 START HERE
CHAPTER 5 START HERE
Which of the following is the correct expression of character 4?
"4" '4'
Which of the following statements are the same? (A) x -= x + 4 (B) x = x + 4 - x (C) x = x - (x + 4)
(A) and (C) are the same
Which of the following is the correct expression that evaluates to True if the number x is between 1 and 100 or the number is negative? 21. To check whether a char variable ch is an uppercase letter, you write ___________.
(ch >= 'A' and ch <= 'Z') ('A' <= ch <= 'Z')
What is y after the following statement is executed? x = 0 y = 10 if x > 0 else -10
-10
25 % 1 is _____
0
Suppose x is 1. What is x after x -= 1?
0
What is min(3, 5, 1, 7, 4)?
1
The following loop displays _______________. for i in range(1, 11): print(i, end = " ")
1 2 3 4 5 6 7 8 9 10
To following code reads two number. Which of the following is the correct input for the code? x, y = eval(input("Enter two numbers: "))
1, 2
How many times is the print statement executed? for i in range(10): for j in range(10): print(i * j)
100
What is the result of 45 / 4?
11.25
What is sum after the following loop terminates? sum = 0 item = 0 while item < 5: item += 1 sum += item if sum >= 4: continue print(sum)
15
2 * 3 ** 2 evaluates to __________.
18
What is x after the following statements? x = 1 x *= x + 1
2
What will be displayed by the following code? x, y = 1, 2 x, y = y, x print(x, y)
2 1
If you enter 1 2 3 in three separate lines, when you run this program, what will be displayed? print("Enter three numbers: ") number1 = eval(input()) number2 = eval(input()) number3 = eval(input()) # Compute average average = (number1 + number2 + number3) / 3 # Display result print(average)term-2
2.0
What is the output for y? y = 0 for i in range(0, 10, 2): y += i print(y)
20
What will be displayed by print(ord('z') - ord('a'))?
25
What will be displayed by the following code? x = 1 x = 2 * x + 1 print(x)
3
What will be displayed by the following code? x = 1 x = x + 2.5 print(x)
3.5
How many times is the print statement executed? for i in range(10): for j in range(i): print(i * j)
45
What is round(6.5)?
6
What is the result of evaluating 2 + 2 ** 3 / 2?
6.0
What is max(3, 5, 1, 7, 4)?
7
What is the result of eval("1 + 3 * 2")?
7
2 ** 3 evaluates to __________.
8
What is round(7.5)?
8
2 ** 3.0 evaluates to __________.
8.0
The Unicode of 'a' is 97. What is the Unicode for 'c'?
99
The "less than or equal to" comparison operator is __________.
<=
Which of the following operators are right-associative
=
The equal comparison operator is __________.
==
What will be displayed by the following code? print("A", end = ' ') print("B", end = ' ') print("C", end = ' ') print("D", end = ' ')
A B C D
Which of the following functions cause an error?
eval("034") int("3.4")
What is the type for object 5.6?
float
To format a number x to 3 digits after the decimal point, use _______.
format(x, "5.3f")
The header of a function consists of ____________.
function name and parameter list
Suppose the input for number is 9. What will be displayed by the following program? number = eval(input("Enter an integer: ")) isPrime = True for i in range(2, number): if number % i == 0: isPrime = False print("i is", i) if isPrime: print(number, "is prime") break else: print(number, "is not prime")
i is 2 followed by 9 is prime
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
if isPrime:
Which of the following code displays the area of a circle if the radius is positive.
if radius > 0: print(radius * radius * 3.14159)
Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1
infinite loop
How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python")
infinite number of times
What function do you use to read a string?
input("Enter a string")
The following code displays ___________. temperature = 50 if temperature >= 100: print("too hot") elif temperature <= 40: print("too cold") else: print("just right")
just right
A function _________.
may have no parameters
What is the number of iterations in the following loop: for i in range(1, n + 1): # iteration
n
What is the number of iterations in the following loop: for i in range(1, n): # iteration
n - 1
Will print(chr(4)) display 4?
no
In the expression 45 / 4, the values on the left and right of the / symbol are called ____.
operands
Arguments to functions always appear within __________.
parentheses
Which of the following statement prints smith\exam1\test.txt?
print("smith\\exam1\\test.txt")
Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
print(chr(i))
To generate a random integer between 0 and 5, use ________________.
random.randint(0, 5) random.randrange(0, 6)
Which of the following statements is correct?
s = "Chapter " + str(1)
In Python, a string literal is enclosed in __________
single-quotes double-quotes
To add number to sum, you write (Note: Python is case-sensitive)
sum += number sum = sum + number
The __________ function immediately terminates the program.
sys.exit()
The time.time() returns ________________ .
the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).
An identifier can contain digits, but cannot start with a digit?
true
To draw a circle of diameter 10 with filled color red, use _________.
turtle.dot(10, "red")
To set the pen size to 5 pixels, use _________.
turtle.pensize(5)
To set a turtle drawing speed to 5, use _________.
turtle.speed(5)
To undo the last turtle action, use _________.
turtle.undo()
Assume x = 4 and y = 5, Which of the following is true
x != 5
To add a value 1 to variable x, you write
x += 1 x = x + 1 x = 1 + x
What is the output of the following code? x = 0 if x < 4: x = x + 1 print("x is", x)
x is 1
What is x after the following statements? x = 1 y = 2 x *= y + 1
x is 3
Which of the following statements are true?
Each object has a unique id. Objects of the same kind have the same type. A variable that holds a value is actually a reference to an object for the value.
What will be displayed by the following code? ch = 'F' if ch >= 'A' and ch <= 'Z': print(ch)
F
Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is the best? I: if age < 16: print("Cannot get a driver's license") if age >= 16: print("Can get a driver?s license") II: if age < 16: print("Cannot get a driver's license") else: print("Can get a driver's license") III: if age < 16: print("Cannot get a driver's license") elif age >= 16: print("Can get a driver's license") IV: if age < 16: print("Cannot get a driver's license") elif age == 16: print("Can get a driver's license") elif age > 16: print("Can get a driver's license")
II
The expression "Good " + 1 + 2 + 3 evaluates to ________.
Illegal expression
Suppose income is 4001, what will be displayed by f the following code? if income > 3000: print("Income is greater than 3000") elif income > 4000: print("Income is greater than 4000")
Income is greater than 3000
Given the following four patterns, Pattern A Pattern B Pattern C Pattern D 1 1 2 3 4 5 6 1 1 2 3 4 5 6 1 2 1 2 3 4 5 2 1 1 2 3 4 5 1 2 3 1 2 3 4 3 2 1 1 2 3 4 1 2 3 4 1 2 3 4 3 2 1 1 2 3 1 2 3 4 5 1 2 5 4 3 2 1 1 2 1 2 3 4 5 6 1 6 5 4 3 2 1 1 Which of the pattern is produced by the following code? for i in range(1, 6 + 1): for j in range(6, 0, -1): print(j if j <= i else " ", end = " ") print()
Pattern C
is the code in natural language mixed with some program code.
Pseudocode
Analyze the following statement: sum = 0 for d in range(0, 10, 0.1): sum += sum + d
The program has a syntax error because the arguments in the range must be integers.
Analyze the following code: even = False if even = True: print("It is even!")
The program has a syntax error in line 2 if even = True is not a correct condition. It should be replaced by if even == True: or if even:.
What is the value of the following expression? True or True and False
True
Which of the Boolean expressions below is incorrect?
True and 3 => 4 !(x > 0) and (x > 0) (x != 0) or (x = 0)
Suppose s is "Welcome", what is s.upper()?
WELCOME
You can place the line continuation symbol __ at the end of a line to tell the interpreter that the statement is continued on the next line.
\
random.random() returns ____________.
a float number i such that 0 <= i < 1.0
Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.
a stack
Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What will be displayed by the call nPrint('a', 4)?
aaaa
If a number is too large to be stored in memory, it _____________.
causes overflow
Given the following function header: def f(p1, p2, p3, p4) Which of the following is correct to invoke it? A. f(1, 2, 3, 4) B. f(p1 = 1, 2, 3, 4) C. f(p1 = 1, p2 = 2, p3 = 3, 4) D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) E. f(1, 2, 3, p4 = 4)
A. f(1, 2, 3, 4) D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) E. f(1, 2, 3, p4 = 4)
Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct? I: if age < 16: print("Cannot get a driver's license") if age >= 16: print("Can get a driver's license") II: if age < 16: print("Cannot get a driver's license") else: print("Can get a driver's license") III: if age < 16: print("Cannot get a driver's license") elif age >= 16: print("Can get a driver's license") IV: if age < 16: print("Cannot get a driver's license") elif age == 16: print("Can get a driver's license") elif age > 16: print("Can get a driver's license")
All correct
What is chr(ord('B')))?
B
Suppose s = "Welcome", what is type(s)?
STR
What is the result of 45 // 4?
11
24 % 5 is _____
4
What will be displayed by the following code? ? (note ? represents a blank space) print(format("Welcome", ">10s"), end = '#') print(format(111, "<4d"), end = '#') print(format(924.656, ">10.2f"))
???Welcome#111?#????924.66
Which of the following is equivalent to 0.025?
A. 0.25E-1 B. 2.5e-2 C. 0.0025E1 D. 0.00025E2 E. 0.0025E+1
The word True is ________.
a Python keyword a Boolean literal
Suppose x is a char variable with a value 'b'. What will be displayed by the statement print(chr(ord(x) + 1))?
c
If a function does not return a value, by default, it returns ___________
none
Which of the following is equivalent to x != y?
not (x == y) x > y or x < y
Given |x - 2| >= 4, Which of the following is true?
x - 2 >= 4 or x - 2 <= -4
What is the output of the following code? x = 0 while x < 4: x = x + 1 print("x is", x)
x is 4
The format function returns _______.
STR
Analyze the following fragment: sum = d = 0 while d != 10.0: d += 0.1 sum += sum + d
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
If you enter 1 2 3 in one line, when you run this program, what will happen? print("Enter three numbers: ") number1 = eval(input()) number2 = eval(input()) number3 = eval(input()) # Compute average average = (number1 + number2 + number3) / 3 # Display result print(average)
The program will have a runtime error on the input.
Suppose s is "\t\tWelcome\n", what is s.strip()?
Welcome
What will be displayed by the following code? ? (note ? represents a blank space) print(format("Welcome", "10s"), end = '#') print(format(111, "4d"), end = '#') print(format(924.656, "3.2f"))
Welcome???#?111#924.66
Is pow(a, b) the same as a ** b?
Yes
The order of the precedence (from high to low) of the operators +, *, and, or is:
*, +, and, or
The function range(5) return a sequence ______________.
0, 1, 2, 3, 4
What is math.sin(math.pi / 6)?
0.5
How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python") count += 1
10
If you enter 1, 2, 3, in one line, when you run this program, what will be displayed? number1, number2, number3 = eval(input("Enter three numbers: ")) # Compute average average = (number1 + number2 + number3) / 3 # Display result print(average)
2.0
Suppose x is 1. What is x after x += 2?
3
What will be displayed when the following code is executed? number = 6 while number > 0: number -= 3 print(number, end = ' ')
3 0
What is math.radians(30) * 6?
3.141592653589793
What is the output for y? y = 0 for i in range(10, 1, -2): y += i print(y)
30
Which of the following expression results in a value 1?
37 % 6
What is round(3.52)?
4
What is the output for y? y = 0 for i in range(0, 10): y += i
45
What is sum after the following loop terminates? sum = 0 item = 0 while item < 5: item += 1 sum += item if sum > 4: break print(sum)
6
What is the value of i printed? j = i = 1 i += j + j * 5 print("What is i?", i)
7
What is math.degrees(math.pi / 2)?
90.0
Which of the following expressions will yield 0.5?
A. 1 / 2 B. 1.0 / 2 C. 1 // 2 D. 1.0 // 2 E. 1 / 2.0
Analyze the following code. count = 0 while count < 100: # Point A print("Welcome to Python!") count += 1 # Point B # Point C A. count < 100 is always True at Point A B. count < 100 is always True at Point B C. count < 100 is always False at Point B D. count < 100 is always True at Point C E. count < 100 is always False at Point C
A. count < 100 is always True at Point A E. count < 100 is always False at Point C
What will be displayed by the following code? isCorrect = False print("Correct" if isCorrect else "Incorrect")
Incorrect
Does the function call in the following function cause syntax errors? import math def main(): math.sin(math.pi) main()
No
Will the following program terminate? balance = 10 while True: if balance < 9: continue balance = balance - 9
No
A function with no return statement returns ______.
None
Will the following program terminate? balance = 10 while True: if balance < 9: break balance = balance - 9
Yes
Suppose x is 345.3546, what is format(x, "10.3f")? (note b represents a blank space)
bbb345.355
Suppose x = 1, y = -1, and z = 1. What will be displayed by the following statement? if x > 0: if y > 0: print("x > 0 and y > 0") elif z > 0: print("x < 0 and z > 0")
nothing displayed
Suppse number contains integer value 4, which of the following statement is correct?
print(format(number, "2d"), format(number ** 1.5, "4.2f")) print(format(number, "2f"), format(number ** 1.5, "4.2f")) print(format(number, "2.1f"), format(number ** 1.5, "4.2f"))
Which of the following function returns a sequence 0, 1, 2, 3?
range(0, 4) range(4)
Consider the following incomplete code: def f(number): # Missing function body print(f(5)) The missing function body should be ________.
return number
Which of the following functions return 4.
round(3.9)
An identifier cannot be a keyword?
true
Given |x - 2| <= 4, Which of the following is true?
x - 2 <= 4 and x - 2 >= -4
Assume x = 4 and y = 5, Which of the following is true?
x < 5 or y < 5
What is x after the following statements? x = 2 y = 1 x *= y + 1
x is 4.
What is y displayed in the following code? x = 1 y = x = x + 1 print("y is", y)
y is 2 because x + 1 is assigned to x and then x is assigned to y.
Which of the following should be defined as a None function? A. Write a function that prints integers from 1 to 100. B. Write a function that returns a random integer from 1 to 100. C. Write a function that checks whether a number is from 1 to 100. D. Write a function that converts an uppercase letter to lowercase.
Write a function that prints integers from 1 to 100.
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
Which of the following loops prints "Welcome to Python" 10 times? A: for count in range(1, 10): print("Welcome to Python") B: for count in range(0, 10): print("Welcome to Python") C: for count in range(1, 11): print("Welcome to Python") D: for count in range(1, 12): print("Welcome to Python")
BC
Analyze the following code. even = False if even: print("It is even!")
The code displays nothing.
What will be displayed by after the following loop terminates? number = 25 isPrime = True for i in range(2, number): if number % i == 0: isPrime = False break print("i is", i, "isPrime is", isPrime)
i is 5 isPrime is False
What will be displayed by after the following loop terminates? number = 25 isPrime = True i = 2 while i < number and isPrime: if number % i == 0: isPrime = False i += 1 print("i is", i, "isPrime is", isPrime)
i is 6 isPrime is False
random.randint(0, 1) returns ____________.
0 or 1
Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if number % 2 == 0: even = True else: even = False Code 2: even = True if number % 2 == 0 else False Code 3: even = number % 2 == 0
All three are correct, but Code 3 is preferred.