Introduction to Programming Using Python, Y. Daniel Liang

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 2 Elementary Programming


Section 2.3 Reading Input from the Console
2.1  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"))

2.2  What is the result of eval("1 + 3 * 2")?
A. "1 + 3 * 2"
B. 7
C. 8
D. "1 + 6"

2.3  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)
A. 1.0
B. 2.0
C. 3.0
D. 4.0

2.4  _______ is the code in natural language mixed with some program code.
A. Python program
B. A Python statement
C. Pseudocode
D. A flowchart diagram

2.5  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)
A. The program runs correctly and displays 1.0
B. The program runs correctly and displays 2.0
C. The program runs correctly and displays 3.0
D. The program runs correctly and displays 4.0
E. The program will have a runtime error on the input.

2.6  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.
A. /
B. \
C. #
D. *
E. &

Section 2.4 Identifiers
2.7  An identifier cannot be a keyword?
A. true
B. false

2.8  An identifier can contain digits, but cannot start with a digit?
A. true
B. false

2.9  Which of the following is a valid identifier?
A. $343
B. mile
C. 9X
D. 8+9
E. max_radius

2.10  Which of the following is a valid identifier?
A. import
B. mile1
C. MILE
D. (red)
E. "red"

Section 2.5 Variables, Assignment Statements, and Expressions
2.11  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)
A. 1.0
B. 2.0
C. 3.0
D. 4.0

2.12  What will be displayed by the following code?

x = 1
x = 2 * x + 1 
print(x)
A. 0
B. 1
C. 2
D. 3
E. 4

2.13  What will be displayed by the following code?

x = 1
x = x + 2.5 
print(x)
A. 1
B. 2
C. 3
D. 3.5
E. The statements are illegal

Section 2.6 Simultaneous Assignments
2.14  What will be displayed by the following code?

x, y = 12
x, y = y, x
print(x, y)
A. 1 1
B. 2 2
C. 1 2
D. 2 1

2.15  To following code reads two number. Which of the following is the correct input for the code?

x, y = eval(input("Enter two numbers: "))
A. 1 2
B. "1 2"
C. 1, 2
D. 1, 2,

Section 2.8 Numeric Data Types and Operators
2.16  What is the result of 45 / 4?
A. 10
B. 11
C. 11.25
D. 12

2.17  In the expression 45 / 4, the values on the left and right of the / symbol are called ____.
A. operators
B. operands
C. parameters
D. arguments

2.18  What is the result of 45 // 4?
A. 10
B. 11
C. 11.25
D. 12

2.19  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

2.20  Which of the following expression results in a value 1?
A. 2 % 1
B. 15 % 4
C. 25 % 5
D. 37 % 6

2.21  25 % 1 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.22  24 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.23  2 ** 3 evaluates to __________.
A. 9
B. 8
C. 9.0
D. 8.0

2.24  2 ** 3.0 evaluates to __________.
A. 9
B. 8
C. 9.0
D. 8.0

2.25  2 * 3 ** 2 evaluates to __________.
A. 36
B. 18
C. 12
D. 81

2.26  What is y displayed in the following code?

x = 1
y = x = x + 1
print("y is", y)
A. y is 0.
B. y is 1 because x is assigned to y first.
C. y is 2 because x + 1 is assigned to x and then x is assigned to y.
D. The program has a compile error since x is redeclared in the statement int y = x = x + 1.

2.27  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

2.28  If a number is too large to be stored in memory, it _____________.
A. causes overflow
B. causes underflow
C. causes no error
D. cannot happen in Python

Section 2.9 Evaluating Expressions and Operator Precedence
2.29  What is the result of evaluating 2 + 2 ** 3 / 2?
A. 4
B. 6
C. 4.0
D. 6.0

Section 2.10 Augmented Assignment Operators
2.30  What is the value of i printed?

j = i = 1
i += j + j * 5
print("What is i?", i)
A. 0
B. 1
C. 5
D. 6
E. 7

2.31  What is x after the following statements?

x = 1
x *= x + 1
A. x is 1
B. x is 2
C. x is 3
D. x is 4

2.32  What is x after the following statements?

x = 2
y = 1
x *= y + 1
A. x is 1.
B. x is 2.
C. x is 3.
D. x is 4.

2.33  To add a value 1 to variable x, you write
A. 1 + x = x
B. x += 1
C. x := 1
D. x = x + 1
E. x = 1 + x

2.34  Which of the following statements are the same?

(A) x -= x + 4
(B) x = x + 4 - x
(C) x = x - (x + 4)
A. (A) and (B) are the same
B. (A) and (C) are the same
C. (B) and (C) are the same
D. (A), (B), and (C) are the same

2.35  To add number to sum, you write (Note: Python is case-sensitive)
A. number += sum
B. number = sum + number
C. sum = Number + sum
D. sum += number
E. sum = sum + number

2.36  Suppose x is 1. What is x after x += 2?
A. 0
B. 1
C. 2
D. 3
E. 4

2.37  Suppose x is 1. What is x after x -= 1?
A. 0
B. 1
C. 2
D. -1
E. -2

2.38  What is x after the following statements?

x = 1
y = 2
x *= y + 1
A. x is 1
B. x is 2
C. x is 3
D. x is 4

Section 2.11 Type Conversions and Rounding
2.39  Which of the following functions return 4.
A. int(3.4)
B. int(3.9)
C. round(3.4)
D. round(3.9)

2.40  Which of the following functions cause an error?
A. int("034")
B. eval("034")
C. int("3.4")
D. eval("3.4")

Section 2.12 Case Study: Displaying the Current Time
2.41  The time.time() returns ________________ .
A. the current time.
B. the current time in milliseconds.
C. the current time in milliseconds since midnight.
D. the current time in milliseconds since midnight, January 1, 1970.
E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).