Saturday, October 5

PYTHON PREVIOUS YEAR PAPER WITH SOLUTIONS

PYTHON PREVIOUS YEAR QUESTION PAPER

(For Solutions See Bottom!!)



Final Exam (3 hours and 1 attempt)

Question 1
Select the expression(s) that evaluate to a float value.
3 // 4
8 % 6
8.0 % 4
3 / 4

Question 2
Consider this code:
x = 2
y = 3 - x
x = 3
After the code above has been executed, what value does y refer to?

Question 3
Consider this code:
def f(y):
x = y * 3
return y + x 
What value is returned by a call on function f with argument 10?

Question 4
Consider this code:
start = 'L'
middle = 8
end = 'R'
Write an expression that evaluates to the string 'L8R' using only the 
variables start, middle,end, one call on function str, and string co ncatenation.
Do not use unnecessary parentheses: you need them for the function call, but 
nothing else.

Question 5
The docstring in this function is correct.
def swap_words(two_words):
'''(str) -> str
Precondition: two_words is a string containing two words separated by one 
space.
Return a new string where the words are in reverse order, again separated 
by
one space.
>>> swap_word('Hello World')
World Hello
'''
first = two_words[:two_words.find(' ')]
second = two_words[two_words.find(' ') + 1:]
print(second + ' ' + first)
The body of the function does not match the docstring. Select the reason(s) why.
The function should use a return statement after the call on function print.
The function should use a return statement instead of a call on function print.
Variables first and second refer to the wrong values.
An error occurs on the second slice.

Question 6
Consider this function:
def larger_of_smallest(L1, L2):
'''(list of int, list of int) -> int
Return the larger of the smallest value in L1 and the smallest value in 
L2.
Precondition: L1 and L2 are not empty.
>>> larger_of_smallest([1, 4, 0], [3, 2])
2
>>> larger_of_smallest([4], [9, 6, 3])
4
'''
return # CODE MISSING HERE
The expression for the return statement is missing. Write it below. Use only the 
parameters, one call on function max, and two calls on function min.
Do not use unnecessary parentheses: you need them for the function calls, but 
nothing else. Do not include the word return; just write the expression.

Question 7
Consider this function:
def same_length(L1, L2):
'''(list, list) -> bool
Return True if and only if L1 and L2 contain the same number of elements.
'''
if len(L1) == len(L2):
return True
else:
return False
The function works, but the if statement can be replaced with a 
single return statement:
return # CODE MISSING HERE
Write the missing expression. Use only the parameters, two calls on function len, 
and operator ==once.
Do not use unnecessary parentheses: you need them for the function calls, but 
nothing else. Do not include the word return; just write the expression.

Question 8
Consider these two functions; we provide only the headers, type contracts, and a 
precondition:
def burble(a, b):
'''(int, float) -> str'''
def snorgle(L):
'''(list of str) -> float 
Precondition: L has at least one element.'''
Below are code fragments that call these two functions in various ways. Select the 
code fragment(s) below that are valid according to the function headers and the type 
contracts.
burble(snorgle(['a', 'b', 'c']), 8 // 3)
burble(8 // 3, snorgle(['a', 'b', 'c']))
burble(len(burble(1, 2.2)), 3.3)
burble(burble(1, 1.0), 'b')

Question 9
Consider this code:
def reverse(s):
'''(str) -> str
Return the reverse of s.
>>> reverse('abc')
'cba'
>>> reverse('a')
'a'
>>> reverse('madam')
'madam' 
>>> reverse('1234!')
'!4321'
'''
result = ''
i = len(s) - 1
while i >= 0:
result = result + s[i]
i = # CODE MISSING HERE
return result
Write the missing expression. Do not use parentheses. Do not include "i =". Just 
write the missing expression.

Question 10
Consider this code:
def get_keys(L, d):
'''(list, dict) -> list
Return a new list containing all the items in L that are keys in d.
>>> get_keys([1, 2, 'a'], {'a': 3, 1: 2, 4: 'w'})
['a', 1]
'''
result = []
for # CODE MISSING HERE
if k in L:
result.append(k)
return result
Write the missing code for the first line of the for loop — everything after the word 
"for", up to and including the colon :.
Do not use any parentheses, and do not call any functions or methods.

Question 11
Consider this code:
def count_values_that_are_keys(d):
'''(dict) -> int
Return the number of values in d that are also keys in d.  
>>> count_values_that_are_keys({1: 2, 2: 3, 3: 3})
3
>>> count_values_that_are_keys({1: 1})
1
>>> count_values_that_are_keys({1: 2, 2: 3, 3: 0})
2
>>> count_values_that_are_keys({1: 2})
0
'''
result = 0
for k in d:
if # CODE MISSING HERE
result = result + 1
return result
Write the missing code for the if statement — everything after the word "if", up to 
and including the colon :.
Your answer should only use operator in, variables k and d, and indexing.
Do not use parentheses.
 
Question 12
Consider this function:
def double_values(collection):
for v in range(len(collection)):
collection[v] = collection[v] * 2
Strings, tuples, lists, and dictionaries can all be iterated over, and function len works 
with all of them.
Select the code fragment(s) below that run without error.
t = (1, 2, 3)
double_values(t)
L = [1, 2, 3]
double_values(L)
d = {0: 10, 1: 20, 2: 30}
double_values(d)
s = '123'
double_values(s)
d = {1: 10, 2: 20, 3: 30} 
double_values(d)

Question 13
The diagonal of a square nested list goes from the top-left to the bottom -right corner. 
For example, consider this square nested list:
[[1, 3, 5], [2, 4, 5], [4, 0, 8]]
That nested list represents this table, where the values on the diagonal are i n bold:
1  3 5
2  4  5
4 0  8
Consider this function:
def get_diagonal_and_non_diagonal(L):
'''(list of list of int) -> tuple of (list of int, list of int)
Return a tuple where the first item is a list of the values on the
diagonal of square nested list L and the second item is a list of the 
rest
of the values in L.
>>> get_diagonal_and_non_diagonal([[1, 3, 5], [2, 4, 5], [4, 0, 
8]])
([1, 4, 8], [3, 5, 2, 5, 4, 0])
'''
diagonal = [] 
non_diagonal = []
for row in range(len(L)):
for col in range(len(L)):
# CODE MISSING HERE
return (diagonal, non_diagonal)
Select the code fragment(s) that correctly complete this function.
if row == col:
diagonal.append(L[row][row])
else:
non_diagonal.append(L[row][col])
if row == col:
diagonal.append(L[row][col])
if row != col:
non_diagonal.append(L[row][col])
if row == col:
diagonal.append(L[row][col])
elif row != col:
non_diagonal.append(L[row][col])
if row == col: 
diagonal.append(L[row][col])
non_diagonal.append(L[row][col])

Question 14
Consider this code:
def add_to_letter_counts(d, s):
'''(dict of {str: int}, str) -> NoneType
d is a dictionary where the keys are single-letter strings and the values
are counts.
For each letter in s, add to that letter's count in d.
Precondition: all the letters in s are keys in d.
>>> letter_counts = {'i': 0, 'r': 5, 'e': 1}
>>> add_to_letter_counts(letter_counts, 'eerie')
>>> letter_counts
{'i': 1, 'r': 6, 'e': 4}
'''
for c in s:
# CODE MISSING HERE 
Write the missing assignment statement. Do not call any functions or methods. Do 
not use unnecessary parentheses.

FOR ANY QUERIES AND DETAILS PLEASE FILL THE CONTACT FORM ON RIGHT SIDE 

FOR ANSWERS TO THE EXAM : Visit Here

4 comments:

  1. Hi! the answers link doesn't work, could you please help me?

    ReplyDelete
  2. Answers for Q1 & Q2

    Question 1
    Select the expression(s) that evaluate to a float value.
    8.0 % 4
    3 / 4

    Question 2
    Consider this code:
    x = 2
    y = 3 - x
    x = 3
    After the code above has been executed, what value does y refer to?
    1

    ReplyDelete