Python Programming MCQ - Conditionals And Loops

13. What will be the output of given Python code?

n=7
c=0
while(n):
if(n>5):
c=c+n-1
n=n-1
else:
break
print(n)
print(c)

A. 2
B. 6 5 2
C. 3
D. 5 2

View Answer


14. What will be the output of given Python code?

str1="hello"
c=0
for x in str1:
   if(x!="l"):
       c=c+1
   else:
       pass
print(c)

A. 2
B. 0
C. 4
D. 3

View Answer


15. Which of the following Python code will give different output from the others?

A. for i in range(0,5):
    print(i)
B. for j in [0,1,2,3,4]:
    print(j)
C. for k in [0,1,2,3,4,5]:
    print(k)
D. for l in range(0,5,1):
    print(l)

View Answer


16. What will be the output of the following Python code?

str1="learn python"
str2=""
str3=""
for x in str1:
if(x=="r" or x=="n" or x=="p"):
str2+=x
pass
if(x=="r" or x=="e" or x=="a"):
str3+=x
print(str2,end=" ")
print(str3)

A. rnpn ea
B. rnpn ear
C. rnp ea
D. rnp ear

View Answer


17. What will be the output of the following Python code?

for i in range(0,2,-1):
print("Hello")

A. Hello
B. Hello Hello
C. No Output
D. Error

View Answer


18. Which of the following is a valid for loop in Python?

A. for(i=0; i < n; i++)
B. for i in range(0,5):
C. for i in range(0,5)
D. for i in range(5)

View Answer


19. Which of the following sequences would be generated bt the given line of code?

range (5, 0, -2)

A. 5 4 3 2 1 0 -1
B. 5 4 3 2 1 0
C. 5 3 1
D. None of the above

View Answer


20. A while loop in Python is used for what type of iteration?

A. indefinite
B. discriminant
C. definite
D. indeterminate

View Answer


21. 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

View Answer


22. What will be the output of the following code?

x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")

A. a b c d e f
B. abcdef
C. i i i i i.....
D. No Output

View Answer


23. What will be the output of the following code?

x = "abcd"
for i in range(len(x)):
print(i)

A. abcd
B. 0 1 2 3
C. 1 2 3 4
D. a b c d

View Answer


24. 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

View Answer







Also check :


Discussion


* You must be logged in to add comment.