Python Programming Quiz

Quiz on Loops in Python

Time left: 300 seconds

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

Select Your Option

With indefinite iteration, the number of times the loop is executed isn't specified explicitly in advance. Rather, the designated block is executed repeatedly as long as some condition is met. A while loop in Python does the same.

Q.2. What is the output of following program?
          
            for i in range(10):
                if i%2==0:
                    print(i,end=" ")
          
          

Select Your Option

The for loop iterates i throught 0 to 9. All values of i which are divisible by 2 are then printed. Hence, the output is 0 2 4 6 8

Q.3. What is the output of following program?
          
            x = 5
            while x!=0:
                print(x,end="")
                x+=1
          
          

Select Your Option

The value of x will always not be equal to 0. Hence, the condition of the while is is true forever. This results in the system going into an infinite loop.

Q.4. What will be the total number of iterations for the following snippet of code?
          
            for i in range(3):
                for j in range(4):
                    for k in range(5):
                        #Code
          
          

Select Your Option

total number of iterations = 3 * 4 * 5 = 60

Q.5. What will be the value of n after code execution?
          
            n = 10
            for i in range(10):
                for j in range(2, 10, 1):
                    if n % 2 == 0:
                        continue
                        n += 1
                n+=1
            else:
                n+=1
          
          

Select Your Option

The innermost n+1 will never execute as it is given after the continue statement. The outer n+1 woll always work. Hence, the value of n will be incremented 10 times. Once, the for condition becomes false, the else statement will again increment n making it 21.

Q.6. What is the number of lines of elements in output?
          
            l1 = [1, 2]
            l2 = [3, 4]

            for x in l1:
              for y in l2:
                print(x,y)
          
          

Select Your Option

There are 4 combinations of outputs for l1 and l2.

Q.7. Which loop is not present in python?

Select Your Option

do..while statement was rejected because it doen't fit in the general format of indented block statement: indented block used by every other Python compound statement.

Q.8. A loop becomes infinite loop if a condition never becomes _____

Select Your Option

“A loop's segment is executed as long as the condition provided is True. If the condition is True forever, the loop becomes an infinite loop.

Q.9. How the body of the loops is identifiable in Python?

Select Your Option

The body of the loops are identifiable in Python by their indentation.

Q.10. What will be the total Number of iterations for the following code?
          
            for i in range(5):
	                break
               for j in range(3):
                   for k in range(2):
                    #Code
          
          

Select Your Option

In this code snippet, the break statement prevents the following loops from being executed. Hence, the total number of iterations for the give code will be 5.

Q.11. What is the output of following program?
          
            l = ["Car","Bike","Ship","Plane"]
            for i in l:
              print(i,end=" ")
          
          

Select Your Option

The i of the for loop iterates over each element of the list and prints them.

Q.12. Which type of loop can be used to perform the following iteration:
You choose a positive integer at random and then print the numbers
from 1 up to and including the selected integer

Select Your Option

Any type of loop can be used to perform the given iteration in Python.

Q.13. Which letter won't be printed?
          
            for c in 'Python':
                if c == 'h':
                    continue
                print('Character: ' + c)
          
          

Select Your Option

The character 'h' will not be printed because of the if condition which skips the iteration whenever Python encouters this character.

Q.14. Which letter won't be printed?
          
            nums = [6,-3,2,-8,-9]
            out = []
            for num in nums:
                if num < 0:
                    continue
                else:
                    out.append(num)
            print(out)
          
          

Select Your Option

The if statement skips all numbers below 0. Hence, only the numbers above or equal to zero will be appended to the out list and printed.

Q.15. What is the output of following code?
          
            c = "HelloWorld"

            for i in c:
              print(i,end=" ")
          
          

Select Your Option

The i of the for iterates over every character of the string and prints them separated by a space.

Instructions:-

  1. This Quiz is based on Loops in Python
  2. Each correct answer will carry 2 points
  3. Once an option is selected you cannot select any other, So choose wisely
  4. Do not refresh the page as it will reset the quiz
  5. Once the time is up the quiz will automatically get submitted with no of Question responded
  6. Your total score alongwith your name will be shown after submission of the Quiz
  7. Wish you ALL THE BEST 👍👍

START

Results:-

Hi




Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post