Ics104 Final v1 Key 211
Ics104 Final v1 Key 211
Name: ID:
Instructor and Section: Select one
Instructor Section
Mr. Mustafa AlTurki [ ] 01 (UT 9 – 9:50) [ ] 10 (UT 11 – 11:50)
Dr. Husni Al-Muhtaseb [ ] 02 (UT 10 – 10:50) [ ] 09 (UT 9 – 9:50)
Dr. Mohammed Amro [ ] 03 (MW 10 – 10:50) [ ] 04 (MW 11 – 11:50)
Dr. Samer Arafat [ ] 5 (UT 13 – 13:50) [ ] 6 (UT 14 – 14:50)
Dr. Mohamed Balah [ ] 7 (MW 8 – 8:50) [ ] 8 (MW 9 – 9:50)
Instructions:
1. Answer all questions. Make sure your answers are clear and readable.
2. The exam is closed book and closed notes. No calculators or any helping aides are allowed. Make
sure to turn off your mobile phone and keep it in your pocket.
3. If there is no space on the front of the page, use the back of the page. Indicate this clearly.
Part 1 : MCQ questions [ 40 x 1.5 = 60 points ]: (Make sure to bubble the correct
answer in the green sheet)
a) a = [1,2,3]
a[1]=5
a = [1,2,3]
b)
print(a[0])
a = (1,2,3)
c)
a[1]=5
a = {"k1":3,"k2":4}
d)
a["k3"]=5
e) None of the above
a) change_time
b) change_seat
c) __init__
d) printTicket
e) None of the above
4) Which of the following code segments add a new key, value pair to a dictionary named d?
a) d{"c":7}
b) d.add("c", 7)
c) d.append("c", 7)
d) d{"c"}=7
e) d["c"]=7
def modify(myList):
for i in range(len(myList)):
myList[i] = myList[i] + 1
a = [1,2,3]
modify(a)
print(a)
a) [2, 3, 4]
b) [1, 2, 3]
c) [2, 4, 6]
d) [1, 1, 1]
e) [1, 2, 3, 1]
a = [1, 2, 3, 10, 5, 8, 9]
i = len(a)//2
while i >=0:
if a[i] % 2 == 1:
print(a[i], end=" ")
i = i - 1
a) 1 3 5 9
b) 9 5 3 1
c) 3 1
d) 1 3
e) 5 3 1
Page 4 of 19
7) Assume the variable names is a list of strings, which of the following is the correct way to
transform the strings in that list to upper case?
a) for n in names:
n = n.upper()
for n in names:
b) n.upper()
for i in range(len(names)):
c) i = i.upper()
for i in range(len(names)):
d) names[i].upper()
for i in range(len(names)):
e) names[i] = names[i].upper()
8) Which of the following is the correct way to handle the exception raised by opening a file for
reading that does not exist?
f = open("hello.txt", "r")
a) if f == IOError:
print("Error")
f = open("hello.txt", "r")
b) except IOError:
print("Error")
try:
f = open("hello.txt", "r")
c) except IOError:
print("Error")
try open("hello.txt", "r")
d) except IOError
except IOError:
print("Error")
e) try:
f = open("hello.txt", "r")
9) In Python, what will happen if you open a file for writing that does not exist?
10) Assume no exception is raised when you run the following Python code fragment, what is its
output?
try:
f = open("output.txt", "w")
except IOError:
print("Something went wrong")
finally:
Page 5 of 19
print("cleaning up")
def f1():
a = 5
def main():
a = 4
print(a, end=" ")
f1()
print(a)
main()
a) 4 5
b) 4 4
c) 5 5
d) 5 4
e) None of the answers is correct
d = {"A": 1, "B": 2}
d["C"] = d["C"] + 1
print(d)
a) {"A": 1, "B": 2}
b) {"A": 1, "B": 2, "C": 1}
c) {"C": 1}
d) {"A": 2, "B": 3, "C": 1}
e) This code will generate an error
Page 7 of 19
x = [2, 3, 5, 7]
y = x
z = list(y)
Given the following statements:
1. x and y are references to the same list.
2. z and y are references to two independent lists.
3. z and x are references to two independent lists.
4. A total of two lists are created by the above program.
Choose the most suitable answer:
a) Only statement 1 is True.
b) Only statement 2 is True.
c) Only statement 3 is True.
d) Only statement 4 is True.
e) All four statements are true.
s = [1, 1, 1]
for i in range(3):
s[3] = s[i] * 2
print(s)
a) [1, 1, 1]
b) [2, 2, 2]
c) [1, 2, 1]
d) [1, 1, 1, 1, 1, 1]
e) This code will generate an error
a = [2, 2, 2]
b = [1, 1, 1]
c = a+b
print(c)
a) Ali Khalid
b) Ahmed Ali Khalid Rami
c) {"Ali": 6, "Khalid": 7}
d) 6 7
e) {"Ahmed": 3, "Ali": 6, "Khalid": 7, "Rami": 1}
21) What will be the output of the following code fragment if the file f.txt does not exist?
try:
print("Start", end =" ")
f = open("f.txt", "r")
print("Opened", end =" ")
except IOError:
print("Error")
a) Opened Error
b) Start Opened Error
c) Error
d) Start
e) Start Error
22) What will be the output of the following code fragment?
class Member:
def __init__(self, name, points=0):
self._name = name
self._points = points
def display(self):
print(self._name, self._points)
m1 = Member("Ali")
m2 = Member("Ahmed", 34)
m1.addPoints(4)
m2.addPoints(2)
m1.display()
m2.display()
a) Ali 4
Page 9 of 19
Ahmed 2
Ali 4
b) Ahmed 36
Ali 4
c) Ahmed 40
Ali 0
d) Ahmed 34
e) None of the other answers is correct.
23) The following Python code fragment is supposed to print out the string s in reverse order
'edcba'..
s = 'abcde'
t = ''
for ch in s:
t = XXX
print(t)
What goes in the place marked XXX?
a) s
b) t + ch
c) ch
d) ch + t
e) s + ch
24) Given the following function definition which returns two values
def func():
x = 1
y = 2
return (x, y)
which of the following code fragments calls the function and prints the second returned value?
a) print(y)
(x, y) = func()
y=func()
b)
print(y)
func(x,y)
c)
print(y)
(y,x) = func()
d)
print(x)
e) There is no correct answer.
25) Which of the following is the correct way of calling the constructor of the class Account to
create an object, assume the constructor takes no arguments?
c) obj = Account()
d) obj = Account. __init__(self)
e) obj = Account(self)
26) What will be the content of the file output.txt after running this code?
content = ["Intro","to","programming"]
f = open('output.txt', 'w')
for w in content:
f.write(w)
f.close()
a) Introtoprogramming
b) Intro to programming
Intro
c) to
programming
d) www
e) None of the other answers is correct.
x = [1, 2, 3]
x = x * 2
evens = []
for i in x:
if i % 2 == 0:
evens.append(i)
print(evens)
a) 2
b) [2, 4, 6]
c) [2]
d) [2, 2]
e) This code will generate an error.
28) Which of the following prints the indexes of the negative numbers in the list x?
For example if x=[ -4, 12, 5, -2], the program will print 0 3
for i in range(x):
a) if i<0:
print(i, end=" ")
for i in range(len(x)):
b) if i<0:
print(i, end=" ")
for i in x:
c) if i < 0:
print(i, end=" ")
Page 11 of 19
for i in range(len(x)):
d) if x[i] < 0:
print(i, end=" ")
e) None of the above
which of the following code segments will result in creating the following dictionary?
d = {}
for i in namesList:
a)
d[namesList[i]] = scoresList[i]
d = {}
b) for i in range(namesList):
d[namesList[i]] = scoresList[i]
c) d = dict(names, scores)
d = {}
d) for i in range(len(namesList)):
d[namesList[i]] = scoresList[i]
for i in range(len(namesList)):
e) d = {}
d[namesList[i]] = scoresList[i]
a) [4, 5, 6]
b) [1, 2, 3, 4]
c) Error: the 2 lists must have same size
d) [4, 5, 6, 1, 2, 3, 4]
e) None of the other answers is correct.
Page 12 of 19
list1 = [3, 5]
list2 = [4, 6]
list3 = []
for i in list1:
list3.append(i)
for j in list2:
list3.append(j)
print(list3)
a) [3, 4, 6, 5, 4, 6]
b) [3, 5, 4, 6]
c) [3, 3, 3, 5, 5, 5]
d) [3, 4, 4, 5, 4, 4]
e) [3, 5, 4, 6, 3, 5, 4, 6]
34) What will be printed after executing the following C code fragment:
int i;
for(i = 0; i<3 ;i++)
i++ ;
printf("%d", i);
A. 3
B. 4
C. 5
D. 2
E. 6
35) What will be printed after executing the following C code fragment:
int i=5 ;
i++ ;
printf("%d", i++);
A. 4
B. 5
C. 6
D. 7
E. 8
Page 14 of 19
36) What will be printed after executing the following C code fragment:
A. OUT OF RANGE
B. 9
C. 4
D. 0
E. -5
38) What will be printed after executing the following C code fragment:
int i, j, m = 0;
for(i = 0; i<3 ;i++)
for(j = 0; j<3 ;j++)
m=m+1;
printf("%d", m);
A. 4
B. 6
C. 16
D. 9
E. 12
Page 15 of 19
40) What will be printed after executing the following C code fragment:
int x=7;
if(x >= 5 )
if(x > 10)
if(x <15)
printf("AA");
else
printf("CC");
else if ( x >= 0)
printf("DD");
else
printf("BB");
else
printf("E");
A. AA
B. BB
C. CC
D. DD
E. E
Page 16 of 19
Part 2: 20 points
What is the output generated by the following C or Python code fragments? Write only what
appears on the screen in the output box shown on the right side.
// 2 pts
int val, sum, x;
val = 1;
0 1
do {
sum = 0; 1 pt each value
for (x = 0; x < val; x++) {
sum = sum + x;
}
val = val + 1;
printf("%d ", sum);
} while (val < 3);
// 2 pts
char c1 = 'z';
char c2 = 'a'; a
char *c3; z
c3 = &c2;
1 pt each value
printf("%c\n", *c3);
c3 = &c1;
printf("%c\n", *c3);
// 2 pts
#include <stdio.h>
void test2(int *a, int b);
5 7
int main(void) {
int a = 5, b = 10; 1 pt each value
test2(&b, a);
printf("%d %d\n", a, b);
return 0;
}
void test2(int *a, int b) {
*a = 7;
b = 8;
}
// 2 pts
int acc = 0, counter = 1;
while (counter <= 3) { 6 4
acc = acc + counter;
counter = counter + 1; 1 pt each value
}
printf("%d %d\n", acc,counter);
# 1.5 pts
Page 17 of 19
# 2 pts
for cnt1 in range(2) : 1 2
for cnt2 in range(1, 3) : 2 3
print(cnt1 + cnt2, end=" ")
print() 0.5 pt each value
Order must match
# 1 pt 50
for indx in range(50, 70, 10) : 60
print(indx)
0.5 pt each value
Order must match
# 2 pts
str1 = "Jeddah, Riyad, and Dhahran" True
print("ah" in str1) 2
print(str1.count("ah"))
1 pt each value
# 2 pts
a=0
b=3 a=4 b=5
while a != 5 and b != 5:
a = b 1 pt each value
b = b + 1
print(" a = " , a, " b = " , b)
# 2 pts 11
def f(i, j=2): 7
return i+j
print (f(f(4,3),4)) 1 pt each value
print(f(5))
In mathematics, the dot product is the sum of the products of the corresponding entries of the two
equal-length sequences of numbers. The formula to calculate dot product of two sequences of
numbers a =[a0, a1, a2, …, an-1] and b =[b0, b1, b2, …, bn-1] is defined as:
n −1
˙
product= ∑ ai × bi
i=0
Write a python program that calculates the dot product. The program should have the following user-
defined functions.
main(): function asks the user for file names that contain the two sequences of real values. The
function call readInput and dotProduct to read data from files and calculates the dot product
readInput(filename): This function receives filename and reads the sequences of real values
from the file, stores them in a list. Then, the function returns the list.
dotProduct(list1, list2): This function receives two lists of real values with same length.
It computes and returns their dot product. You are not allowed to print the dot product from this
function.
Note:
Make sure that your indentation is clear, otherwise you may lose points.
Your functions must be general and not specific to the given example.
It is not allowed to use global variables and any external library to calculate the dot
product.
Handle all exceptions when dealing with files.
Sample content of the input
The following are sample runs of the program:
files:
Sample run#1
Enter first file name : input1.txt input1.txt
Enter second file name: input3.txt 3.4
The two sequences numbers are not equal in length. -5.2
6
Sample run#2
Enter first file name : input4.txt input2.txt
Error: the input file: input4.txt is not found 2.5
1.6
Sample run#3 -2.9
Enter first file name : input1.txt
Enter second file name: input2.txt input3.txt
Dot product = -17.22 -1.5
1.8
Sample run#4
Enter first file name : input1.txt input4.txt file does not exist
Enter second file name: input4.txt
Error: the input file: input4.txt is not found
Page 19 of 19