0% found this document useful (0 votes)
5 views

week8-worksheet

The document is a worksheet for a CS303E course focused on strings in Python, containing true/false questions, multiple choice questions, and code tracing exercises. Students are required to answer questions about string properties, methods, and behaviors in Python. The worksheet is structured to assess understanding of string manipulation and related concepts.

Uploaded by

Orin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

week8-worksheet

The document is a worksheet for a CS303E course focused on strings in Python, containing true/false questions, multiple choice questions, and code tracing exercises. Students are required to answer questions about string properties, methods, and behaviors in Python. The worksheet is structured to assess understanding of string manipulation and related concepts.

Uploaded by

Orin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS303E Week 8 Worksheet: More on Strings

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. (11 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) Characters are a different type from strings in Python.

(b) If strings s1 and s2 are equivalent, the expression s1 in s2 will return False
because “in” checks for partial matches and not the complete operand length.

(c) In Python, you can directly modify a character within a string by using the
index notation, like s[2] = "z".

(d) To loop over the indices of string s, you can do


for i in range(len(s) + 1).

(e) Strings in Python are 0-based indexed (i.e., the first character is at index 0).

(f) The strip() method in Python removes both leading and trailing whitespace
from a string.

(g) Python allows you to compare strings using the relational operators
(<, <=, >, >=) based on their lengths.

(h) Strings can indexed using negative numbers.

(i) On string s, min(s) == max(s) will always evaluate to False.

(j) islower(), isdigit() and isalpha() are all functions, so they take a string
as a parameter rather than being directly called on a string.

(k) When s is a string, in loops like for i in s, i will refer to a character of s


each iteration.

a b c d e f g h i j k

Page total: /11


CS303E Week 8 Worksheet: More on Strings 2

Questions 2-9 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. What is the result of "oink oink" * 2 + " oink"?


A. "oink oinkoink oink oink"
B. "oink oinkoink"
C. "oink oink oink oink"
D. Error
E. None of the above
3. What is the result of "Teehee".find("e")?

A. 1 B. 2 C. 4 D. 5 E. -1

4. Which operator can be used to check if a substring is present in a string?

A. contains B. in C. exists D. substring E. Both A and B

5. If s = "Basil and Wybie =)", what is the result of the expression


len(s) - len(s.replace(" ", ""))?

A. 0 B. 1 C. 2 D. 3 E. None of the above

6. Which of the following statements is true about Python strings?


A. Strings can only contain letters and numbers.
B. Strings can be modified in place.
C. Strings must always be enclosed in double quotes.
D. Strings can be converted to other data types.
E. Both C and D
7. What does the replace() method do when applied to a string?
A. It removes all whitespace from the string.
B. It replaces all occurrences of one substring with another.
C. It reverses the characters in the string.
D. It raises an error since strings are immutable.
CS303E Week 8 Worksheet: More on Strings 3

8. If you want to traverse through a string S using negative indices in Python, which
loop should you use?
A. for char in S
B. for i in range(len(S))
C. for i in range(-len(S), 0, -1)
D. for i in range(-len(S), 0)
E. for i in range(-1, -len(S)-1, -1)
F. C, D, and E
G. C and E
9. If s is a string, which expression will always evaluate to True?
A. chr(ord(s))) == s
B. s.upper().lower() == s
C. s[0:len(s)] == s
D. All of the above

2 3 4 5 6 7 8 9

Page total: /16


CS303E Week 8 Worksheet: More on Strings 4

The following 7 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
on the provided line.

10. (3 points)

s = "vpduwFrrnlh"
new = ""
for char in s:
new += chr(ord(char) - 3)
print(new)

11. (3 points)

s = "SillyGoose-781"
result = ""

for char in s:
if char in str(not s):
result += "B"
elif char.isalpha():
result += char.upper()
elif char.isdigit():
result += str(int(char) + 5)
else:
result += char

print(result)

12. (3 points)

s = "oompaLoompa"
result = (s[-len(s):-len(s)+5] + " " + s[-6:])

print(result)

Page total: /9
CS303E Week 8 Worksheet: More on Strings 5

13. (3 points)
spongebob = "goofyG00ber"
patrick = "krus7ykr4b"
gary = "+25"

sandy = patrick.islower() and patrick[999:9999] == ""


squidward = gary.isalnum() and gary.isdigit()
mrKrabs = spongebob[6:8].isdigit() and spongebob.count("o") == 2

print(sandy, squidward, mrKrabs)

14. (3 points)
def catInTheHat(thing1, thing2):
thing1 = thing1 + thing2
thing2 = thing1
print(thing1 is thing2)

string1 = "greenEggs"
string2 = "Ham"
catInTheHat(string1, string2)
print(string1, string2)

15. (3 points)
myGuineaPig = "Jerry" # =)
print(myGuineaPig[0:1000] * 2)

16. (3 points)
alpha, beta = "castiel", "dean"
omega = beta[:2] + alpha[2:]
sigma = beta[:len(beta)] + alpha[:3]
print(min(omega, sigma), max(omega, sigma))

Page total: /12

You might also like