Programming and Problem Solving Through Python Language Theory Question

Conditional and Iterative Statements - Set 2

  1. How many times loop run ?
    i=5
    while(i>1):
        i=i-1
    
    1. 2
    2. 5
    3. 4
    4. 6
    Answer: C
    Explain : The loop will run 4 times after that i=1 the loop will stop because the value should be >1.
  2. Output of following code ?
    for i in range(0,2,-1):
        print("Python")
    
    1. Pyhton
    2. Python Python
    3. No Output
    4. PythonPython
    Answer: C
    Explain :The code you provided will not produce any output. This is because the range() function does not generate any values when the start value is greater than or equal to the stop value, and a negative step is provided.
  3. Which one of the following is a valid Python if statement :
    1. if a>=15 :
    2. if (a >= 20)
    3. if (a =>5)
    4. if a >= 6
    Answer: A
    Explain : if statement is end with semi color (:).
  4. what condition is use an alternative of 'if' statement ?
    1. else if
    2. elseif
    3. elif
    4. None of the above
    Answer: C
    Explain : An alternative to the 'if' statement in Python is the elif (short for "else if") statement. The 'elif' statement allows you to check multiple conditions in a sequence, providing a more organized and readable way to handle multiple possible cases.
  5. Determine the output :
    for i in range(20,30,10) :
    j=i/2
    print(j)
    1. 10.0
    2. 10 15
    3. 10.0 15.0
    4. None of these
    Answer: A
    Explain : Only on time for loop run, start- 20 and stop value-30.
  6. To repeat a particular task, we use __________.
    1. Input
    2. Loop
    3. Output
    4. Condition
    Answer: B
    Explain : The loop is used to repeat a task number of times.
  7. __________ immediately terminates a loop entirely.
    1. break
    2. continue
    3. pass
    4. none of these
    Answer: A
    Explain : The Python break statement immediately terminates a loop entirely.
  8. What is the output of the following ?
    i = 2 
     while True: 
       if i%3 == 0: 
        break 
       print(i,end=" " ) 
       i += 2
    
    1. 2 4 6 8 10 ....
    2. 2 4
    3. 2 3
    4. error
    Answer: B
    Explain : While condition run 2 time when i value reach to 6, 6%3==0 which is true, it break.
  9. What is the output of the following ?
    for i in range(10): 
     if i == 5: 
     	break 
     else: 
     	print(i) 
    else: 
     print("Here") 
    
    1. 0 1 2 3 4 Here
    2. 0 1 2 3 4 5 Here
    3. 0 1 2 3 4
    4. 1 2 3 4 5
    Answer: A
    Explain : When the i value reach 5 it stop and at last print("Here").
  10. What will be the output of the following algorithm for a=5, b=8, c=6 ?
     Step 1: Start 
     Step 2: Declare variables a,b and c. 
     Step 3: Read variables a,b and c. 
     Step 4: If a > b 
     			If a > c 
     				Display a is the largest number. 
     			Else 
     				Display c is the largest number. 
     Else 
     	If b > c 
     		Display b is the largest number. 
     	Else 
     		Display c is the greatest number. 
     Step 5: Stop 
    
    1. b is the largest number
    2. c is the largest number
    3. a is the largest number
    4. stop
    Answer:
    Explain :
  11. What will following code segment print ?
     a = True 
     b = False 
     c = False 
     if a or b and c: 
     print "HELLO" 
     else: 
     print "hello" 
    
    1. HELLO
    2. Hello
    3. HellO
    4. None of these
    Answer: A
    Explain : Hierarchy order of logial operator is NOT>AND>OR. ( So if True or False and False = True or False = True , that why this statment print.)
  12. What will be the output after the following statements?
     a = 0 
     b = 3 
     while a + b < 8: 
     a += 1 
     print(a, end='')
     
    1. 0 1 2 3 4
    2. 1 2 3 4 5 6
    3. 1 2 3 4 5
    4. None of these
    Answer: C
  13. What will be the output of following code ?
    if 2 + 5 == 8: 
     	print("TRUE") 
    else: 
     	print("FALSE") 
    print("TRUE")
    
    1. TRUE
    2. False
    3. TRUE TRUE
    4. FALSE TRUE
    Answer: D
  14. What will be the output of the following?
    print(range(4))
    1. 0,1,2,3
    2. [0,1,2,3]
    3. range(0,4)
    4. (0,1,2,3)
    Answer: A
    Explain : Starting value 0 and stop value - 4.
  15. What will be the output of the following expression ?
     
     a = 2 
     b = 8 
     print(a | b) 
     print(a >> 1) 
    
    1. 10 0
    2. 10 2
    3. 2 2
    4. 10 1
    Answer: D
    Explain : Use or and right shift operator.
  16. What is the output of print(bool('False')) ?
    1. bool('False')
    2. True
    3. False
    4. 0
    Answer: B
    Explain : The value 'False' is treated as string value.
  17. elif can be considered to be abberviation of
    1. nested if
    2. if else
    3. else
    4. else if
    Answer: D
  18. Which of following play important role in Pyhton programming ?
    1. Statements
    2. Control
    3. Indentation
    4. Calculations
    Answer: C
    Explain : Indentation play an important role in python programming.
  19. Which statement is generally used as a placeholder ?
    1. Continue Statements
    2. Pass Statements
    3. Break Statements
    4. None of these
    Answer: B
    Explain : The pass statement is used as a placeholder for future code.
  20. Python operator always yields the result of
    1. Interger
    2. Foating point
    3. Complex
    4. None of these
    Answer: B
    Explain :In Python 3, the operator always yields a floating point result.

Next Set

1