Skip to document
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Pratice Final 3

practice material for final exam
Course

Introduction to Computing I (ITI1120)

169 Documents
Students shared 169 documents in this course
Academic year: 2022/2023
Listed bookDigital Design
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of Ottawa

Comments

Please sign in or register to post comments.

Preview text

ITI1120 - Final Practice

Q1. What is the type for the parameters and the result in the following function

definition?

def test(x):

'''

Returns True if the value 0 is in x, and False otherwise

>>> test((1,2,0,4,5))

True

>>> test((1,5,6))

False

'''

res = False

for val in x:

if val == 0:

res = True

return res

Answers (choose one):

a) (str,str)->bool

b) (tuple)->bool

c) (tuple,int)->noneType

d) (str)-> int

Q2. What is the correct description of the following function?

def my_function(m):

'''(list) -> bool

Description:

>>> m = [[1,2,3],[4,5,6],[7,0,8]]

>>> my_function (m)

True

>>> m2 = [[0],[0,0,0]]

>>> my_function(m2)

False

'''

res = False

for rang_val in m:

for col_val in rang_val:

if col_val != 0:

res = True

break

return res

Answers (choose one):

a) Returns True if the matrix m (or list of lists) does not contain zeros

b) Returns True if the matrix m (or list of lists) contains at least one nonzero

element

c) Changes all elements in the matrix m (or list of lists) to zero

d) None of the above

Q3. How many operations are performed in the function, approximately, when n

becomes large?

def f1(n):

i = n

while i >= 1 :

i = i // 2

Answers (choose one):

a) O(log 2 n)

b) O(n)

c) O(n 2 )

d) O(n log 2 n)

Q4. How many operations are performed in the function, approximately, when n

becomes large?

def f2(n):

for i in range(n):

j = n

while j >= 1:

j = j // 2

Answers (choose one):

a) O(log 2 n)

b) O(n)

c) O(n 2 )

d) O(n log 2 n)

Q5. Give the value of fifi('ITI1120') after running the following program:

def fifi(ch):

if len(ch) <= 1:

return ch

return fifi(ch[1:]) + 'X'

Answers (choose one):

a) ITI112X

b) 0211ITX

c) 0XXXXXX

Q8. What does the following Python program print?

i=

while i < 2:

j = 0

while True:

print ("S", end="")

if j == 2 :

break

j = j+

print("A", end="")

i=i+

Answers (choose one):

a) SAAASAA

b) SSSASSSA

c) SASASA

d) None of the above answers

Q9. What does the following Python program print?

class Fany(object):

num = 0

def count(self):

if (Fany % 2 != 0):

print("Fany")

else:

print("Oops!")

Fany += 1

b = Fany()

b. count ()

b. count ()

c = Fany ()

c. count ()

Answers (choose one):

a) Oops!

Oops!

Oops!

b) Oops!

Fany

Oops!

c) Fany

Fany

Fany

a) None of the above answers

Q10. How many calls to the my_function function are made in the following program?

def my_function(n):

if n == 1:

return 1

else:

return n + my_function(n - 1)

print(my_function (4))

Answers (choose one):

a) 4

b) 3

c) 10

d) infinity

Q11. In the following Python function a line of code is missing.

def myFunction(x):

index = 0

# missing code

x[index] = x[index] * 2

index = index + 1

>>> a = [10, 20, 30]

>>> myFunction(a)

>>> print(a)

[20, 20, 60]

Which line should be the missing line?

Answers (choose one):

a) while index < len(x) and index % 2 == 1:

b) while index < len(x) and index % 2 == 0:

c) while index < len(x):

if index % 2 == 1:

d) while index < len(x):

if index % 2 == 0:

Answers (choose one):

a) [1, 3, 5]

b) [0, 2, 4]

c) [[0, 1], [2, 3], [4, 5]]

d) [[0, 1, 2, 3, 4, 5]

e) None of the above

Q15. What does the following Python program print?

class Pony:

def repr(self):

return "Pony"

class SousPony(Pony):

def repr(self):

return "SousPony"

p = Pony()

q = SousPony()

print(p)

print(q)

Answers (choose one):

a) Pony

SousPony

b) Pony

Pony

SousPony

c) SousPony

SousPony

f) None of the above

Q16. Considering the function prime seen in class which returns True if its parameter

n is prime, and False otherwise:

def prime(n):

'''(int)->bool

returns True if n is prime, and False otherwise

Precondition: n is a positive Integer

'''

if(n==1):

return False

for i in range(2,n):

if(n%i == 0):

return False

return True

What does the following Python other function do:

def other(n):

'''(int)->int

Precondition: n is a positive Integer

'''

x=n+

while not(prime(x)):

x=x+

return x

Answers (choose one):

a) Returns the smallest non-prime number strictly greater than n

b) Returns the smallest prime number strictly greater than n

c) Returns the smallest number strictly greater than n

d) None of the above

Q17. Consider the following luky function which takes a string of space-separated ch

words (assuming no punctuation or capitalization), and a word “target”:

def luky(ch, target):

words=ch(" ")

result =[]

for i in range(len(words)):

if words[i]== target:

result(i)

return result

what does the following call return?

luky("I am with my father my friend and my neighbor", "my")

Answers (choose one):

a) [3, 5, 8]

b) [11, 21, 35]

c) ["my", "my", "my"]

d) None of the above

Q21. What does the following Python program print?

class Pony:

def init(self):

print("Pony")

class SousPony(Pony):

def init(self):

super().init()

print("SousPony")

p = SousPony()

Answers (choose one):

a) Pony

SousPony

b) Pony

c) SousPony

e) None of the above

Q22. What does the following Python program print?

def coco(x):

lis=[]

i=

while i < len(x):

while (i<len(x)-1 and x[i] == x[i+1] ):

i=i+

lis(x[i])

i=i+

return lis

a=[2,3,3,6,6,6,8,0,0,1]

print(coco(a))

Answers (choose one):

a) [2,3,3,6,6,6,8,0,0,1]

b) [2,3,3,6,6,6,8,1]

c) [2, 3, 6, 8, 0, 1]

d) [2,3,6,6,8,0,1]

e) None of the above

Q23. What does the following Python program print?

def nana(x, y):

print(x-y)

return x

n = 2 * nana(10,10)

print(n)

Answers (choose one):

a) 0

b) 0 and 20

c) 20

d) 0 and gives an error

e) None of the above

Q24. What does the following Python program print?

a = [10, 20, 14]

b = a

a[1] = 0

print(b)

Answers (choose one):

a) [10, 20, 14]

b) [10, 0, 14]

c) [0, 20, 14]

d) None of the above

Q25. What does the following Python program print?

class Point(object):

def init(self, x=0, y=0):

self = x

self = y

def repr(self):

return 'Point('+str(self)+','+str(self)+')'

def foo(x,a):

a=

a=

x=x+

x = 0

p1=Point(1,2)

p = Point()

print(p)

Answers (choose one):

a) Point(0,0)

b) Point()

c) Gives an error

Was this document helpful?
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Pratice Final 3

Course: Introduction to Computing I (ITI1120)

169 Documents
Students shared 169 documents in this course
Was this document helpful?

This is a preview

Do you want full access? Go Premium and unlock all 13 pages
  • Access to all documents

  • Get Unlimited Downloads

  • Improve your grades

Upload

Share your documents to unlock

Already Premium?
ITI1120 - Final Practice
Q1. What is the type for the parameters and the result in the following function
definition?
def test(x):
'''
Returns True if the value 0 is in x, and False otherwise
>>> test((1,2,0,4,5))
True
>>> test((1,5,6))
False
'''
res = False
for val in x:
if val == 0:
res = True
return res
Answers (choose one):
a) (str,str)->bool
b) (tuple)->bool
c) (tuple,int)->noneType
d) (str)-> int
Q2. What is the correct description of the following function?
def my_function(m):
'''(list) -> bool
Description:
>>> m = [[1,2,3],[4,5,6],[7,0,8]]
>>> my_function (m)
True
>>> m2 = [[0],[0,0,0]]
>>> my_function(m2)
False
'''
res = False
for rang_val in m:
for col_val in rang_val:
if col_val != 0:
res = True
break
return res
Answers (choose one):
a) Returns True if the matrix m (or list of lists) does not contain zeros
b) Returns True if the matrix m (or list of lists) contains at least one nonzero
element

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.