CSCA08H3 Study Guide - Final Guide: Empty String, Init, Rob Ford

97 views10 pages
16 Oct 2018
School
Course
Professor
CSCA08_Final_2012F Solutions
Question 1
Solution
1.8
1
4
4
Error
Error
‘eep’
[8663, ‘B’, ‘C’]
Explanation
This is a normal float divide which returns a decimal as shown
This is an integer divide which truncates the decimal value of .8, leaving just the whole
number being 1
This prints the remainder when we divide 9 by 5, which is 1 and 4 remaining, hence the
answer is 4.
The element at the 3rd index of list <a>, remember lists are 0-indexed, is printed, which
results in 4.
The index 2 exceeds the bounds of the indices of list <b>, two elements means the last
index is referenced by n-1, which is 1.
Strings are immutable, thus we cannot change the value of <c[0]> to another value.
d[0] = [‘little’, ‘bo’, ‘peep’]
d[0][-1] = ‘peep’
d[0][-1][1:] = ‘eep’
Since lists are immutable and the indice 0 is within the range of indices for <e>, we can
set the element at the 0’th index of <e> to 8663, then print the resultant list.
Question 2
Solution
Squaring 5
Adding -25 25
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 10 pages and 3 million more documents.

Already have an account? Log in
Squaring 3
Adding 9 0
Rats 9
Explanation
square(5) is called in the call to add(), this thus prints out Squaring 5 and returns 25
The 25 is then sent with -25 to add(), which prints the adding statement, and returns 0
Since 0 !> 0, this if statement fails and we continue to the else statement. This calls
Square with 3 and returns 9
Awe then send 9 and 0 to add(), which results in the Adding 9 0 and returns 9.
‘Rats’ and 9 is then printed, resulting in the string shown
Question 3
Solution
DESCRIPTION
L
EXPECTED RETURN
VAL
ACTUAL RETURN
VAL
One Negative
Number
[-1,2,3]
[2,3]
[]
Explanation
The function will return the second it hits a negative number and not explore the rest of the list,
since we want to show this, we put the negative number at the front of the list, where in reality
we expect the latter 2 positive integers to be returned in a list, but instead we get an empty list
because the functions returns the second it hits a negative number.
Question 4
Solution
[1,2,3,5]
[1,2,3,5]
[1,2,3]
[1,2,3,5]
[1,2,3]
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 10 pages and 3 million more documents.

Already have an account? Log in
Explanation
<x1> is a copy of <x> in the sense that both variables point to the same list. This means that any
change to <x> will result in the same change occurring in <x1>. Consequently, <x2> is a list
copy of <x> meaning that it has the same elements as <x> at initialization, but these contents live
at a different memory address than that of <x>. Hence, <x2> and <y2> refer to the original lists,
being [1,2,3], whereas <x1> will be the same value of <x> after the modifications, being that
my_function(x,y) adds a 5 to the end of the lists.
Question 5
Solution
Condition
b1
b2
i
s
2
X
X
X
X
3
True
Any
0
Any
4
X
X
X
X
5
False
True
0
6
False
True
0
not “
Explanation
This isn’t possible to reach because the inner if-statement requires <i> to be less than 0,
but to reach this statement, <i> is required to be equal to 0, thus not possible.
If <b1> is true, the inner if-statement is true and we hit the condition, other than this, we
just make <b2> false, and <i> equal to 0, since <s> isn’t referenced, we don’t care about
it
This is not possible because if <b1> and <b2> are false, the (b1 or b2) requirement in the
outer if-statement is false, and we never get to reach Condition 4
In order to get to this statement, we need <b1> to be false, otherwise Condition 3 is met,
and make <b2> True as well as <i> to 0 in order to pass the outer if-statement. Then we
make the length of <s> to 0, which is the empty string in order to hit condition 5
In order to get to this statement, we need <b1> to be false, otherwise Condition 3 is met,
and make <b2> True as well as <i> to 0 in order to pass the outer if-statement. Then we
make the length of <s> to anything other than 0, which is not the empty string, otherwise
we hit condition 5.
Question 6 part a
Solution
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 10 pages and 3 million more documents.

Already have an account? Log in

Document Summary

This is a normal float divide which returns a decimal as shown. This is an integer divide which truncates the decimal value of . 8, leaving just the whole number being 1. This prints the remainder when we divide 9 by 5, which is 1 and 4 remaining, hence the answer is 4. The element at the 3rd index of list , remember lists are 0-indexed, is printed, which results in 4. The index 2 exceeds the bounds of the indices of list , two elements means the last index is referenced by n-1, which is 1. Strings are immutable, thus we cannot change the value of to another value. d[0] = [ little", bo", peep"] d[0][-1] = peep" d[0][-1][1:] = eep". Since lists are immutable and the indice 0 is within the range of indices for , we can set the element at the 0"th index of to 8663, then print the resultant list.