Programming and Problem Solving Through Python Language Theory Question

Operators, Expressions and Python Statements- Set 2

  1. Which among the following is a Bitwise XOR operator in Python ?
    1. |
    2. ~
    3. &
    4. ^
    Answer : D
    Explain : For XOR use ^ (caret ) symbol.
  2. Which of following show error ?
    1. print (float('1.0'))
    2. print (float('1'))
    3. print (int('1.0'))
    4. print (int('1'))
    Answer : C
    Explain : The int() function in Python is used to convert a value to an integer. However, when we try to convert a string like '1.0' that represents a floating-point number with a decimal point, you will encounter a ValueError because the string is not a valid integer representation.
  3. Let us assume 4 is 100 in binary and 11 is 1011. What is the output of the following butwise operators ?
    a= 4
    b= 11
    print ( a|b)
    print( a>>2)
    
    1. 15
      1
    2. 14
      1
    3. 17
      2
    4. 16
      2
    Answer : A
    Explain : a|b means a or b ( or operator use) by solving it we get 15 and a>>2 ( means right shift operator) we get 1
  4. What is the output of 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 : while we solving (a and b) we get 6, and when we solve (a or b ) we get 15.
  5. What will be output of following code ?
    a= True
    b= False
    c= False
    if a or b and c :
        print ("HELLO")
    else:
        print("hi")
    
    
    1. HELLO
    2. hi
    3. hello
    4. None of the above
    Answer : A
    Explain : Because first it solve b and c - false then a or (b and c) - which is true because one condition in or is True.
  6. A raw data assigned to a variable is known as a ?
    1. Variable
    2. Identifier
    3. literal
    4. Comment
    Answer: C
    Explain : A literal represents a fixed value in source code. It is a constant value that is directly written in the code and does not change during program execution.
  7. What will the expression 'hello' + 'world' evaluate to ?
    1. 'hello world'
    2. 'helloworld'
    3. 'hello + world'
    4. Error
    Answer : B
    Explain : when we execute this it show helloworld because + sign concatination both.
  8. Solve it :
    a=5
    b=0
    b&=a
    print(b)
    
    1. 0
    2. 5
    3. Error
    4. None of above
    Answer : A
    Explain : b&a is equal to 0 (solve it with and operator).Update value of b=0.
  9. What will the expression 'True and False' evaluate to ?
    1. True
    2. False
    3. None
    4. Error
    Answer : B
    Explain : one codition is false in this. So and operator gives false. For true both needs to true.
  10. What is the result of the expression '7 / 2'in Python?
    1. 3.5
    2. 3
    3. 4
    4. 2.5
    Answer : A
    Explain : It give us proper value which is 3.5.
  11. What is the result of the expression '5 & 3' using bitwise AND?
    1. 5
    2. 8
    3. 0
    4. 1
    Answer : D
    Explain : Firstly convert these in binary form which is 5= 0101 and 3= 0001. Now and (&) operator work, we get 0001 = 1.
  12. Given the binary representation '1010' for the number 10, what is the result of '10 << 1' ?
    1. 20
    2. 5
    3. 21
    4. 50
    Answer : A
    Explain : In this left shift operator use '1010' becomes '10100' which becomes = 20.
  13. Given the binary representation '1101' for the number 13, what is the result of '13 >> 2' ?
    1. 6
    2. 3
    3. 1
    4. 13
    Answer : B
    Explain : In this use right shift operator but shift to 2 position. '1101' becomes '0011' =3.
  14. What does the Python membership operator 'in' do ?
    1. It checks if two values are equal.
    2. It checks if a value is contained within a sequence.
    3. It performs a bitwise OR operation.
    4. It performs string concatenation.
    Answer : B
    Explain : Membership operators (in and not in) are used to test whether a value exists within a sequence (such as a list, tuple, or string) or not.
  15. What data types can the membership operator 'in' be used with?
    1. Only lists and tuples
    2. Only strings and sets
    3. Only dictionaries and sets
    4. Any sequence type (lists, tuples, strings, etc.)
    Answer : D
    Explain : Any sequence type (lists, tuples, strings, etc.) used membership operator.
  16. What will the following expression evaluate to ?
    'cat' in ['dog', 'cat', 'rabbit']
    1. True
    2. False
    3. None
    4. Error
    Answer : A
    Explain : In operator check that item is available in. cat is avaiable in list. So, it give true.
  17. 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 : print(a|b) - means a or b (or operator use) and print (a>>b) means- a right shift b.
  18. What is the output of the following statement ?
    print ((2, 4) + (1, 5))
    1. (2 , 4), (4 , 5)
    2. (3 , 9)
    3. (2, 4, 1, 5)
    4. Invalid Syntax
    Answer : C
    Explain : (2, 4) (1,5) represents a two tuple with two elements and use '+'' concatenate operation on two tuples.
  19. What is the output of >>>print(float(12.6)) ?
    1. 12
    2. 12.6
    3. '12.6'
    4. syntax error
    Answer : B
    Explain : It print float value which is 12.6
  20. What value does the following expression evaluate to ?
    print(5 + 8 * ((3* 5)-9) /10)
    1. 9.0
    2. 9.8
    3. 10
    4. 11
    Answer : B
    Explain : First step 3*5=15. (5+8*(15-9)/10). Second step- 15-9=6, (5+8*6/10). Third step - 8*6=42, (5+48/10). Fourth step - 48/10=4.8 (5+4.8). Now final step - 9.8

Next Set

1 3