Programming and Problem Solving Through Python Language Theory Question
Operators, Expressions and Python Statements- Set 2
Which among the following is a Bitwise XOR operator in Python ?
|
~
&
^
Answer : D
Explain : For XOR use ^ (caret ) symbol.
Which of following show error ?
print (float('1.0'))
print (float('1'))
print (int('1.0'))
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.
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)
15 1
14 1
17 2
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
What is the output of following code ?
a= 15
b= 6
print(a and b)
print(a or b)
True True
False False
6 15
15 6
Answer : C
Explain : while we solving (a and b) we get 6, and when we solve (a or b ) we get 15.
What will be output of following code ?
a= True
b= False
c= False
if a or b and c :
print ("HELLO")
else:
print("hi")
HELLO
hi
hello
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.
A raw data assigned to a variable is known as a ?
Variable
Identifier
literal
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.
What will the expression 'hello' + 'world' evaluate to ?
'hello world'
'helloworld'
'hello + world'
Error
Answer : B
Explain : when we execute this it show helloworld because + sign concatination both.
Solve it :
a=5
b=0
b&=a
print(b)
0
5
Error
None of above
Answer : A
Explain : b&a is equal to 0 (solve it with and operator).Update value of b=0.
What will the expression 'True and False' evaluate to ?
True
False
None
Error
Answer : B
Explain : one codition is false in this. So and operator gives false. For true both needs to true.
What is the result of the expression '7 / 2'in Python?
3.5
3
4
2.5
Answer : A
Explain : It give us proper value which is 3.5.
What is the result of the expression '5 & 3' using bitwise AND?
5
8
0
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.
Given the binary representation '1010' for the number 10, what is the result of '10 << 1' ?
20
5
21
50
Answer : A
Explain : In this left shift operator use '1010' becomes '10100' which becomes = 20.
Given the binary representation '1101' for the number 13, what is the result of '13 >> 2' ?
6
3
1
13
Answer : B
Explain : In this use right shift operator but shift to 2 position. '1101' becomes '0011' =3.
What does the Python membership operator 'in' do ?
It checks if two values are equal.
It checks if a value is contained within a sequence.
It performs a bitwise OR operation.
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.
What data types can the membership operator 'in' be used with?
Only lists and tuples
Only strings and sets
Only dictionaries and sets
Any sequence type (lists, tuples, strings, etc.)
Answer : D
Explain : Any sequence type (lists, tuples, strings, etc.) used membership operator.
What will the following expression evaluate to ?
'cat' in ['dog', 'cat', 'rabbit']
True
False
None
Error
Answer : A
Explain : In operator check that item is available in. cat is avaiable in list. So, it give true.
What will be the output of the following expression ?
a = 2
b = 8
print(a | b)
print(a >> 1)
10 0
10 2
2 2
10 1
Answer : D
Explain : print(a|b) - means a or b (or operator use) and print (a>>b) means- a right shift b.
What is the output of the following statement ?
print ((2, 4) + (1, 5))
(2 , 4), (4 , 5)
(3 , 9)
(2, 4, 1, 5)
Invalid Syntax
Answer : C
Explain : (2, 4) (1,5) represents a two tuple with two elements and use '+'' concatenate operation on two tuples.
What is the output of >>>print(float(12.6)) ?
12
12.6
'12.6'
syntax error
Answer : B
Explain : It print float value which is 12.6
What value does the following expression evaluate to ? print(5 + 8 * ((3* 5)-9) /10)
9.0
9.8
10
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