Topics:
o String and List
Program
1. Numeric difference(strings)
Consider the numeric value of a is 1, b is 2, c is 3,.... z is 26.
Then Numeric value of word "raghu" is r(18)+a(1)+g(7)+h(8)+u(21) = 55
Similarly numeric value of "college" is 3+15+12+12+5+7+5 = 59
Now, Numeric difference among two words "raghu" and "college" is 55-59 =
-4
Find the numeric difference among two words entered by user
Input Format
First line of input is the first word entered by user
Second line of input is the second word entered by user
Constraints
Assume both the strings will have only lower case alphabets
Output Format
display numeric difference
Sample Input 0
raghu
college
Sample Output 0
-4
Sample Input 1
rec
rei
Sample Output 1
-6
Test cases
raghu -4
college
rec -6
rei
hagsdkfigwieuy 29
kuhweiufv
iwgeif -19
hyvw
MCQs
1. Which of the following is True regarding loops in Python?
A. Loops should be ended with keyword "end".
B. No loop can be used to iterate through the elements of strings.
C. Keyword "break" can be used to bring control out of the current loop.
D. Keyword "continue" is used to continue with the remaining statements inside the
loop.
Ans: C
2. How many times will the loop run?
i=2
while(i>0):
i++
A. 2
B. 3
C. Infinite times
D. error
Ans: D
3. What will be output?
a=10
b=10
if a is b:
print("Yes")
else:
print("No")
A. No output
B. Error
C. Yes
D. No
Ans:C
4. What is the output of the following range() function?
for num in range(-5,-2,-1):
print(num, end=", ")
A. -5, -4, -3, -2
B. -5, -4, -3
C. Error
D. No output
Ans: D
5.
Given the nested if-else below, what will be the value x when the code executed
successfully
x = 0
a = 7
b = 6
if a > 0:
if b < 0:
x=x+5
elif a > 5:
x=x+4
else:
x=x+3
else:
x = x + 2
print(x)
A. 0
B. 4
C. 3
D. 2
Ans: B
6. Which of the following Python code will give different output from the others?
A. for i in range(-5,-2):
print(i)
B. for i in [-5,-4,-3]:
print(i)
C. for i in range(-5,-2,1):
print(i)
D. for i in range(-5,-2,-1):
print(i)
Ans: D
7. When does the else statement written after loop executes?
A. When break statement is executed in the loop
B. When loop condition becomes false
C. Else statement is always executed
D. None of the above
Ans: B
8. When you execute a break command inside the inner loop of a pair of nested
loops ...
A. ...both the inner loop and the outer loop are interrupted.
B. ... the inner loop is interrupted and the outer loop is not affected.
C. ... the outer loop is interrupted and the inner loop is not affected.
D. ... neither the inner loop, nor the outer loop are interrupted.
Ans: B
9. Given two variables, num1 and num2, which of the following would mean that both
num1 and num2 are positive integers?
A. (num1 = num2)
B. (num1 = num2) OR (num1 ≠ num2)
C. (num1 = num2) AND (num1<0)
D. (num1 = num2) AND (num2>0)
Ans: D
10. What will be the output of the following code?
x = 12
for i in x:
print(i)
A. 12
B. 1 2
C. Error
D. None of the above
Ans: C