0% found this document useful (0 votes)
24 views20 pages

20 Batch (2022)

ppr

Uploaded by

sakurasewwandi15
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)
24 views20 pages

20 Batch (2022)

ppr

Uploaded by

sakurasewwandi15
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/ 20

UNIVERSITY OF MORATUWA

Faculty of Engineering

Department of Computer Science and Engineering

B.Sc. Engineering

Semester 1 Examination (2020 Intake)

CS 1033 Programming Fundamentals

Time allowed: 3 Hours January 2022

Instructions to candidates

• The Answer Sheet for this exam is on the next page (overleaf). Remove this sheet from
the remainder before starting to answer the questions. Return only the Answer Sheet at
the end of the exam.
• Write your Registration Number clearly on top of the Answer Sheet.
• This is a closed-book examination.
• This paper consists of 20 pages, including this page.
• This paper consists of 90 multiple-choice, Yes/No and short answer questions; each
question is worth 1 mark.
o For multiple choice questions, select only one choice among the given choices.
Writing none or more than one choice or illegibly will result in a zero mark. There
is no penalty for wrong choices for multiple choice questions.
o For Yes/No questions, clearly write one choice – Yes or No. There is a -1 mark
penalty for wrong answers. If you do not know the answer, leave it blank, do not
guess the answer.
o For short answer questions, write your answer in the space provided in the answer
sheet. There is no penalty for wrong answers for short answer questions.

• This examination accounts for 90% of the course module assessment.


• Python version 3 is assumed in this examination (the same as during the semester).
• Note on number representations: In Python, hexadecimal, octal and binary numbers are
specified with the prefix ‘0x’, ‘0o’ and ‘0b’, respectively. For example, 1910 is
represented in hexadecimal, octal and binary as 0x13, 0o23 and 0b10011, respectively.

-----------------------------
CS1033-2020 Intake S1 (Jan 2022) Registration
Number

Answer Sheet
Write your answer clearly in the space provided for each question. For multiple-choice
questions, write your choice, (a), (b),… and for Yes-No questions, write either Yes or No.

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

17 18 19 20

21 22 23 24

25 26 27 28

29 30 31 32

33 34 35 36

37 38 39 40

41 42 43 44

45 46 47 48

49 50 51 52

53 54 55 56

57 58 59 60

61 62 63 64

65 66 67 68

69 70 71 72

73 74 75 76

77 78 79 80

81 82 83 84

85 86 87 88

89 90
-----------------------------

Page 2 of 20
CS1033-2020 Intake S1 (Jan 2022)

Consider the flowchart in Fig.


1 to answer the next three Start
questions.

The algorithm takes inputs as a Input list L, N


list L of positive integers and a
positive integer N, where min ← L[0]
N=length of L and N > 1. The max ← L[0]
starting index of L is 0. The x←1
algorithm is expected to output
the minimum and maximum
elements in L. Three blanks
(A), (B) and (C) are to be filled Yes
in. Is …. (A) ….?

min ← L[x]
No
1. For the algorithm to work
as expected, what should be
in blank (A) in Fig. 1?
Yes
Is max < L[x]?
2. For the algorithm to work
…. (B) ….
as expected, what should be No
in blank (B) in Fig. 1?

3. For the algorithm to work x←x+1


as expected, what should be
in blank (C) in Fig. 1?
Is …. (C) ....?
Yes
No
Output min, max

Stop Fig. 1

Page 3 of 20
CS1033-2020 Intake S1 (Jan 2022)

For each of the next three questions, find the most suitable word or words to fill the blank.

4. When we use flowcharts to express algorithms, the decision () symbol is used to
implement the selection control structure and the ________________ control structure.

5. When a program has bugs (run-time or logic errors) that are difficult to find, using a
software tool such as a __________ is generally the next step towards fixing the bugs.

6. If we can code a program directly using the language, then a translation


step that requires either a compiler or an interpreter will not be necessary before
execution.

7. Suppose there are two Python programs A and B to solve a problem. If program A is
much longer (has many more lines of code) than program B, then program A will always
take more time to execute than program B.
[Yes/No]

Consider the following algorithm to answer the next three questions.

The algorithm is expected to take as input two positive integers 𝑥 and 𝑛 and output 𝑥 𝑛 , that is
the 𝑛-th power of 𝑥. Note that the algorithm uses the following two properties:
(1). If 𝑛 is odd: 𝑥 𝑛 = 𝑥 ∙ (𝑥 𝑛⁄2 ) ∙ (𝑥 𝑛⁄2 )
(2). If 𝑛 is even: 𝑥 𝑛 = (𝑥 𝑛⁄2 ) ∙ (𝑥 𝑛⁄2 )

Two blanks (D) and (E) are to be filled in. Note that “*” is the multiplication operator.

1. Start
2. Input x and n
3. P ← 1
4. if n = 0 then go to Step 9
5. if ....(D).... then P ← P * x
6. x = x * x
7. .... (E) ....
8. Go to Step 4
9. Output P
10. Stop

8. In the above algorithm, what should be in the blank (D) for the algorithm to work as
expected?

9. In the above algorithm, what should be in the blank (E) for the algorithm to work as
expected?

Page 4 of 20
CS1033-2020 Intake S1 (Jan 2022)

10. Consider the following statements about the problem of computing 𝑥 𝑛 and the algorithm
given above.
I. A recursive algorithm can be developed based on the same two properties (1) and
(2) corresponding to the cases where 𝑛 is odd and even.
II. A simpler alternative algorithm can be developed for computing 𝑥 𝑛 using the
property 𝑥 𝑛 = 𝑥 ∙ (𝑥 𝑛−1 ).
III. The given algorithm will not work correctly if 𝑛 = 0.

Which of the above is/are correct?


(a) I only.
(b) II only.
(c) III only.
(d) I and II only.
(e) I, II and III – all are correct.

11. What will be the result when the following Python code is executed?
2 ** (True * 5)
(a) 1
(b) 32
(c) 10
(d) An error message

12. What will be the output of the following Python code?


print((3+2j) * int(2.45) ** len("UoM") - 0b011)
(a) 21+16j
(b) 3+2j
(c) -75+368j
(d) 1+0j

13. Consider the following Python code:


a = b = 1500
c = a
a = 2500
del a
Which of the following statements is/are correct after the above code is executed?
I. Two integer objects will remain.
II. The object whose value is 1500 has two names.
III. Using the variable name “a” after this will generate an error.

(a) I only.
(b) II only.
(c) III only.
(d) I and II only.
(e) I, II and III – all are correct.

Page 5 of 20
CS1033-2020 Intake S1 (Jan 2022)

14. Consider the following statements about objects in Python.


I. What operations can be performed on an object depends on its type.
II. The type of a mutable object can change.
III. There can be two objects having the same value but having no name.

Which of the above statements is/are correct?


(a) I only.
(b) II only.
(c) I and II only.
(d) III only.
(e) I and III only.

15. Consider the following Python code.


a = [5, 6, [1, "UoM", 2], "xyz"]
b = a + 3
c = a[3] * 3

Which of the following is correct about the above 3 lines of code?


(a) Code will be executed without a problem.
(b) The second line has an error; there is no other error.
(c) The third line has an error; there is no other error.
(d) The second and third lines have errors.

16. Consider the following Python code.


a = [5, 6, [1, "UoM", 2], "xyz"]
a[1] += 3
a[3][2] = "z"

Which of the following is correct about the above 3 lines of code?


(a) Code will be executed without a problem.
(b) The second line has an error; there is no other error.
(c) The third line has an error; there is no other error.
(d) The second and third lines have errors.

17. What will be the output of the following Python code?


a = [5, 6, [1, "UoM", 2], "xyz"]
b = 6
c = 'xy'
print(c in a and b in a or c in a[3])

18. Is the following statement correct?

“In Python, comparison operators have a lower precedence than logical operators.”
[Yes/No]

Page 6 of 20
CS1033-2020 Intake S1 (Jan 2022)

19. What will be the output of the following Python code?


print(0b01111011 & ~(0x91) | (0o114 >> 1))

20. What will be the output of the following Python code, if the input is “a b c d”?
instring = input('Enter your input: ')
s = instring.split()
print(s)

21. The objective of the following Python code is to read a file and display its contents.
with open('foo.txt', 'r') as fo:
s = fo.read()
print( "file contents:", s)

Consider the following statements about the above Python code:


I. The code does not have any syntax errors.
II. Code must be added to close the file.
III. An exception could occur at run-time.

Which of the above statements is/are correct about the given Python code?
(a) I only
(b) II only
(c) I and II only
(d) III only
(e) I and III only

22. What will be the output of the following Python code, if the input is ‘LPGas’ ?
x = len(input('Enter a word: '))
if (x >= 2):
k = x ** 2
if (x >= 4):
k = k + 100
if (x == 4):
k = k + 200
else:
k = k + 300
else:
k = k + 500
else:
k = x + 600
print(k)

(a) 410
(b) 316
(c) 425
(d) 525

23. What is the minimum length of the register (number of bits) that can be used to represent
-25310 in two’s complement?

Page 7 of 20
CS1033-2020 Intake S1 (Jan 2022)

24. An integer is represented in 8-bit two’s complement notation as 110110112. What is the
correct decimal value of this integer?
(a) -3710
(b) 21910
(c) 21810
(d) -3810

25. How will the number 0.00145698395210 be represented in a decimal floating-point


notation which has a 6-digit mantissa, the decimal point after the second digit from left
and 10 as the base for the exponent?

The IEEE single-precision format for floating-point representation uses 32 bits, which is
divided as follows:
sign → 1 bit
exponent → 8 bits
mantissa → 23 bits

Answer the next two (2) questions based on the IEEE-754 Single Precision format.

26. If the decimal number 17.875 is represented in this format, what is the exponent?

27. How many insignificant zero digits (trailing zeros) are there in the mantissa?

28. Which of the following statements is false regarding representation of non-numeric data?
(a) An ASCII code can be represented by a 7-bit binary number.
(b) Unicode provides a unique code for every character irrespective of the platform,
program, or the writing system.
(c) ASCII code can represent characters in any language or writing system on a given
platform.
(d) Unicode can use up to 32 bits to represent a given character.

29. What will be the output of the following Python code?


count = 10
total = 0
while (count < 12):
total = total + 1
count += 2
else:
total = total + 10
total = total + 10
print (total)

Page 8 of 20
CS1033-2020 Intake S1 (Jan 2022)

30. What will be the output of the following Python code?


total=0
for i in range(10):
total += 2
break
else:
total += 1
print(total)

31. What will be the output of the following Python code?


total = 0
for counter in range(0,101,4):
total += counter
print('total = ', total)

(a) Will print the sum of all even numbers between 0 and 100.
(b) Will print the sum of all odd numbers between 0 and 100.
(c) Will print the sum of all the even numbers between 0 and 102.
(d) None of the other answers are correct.

32. What will be the output of the following Python code?


total=0
for i in range(3):
for j in range(3):
if i == j:
continue
total+=i+j
print(total)

33. What will be the output of the following Python code?


for num in range(110,125):
for i in range(2,num):
if num%i == 0:
break
else:
print(num)

34. What will be the output of the following Python code?


i=3
while (i < 10) :
j=0
while j < i:
if i!=5:
j+=1
continue
print(j,end="")
j+=1
i+=1

35. How should we replace the line of CODE in the Code segment 2 to also produce the same
output as the Code segment 1?

Page 9 of 20
CS1033-2020 Intake S1 (Jan 2022)

# Code segment 1
number = 1
while number < 10 :
if number % 2 == 0:
print("The number ",number," is even")
number+=1

# Code segment 2
number = 1
while number < 10 :
while number % 2 == 0:
print("The number ", number," is even")
CODE # code to be replaced
number+=1
(a) exit
(b) pass
(c) break
(d) continue

36. What will be the output of the following Python code?


myList = ['a', 'b', 'c', 'd', 'e']
myList[-3] = 'x'
print (myList[1:-1])

37. What will be the output of the following Python code?


x = "University of Moratuwa"
print (x[-8] + x[11] + x[5] + 'a')

38. What will be the output of the following Python code?


c1=complex(-5,8)
c2=complex(8,-3)
print(c1+c2)
(a) (12+11j)
(b) (3+5j)
(c) (3+8j-3j)
(d) (-5,8j)+(8,-3j)

39. What will be the output of the following Python code?


a = 1234.56
b = a
a+=1
print(id(a)==id(b))

40. What will be the output of the following Python code?


def changeMe( p ):
p+=10
return
p=100
changeMe(p)
print(p)

Page 10 of 20
CS1033-2020 Intake S1 (Jan 2022)

(a) 10
(b) 100
(c) 110
(d) [10,100]

41. What will be the output of the following Python code?


def printme(a="", b = "Computer", c = "Science"):
print(a,b,c)
return
printme(b = "Data")
(a) Will print “Computer Science”.
(b) Will print “ Data Science”.
(c) Code will execute but will exit with an error.
(d) Code will not execute due to syntax error.

42. What will be the output of the following Python code?


def myFunc(b):
global a
a = 50
return b
myFunc(100)
print(a)

43. What will be the output of the following Python code?


def myMulti(a = 1, *b):
for p in b:
a *= p
return a
print (myMulti() * myMulti(2) * myMulti(3,4,5))

(a) Will print 120


(b) Will print 60
(c) Code will execute but will exit with an error.
(d) Code will not execute due to syntax error.

44. What will be the output of the following Python code?


print ("%x%x%x%x" % (1,14,10,13))

45. What will be printed as the output of the following Python code?
print("x%6.2fx"%(15))

(a) x 15.00x
(b) x15.00x
(c) x15x
(d) Code will not execute due to syntax error.

Page 11 of 20
CS1033-2020 Intake S1 (Jan 2022)

46. What will be the output of the following Python code?


def myFunct(a = 100, *b):
for p in b:
a += p
return a
print (myFunct(a=10,20,30,40))

(a) Will print 200


(b) Will print 100
(c) Code will execute but will exit with an error.
(d) Code will not execute due to syntax error.

47. What is correct about the “docstring” in a Python function?


(a) Without it you cannot write a function.
(b) It is used to return string arguments from the function to the main program.
(c) It is where we define all the string variables used in the function.
(d) It can be used by programmers to describe the functionality of the function.

48. What will be the output of the following Python code?


print('*'*2)

49. What will be the output of the following Python code?


a = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
print (a[0][1][1])

50. What will be the output of the following Python code?


a = 10
def myFunc(a, b):
a,b=b,a
return
b = 5
myFunc(a, b)
print(a - b)

Consider the following abstract data type Deque implemented in Python to provide the
behavior of a queue and stack simultaneously to answer the next three (3) questions.

Fill the three blanks (P), (Q), and (R) in the following Python code and write them as answers
to the next three (3) questions.

buffer = []
size = 0

def insert(data): # insert item to queue


global buffer
global size

buffer.append(data)
size += 1

Page 12 of 20
CS1033-2020 Intake S1 (Jan 2022)

def remove(): # remove item from queue


global buffer
global size

if(size > 0):


data = buffer[0]
buffer = _____(P)_____
size -= 1
return data
else:
return None

def push(data): # push item to stack


_____(Q)_____

def pop(): # pop item from stack


global buffer
global size

if(size > 0):


size -= 1
data = buffer[size]
buffer = _____(R)_____
return data
else:
return None

51. Write the appropriate code to fill the blank (P).

52. Write the appropriate code to fill the blank (Q).

53. Write the appropriate code to fill the blank (R).

Consider the following abstract data type Linked List implemented in Python to provide the
behavior of a dynamically growing list to answer the next three (3) questions.

Fill the three blanks (P), (Q) and (R) in the following Python code and write them as answers
to the next three (3) questions.

class Node:
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None

def insert(self,node): # insert new node at the end of list


if self.head == None:
_____(P)_____
else:
ptr = self.head
while(_____(Q)_____):
_____(R)_____
ptr.next = node

Page 13 of 20
CS1033-2020 Intake S1 (Jan 2022)

54. Write the appropriate code to fill the blank (P).

55. Write the appropriate code to fill the blank (Q).

56. Write the appropriate code to fill the blank (R).

Consider the following recursive Python function for finding the maximum value in an array
to answer the next three (3) questions. As an example, the function call func([13, 19,
11, 17],4) will return the value 19.

Fill the three blanks (P), (Q), and (R) in the following Python code and write them as answers
to the next three (3) questions.

def func(a,n):
# base case
if(n == _____(P)_____):
return a[0]
# recursive case
else:
x = _____(Q)_____
if(x > a[n - 1]):
return x
else:
return _____(R)_____

57. Write the appropriate code to fill the blank (P).

58. Write the appropriate code to fill the blank (Q).

59. Write the appropriate code to fill the blank (R).

Consider the following recursive Python function for computing the exponentiation ab to
answer the next three (3) questions. As an example, the function call func(2,10) will return
the value 1024.

Fill the three blanks (P), (Q), and (R) in the following Python code and write them as answers
to the next three (3) questions.

def func(a, b):


# base case
if (b == 0):
return _____(P)_____

# recursive case
if (b % 2 == 0):
return _____(Q)_____

return _____(R)_____

60. Write the appropriate code to fill the blank (P).

61. Write the appropriate code to fill the blank (Q).

62. Write the appropriate code to fill the blank (R).

Page 14 of 20
CS1033-2020 Intake S1 (Jan 2022)

Consider the following recursive Python function for converting a string to a number to
answer the next three (3) questions. As an example, the function call func("2021",0) will
return the integer value 2021.

Fill the three blanks (P), (Q), and (R) in the following Python code and write them as answers
to the next three (3) questions.

def func(s,n):

# base case
if len(s) == 1:
return _____(P)_____

# add the next value in the string to number


n = (ord(s[0:1]) - ord('0')) + _____(Q)_____

# recursive case
return _____(R)_____

63. Write the appropriate code to fill the blank (P).

64. Write the appropriate code to fill the blank (Q).

65. Write the appropriate code to fill the blank (R).

To answer the next four (4) questions, consider the following divide-and-conquer paradigm
based Python function for finding the total value of a binary tree in which each node stores a
value. As an example, for the binary tree shown in figure below, it will return the value 99.

Fill the four blanks (P), (Q), (R) and (S) in the following Python code and write them as
answers to the next four (4) questions.

class Node():

def __init__(self, value):


self.value = value
self.left = None
self.right = None

Page 15 of 20
CS1033-2020 Intake S1 (Jan 2022)

def getNodeValueTotal(root):

if (root == None):
return 0

total = root.value
# divide and conquer
if (_____(P)_____):
_____(Q)_____
if(_____(R)_____):
_____(S)_____
return total

66. Write the appropriate code to fill the blank (P).

67. Write the appropriate code to fill the blank (Q).

68. Write the appropriate code to fill the blank (R).

69. Write the appropriate code to fill the blank (S).

Consider the following hash table data structure implemented in Python that handles hash
collisions by linear probing to answer the next four (4) questions.

Fill the four blanks (P), (Q), (R) and (S) in the following Python code and write them as
answers to the next four (4) questions.

hash_table = [None] * 10

# compute the hash code to be between 0 and


# the length of the hash table
def hash_code(key):
return _____(P)_____

def insert(hash_table, key, value):


hash_key = hash_code(key)
if hash_table[hash_key] == None:
hash_table[hash_key] = value
else:
inserted = False
count = len(hash_table)
while((not inserted) and (count > 0)):
# update the hash_key to linearly probe for
# a free slot in the table
hash_key = (_____(Q)_____) % len(hash_table)
count -= 1
if _____(R)_____:
hash_table[hash_key] = value
inserted = True

if(not inserted):
print(value,'could not be inserted')

Page 16 of 20
CS1033-2020 Intake S1 (Jan 2022)

# add the ASCII values of the characters in the input string


# together to compute the key
def derive_key(str):
key = 0
for _____(S)_____:
key += ord(str[i])
return key

70. Write the appropriate code to fill the blank (P).

71. Write the appropriate code to fill the blank (Q).

72. Write the appropriate code to fill the blank (R).

73. Write the appropriate code to fill the blank (S).

74. Which of the following is not a module in the logical organization of a computer system?
(a) CPU
(b) Input devices
(c) System bus
(d) Cache memory

75. Which of the following is not part of an instruction cycle?


(a) Decode sub-cycle
(b) Opcode Fetch sub-cycle
(c) Operand fetch sub-cycle
(d) ALU sub-cycle

76. What would be the size of RAM installed in a desktop or notebook computer sold at
present?
(a) About 4 GB
(b) About 1 GB
(c) About 4 KB
(d) About 4 MB

77. Which of the following is not a type of operation normally handled by the ALU of a
Central Processing Unit?
(a) Addition and subtraction of numbers
(b) Converting between character values and their ASCII codes
(c) Comparing two numerical values
(d) Performing logical operations on binary-coded values

Page 17 of 20
CS1033-2020 Intake S1 (Jan 2022)

78. Which of the following is not a function of the Control Unit in a CPU?
(a) Decoding an instruction
(b) Decoding addresses in the Address bus
(c) Fetching instruction opcodes
(d) Activating ALU operations

79. Consider the following operations carried by the CPU when working with the main
memory of a computer
A. Send the address of the memory location through the address bus
B. Read data from the data bus
C. Activate the memory read control in the control bus

What will be the correct sequence of operation when the CPU is reading from memory?
(a) A, B, C
(b) A, C, B
(c) A, B only
(d) None of the above

80. Which of the following statements are false about the CPU's Program Counter
(Instruction Pointer) register?
(a) It maintains the address of the next instruction to be executed
(b) It is not used to store data
(c) It is directly connected ALU of the CPU
(d) Each time an instruction is fetched, the register is incremented

81. The Flags register of a CPU is used to


(a) To store the results of operations carried by the ALU
(b) To maintain the status of the last instruction fetch operation
(c) To maintain the status of the last operation carried by the ALU
(d) To store the address of the last instruction

82. Most CPU instructions would need one or more data values for the instruction to be
executed. Which part of the instruction cycle would load the data into the CPU?
(a) The Fetch sub-cycle
(b) The Decode sub-cycle
(c) The Operand fetch sub-cycle
(d) The Execute sub-cycle

Page 18 of 20
CS1033-2020 Intake S1 (Jan 2022)

83. Which of the following instruction opcodes would not require any data for execution?
(a) LOAD
(b) STORE
(c) NOP
(d) JUMP

Consider the following description to answer the next four (4) questions.

The illustration in the Figure below shows part of the CPU internal structure containing the
ALU, a few registers and interconnections between them for data transfer. Note that arrows
indicate the possible direction of data transfer through these connections. Based on the
diagram, indicate which of the following CPU operations would be possible or not possible.

Write whether it is “possible” or “not possible” for each operation below.

84. Adding the contents of Register A to itself.

85. Subtracting register B from Register C and storing the result in Register A.

86. Subtracting Register D from Register C and storing the result in Register A.

87. Adding Register A to Register C and storing the result in Register D.

Consider the following description to answer the next three (3) questions.

Table 1 below shows the number of clock cycles required by two CPUs to carry out its
internal operations (assume that 0 clock cycles are used for any operation not included in the
table).

Page 19 of 20
CS1033-2020 Intake S1 (Jan 2022)

CPU Alpha CPU Beta


Operation type
(# of cycles) (# of cycles)
Loading from memory to an internal register 6 3
Storing in memory from an internal register 8 4
Decoding an instruction 2 2
Copying data between two internal registers 2 2
ALU operations 4 8

The percentage of different types of instructions used in a particular machine language


program that process a batch of data records is illustrated in Table 2 below.

Percentage in the
Instruction type
program
Loading data to a register from a memory location following
40%
the instruction opcode
Storing data in memory location whose address is present
20%
immediately following the instruction opcode
Arithmetical and logical operations 10%
Copying data from one register to another 20%
No operations 10%

88. If CPU Alpha runs at 2.0 MHz and CPU Beta runs at 1.0 MHz clock speeds, then CPU
Alpha will be faster than CPU Beta in executing the program.
[Yes/No]

89. If both CPUs run at the same clock speed, then CPU Beta will be faster than CPU Alpha
in executing the program.

[Yes/No]

90. If CPU Beta runs at 1.0 MHz clock speed, what percentage of time will it spend on “No
Operation” instructions?

----------------------------- End of the Examination Paper -----------------------------

Page 20 of 20

You might also like