What is the output of the following code snippet?
❌ foo<->bar<->baz
❌ baz<->bar<->foo
❌ baz<->bar<->foo<->Done.
❌ ('foo', 3)<->('bar', 2)<->('baz', 1)<->Done.
✅ ('baz', 3)<->('bar', 2)<->('foo', 1)<->Done.
❌ The snippet doesn’t generate any output
Explanation:
The .popitem() method removes one key-value pair from d and returns it as a tuple. So the body of this while loop displays the contents of d as tuples.
Once the last key-value pair has been removed, d is empty and is false in Boolean context. The while loop then terminates and displays the line following the loop body.
d = {'foo': 1, 'bar': 2, 'baz': 3}
while d:
print(d.popitem(), end = '<->')
print('Done.')
❌Done.❌ foo<->bar<->baz
❌ baz<->bar<->foo
❌ baz<->bar<->foo<->Done.
❌ ('foo', 3)<->('bar', 2)<->('baz', 1)<->Done.
✅ ('baz', 3)<->('bar', 2)<->('foo', 1)<->Done.
❌ The snippet doesn’t generate any output
Explanation:
The .popitem() method removes one key-value pair from d and returns it as a tuple. So the body of this while loop displays the contents of d as tuples.
Once the last key-value pair has been removed, d is empty and is false in Boolean context. The while loop then terminates and displays the line following the loop body.
What will be the value of the i variable when the while loop finishes its execution?
✅ 1
❌ 0
❌ 2
❌ the variable becomes unavailable
Explanation:
The initial value of 'i' is 0. Accordingly, the program will not go into the while loop because 'i' is already equal to zero. As a next step, the 'else' statement is executed and 'i' is incremented.
As a result, the value of 'i' is equal to '1'.
i=0
while i != 0:
i = i - 1
else:
i = i + 1
✅ 1
❌ 0
❌ 2
❌ the variable becomes unavailable
Explanation:
The initial value of 'i' is 0. Accordingly, the program will not go into the while loop because 'i' is already equal to zero. As a next step, the 'else' statement is executed and 'i' is incremented.
As a result, the value of 'i' is equal to '1'.
What is the value of the expression a and b?
❌ True
❌ False
❌ 100
✅ 200
Explanation:
When two non-Boolean values are joined by and or or, the value of the expression is one of the operands, not True or False.
For two non-Boolean values a and b:
If a is truthy then:
1) a or b is a
2) a and b is b
If a is falsy then:
1) a or b is b
2) a and b is a
a = 100❌ 0
b = 200
❌ True
❌ False
❌ 100
✅ 200
Explanation:
When two non-Boolean values are joined by and or or, the value of the expression is one of the operands, not True or False.
For two non-Boolean values a and b:
If a is truthy then:
1) a or b is a
2) a and b is b
If a is falsy then:
1) a or b is b
2) a and b is a
Python strings have a property called “immutability.” What does this mean?
❌ Strings in Python can be represented as arrays of chars
✅ Strings in Python can’t be changed
❌ Strings can’t be divided by numbers
❌ You can update a string in Python with concatenation
Explanation:
Immutability is a key property of strings as implemented in Python. While it is true that strings can’t be divided by numbers, that is not the meaning of immutability. Instead, immutability means that strings can not be changed.
❌ Strings in Python can be represented as arrays of chars
✅ Strings in Python can’t be changed
❌ Strings can’t be divided by numbers
❌ You can update a string in Python with concatenation
Explanation:
Immutability is a key property of strings as implemented in Python. While it is true that strings can’t be divided by numbers, that is not the meaning of immutability. Instead, immutability means that strings can not be changed.
What is the output of the following code snippet?
❌ 2.0
❌ 2
❌ x
✅ SyntaxError
Explanation:
A lambda function can’t contain the return statement. In a lambda function, statements like return, pass, assert, or raise will raise a SyntaxError exception.
func = lambda x: return x❌ 0
print(func(2))
❌ 2.0
❌ 2
❌ x
✅ SyntaxError
Explanation:
A lambda function can’t contain the return statement. In a lambda function, statements like return, pass, assert, or raise will raise a SyntaxError exception.
What will be the output of the following Python code?
✅ [‘ab’, ‘cd’]
❌ [‘AB’, ‘CD’]
❌ [None, None]
❌ none of the mentioned
Explanation:
The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.
x = ['ab', 'cd']
for i in x:
i.upper()
print(x)
✅ [‘ab’, ‘cd’]
❌ [‘AB’, ‘CD’]
❌ [None, None]
❌ none of the mentioned
Explanation:
The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.
What is the result when you run the code?
❌ SyntaxError
❌ 3
✅ 6
❌ 2
Explanation:
reduce() applies the function cumulatively to the items of the given sequence. Hence, it becomes 1 + 2 = 3 and then 3 + 3 = 6.
from functools import reduce
numbers = [1, 2, 3]
reduce(lambda x, y: x + y, numbers)
❌ SyntaxError
❌ 3
✅ 6
❌ 2
Explanation:
reduce() applies the function cumulatively to the items of the given sequence. Hence, it becomes 1 + 2 = 3 and then 3 + 3 = 6.
What will be the output of the following Python code?
✅2
❌ 1
❌ 0
❌ Error
Explanation:
Since ‘x’ has been declared a global variable, it can be modified very easily within the function. Hence the output is 2.
x=1
def cg():
global x
x=x+1
cg()
x
✅2
❌ 1
❌ 0
❌ Error
Explanation:
Since ‘x’ has been declared a global variable, it can be modified very easily within the function. Hence the output is 2.
Which of the following lines of code will not show a match?
❌ >>> re.match(‘ab*’, ‘a’)
❌ >>> re.match(‘ab*’, ‘ab’)
❌ >>> re.match(‘ab*’, ‘abb’)
✅ >>> re.match(‘ab*’, ‘ba’)
Explanation:
In the code shown above, ab* will match to ‘a’ or ‘ab’ or ‘a’ followed by any number of b’s. Hence the only line of code from the above options which does not result in a match is: >>> re.match(‘ab*’, ‘ba’).
❌ >>> re.match(‘ab*’, ‘a’)
❌ >>> re.match(‘ab*’, ‘ab’)
❌ >>> re.match(‘ab*’, ‘abb’)
✅ >>> re.match(‘ab*’, ‘ba’)
Explanation:
In the code shown above, ab* will match to ‘a’ or ‘ab’ or ‘a’ followed by any number of b’s. Hence the only line of code from the above options which does not result in a match is: >>> re.match(‘ab*’, ‘ba’).
What will be the output of the following Python code?
✅ [0]
❌ [1]
❌ [1, 0]
❌ [0, 1]
Explanation:
A new list object is created in the function and the reference is lost. This can be checked by comparing the id of k before and after k = [1].
def foo(k):
k = [1]
q = [0]
foo(q)
print(q)
✅ [0]
❌ [1]
❌ [1, 0]
❌ [0, 1]
Explanation:
A new list object is created in the function and the reference is lost. This can be checked by comparing the id of k before and after k = [1].
What will be the output of the following Python code?
❌ abc def
❌ ABC DEF
✅ Abc def
❌ Abc Def
Explanation:
The first letter of the string is converted to uppercase and the others are converted to lowercase.
print("abc DEF".capitalize())❌ abc def
❌ ABC DEF
✅ Abc def
❌ Abc Def
Explanation:
The first letter of the string is converted to uppercase and the others are converted to lowercase.
What will be the output of the following Python code?
❌ Error
❌ Warning
✅ 100
❌ No output
Explanation:
The code shown above demonstrates rebinding using a static method. This can be done with or without a decorator. The output of this code will be 100.
class A:
@staticmethod
def a(x):
print(x)
A.a(100)
❌ Error
❌ Warning
✅ 100
❌ No output
Explanation:
The code shown above demonstrates rebinding using a static method. This can be done with or without a decorator. The output of this code will be 100.
What will be the output of the following Python code?
❌ [1]
❌ [1, 0]
❌ [0, 1]
Explanation:
A new list object is created in the function and the reference is lost. This can be checked by comparing the id of k before and after k = [1].
def foo(k):✅ [0]
k = [1]
q = [0]
foo(q)
print(q)
❌ [1]
❌ [1, 0]
❌ [0, 1]
Explanation:
A new list object is created in the function and the reference is lost. This can be checked by comparing the id of k before and after k = [1].
What will be the output of the following Python code snippet?
✅ 0 1 2 3
❌ 0 1 2 2
❌ 3 3 3 3
❌ 0 0 0 0
❌ error
Explanation:
The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.
a = [0, 1, 2, 3]
for a[0] in a:
print(a[0])
✅ 0 1 2 3
❌ 0 1 2 2
❌ 3 3 3 3
❌ 0 0 0 0
❌ error
Explanation:
The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.
What will be the output of the following Python code?
❌ The program has an error because there isn’t any function to return self.a
❌ The program has an error because b is private and display(self) is returning a private member
✅ The program has an error because b is private and hence can’t be printed
❌ The program runs fine and 1 is printed
Explanation:
Variables beginning with two underscores are said to be private members of the class and they can’t be accessed directly.
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def display(self):
return self.__b
obj = Demo()
print(obj.__b)
❌ The program has an error because there isn’t any function to return self.a
❌ The program has an error because b is private and display(self) is returning a private member
✅ The program has an error because b is private and hence can’t be printed
❌ The program runs fine and 1 is printed
Explanation:
Variables beginning with two underscores are said to be private members of the class and they can’t be accessed directly.
What will be the value of X in the following Python expression
❌ 30.0
❌ 30.8
❌ 28.4
✅ 27.2
Explanation:
The expression shown above is evaluated as: 2+9*(36-8)/10, which simplifies to give 2+9*(2.8), which is equal to 2+25.2 = 27.2. Hence the result of this expression is 27.2.
X = 2+9*((3*12)-8)/10
❌ 30.0
❌ 30.8
❌ 28.4
✅ 27.2
Explanation:
The expression shown above is evaluated as: 2+9*(36-8)/10, which simplifies to give 2+9*(2.8), which is equal to 2+25.2 = 27.2. Hence the result of this expression is 27.2.
What will be the output of the following Python code?
❌ [‘M a t h s i s a d i f f i c u l t s u b j e c t’]
❌ [‘Maths’, ‘is’, ‘a’, ‘difficult’, ‘subject’]
❌ ‘Maths is a difficult subject’
✅ [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’]
Explanation:
The code shown above demonstrates the use of the function re.match. The first argument of this function specifies the pattern. Since the pattern contains groups, those groups are incorporated in the resultant list as well. Hence the output of the code shown above is [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’].
re.split(r'(a)(t)', 'Maths is a difficult subject')
❌ [‘M a t h s i s a d i f f i c u l t s u b j e c t’]
❌ [‘Maths’, ‘is’, ‘a’, ‘difficult’, ‘subject’]
❌ ‘Maths is a difficult subject’
✅ [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’]
Explanation:
The code shown above demonstrates the use of the function re.match. The first argument of this function specifies the pattern. Since the pattern contains groups, those groups are incorporated in the resultant list as well. Hence the output of the code shown above is [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’].
What is the output of print 0.1 + 0.2 == 0.3?
❌ True
✅ False
❌ Machine dependent
❌ Error
Explanation:
Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3.
❌ True
✅ False
❌ Machine dependent
❌ Error
Explanation:
Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3.
What is the output of the following code?
✅ TypeError
❌ (100, 800, 200, 300, 400, 500)
❌ (800, 100, 200, 300, 400, 500)
Explanation:
A tuple is immutable. Once a tuple is created, you cannot change its values. If you try to change its value, you will receive a TypeError: 'tuple' object does not support item assignment
aTuple = (100, 200, 300, 400, 500)
aTuple[1] = 800
print(aTuple)
✅ TypeError
❌ (100, 800, 200, 300, 400, 500)
❌ (800, 100, 200, 300, 400, 500)
Explanation:
A tuple is immutable. Once a tuple is created, you cannot change its values. If you try to change its value, you will receive a TypeError: 'tuple' object does not support item assignment
What happens in the code?
❌ Class B inherits all the data fields of class A.
❌ Class B needs an Argument.
❌ The data field ‘j’ cannot be accessed by object b.
✅ Class B is inheriting class A but the data field ‘i’ in A cannot be inherited.
Explanation:
Reason being that i is initiated with self thus making it a instantiate variable of that class which cannot be inherited by the above way.
class A:
def __init__(self, i=100):
self.i=i
class B(A):
def __init__(self,j=0):
self.j=j
def main():
b= B()
print(b.i)
print(b.j)
main()
❌ Class B inherits all the data fields of class A.
❌ Class B needs an Argument.
❌ The data field ‘j’ cannot be accessed by object b.
✅ Class B is inheriting class A but the data field ‘i’ in A cannot be inherited.
Explanation:
Reason being that i is initiated with self thus making it a instantiate variable of that class which cannot be inherited by the above way.
What is output for the following code?
❌ [‘hat’,’mat’,’rat’,’rhyme’]
❌ ‘hatmatratrhyme’
❌ [‘hat mat rat rhyme’]
✅ ‘hatrhymematrhyme rat’
Explanation:
The method join() takes list of string as input and returns string as output. It removes ‘,’ and add the given string with join to the list.
a = ['hat', 'mat', 'rat']
'rhyme'.join(a)
❌ [‘hat’,’mat’,’rat’,’rhyme’]
❌ ‘hatmatratrhyme’
❌ [‘hat mat rat rhyme’]
✅ ‘hatrhymematrhyme rat’
Explanation:
The method join() takes list of string as input and returns string as output. It removes ‘,’ and add the given string with join to the list.