0% found this document useful (0 votes)
42 views8 pages

Python MCQ

This document provides a summary of core Python basics including: - Literals are data entered directly into code like integers, floats, and strings. Expressions produce data through operations. - Arithmetic operators include addition, subtraction, multiplication, division, floor division, modulo, and exponentiation. Operations are evaluated using order of operations. - Variables are names mapped to values in a namespace. Valid identifiers follow naming rules. Keywords cannot be used as identifiers. - Statements are complete commands. The equal sign is the assignment operator. Lvalues appear left of equals, expressions produce values on the right.

Uploaded by

Harshitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
42 views8 pages

Python MCQ

This document provides a summary of core Python basics including: - Literals are data entered directly into code like integers, floats, and strings. Expressions produce data through operations. - Arithmetic operators include addition, subtraction, multiplication, division, floor division, modulo, and exponentiation. Operations are evaluated using order of operations. - Variables are names mapped to values in a namespace. Valid identifiers follow naming rules. Keywords cannot be used as identifiers. - Statements are complete commands. The equal sign is the assignment operator. Lvalues appear left of equals, expressions produce values on the right.

Uploaded by

Harshitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Core basics

Input and type converion


Conditional Statements
Function
List and for-loops
More on for-loops_list_iterables
Strings
Dictionaries
Files

ck
ra
C
t
en
m
ace
Pl
CORE BASICS

2.9 Chapter Summary

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

A literal float has a decimal point or con- +, - ⇔ addition and subtraction


/, // ⇔ float and floor division
m

tains the power-of-ten exponent indicator e,


e.g., 1.1e2, 11e1, and 110.0 are equivalent % ⇔ modulo (or remainder)
ce

floats. * ⇔ multiplication

a

** exponentiation
Pl

A literal int does not contain a decimal point


float division (/) yields a float regard-
(nor the power-of-ten exponent indicator e).
less of the types of the operands. For all other
A literal str is enclosed in either a matching arithmetic operators, only when both operands
pair of double or single quotes. Quotation marks are integers does the operation yield an integer.
can be repeated three times at the beginning and Said another way, when either or both operands
end of a string, in which case the string can span are floats, the arithmetic operation yields a
multiple lines. float.

\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

divmod(a, b): equivalent to ⇒ a // b, A variable can also be thought of as a name


a % b. within a namespace. A namespace maps names
to their corresponding values (or objects).
Evaluation of expressions containing multiple
operations follows the rules of precedence to de- Valid identifiers start with a letter or underscore
termine the order of operation. Exponentiation followed by any number of letters, digits, and
has highest precedence; multiplication, integer underscores.
and float division, and modulo have equal
precedence which is below that of exponentia- There are 33 keywords that cannot be used as
tion; addition and subtraction have equal prece- identifiers.
dence which is below that of multiplication, di-
vision, and modulo. Augmented operators can be used as shorthand
for assignment statements in which an identi-
Operations of equal precedence are evaluated fier appears in an arithmetic operation on the left
left to right except for exponentiation operations side of the equal sign and on the the right side
which are evaluated right to left. of the assignment operator. For example, x +=
1 is equivalent to x = x + 1.

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

of comma-separated lvalues appear to the left


Parentheses can be used to change the order or side of the equal sign.
m

precedence.
ce

A statement can span multiple lines if it is en-


Statements are complete commands. closed in parentheses or if the newline character
a

at the end of each line of the statement (other


Pl

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.

2.10 Review Questions


1. What does Python print as a result of this statement:
print(7 + 23)

(a) 7 + 23
(b) 7 + 23 = 30
44 CHAPTER 2. CORE BASICS

(c) 30
(d) This produces an error.

2. Which of the following are valid variable names?

(a) _1_2_3_
(b) ms.NET
(c) WoW
(d) green-day
(e) big!fish
(f) 500_days_of_summer

3. Which of the following are valid identifiers?

(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

what will the values of x and y be, respectively?

(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.

7. What does Python print as a result of this statement?


2.10. REVIEW QUESTIONS 45

print(5 + 6 % 7)

(a) This produces an error.


(b) 5 + 6 % 7
(c) 11
(d) 4

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

9. What is the output produced by the following code?


ce

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

17. Given that the following Python statements are executed:


a = 2
b = a + 1 // 2
c = a + 1.0 // 2
d = (a + 1) // 2
e = (a + 1.0) // 2
f = a + 1 / 2
g = (a + 1) / 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

(a) hello, world


a

(b) yo dude
Pl

(c) "yo" "dude"


(d) yodude
(e) This produces an error.

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

(e) This produces an error.


a

22. True or False: In general, “x / y * z” is equal to “x / (y * z)”.


Pl

23. True or False: In general, “x / y ** z” is equal to “x / (y ** z)”.

24. True or False: In general, “w + x * y + z” is equal to “(w + x) * (y + z)”.

25. True or False: In general, “w % x + y % z” is equal to “(w % x) + (y % z)”.

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

28. Given that the following Python statements are executed:


x = 3 % 4
y = 4 % 3

What are the values of x and y?

29. To what value is the variable z set by the following code?


z = 13 + 13 // 10

(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.

ANSWERS: 1) c; 2) a and c are valid; 3) a, c, d, and g are valid; 4) c; 5) True; 6) False, if x is an


integer x * x yields an integer while x ** 2.0 yields a float; 7) c; 8) b; 9) b; 10) 0.0; 11)
0.5; 12) 3; 13) 7; 14) 7; 15) 2.0; 16) 8.0; 17) b = 2, c = 2.0, d = 1, e = 1.0, f =
2.5, g = 1.5; 18) b; 19) a; 20) d; 21) b; 22) False (the first expression is equivalent to “x * z
/ y”); 23) True; 24) False; 25) True; 26) False (“m / n” evaluates to a float); 27) False (the
first two are equivalent arithmetic expressions, but the third statement assigns a string to x); 28) 3
and 1; 29) d; 30) a; 31) b.

You might also like