Explanations "Top Python Quiz Questions"
349 subscribers
80 photos
1 link
Explanations "Top Python Quiz Questions"
Download Telegram
In Python 3, the maximum value for an integer is 2^63 - 1

True
False

Explanation:
In Python 2, there was an internal limit to how large an integer value could be. But that limit was removed in Python 3.
This means there is no explicitly defined limit, but the amount of available address space forms a practical limit depending on the machine Python runs on.
In a Python program, a control structure:

Manages the input and output of control characters
Defines program-specific data structures
Directs the order of execution of the statements in the program
Dictates what happens before the program starts and after it terminates

Explanation:
Control structures determine which statements in the program will be executed and in what order, allowing for statements to be skipped over or executed repeatedly.

if, if/else, and if/elif/else statements are all examples of control structures that allow for statements to be skipped over or executed conditionally:

if <expr>:
<statement(s)>
elif <expr>:
<statement(s)>
elif <expr>:
<statement(s)>
...
else:
<statement(s)>


The order of execution of statements in a program is called control flow.
Which of the following are true of Python dictionaries?(can be multiple correct answers)

Items are accessed by their position in a dictionary.
Dictionaries are mutable.
All the keys in a dictionary must be of the same type.
Dictionaries are accessed by key.
Dictionaries can be nested to any depth.
A dictionary can contain any object type except another dictionary.

Explanation:
Python dictionaries are similar to lists in that they are mutable and can be nested to any arbitrary depth (constrained only by available memory).

A dictionary can contain any type of Python object, including another dictionary. The keys in a given dictionary do not need to be the same type as one another, nor do the values.

Dictionary elements are accessed by key. Unlike with list indexing, the order of the items in a dictionary plays no role in how the items are accessed.

Even though dictionary access does not rely on item order, as of version 3.7 the Python language specification does guarantee that the order of items in a dictionary is maintained once the dictionary is created.
In the Python statement x = a + 5 - b:
a and b are ________
a + 5 - b is ________


terms, a group
operands, an expression
operators, a statement
operands, an equation

Explanation:
The objects that operators act on are called operands. An expression involving operators and operands is called an expression.
After these are executed, what is the value of y?
x = 10.0
y = (x < 100.0) and isinstance(x, float)

1
None
True
False
0

Explanation:
This is a case where the terms of the expression, (x < 100.0) and isinstance(x, float), are both not only truthy, but actually equal to the Python value True. The expression is therefore also True.
What is the output of the following code snippet?
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?

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?
a = 100
b = 200

0
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.
What is the output of the following code snippet?

func = lambda x: return x
print(func(2))

0
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?

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?

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?

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’).
What will be the output of the following Python code?

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?
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?
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?
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 snippet?
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.