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

Test1 v1

csc108 term test 1 2023

Uploaded by

hadifabakhtiar
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)
112 views8 pages

Test1 v1

csc108 term test 1 2023

Uploaded by

hadifabakhtiar
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
You are on page 1/ 8

XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

UNIVERSITY OF TORONTO csc108h1-winter-2023-v1-midterm


Faculty of Arts & Science #999 1 of 8
Winter 2023 Term Test 1

CSC 108 H1S Do not turn this page until


you have received the signal to start.
Duration: 50 minutes In the meantime, write your name, student
number, UTORid and email address below
Aids Allowed: None
(please do this now!) and carefully read all
the information on the rest of this page.

First (Given) Name(s):

Last (Family) Name(s):

10-Digit Student Number: UTORid (e.g., pitfra12):

UToronto Email address (e.g., firstname.lastname@mail.utoronto.ca):

Marking Guide

No 1: / 2
No 2: / 2
• This test consists of 6 questions on 8 pages (including this one), printed on both
sides of the paper. When you receive the signal to start, please make sure that No 3: / 7
your copy of the test is complete.
No 4: / 3
• Answer each question directly on the test paper, in the space provided.
No 5: / 2
No 6: / 6

TOTAL: /22
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX Winter 2023 Midterm Test
csc108h1-winter-2023-v1-midterm CSC 108 H1S
#999 2 of 8 Duration: 50 minutes

Question 1. Python Expressions [2 marks]


Beside each expression in the table below, write the value produced when it is evaluated. If the code would
cause an error, write ERROR.
Expression Expression Value/ERROR

2 == 2 or 2/0 == 0

'ring ' * 2 + ', hello?'

False or (True and False)

'p' in 'APPLE'

Question 2. While Loops [2 marks]


Beside each code fragment in the table below, write what is printed when the code fragment is executed.

Code What is Printed

count = 10
while count > 0:
print("hello")
count = count - 5

s = 'bumbly mia'
i = len(s) - 1
n = 2
while i > 0:
print(s[i])
i = i - n
Winter 2023 Midterm Test XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
CSC 108 H1S csc108h1-winter-2023-v1-midterm
Duration: 50 minutes #999 3 of 8

Question 3. Functions and String Slicing [7 marks]


Uh oh. Bob got frustrated with the Function Design Recipe and chopped three functions into pieces, and also
renamed them so they all were the same. Unscramble them by matching the doctest examples and bodies to
the appropriate function.
def mangle_word(word: str, num_chars: int) -> str:
Function A: """Return a copy of word where num_chars characters have been moved
from the front of word to the end.
def mangle_word(word: str, num_chars: int) -> str:
Function B: """Return a copy of word where num_chars characters have been moved
from the end of the word to the front.
def mangle_word(word: str, num_chars: int) -> str:
"""Return a copy of word where num_chars leading characters
Function C:
(from the beginning) and num_chars trailing characters
(from the end) have been swapped.
Part (a) [3 marks]
Match the doctests to the appropriate functions by writing the appropriate letter (A, B, or C) next to each
doctest.
>>> mangle_word('CAT', 1)
'TAC'
>>> mangle_word('CAT', 1)
'ATC'
>>> mangle_word('CAT', 1)
'TCA'
>>> mangle_word('MONGOOSE', 3)
'OSEGOMON'
>>> mangle_word('MONGOOSE', 3)
'GOOSEMON'
>>> mangle_word('MONGOOSE', 3)
'OSEMONGO'

Part (b) [4 marks]


Match the function bodies to the appropriate functions by writing the appropriate letter (A, B, or C) next to
each function body.
Note that one of these choices does not match any function. Write an X next to that one.

return word[num_chars:] + word[:num_chars]

return word[num_chars / 2:] + word[:num_chars / 2]

return word[-1 * num_chars:] + word[:-1 * num_chars]

return word[-num_chars:] + word[num_chars:-num_chars] + word[:num_chars]


XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX Winter 2023 Midterm Test
csc108h1-winter-2023-v1-midterm CSC 108 H1S
#999 4 of 8 Duration: 50 minutes

Question 4. If Statements [3 marks]


For each of the functions below, write the value that k refers to in the boxes indicated. In each box, write
the value as it would appear at a breakpoint, before the line is executed, just like the debugger does. This is
demonstrated in the first box below which we have filled out for you.
If a breakpoint is not reached, write N/A in the box. (N/A means ‘not applicable’.)

1 def midterm_function_a ( ) :
2 " " " Used t o t e s t u n d e r s t a n d i n g
3 o f how i f s t a t e m e n t s a r e e x e c u t e d .
4 """
5 k = 'z '
6
7 i f k != ' e ' :
8 k = 'e ' Breakpoint before line 8: k ‘z’
9 else :
10 k = 'i ' Breakpoint before line 10: k
11
12 i f k == ' e ' :
13 k = '? ' Breakpoint before line 13: k
14
15 k = 'w ' Breakpoint before line 15: k
16
17 def midterm_function_b ( ) :
18 " " " Used t o t e s t u n d e r s t a n d i n g
19 o f how i f s t a t e m e n t s a r e e x e c u t e d .
20
21 This d o c s t r i n g doesn ' t d e s c r i b e
22 t h e r e t u r n v a l u e . Bad s t y l e !
23 : −)
24 """
25 k = 'z '
26
27 i f k == ' e ' :
28 return k Breakpoint before line 28: k
29 else :
30 k = 'i ' Breakpoint before line 30: k
31
32 k = 'w ' Breakpoint before line 32: k
Winter 2023 Midterm Test XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
CSC 108 H1S csc108h1-winter-2023-v1-midterm
Duration: 50 minutes #999 5 of 8

Question 5. Debugging / While Loops [2 marks]


The function below is an incorrect attempt to return True if a string s contains a digit. Your friend Bob
believes the function is correct because he tried calling the function with some string arguments which the code
did work correctly for. Answer the questions below.
def has_digit(s: str) -> bool:
"""Return True iff s contains a digit. Return False otherwise."""

index = 0
contains = False

while contains == False:

if s[index].isdigit():
contains = True

index += 1

return contains

Part (a) [0.5 MARKS]


Give an example of a valid argument for s where this function will return the correct expected value (according
to the docstring):

Part (b) [0.5 MARKS]


Give an example of a valid argument for s where this function will NOT return the correct expected value:

Part (c) [1 mark]


Briefly explain (in 1–2 sentences) why this function is incorrect (identify the issue in the code):
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX Winter 2023 Midterm Test
csc108h1-winter-2023-v1-midterm CSC 108 H1S
#999 6 of 8 Duration: 50 minutes

Question 6. Function Design Recipe / For Loops [6 marks]


Complete function get_non_digits below. Step 1 of the Function Design Recipe, example creation, has been
completed for you below. Based on this, complete steps 2, 3, and 4 of the Function Design Recipe:
• Step 2: fill in the function header (including the type contract) in the first box,
• Step 3: write a good description in the second box, and
• Step 4: write the body of the function in the third box. (Hint: use str.isdigit)

def get_non_digits(

"""

>>> get_non_digits('mia')
'mia'
>>> get_non_digits('123bumbly!!456')
'bumbly!!'
>>> get_non_digits('CSC108 is #1')
'CSC is #'
>>> get_non_digits('012345')
''
"""

new =

for :

return
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
UNIVERSITY OF TORONTO csc108h1-winter-2023-v1-midterm
Faculty of Arts & Science #999 7 of 8
Winter 2023 Term Test 1

Short Python function/method descriptions:


int(x: object) -> int
Convert x to an integer, if possible. A floating point argument will be truncated towards zero.
len(x: object) -> int
Return the length of list, tuple, or string x.
min(iterable: object) -> object
min(a, b, c, ...) -> object
With a single iterable argument, return its smallest item.
With two or more arguments, return the smallest argument.
print(values: object) -> None
Prints the values.
range([start: int], stop: int, [step: int]) -> list-like-object of int
Return the integers from start (inclusive) to stop (exclusive) with step
specifying the amount to increment (or decrement). If start is not specified,
the sequence starts at 0. If step is not specified, the values are incremented by 1.
str(x: object) -> str
Return an object converted to its string representation, if possible.
type(x: object) -> the object's type
Return the type of the object x.
str:
x in s -> bool
Produce True if and only if string x is in string s.
S.count(sub: str[, start: int[, end: int]]) -> int
Return the number of non-overlapping occurrences of substring sub in string S[start:end].
Optional arguments start and end are interpreted as in slice notation.
S.find(sub: str[,i: int]) -> int
Return the lowest index in S (starting at S[i], if i is given) where the
string sub is found or -1 if sub does not occur in S.
S.isalpha() -> bool
Return True if and only if all characters in S are alphabetic
and there is at least one character in S.
S.isalnum() -> bool
Return True if and only if all characters in S are alphanumeric
and there is at least one character is S.
S.isdigit() -> bool
Return True if and only if all characters in S are digits
and there is at least one character in S.
S.islower() -> bool
Return True if and only if all cased characters in S are lowercase
and there is at least one cased character in S.
S.isupper() -> bool
Return True if and only if all cased characters in S are uppercase
and there is at least one cased character in S.
S.lower() -> str
Return a copy of the string S converted to lowercase.
S.replace(old: str, new: str) -> str
Return a copy of string S with all occurrences of the string old replaced with the string new.
S.upper() -> str
Return a copy of the string S converted to uppercase.
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX Winter 2023 Midterm Test
csc108h1-winter-2023-v1-midterm CSC 108 H1S
#999 8 of 8 Duration: 50 minutes

[Use the space below for rough work. This page will not be marked unless you clearly indicate the part of your
work that you want us to mark.]

You might also like