Programming and Problem Solving Through Python Language Theory Question

Operators, Expressions and Python Statements- Set 3

  1. What will be the output of the following expression ?
     x = 4 
     print(x<<2) 
    
    1. 4
    2. 16
    3. 6
    4. 2
    Answer : B
    Explain : In this use left shit (convert 4 into binary 0100 which become 010000- convert it into value - 16.)
  2. What will be the output of the following expression ?
    print (7//2)
    print (-7//2)
    1. 3 -3
    2. 3 -4
    3. 4 -4
    4. 3 3
    Answer : B
    Explain :
  3. What will be the output of the following pseudocode, where ʌ represent XOR operation ?
    Integer a, b, c
    Set b = 4, a = 3
    c = a ^ b
    Print c
    1. 4
    2. 3
    3. 5
    4. 7
    Answer : D
    Explain : Solve this using XOR operator.
  4. What will be the output of the following ?
    Integer a, b
    Set a = 9, b = 5
    a = a mod (a - 3)
    b = b mod (b – 3)
    Print a + b
    1. 4
    2. 8
    3. 9
    4. 6
    Answer : A
    Explain : a=9 % 6 = 3 and b=5%2 = 1. print(a+b)=print(3+1)=4.
  5. What is the output of the following code ?
    print(bool(0), bool(3.14159), bool(23), bool(1.0+1j))
    1. True True False True
    2. False True False True
    3. False False False True
    4. False True True True
    Answer : D
    Explain : bool(0) value is False, and every - True.
  6. Which of the following operators has the highest precedence ?
    1. &
    2. *
    3. not
    4. +
    Answer : B
    Explain : The highest precedence in these - * (multiplication).
  7. Let us assume 4 is 100 in binary and 11 is 1011. What is the output of the following bitwise operators ?
    a = 4
    b = 11
    print(a | b)
    print(a >> 2)
    1. 15 or 1
    2. 14 or 1
    3. 16 or 2
    4. 17 or 2
    Answer : A
    Explain : Use Or and right shit operator.
  8. What will be the output of the following pseudo code, where ʌ represent XOR operation ?
    Integer a, b, c
    Set b = 5, a = 1
    c = a ^ b
    Print c
    1. 4
    2. 3
    3. 5
    4. 7
    Answer : A
    Explain : Use XOR operation, first convert into binary and then perform xor operation.
  9. What is the output of the following code ?
    a = 15
    b = 6
    print(a and b)
    print(a or b)
    1. True True
    2. False False
    3. 6 15
    4. 15 6
    Answer : C
    Explain : Use and and or operator (Do it yourself).
  10. What is the output of print((-3)** 2) ?
    1. -6
    2. -9
    3. 6
    4. 9
    Answer : D
    Explain : ** is used for exponent. So -3**2=9.
  11. What will be the output of the following Python code ? min(max(False,-3,-4), 2,7)
    1. -4
    2. -3
    3. 2
    4. False
    Answer : D
    Explain : Value of False will be treated as 0.
  12. What is the output of the following code ?
    a = 50
    b = a= a*5
    print(b)
    1. 250
    2. 10
    3. 50
    4. Syntax error
    Answer : A
    Explain : b=25=50*5=250

Next Set

1 2