Programming and Problem Solving Through Python Language Theory Question

Operators, Expressions and Python Statements

  1. In a+b, which is operand ?
    1. a & b
    2. +
    3. only a
    4. only b
    Answer : A
    Explain : a & b are operand where as + is operator.
  2. Which is the correct operator for power(xy)?
    1. x*y
    2. x**y
    3. x^y
    4. x/y
    Answer : B
    Explain : For power/exponent we use ** double asterisk.
  3. Which one of these is floor division?
    1. /
    2. //
    3. %
    4. *
    Answer : B
    Explain : For floor division we use //. Example divide -11/2 = 5.5 but for floor division we use 11//2 = 5
  4. What is the outuput of given code ?
    print(bool(0),bool(3.14159),bool(-3),bool(1.0+1j))
    1. True True False False
    2. True False True True
    3. False False False False
    4. False True True True
    Answer : D
    Explain : Any list, tuple, set, and dictionary are True, except empty ones- False. bool(0) is False and other bool value are true always.
  5. Solve 25 % 5 ?
    1. 0
    2. 1
    3. 5
    4. None
    Answer : A
    Explain : % - modulus gives remainder. So we got 0 remainder.
  6. Which one of the following has the same precedence level ?
    1. Division, Power, Multiplication, Addition and Subtraction
    2. Division and Multiplication
    3. Subtraction and Division
    4. Power and Division
    Answer : B
    Explain : Multiplication and Division are at the same precedence level. Similarly, “Addition and Subtraction” are at the same precedence level.
  7. Which operator has highest precedence ?
    1. ()
    2. /
    3. **
    4. %
    Answer : A
    Explain : Parentheses have highest precendence in python. Use PEMDAS to remeber precedence P- parenthese, E- exponent, M- multiplation, D- division, A- Addition, S-subtraction.
  8. what will be the output of following pseudocode, where ^ repersent XOR operator ?
    integer a, b, c
    set b=4, a=3 
    c=a^b
    print c
    
    1. 0
    2. 1
    3. 7
    4. 5
    Answer: C
    Explain : In this b=4 = 0110 and a=3=0011. Now solve with XOR it give = 0111 = 7.
  9. Operators with the same precedence are evaluated from ?
    1. Left to right
    2. right to left
    3. Anywhere
    4. None of these
    Answer : A
    Explain : Same precedence operator are evaluated from left to right .
  10. What is the output of this expression, 4*1**6?
    1. 4096
    2. 1296
    3. 0
    4. 4
    Answer : D
    Explain : As we known first exponent solve then power. 1**6 = 1 then 4*1 = 4.
  11. What is the return value of trunc() ?
    1. float
    2. complex
    3. integer
    4. string
    Answer : C
    Explain : trunc() return integer value. It just remove the no. which is after the decimal.
  12. Which of following is Identity operator ?
    1. is, is not
    2. in , not in
    3. and, or , not
    4. None of above
    Answer : A
    Explain : Identity operator in python are : is and is not. In, not in is membership operator.
  13. Solve ((5.00/5)+4.00/4) what we get when executed in python ?
    1. 2
    2. 2.00
    3. 2.0
    4. 2.000
    Answer :C
    Explain : When we solve this we get 2.0 . To remove decimal we use int() function.
  14. Which of the following operators has right-to-left associativity in Python ?
    1. %
    2. //
    3. **
    4. *
    Answer : C
    Explain : Exponent operator(**) has right-to-left associativity in Python.
  15. Which of the following is the truncation division operator ?
    1. /
    2. //
    3. %
    4. *
    Answer : B
    Explain : // ( flow division )is the operator for truncation division. It is called so because it returns only the integer part. For example: 22//3 = 1.
  16. What will be the output of following statement ?
    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 the above
    Answer : C
    Explain : First while condition check 0+3<8, true then a value change to a=1 and print (a) when the value reach to a=5 then print (a) but after that 5+3<8 condition false. Then while stop.
  17. What is the output of following code ?
    x=2
    if x<5:
    	print(x)
    else:
     	pass
    
    1. 2 3 4
    2. 1 2 3 4
    3. 2
    4. 5
    Answer : C
    Explain : It checks x<5 which is true 2<5, then print (x) = 2.
  18. What is output of int(2.39) ?
    1. 2
    2. 2.39
    3. 2.00
    4. 2.4
    Answer : A
    Explain : int() function gives only integer value. So it gives only 2.
  19. Which of the following is right shift operator ?
    1. >>
    2. <<
    3. |
    4. ~
    Answer : A
    Explain : Right shift operator denote as >>.
  20. What is the output of 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 : First time i value is 2, i%3 ( 2%3 = 2) then i value print but when i value is 6 ( 6%3 ==0) which is true then break condition active and break the loop.

Next Set

2 3