1/76
Study as a Practice Test
Which of the following codes is correct?
a) print(“Programming is fun”)
print(“Python is fun”)
b) print(“Programming is fun”)
print(“Python is fun”)
c) print(“Programming is fun)
print(“Python is fun”)
d) print(“Programming is fun)
print(Python is fun”)
print(“Programming is fun”)
print(“Python is fun”)
Which of the following code is correct?
a) //comment
b) /* comment
c) # comment
d) $$ comment
#comment
Which of the following code is correct?
a) // comments //
b) /* comments */
c) ''' comments '''
d) /# comments #/
''' comments '''
Which of the following code is correct?
a) print("Programming is fun")
print("Python")
print("Computer Science")
b) print("Programming is fun")
print("Python")
print("Computer Science")
c) print("Programming is fun")
print("Python")
print("Computer Science")
d) print("Programming is fun")
print("Python")
print("Computer Science")
print("Programming is fun")
print("Python")
print("Computer Science")
What function do you use to read a string?
a) input("Enter a string")
b) eval(input("Enter a string"))
c) enter("Enter a string")
d) eval(enter("Enter a string"))
input("Enter a string")
What is the result of eval("1 + 3 * 2")?
7
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)
2.0
If you enter 1 2 3 in one line, 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)
The program will have a runtime error on the input.
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.
\
Which of the following is a valid identifier?
mile
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
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
What will be displayed by the following code?
x, y = 1, 2
x, y = y, x
print(x, y)
2, 1
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
What is the result of 3 / 2?
1.5
What is the result of 3 // 2?
1
Which of the following expressions will yield 0.5?
1 / 2
Which of the following expression results in a value 1?
2 % 1
25 % 1 is _____
0
24 % 5 is _____
4
2 ** 3 evaluates to __________.
8.0
2 3 * 2 evaluates to __________.
18
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
What is the result of evaluating 2 + 2 ** 3 / 2?
6.0
What is the value of i printed?
j = i = 1
i += j + j * 5
print("What is i?", i)
7
What is x after the following statements?
x = 1
x *= x + 1
x is 2
What is x after the following statements?
x = 2
y = 1
x *= y + 1
x is 4
To add a value 1 to variable x, you write:
x += 1
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
Suppose x is 1. What is x after x += 2?
3
Suppose x is 1. What is x after x -= 1?
0
What is x after the following statements?
x = 1
y = 2
x *= y + 1
x is 3
Which of the following functions returns 4?
round(3.9)
Which of the following functions cause an error?
eval("3.4")
What is max(3, 5, 1, 10, 4)?
10
What is min(3, 5, 1, 7, 4)?
1
What is round(3.52)?
4
What is round(6.5)?
6
What is round(7.5)?
8
Which of the following statement prints smith\exam1\test.txt?
print("smith\\exam1\\test.txt")
The expression "Good " + 1 + 2 + 3 evaluates to ________.
Illegal expression
What will be displayed by the following code?
print("A", end = ' ')
print("B", end = ' ')
print("C", end = ' ')
print("D", end = ' ')
A B C D
To format a number x to 3 digits after the decimal point, use:
format(x, "5.3f")
Suppose x is 345.3546, what is format(x, "10.3f")?
(note b represents a blank space)
bbb345.355
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
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
Suppse ‘number’ contains integer value 4, which of the following statement is correct?
print(format(number, "2d"), format(number ** 1.5, "4.2f"))
The "less than or equal to" comparison operator is __________.
<=
The equal comparison operator is __________.
==
To generate a random integer between 0 and 5, use ______.
random.randint(0, 5)
random.randint(0, 1) returns ____________.
0 or 1
random.random() returns ____________.
a float number i such that 0 <= i < 1.0
Which of the following code displays the area of a circle if the radius is positive.
if radius > 0: print(radius * radius *3.14159)
What is the output of the following code?
x = 0
if x < 4:
x = x + 1
print("x is", x)
x is 1
Suppose isPrime is a boolean variable, which of the following is the correct statement for testing if isPrime is true.
if isPrime:
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:.
Analyze the following code:
even = False
if even:
print("It is even!")
The code displays nothing.
Suppose x = 1, y = 2, and z = 1. What will be displayed by the following statement?
if x > 0:
print("***" , y, end=" ")
if z > 0:
print("$$$", y, end=" ")
*** 2 $$$ 2
The following code displays ___________.
temperature = 50
if temperature >= 100:
print("too hot")
else:
print("just right")
just right
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
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
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
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
Which of the Boolean expressions below is incorrect?
!(x > 0) and (x > 0)
Which of the Boolean expressions below is incorrect?
True and 3 => 4
Given |x - 2| <= 4, Which of the following is true?
x - 2 <= 4 and x - 2 >= -4
Given |x - 2| >= 4, Which of the following is true?
x - 2 >= 4 or x - 2 <= -4
Assume x = 4 and y = 5, Which of the following is true?
x < 5 or y < 5
Assume x = 4 and y = 5, Which of the following is true?
x != 5
Which of the following is equivalent to x != y?
not (x == y)
What is y after the following statement is executed?
x = 0
y = 10
if x > 0
else -10
-10
What will be displayed by the following code? isCorrect = False print("Correct" if isCorrect else "Incorrect")
Incorrect
The order of the precedence (from high to low) of the operators +, *, and, or is:
*, +, and, or
Which of the following operators are right-associative.
=
What is the value of the following expression? True or True and False
True
Which of the following statements are True?
(x > 0 or x < 10 and y < 0) is same as (x > 0 or (x < 10 and y < 0))