Programming and Problem Solving Through Python Language Theory Question

Conditional and Iterative Statements

  1. What is the purpose of an 'if' statement in Python ?
    1. To create a loop that iterates over a sequence.
    2. To define a function.
    3. To execute a block of code conditionally based on a boolean expression.
    4. To perform arithmetic calculations.
    Answer : C
    Explain :To execute a block of code conditionally based on a boolean expression. Like if this condition is true execute and else means if condtion is false then do this.
  2. Which statement is used to terminate the current loop iteration and move to the next iteration ?
    1. break
    2. continue
    3. exit
    4. return
    Answer : B
    Explain : With the continue statement we can stop the current iteration, and continue with next.
  3. What is the output of the following code ?
    x = 5
    if x > 3:
       	print("x is greater than 3")
    else:
       	print("x is not greater than 3")
    1. x is greater than 3
    2. x is not greater than 3
    3. x
    4. 5
    Answer : A
    Explain : x is greater than 3. So if X>3 is executed.
  4. Which statement is used to exit a loop prematurely?
    1. quit
    2. end
    3. continue
    4. break
    Answer : D
    Explain : Python's break statement allows you to exit the nearest enclosing while or for loop. Often you'll break out of a loop based on a particular condition.
  5. What is the output of the following code ?
    for i in range(3):
       print(i)
    1. 0 1 2
    2. 0 1 2 3
    3. 0 1
    4. 0 1 1
    Answer : A
    Explain : In this range is 0 to 3 , 3 is stop value. It print 0 1 2 after that it stops.
  6. What is the purpose of the 'range()'' function in a 'for' loop ?
    1. To generate a sequence of numbers.
    2. To define the number of iterations.
    3. To create a list.
    4. To skip iterations.
    Answer : A
    Explain : range() is used to generate a sequence of given number in it.
  7. Which of following is not a jump statement ?
    1. break
    2. continue
    3. while
    4. None of the above
    Answer : C
    Explain : Jump statements in Python are used to control the flow of a program by altering the normal sequence of execution. Python has two primary jump statements: 'break' and 'continue'.
  8. What value does the following expression evaluate to ?
    x=5
    while x<10:
        print(x, end="")
    
    
    1. Closed Loop
    2. One time loop
    3. Infinite loop
    4. Evergreen loop
    Answer : C
    Explain : This is infinite loop because the value of x=5 remain same.
  9. The combination of operators, operands, and constants is known as ?
    1. Identifier
    2. Variable
    3. function
    4. Expression
    Answer : D
    Explain : In computer programming, an expression is a combination of values (operands) and operators that can be evaluated to produce a result.
  10. Which python function returns unique nunber assign to an object ?
    1. id()
    2. fun()
    3. Valu()
    4. unq()
    Answer : A
    Explain : In Python, the 'id()'' function returns a unique integer identifier (often referred to as "object identity") that is assigned to an object.
  11. Which operator is used to check if two variables refer to the same object in memory.
    1. in
    2. ==
    3. if
    4. is
    Answer : D
    Explain : In Python, the 'is' operator is used to check if two variables refer to the same object in memory. It compares the memory addresses of the objects rather than their values.
  12. Which programming construct is used to repeat a block of code a specific number of times?
    1. Function
    2. If statement
    3. Loop
    4. Class
    Answer : C
    Explain : Loop is defined as a block of instruction repeated for a certain numbers of times.
  13. How can you create an infinite loop in Python ?
    1. for i in range(0, 10):
    2. while True:
    3. if condition:
    4. continue
    Answer : B
    Explain : While condition true loop run.So this make loop infinite loop.
  14. A Loop inside another Loop is known as ?
    1. For loop
    2. while
    3. do while
    4. nested loop
    Answer : D
    Explain : A loop inside another loop is known as a "nested loop." In programming, a nested loop is a loop that is placed inside the body of another loop.
  15. A function is a group of related statements designed specifically to perform ?
    1. Write code
    2. Specific task
    3. create executable file
    4. None of the above
    Answer: B
    Explain : A function is a group of related statements designed specifically to perform a specific task or action.
  16. Which of the following is not used as loop in Python ?
    1. for loop
    2. while loop
    3. do while loop
    4. None of above
    Answer: C
    Explain : Python doesn't have a do-while loop construct. Do while loop is in java or c++ language.
  17. What will be output of following code ?
    n=7
    c=0
    while(n):
        if(n>5):
            c=c+n-1
            n=n-1
        else:
            break
    print(n)
    print(c)		
    				
    1. 5,11
    2. 0,0
    3. infinity loop
    4. 7,0
    Answer: A
    Explain :
    while(7):
    	if(n>5): true
    		c= c+n-1 = 0+7-1 = 6
    		n= n-1 = 7-1 = 6
    		 now n= 6 value goes to while.
    while(6):
    	if(n>5): true
    		c= c+n-1 = 6+6-1 = 11
    		n= n-1 = 6-1 = 5
    		now n= 5 value goes to while.
    while(5)
    	else: 
    		break
    		it will be stop.
    print latest value of n and c which is 5 , 11
    
  18. What will be the output of following code ?
    str1="desktop"
    c=0
    for x in str1:
       if(x!="o"):
           c=c+1
       else:
           pass
    print(c)
    
    1. 0
    2. 7
    3. desktop
    4. 6
    Answer: D
    Explain :
    for x in str1 : means x in 'desktop' - it is range for x
    if (x!="o") which is true x = 'd'
        c=0+1 =1
     Now x='e'
      c=1+1 = 2
     when x='0'
      else condtion is use which is pass it simply pass to next
     Now, x="p"
       c= 5+1 = 6
    range of x complete
    print (c) which is c=6
    
  19. Which of the following Python code will give different output from the others ?
    1. for i in range(0,3):
       print(i)
    2. for j in [0,1,2]:
       print(j)
    3. for k in [0,1,2,3]:
       print(k)
    4. for l in range(0,3,1):
       print(l)
    Answer: C
    Explain : You can solve this yourself.
  20. What will be the output of following code ?
    a=0
    b=1
    while(b<8):
    	print(b)
    	a,b=b,a+b
    
    1. 0 1 2 3 4 5 6 7
    2. 0 2 4 5 6
    3. 1 1 2 3 5
    4. 1 1 2 3 5 8
    Answer: C
    Explain : The loop will start at 8. It does not print 8.

Next Set

2