Computer Programming
Faculty of Engineering
NUB
1. Which of the following expression results in a value 0?
A. 1%2 B. 25%3 C. 15%5 D. 37%6
2. Which of the following is a valid identifier?
A. 2A B. 8a+9 C. x_2 D. a+b
3. A Python line comment begins with ________.
A. \\ B. \* C. # D. %
4. Which of the following is the correct extension of the Python file?
A). python B) .pl print ((4 <= 5) or (4 >= 3)) C) .py D) .p
5. What is the output of >>>print ((4 <= 5) or (4 => 3))?
A) True B) Machine dependent C)Error D) False
6. What are Numeric Types of Data Types?
A. int B. float C. complex D. All of the above
7. In python, the function to display a specified message on the screen is ……... ?
A. run B. display C. output D. print
Q2: What will be the output after the following statements? (1.5x12 = 18 marks)
1) A, B = 10, 11 2) x = 5 3) z, y, x = 40, 20, 10
B, A = A, 7 while (x < 10): x, z, x = z, y, x+3
print (A, B) print(x) print(x, y, z)
x=x+3
4) x = “BUC2030” 5) x=10 6) x=60
y=5 y="BUCBUC" if(x%2==0):
p = ((len(x) < y) or (y > 10)) if((x>3) and (len(y)== 6)): print("even")
print(p) print(3*x) else:
else: print("odd")
print(10*len(y))
7) sum=0 8) for i in range (0,7,2): 9) s="Welcome In BUC"
for k in range (0,18,5): i=0 print(i) print (s [3])
while (i<7):
sum+=k print(i)
print ('sum=', sum) i+=2
10) y="BUC2030" 11) x=6 12) sum=0
k=0 y="BUCBUC" k=0
while(k < len(y)): if((len(y)== x): while (k < 10):
print(y[k]) print(3*x) sum+=k
k=k+2 else: print(k)
print(10*len(y)) x=x+3
print(“sum=”,sum)
Q5: (2 × 4 = 8 marks)
1) Write a PYTHON program that reads the number and check the number is positive or
negative? number = int(input("Enter a number: "))
if number > 0:
print("Positive number")
else:
Hanafy M. Ali- NUB-2024 print("Negative number")
(Q6) (Q8)
(Q5) num1 = float(input("Enter first number: ")) n = int(input("Enter The Number: "))
(Q2) number = int(input("Enter a number: ")) num2 = float(input("Enter second number: ")) factorial = 1
sum=0 if (number==0): if (num1 >= num2):
sum = 0
for x in range (0,101,2): print("The largest number is", num1) for x in range (1,n+1):
print("the number is zero") factorial = factorial * x
sum+=x else:
else:
print("The largest number is", num2) sum+=1 / factorial
print ("sum=", sum)
print("the number is not zero") print(f"The Result of the summation is: {sum}")
------------------------------
sum=0 2) Write a PYTHON program to print sum of even numbers from 0 to 100?
x=0
while (x < 100): 3) Write a PYTHON program that reads the number and check the number is positive or
sum+=x negative?
x=x+2
print(“sum=”,sum) 4) Write a PYTHON program to print sum of even numbers from 0 to 100?
5) Write a PYTHON program that reads a value of n and check the number is zero or non-
zero value.
6) Write a PYTHON program to find a largest of two numbers.
7) Write a PYTHON program that reads the number and check the no is positive or
negative.
8) Write a PYTHON program to sum the given sequence. 1 + 1/ 1! + 1/ 2! + 1/3! + …. +
1/n! (Q9)
n = int(input("Enter The Number: "))
9) Write a PYTHON program to print odd numbers up to n for i in range (1,n,2):
print(i)
Q1: Choose the correct answer of the following: (0.5 × 10 = 5 marks)
1) Which of the following is an invalid variable?
A) My_string B) st_string C) Foo+1 D) A1
2) A Python line comment begins with ________.
A) \\ B) \* C) # D) $$
3) A Python paragraph comment uses the style ________.
A) // comments // B) /* comments */ C) ''' comments ''' D) /'''
comments '''/
4) Which of the following expression results in a value 5?
A) 25%3 B) 25%8 C) 15%4 D) 25%10
5) Which of the following is an invalid statement?
A) abc = 1000 C) a b c = 1000 2000 3000
B) a,b,c = 1000, 2000, 3000 print ((4 != 3) or (4 <= 5)) D) a_b_c = 1000
6) What is the output of >>>print ((4! = 3) or (4 =< 5))?
A) True B) False C) Machine dependent D) Error
7) What is the output of >>>print ((4 =< 5) or (4>3))? print ((4 <= 5) or (4>3))
A) True B) Error C) Machine dependent D) False
8) What is the output of >>>print ((4 >= 3) or (3 == 3))?
A) True B) Error C) Machine dependent D) False
9) What is the output of >>>print ((4 < 3) and (4>6))?
A) False B) Error C) Machine dependent D) True
10) What is the output of >>>print ((22 < 12) or (22 == (2*10+2)))?
A) False B) Error C) Machine dependent D) True
Q2: What will be displayed by the following code? (1 × 5 = 5 marks)
The code The output
1 A, B = 5, 11
B, A = A, 7
print (A, B)
2 x=9
y="BUCBUC"
if((x<3) and (len(y)== x)):
print(3*x)
else:
Hanafy M. Ali- NUB-2024
print(10*len(y))
3 z, y, x = 5, 20, 10
x, z, x = z, y, x+3
print(x, y, z)
4 s="Python"
print(s[3])
5 y="BUC124"
k=1
while(k <= len(y)):
print(k)
k=k+2
Q2: What will be the output after the following statements? (2x9 = 18 marks)
1) x, y = 2, 6 2) x = 1 3) x = 3
x, y = y, x + 2 while x < 10: y=2
print x, y print(x) x += y
x=x+3 print(x)
4) x = 4 5) y = 5 6) x=41
y=5 z = len(“BUC”) if(x%2==0):
p = x < y or x < 10 x=y>z print("even")
print(p) print(x) else:
print("odd")
7) sum=0 8) for i in range (0,5,2): 9) s="Welcome In BUC"
for k in range (0,16,5): print(i) print (s [-1])
sum+=k
print ('sum=', sum)
Q1: Choose the correct answer of the following: (0.5 × 10 = 5 marks)
8) Which of the following is an invalid variable?
A) My_string B) st_string C) Foo D) A_1+10
9) A Python line comment begins with ________.
B) \\ B) \* C) # D) $$
10) A Python paragraph comment uses the style ________.
A) // comments // B) /* comments */ C) ''' comments ''' D) /'''
comments '''/
11) Which of the following expression results in a value 4?
B) 25%3 B) 25%8 C) 15%4 D) 25%10
12) Which of the following is an invalid statement?
C) abc = 1000 C) a b c = 1000 2000 3000
D) a,b,c = 1000, 2000, 3000 D) a_b_c = 1000
13) What is the output of >>>print ((4! = 3) or (4 < 5))?
A) True B) False C) Machine dependent D) Error
14) What is the output of >>>print ((4 <= 5) or (4>3))?
A) True B) Error C) Machine dependent D) False
8) What is the output of >>>print ((2 >= 3) and (3 == 3))?
Hanafy M. Ali- NUB-2024
A) True B) Error C) Machine dependent D) False
9) What is the output of >>>print ((4 < 10) and (4>3))?
A) False B) Error C) Machine dependent D) True
10) What is the output of >>>print ((22 > 12) and (22 == (10+10+2)))?
A) False B) Error C) Machine dependent D) True
Q2: What will be displayed by the following code? (1 × 5 = 5 marks)
The code The output
1 A, B = 10, 11
B, A = A, 7
print (A, B)
2 x=9
y="BUCBUCBUC"
if((x<3) and (len(y)== x)):
print(3*x)
else:
print(10*len(y))
3 z, y, x = 40, 20, 10
x, z, x = z, y, x+3
print(x, y, z)
4 s="Python"
print(s[-3])
5 y="Biotechnology"
k=1
while(k <= len(y)):
print(k)
k=k+3
Hanafy M. Ali- NUB-2024