XAVIER UNIVERSITY – ATENEO DE CAGAYAN
COLLEGE OF ENGINEERING
ESC 12 FINAL EXAMINATION
NAME: DATE:
I. Multiple Choice. Encircle the letter which corresponds to the best answer.
1. If you want to build a string that reads:
Peter's sister's name's "Anna"
Which of the following literals would you use?
A. "Peter's sister's name's \"Anna\""
B. 'Peter\'s sister\'s name\'s \"Anna\"'
C. "Peter's sister's name's "Anna""
D. 'Peter's sister's name's "Anna"'
2. The following expression
2 ** 3 ** 2 ** 1. Is
A. invalid C. equal to 16.0
B. equal to 16 D. equal to 512
3. What is the expected output of the following snippet?
i = 250
while len(str(i)) > 72:
i *= 2
else:
i //= 2
print(i)
A. 125 C. 72
B. 250 D. 500
4. What snippet would you insert in the line indicated below:
n=0
while n < 4:
n += 1
# insert your code here
to print the following string to the monitor after the loop finishes its execution:
1 2 3 4
A. print(n) C. print(n, end=" ")
B. print(n, sep=" ") D. print(n, " ")
5. What is the value type returned after executing the following snippet?
x=0
y=2
z = len("Python")
x=y>z
print(x)
A. int C. str
B. float D. bool
6. What will the final value of the Val variable be when the following snippet finishes its execution?
Val = 1
Val2 = 0
Val = Val ^ Val2
Val2 = Val ^ Val2
Val = Val ^ Val2
A. 0 C. 2
B. 1 D. 4
7. Which line can be used instead of the comment to cause the snippet to produce the following expected
output? (Select all that apply)
z, y, x = 2, 1, 0
x, z = z, y
y=y-z
# put line here
print(x, y, z)
Expected output:
0, 1, 2
A. x, y, z = y, z, x C. y, z, x = x, y, z
B. z, y, x = x, z, y D. The code is erroneous
8. What is the result of the following comparison?
x = "20"
y = "30"
print(x > y)
A. True C. None
B. False D. Runtime exception/error
9. What is the expected output of the following snippet?
s = "Hello, Python!"
print(s[-14:15])
A. Hello, Python! C. Hello, Python!Hello, Python!
B. !nohtyP ,olleH D. !nohtyP ,olleH!nohtyP ,olleH
10. What is the expected output of the following snippet?
lst = [i // i for i in range(0,4)]
sum = 0
for n in lst:
sum += n
print(sum)
A. 0 C. 7
B. 3 D. The program will cause a runtime exception
II. Programming Skills. (10pts)
1) In economics, the percentage rate of inflation for a period of time is calculated based on the final value F
of a commodity and the initial value I of the commodity, using the formula ((F −I)/I)×100. Write a Python
function inflation rate(initial, final) to compute and return the inflation rate given the initial and final values
of a commodity.
2) Using the function from #1, write a Python function average inflation rate(), that computes and
returns the average rate of inflation for the 3-year period represented in the table below:
Year Initial Value Final Value
1 23.50 24.00
2 24.00 24.25
3 24.25 24.38
The function is required to call the function from #