Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

COMP1127

PRACTICE
QUESTIONS
COMP1127
Practice Questions

Multiple Choice Questions


Practice Question – MCQ 1

What arguments would you pass to filter_foo() to


generate the list of prime numbers from
[3,4,5,6,7,8,9,10,11,12]? Assume that you have
access to the predicate isPrime(), which returns True if the
argument is a prime number and False otherwise.
def filter_foo(p, foo_list):
return [x for x in foo_list if p(x)]
a. filter_foo(isPrime, [3,4,5,6,7,8,9,10,11,12])
b. filter_foo(lambda x : x%2 and x%x, \
[3,4,5,6,7,8,9,10,11,12])
c. filter_foo(isPrime(x), [3,4,5,6,7,8,9,10,11,12])
d. filter_foo(isPrime(x), \
map(isPrime,[3,4,5,6,7,8,9,10,11,12]))
Practice Question – MCQ 2

What would be the value of my_str or output of the last


statement if the following were entered in python

>>> my_str="cat"
>>> my_str[0]="b"

a. "bat"
b. "b"
c. "bcat"
d. Syntax Error
Practice Question – MCQ 3
Assume you have a function get_pos() which returns the position at
which a number should be inserted into a sorted list. If the number
already exists in the list, the function returns the position at which the
number is found.
def get_pos(el, lst):
if lst == []:
return 0
elif el == lst[0]:
return 0
elif el < lst[0]:
return get_pos(el,[])
else:
return ……

Identify the recursive call for the function get_pos()


a. 1 + get_pos(el,lst[1:])
b. 0 + get_pos(el,lst[1:])
c. 1 + get_pos(lst[1:])
d. 1 + get_pos(el,[])
Practice Question – MCQ 4
What would be the output of the last statement if the
following were entered in python

>>>lst=[3,5,2,6]
>>>lst=lst.sort()
>>>lst

a. [3,5,2,6]
b. [2,3,5,6]
c. Nothing
d. [6,5,3,2]
Practice Question – MCQ 5
A Tree Position ADT creates a tree position pstn as a tuple
that specified x and y co-ordinates (in that order). Which of
the following pairs of functions would be appropriate
selectors for each of the x and y co-ordinate values?

a. def xcord(pstn):return pstn[0],pstn[1]


def ycord(pstn):return pstn[1],pstn[0]
b. def xcord(pstn):return pstn[0]
def ycord(pstn):return pstn[1]
c. def xcord(ps,tn):return ps
def ycord(ps,tn):return tn
d. def xcord(pstn):return pstn[0:]
def ycord(pstn):return pstn[1:]
Practice Question – MCQ 6
Consider the following statement

marks=dict(zip(["CBE","GM","EKM"],[95,32,63]))

Which of the following could produce the list [63,95] as its


output?

a. list(filter((lambda x:x>50),marks))
b. list(filter((lambda x:x>50),marks[1]))
c. list(filter((lambda x:x>50),marks.values()))
d. list(filter((lambda x:x>50),marks.keys()))
Practice Question – MCQ 7
What does the following function accomplish given a
dictionary D, and value V?

def loopy(D,V):
for K in D:
if D[K]==V:
return K

a. returns the value V, associated with key K


b. sorts the dictionary by the K values
c. sorts the dictionary by K, then V values
d. returns the key K, associated with value V
Practice Question – MCQ 8
A higher order function rat_algebra has been defined as
following.
def rat_algebra(fn,rat1,rat2):
numer = fn(get_numer(rat1)*get_denom(rat2),\
get_numer(rat2)*get_denom(rat1))
denom = get_denom(rat1) * get_denom(rat2)
return make_rational(numer,denom)
r1 = make_rational(1,2)
r2 = make_rational(1,3)

Which of the following python statements will subtract the


two rational numbers r1 and r2?
a. rat_algebra(r1-r2, r1, r2)
b. rat_algebra(lambda : r1-r2, r1, r2)
c. rat_algebra(lambda x, y: x-y, r1, r2)
d. rat_algebra(lambda : x-y, r1,r2)
Practice Question – MCQ 9
What is the output of the function g when called with the
value 4 ?
def g(n):
v=1
f=1
while v <= n:
f *= v
v += 1
return f

a. f
b. 6
c. 2
d. 24
Practice Question – MCQ 10
What would the output of the following statement.
>>>[x for x in [10,25,30,45] if x % 10 == 0 ]

a. [0, 5, 0, 5]
b. [10, 30]
c. [True False, True, False]
d. [10, 25, 30, 45]
COMP1127
Practice Questions

Short Answer Questions


Practice Question – Short Answer 1
In support of a system to help manage an electronics store,
the store manager has decided to acquire an ADT to manage
the information on personal computing devices (PCD) that are
sold.
Each PCD record contains the following: The PCD’s
1. Stock number
2. Description. This includes its make, model and type of form factor
3. Specifications. This includes its storage in GB, memory in GB and
processor in GHz

An example of such a PCD is a Motorola Xoom tablet with 32GB storage,


2GB of memory and a 1.2GHz processor. The representation of a PCD is
given below

('pcd',[sn,[make,model,form],[storage,memory,processor]])
Practice Question – Short Answer 1
As an example, pcd_list has been created with
three devices as shown,

pcd1=make_pcd(1001,'Motorola','Xoom','Tablet',32,2,1.2)
pcd2=make_pcd(1002,make_desc'HP','3115M','Netbook',320,
8,1.3)
pcd3=make_pcd(1003,'Sony','PSP','Console',.32,.32,.33)

>>> pcd2
('pcd',[1002,['HP','3115M','Netbook'],[320,8,1.3]])

>>> pcd_list=[pcd1,pcd2,pcd3]
Practice Question – Short Answer 1
The functions of the PCD ADT are as follows:
Name Type Description
make_desc(make,model,form) Const returns a list of the 3 string arguments
make_specs(strge,mem,proc) Const returns a list of the 3 floating point arguments
make_pcd(sn,make,model,for Const returns the representation of the PCD as the tagged tuple
m,strge,mem,proc) shown above given the PCD's information
is_pcd(pcd) Pred checks whether the given argument is a valid PCD. A PCD
is a tagged tuple where the tag is “pcd”.
get_sn(pcd) Sel returns the stock number of a PCD
get_desc(pcd) Sel returns the list of make, model and form of a PCD
get_specs(pcd) Sel returns the list of storage, memory and processor of a PCD

get_make(pcd) Sel returns the make of a PCD


get_model(pcd) Sel returns the model of a PCD
get_form(pcd) Sel returns the form factor type of a PCD
get_storage(pcd) Sel returns the amount of storage of a PCD
get_memory(pcd) Sel returns the amount of memory of a PCD
get_proc(pcd) Sel returns the processor speed of a PCD
Practice Question – Short Answer 1
a.
Write the code for the following PCD ADT functions

(1) make_pcd()
(2) is_pcd()
(3) get_model()
(4) get_proc()

  [8 marks]
Practice Question – Short Answer 1

def make_pcd(sn,make,model,form,strge,mem,proc):

def is_pcd(pcd):

def get_model(pcd):

def get_proc(pcd):
Practice Question – Short Answer 1
Student Attempt
def make_pcd(sn,make,model,form,strge,mem,proc):

def is_pcd(pcd):

def get_model(pcd):

def get_proc(pcd):
Practice Question – Short Answer 1

def make_pcd(sn,make,model,form,strge,mem,proc):
    return ('pcd', [sn,[make,model,form], \
 [storage,memory,processor]])

def is_pcd(pcd):
    return type(pcd) == tuple and pcd[0] == 'pcd’ and
              type(pcd[1]) == list and len(pcd) == 2

def get_model(pcd):
    return pcd[1][1][1]

def get_proc(pcd):
    return pcd[1][2][2]
Practice Question – Short Answer 1
b.
Using the higher order function map, write a function
called list_all_makes() that accepts a list of
PCDs and returns all the makes of PCD that are
available.
e.g. >>> list_all_makes(pcd_list)
['Motorola','HP','Sony']
  [3 marks]
Practice Question – Short Answer 1

def list_all_makes(plist):

[6 marks]
Practice Question – Short Answer 1
Student Attempt
def list_all_makes(plist):

[6 marks]
Practice Question – Short Answer 1

def list_all_makes(plist):
    return list(map([1][2][1], plist))

def list_all_makes(plist):
    return list(map(lambda pcd: pcd[1][2][1], plist))

def list_all_makes(plist):
    return list(map(get_make, plist))
Practice Question – Short Answer 1
c.
Using the inbuilt append function, write a function called
insert_pcd that takes a list of PCDs and the information
for a new PCD and inserts the PCD into the list.
e.g.
>>> insert_pcd(pcd_list,1004,'Apple','iPad','Tablet',16,1.5,1.2)
>>> pcd_list
[('pcd', [1001, ['Motorola', 'Xoom', 'Tablet'], [32, 2, 1.2]]),

('pcd', [1002, ['HP', '3115M', 'Netbook'], [320, 8, 1.3]]),


('pcd', [1003, ['Sony', 'PSP', 'Console'], [0.32, 0.32, 0.33]]),
('pcd', [1004, ['Apple', 'iPad', 'Tablet'], [16, 1.5, 1.2]])]
  [3 marks]
Practice Question – Short Answer 1

def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
    

[6 marks]
Practice Question – Short Answer 1
Student Attempt
def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
    

[6 marks]
Practice Question – Short Answer 1

def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
    pcd = ('pcd', [sn,[make,model,form], \
 [strge,mem,processor]])

    pcd = make_pcd(sn,make,model,form,strge,mem,proc)

    pcd_list.insert(-1, pcd)

    pcd_list.append(pcd)
Practice Question – Short Answer 1

def insert_pcd(pcd_list,sn,make,model,form, \
strge,mem,proc):
    pcd = make_pcd(sn,make,model,form,strge,mem,proc)

    pcd_list.append(pcd)

[6 marks]
Practice Question – Short Answer 1
d.
Using the higher order functions filter and map, write a
function called min_mem that accepts a list of PCDs and
returns the type of form factor for those devices in the list
with an amount of memory that is greater than or equal to
the amount specified. You do not need to consider the
possibility of multiple devices with the same form factor that
satisfy the requirements. (Hint: you will also need to write a
helper function that selects the PCDs that satisfy the
memory requirement).
e.g. >>> min_mem(pcd_list, min)
['Tablet','Netbook'] [6 marks]
Practice Question – Short Answer 1

def min_mem(pcd_list, min)

[6 marks]
Practice Question – Short Answer 1
Student Attempt
def min_mem(pcd_list, min)

[6 marks]
Practice Question – Short Answer 1

def min_mem(pcd_list, min)


    min_lst = filter(lambda pcd: get_memory(pcd) >= \
 min, pcd_list)
    result = list(map(get_form, min_lst))
    return result
COMP1127

PRACTICE
QUESTIONS

You might also like