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

Think Twice

Code Once

The Islamic University of Gaza


Engineering Faculty
Department of Computer Engineering
Fall 2017
LNGG 1003
Khaleel I. Shaheen

Introduction to Computers

Laboratory Manual

Quiz #2 Solution
The Islamic University of Gaza Student Name: _____________________
Engineering Faculty Student ID: ________________________
Department of Computer Engineering
Fall 2017
Introduction to Computers
LNGG 1003
Khaleel I. Shaheen

Quiz #2
Part I) Theoretical Questions.
Question 1) Choose the correct answer:
1. What is the result of evaluating 2 + 2 ** 3 / 2?
4 6 4.0 6.0
2. What is the value of i printed?
j = i = 1
i += j + j * 5
print("What is i?", i)
1 5 7 11
3. Which of the following functions return 4?
Int (3.4) Int (3.9) round (3.4) round (3.9)
4. What is the output of the following code?
x = 0
if x < 4:
x = x + 1

print("x is", x)
x is 0 x is 1 x is 2 x is 3
5. Suppose x = 1, y = -1, and z = 1. What will be displayed by the following statement?
if x > 0:
if y > 0:
print("x > 0 and y > 0")
elif z > 0:
print("x < 0 and z > 0")
x > 0 and y > 0 x < 0 and z > 0 x < 0 and z < 0 nothing displayed
6. Suppose income is 4001, what will be displayed by the following code?
if income > 3000:
print("Income is greater than 3000")
elif income > 4000:
print("Income is greater than 4000")
Income is greater Income is greater Income is greater Income is greater
than 3000 than 3000 followed than 4000 than 4000 followed
by Income is greater by Income is greater
than 4000 than 3000
7. What is the value of the following expression?
True or True and False
True False None Syntax Error
8. What is the output of the following code?
x = 0
while x < 4:
x = x + 1

print("x is", x)
x is 0 x is 2 x is 4 x is 5
9. Which of the following function returns a sequence 0, 1, 2, 3? (multiple)
range (0, 3) range (0, 4) range (3) range (4)
10. What is the output for y?
y = 0
for i in range(0, 10, 2):
y += i

print(y)
10 11 17 20
11. How many times is the print statement executed?
for i in range(10):
for j in range(10):
print(i * j)
10 20 100 200
12. What is sum after the following loop terminates?
sum = 0
item = 0
while item < 5:
item += 1
sum += item
if sum > 4: break

print(sum)
5 6 7 8
13. Consider the following incomplete code:
def f(number):
# Missing function body

print(f(5))
The missing function body should be ________.
return "number" print(number) print("number") return number
14. Given the following function:
def nPrint(message, n):
while n > 0:
print(message)
n -= 1
What will be displayed by the call nPrint('a', 4)?
aaaa aaa infinite loop Invalid function call
15. What will be displayed by the following code?
def f1(x = 1, y = 2):
return x + y, x - y

x, y = f1(y = 2, x = 1)
print(x, y)
1 3 -1 3 3 1 3 -1

Part II) Practical Questions: Solve the following questions in PyCharm on your laptop.
Question 2) Write a Python Program that checks whether a number (entered by user) is even
or odd.
n = input("Enter a number: ")
if n % 2 == 0:
print "Number is even"
else:
print "Number is odd"

Question 3) Write a program that computes the factorial of an integer entered by user.
n = input("Enter a number: ")
factorial = 1
for i in range(1, n + 1):
factorial *= i
print factorial
Question 4) Write a function that take two numbers, x and y, and returns the sum of numbers
from x to y.
def sum(x, y):
sum = 0
for i in range(x, y + 1):
sum += i
return sum

You might also like