Python MCQ
Python MCQ
ck
ra
C
t
en
m
ace
Pl
CORE BASICS
Literals are data that are entered directly into the string.
code.
An expression produces data. The simplest ex-
Data has type, for example, int (integer), pression is a literal.
ck
float (real number with finite precision), and
str (string, a collection of characters). ra
There are numerous arithmetic operators. Bi-
nary operators (which require two operands) in-
C
type() returns the type of its argument. clude:
t
en
floats. * ⇔ multiplication
⇔
a
** exponentiation
Pl
\n is used in a string to indicate the newline Floor division (//) yields a whole number
character. (which may be either an int or a float, de-
pending on the operands). The result is the
Characters in a string may be escaped by pre- largest whole number that does not exceed the
ceding the character with a backslash. This value that would be obtained with float divi-
causes the character to have a different mean- sion.
ing than usual, e.g., a backslash can be placed
before a quotation mark in a string to prevent Modulo (%) yields the remainder (which may
it from indicating the termination of the string; be either an int or a float, depending on
the quotation mark is then treated as part of the the operands) after floor division has been per-
formed.
2.10. REVIEW QUESTIONS 43
ck
The negative sign (-) and positive sign (+) can
ra
be used as unary operators, e.g., -x changes the Simultaneous assignment occurs when multi-
sign of x. The expression +x is valid but has no ple comma-separated expressions appear to the
C
effect on the value of x. right side of an equal sign and an equal number
t
en
precedence.
ce
The equal sign = is the assignment operator. than the last) is escaped using a backslash.
The value of the expression on the right side of
the equal sign is assigned to the lvalue on the Multiple statements can appear on one line if
left side of the equal sign. they are separated by semicolons.
An lvalue is a general name for something that A magic number is a numeric literal whose un-
can appear to the left side of the assignment op- derlying meaning is difficult to understand from
erator. It is typically a variable that must be a the code itself. Named constants should be used
valid identifier. in the place of magic numbers.
(a) 7 + 23
(b) 7 + 23 = 30
44 CHAPTER 2. CORE BASICS
(c) 30
(d) This produces an error.
(a) _1_2_3_
(b) ms.NET
(c) WoW
(d) green-day
(e) big!fish
(f) 500_days_of_summer
(a) a1b2c
ck
(b) 1a2b3
(c) a_b_c ra
C
(d) _a_b_
t
(e) a-b-c
en
(f) -a-b-
m
(g) aBcDe
ce
(h) a.b.c
a
Pl
4. Suppose the variable x has the value 5 and y has the value 10. After executing these state-
ments:
x = y
y = x
(a) 5 and 10
(b) 10 and 5
(c) 10 and 10
(d) 5 and 5
5. True or False: “x ** 2” yields the identical result that is produced by “x * x” for all
integer and float values of x.
6. True or False: “x ** 2.0” yields the identical result that is produced by “x * x” for all
integer and float values of x.
print(5 + 6 % 7)
8. What is the output from the print() statement in the following code?
x = 3 % 4 + 1
y = 4 % 3 + 1
x, y = x, y
print(x, y)
ck
(a) 2 4
(b) 4 2 ra
C
(c) 0 3
t
en
(d) 3 0
m
x = 3
y = 4
a
Pl
print("x", "y", x + y)
(a) 3 4 7
(b) x y 7
(c) x y x + y
(d) 3 4 x + y
(e) x y 34
For each of the following, determine the value to which the expression evaluates. (Your answer
should distinguish between floats and ints by either the inclusion or exclusion of a decimal
point.)
10. 5.5 - 11 / 2
11. 5.5 - 11 // 2
12. 10 % 7
46 CHAPTER 2. CORE BASICS
13. 7 % 10
14. 3 + 2 * 2
15. 16 / 4 / 2
16. 16 / 4 * 2
ck
Determine the values to which the variables b through g are set.
ra
18. What output is produced when the following code is executed?
C
hello = "yo"
t
en
world = "dude"
print(hello, world)
m
ce
(b) yo dude
Pl
19. The following code is executed. What is the output from the print() statement?
x = 15
y = x
x = 20
print(y)
(a) 15
(b) 20
(c) y
(d) x
(e) This produces an error.
2.10. REVIEW QUESTIONS 47
20. The following code is executed. What is the output from the print() statement?
result = "10" / 2
print(result)
(a) 5
(b) 5.0
(c) ’"10" / 2’
(d) This produces an error.
21. The following code is executed. What is the output from the print() statement?
x = 10
y = 20
a, b = x + 1, y + 2
print(a, b)
ck
(a) 10 20
ra
C
(b) 11 22
t
en
(c) ’a, b’
m
(d) ’x + 1, y + 2’
ce
26. True or False: If both m and n are ints, then “m / n” and “m // n” both evaluate to
ints.
27. True or False: The following three statements are all equivalent:
x = (3 +
4)
x = 3 + \
4
x = """3 +
4"""
48 CHAPTER 2. CORE BASICS
(a) 14.3
(b) 14.0
(c) 2
(d) 14
(e) 16
ck
30. Assume the float variable ss represents a time in terms of seconds. What is an appropriate
ra
statement to calculate the number of complete minutes in this time (and store the result as an
C
int in the variable mm)?
t
en
(a) mm = ss // 60
m
(b) mm = ss / 60
ce
(c) mm = ss % 60
a
(d) mm = ss * 60
Pl
31. To what values does the following statement set the variables x and y?
x, y = divmod(13, 7)
(a) 6 and 1
(b) 1 and 6
(c) 6.0 and 2.0
(d) This produces an error.