0% found this document useful (0 votes)
48 views4 pages

CS303E Python Week 2 Worksheet

Uploaded by

asher2020zhang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

CS303E Python Week 2 Worksheet

Uploaded by

asher2020zhang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS303E Week 2 Worksheet: Simple Python

Name: EID:

Read the questions carefully, and answer each question in the space provided. Use scratch
paper to do your work and then copy your answers neatly and legibly onto the test paper.
Only answers recorded on the test paper will be graded.

1. (10 points: 1 point each) The following are true/false questions. Write either T
or F in the boxes at the bottom of page 1. If there’s any counterexample, it’s
false.

(a) In Python, a variable is used to store data values in memory.

(b) Mutable data objects in Python can be changed after they are created.

(c) Variables do not need to be declared with a specific data type.

(d) Python is a dynamically typed language, which means variables are untyped,
and values have associated types.

(e) Python variables can be assigned new values of different types without any
issues.

(f) In Python, “types” refer to the classification of data values that indicate the
kind of data they hold, such as integers or strings.

(g) Python variable names are not case-sensitive, so “VAR1” and “var1” refer to
the same variable.

(h) Using function names like “print” and “min” as variable names in Python is
always recommended to avoid conflicts.

(i) Batch mode allows you to write code in a script and execute it all at once,
while interactive mode allows you to write and execute code line by line.

(j) Syntax errors are detected by the interpreter during the compilation process,
runtime errors occur while the program is executing, and logic errors are mis-
takes in the program’s algorithm.

a b c d e f g h i j

Page total: /10


CS303E Week 2 Worksheet: Simple Python 2

Questions 2-8 are multiple choice. Each counts 2 points. Write the letter of the
BEST answer in the box on the next page. Please write your answer in
UPPERCASE. Each problem has a single answer.

2. Given the following code snippet, what will be the type of the value stored in
variable z?

x = 5
y = 3
z = x / y

A. int B. float C. str D. bool

3. Which of the following variable names is not allowed in Python?

A. my var B. private C. 123 var D. max value

4. How does the round() function work in Python?


A. round() always rounds up to the nearest integer value.
B. round() rounds to the nearest integer value, always rounding up if the decimal
part is greater than or equal to 0.5.
C. round() rounds to the nearest integer value, always rounding down if the
decimal part is greater than or equal to 0.5.
D. round() rounds to the nearest integer value, but if the decimal part is exactly
0.5, it rounds to the nearest even integer.
5. Which of the following statements accurately describes the mutability of the int,
float, and str data types?
A. Mutable: int, float. Immutable: str
B. Mutable: int. Immutable: float, str
C. All three are immutable.
D. All three are mutable
6. What does it mean for a data type to be “immutable”?
A. An immutable data type cannot be modified after it has been created. Any
operation that seems to modify it actually creates a new instance.
B. An immutable data type can be changed after it has been created without
creating a new instance.
C. An immutable data type means that you can only ever have one variable of
this type in your program.
D. An immutable data type means that no variable can be of this type.
CS303E Week 2 Worksheet: Simple Python 3

7. What is the relationship between the values “25” and 25?


A. They are of the same type since they both represent the number 25.
B. They are of different types: “25” is a string, and 25 is an integer.
C. They have different types, but they are interchangeable because Python treats
them as equivalent values.
D. They’re both funnier than 24.
8. How does the int() function work on floats?
A. int() always rounds up to the nearest integer value.
B. int() rounds to the nearest integer value, always rounding up if the decimal
part is greater than or equal to 0.5.
C. int() truncates the decimal part of a float and returns the whole number part.
D. int() rounds to the nearest integer value, but if the decimal part is exactly
0.5, it rounds to the nearest even integer.

2 3 4 5 6 7 8

Page total: /14


CS303E Week 2 Worksheet: Simple Python 4

The following questions require you to trace the behavior of some Python code and
identify the output of that code. For each question, write the output for the code segment
in the box provided. Don’t worry about line breaks.

9. (3 points)

m = 5
n = 8
m, n = n, m
print(m, n)

10. (3 points)

print(10 // 2)
print(10 / 2)

11. (3 points)

print(3 * 3)
print(3.3 * 3) # take a guess on this one

12. (3 points)

x = 10
y = 3
result = x % y
print(result)

13. (3 points)

print(17 // 3 + 1.5)

Page total: /15

You might also like