Python Programming Quiz

Quiz on Conditional Statements in Python

Time left: 300 seconds

Q.1. What is the output of the following code?
          
            print(1, end=" ")
            if(False == False):
                print(2, end=" ")
            print(3,end=" ")
          
          

Select Your Option

As False is equal to False, the if condition is true. Hence, the output is 1 2 3.

Q.2. Fill in the correct operators in the blank place.
          
            x = 10
            if x __ 0:
               print("Value is greater than 0.")
            elif x __ 0:
               print("Value is less than 0.")
            else:
               print("Value is Zero.")
          
          

Select Your Option

Referring to the respective print statements, 1st blank should be filled with greater than (>) and the 2nd blank should be filled with (<).< /p>

Q.3. In a Python program, a control structure:

Select Your Option

Control structures determine which statements in the program will be executed and in what order, allowing for statements to be skipped over or executed repeatedly. if, if/else, and if/elif/else statements are all examples of control structures that allow for statements to be skipped over or executed conditionally.

Q.4. Which statement will check if a is equal to b?

Select Your Option

if a == b: statement will check if a is equal to b. So, option B is correct.

Q.5. Which of the following represents the correct order for keywords?

Select Your Option

In case of IF statement, the keyword IF is followed by the condition and then the keyword THEN. After this any other condition is entered by using ELSIF keyword and all the other exceptions are handled by using ELSE keyword. So, the correct order is shown in option a which is IF, THEN, ELSIF, THEN, ELSE.

Q.6. Given the nested if-else structure below, what will be the value of x after code execution completes
          
            x = 0
            a = 0
            b = -5
            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)
          
          

Select Your Option

Here the outer if condition fails because a is equal to 0 not more than 0. Hence, outer else statement is executed.

Q.7. What is the output of the following if statement -
          
            a, b = 12, 5
            if a + b:
                print('True')
            else:
              print('False')
          
          

Select Your Option

In given snippet of code, the if condition, a+b gives a defined value, implying that the if condition is satisfied. Hence Python executes the if condition.

Q.8. What keyword would you use to add an alternative condition to an if statement?

Select Your Option

'elif' is used when the first condition isnt true but there are more different conditions to be checked.

Q.9. What signifies the end of a statement block or suite in Python?

Select Your Option

In Python, blocks are defined by indentation in accordance with the off-side rule. When a statement occurs on a line which is indented less than the previous one, it indicates the end of a block.

Q.10. Consider the following code segment:
          
            a = int(input("Enter an integer: "))
            b = int(input("Enter an integer: "))
            if a <= 0:
                b = b +1
            else:
                a = a + 1
            if a > 0 and b > 0:
                print ("W")
            elif a > 0:
                print("X")
            if b > 0:
                print("Y")
            else:
                print("Z")
          
          
What letters will be printed if the user enters -1 for a and -1 for b?

Select Your Option

Here a=-1 and b=-1 for 1st Condition a<=0 hence b=-1+1=0. Then for next conditions all the condition are false so else condition is executed.

Q.11. What is output for:
          
            if 'bar' in {'foo': 1, 'bar': 2, 'baz': 3}:
                print(1,end=" ")
                print(2, end=" ")
                if 'a' in 'qux':
                    print(3, end=" ")
            print(4,end=" ")
          
          

Select Your Option

In given code snippet, the second if condition is false so 3 is not printed.

Q.12. Which one of the following is a valid Python if statement :

Select Your Option

If statment always ends with a colon (:). Hence, option A is correct.

Q.13. What is the possible output of the following code?
          
            d = {'a': 0, 'b': 1, 'c': 0}

            if d['a'] > 0:
                print('ok')
            elif d['b'] > 0:
                print('ok')
            elif d['c'] > 0:
                print('ok')
            elif d['d'] > 0:
                print('ok')
            else:
                print('not ok')
          
          

Select Your Option

d['d'] refers to an invalid key. But the expression in that elif clause is never evaluated. Once the elif d['b'] > 0 clause on line 5 is found to be True, the remaining elif clauses are skipped because of short-circuit evaluation.

Q.14. Can we write if/else into one line in python?

Select Your Option

Yes, we can write if/else in one line. For example: i = 5 if a > 7 else 0. Hence, option A is correct. 6 times.

Q.15. What will be output of this expression:
          
            print('p' + 'q' if '12'.isdigit() else 'r' + 's')
          
          

Select Your Option

If condition is true so pq will be the output. Hence, option A is correct.

Instructions:-

  1. This Quiz is based on Conditional Statements 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