Showing 31–40 of 119 questions
What is the output of: print(len('Python'))?
EXPLANATION
The string 'Python' has 6 characters: P-y-t-h-o-n. len() returns the number of characters in a string.
Consider the code: x = [1, 2, 3]; y = x; y.append(4). What is x?
A
[1, 2, 3]
B
[1, 2, 3, 4]
C
[4]
D
Error
Correct Answer:
B. [1, 2, 3, 4]
EXPLANATION
y = x creates a reference to the same list object. When y.append(4) is called, it modifies the original list. Both x and y point to [1, 2, 3, 4].
What will be the data type of x after executing: x = 5 / 2?
A
int
B
float
C
complex
D
str
EXPLANATION
In Python 3, the division operator (/) always returns a float, regardless of whether the operands are integers. 5 / 2 = 2.5, which is a float.
What is the output of: print(10 // 3 + 10 % 3)?
EXPLANATION
10 // 3 = 3 (floor division), 10 % 3 = 1 (modulo). 3 + 1 = 4. Wait, let me recalculate: 10//3=3, 10%3=1, so 3+1=4. Actually: output is 4.
Which of the following is NOT a valid Python variable name?
A
_private_var
B
2nd_variable
C
variable_name
D
__dunder__
Correct Answer:
B. 2nd_variable
EXPLANATION
Variable names cannot start with a digit. '2nd_variable' is invalid because it begins with '2'. Valid names start with a letter or underscore.
What is the output of: print(type(lambda x: x))?
EXPLANATION
Lambda functions are functions in Python. type(lambda x: x) returns <class 'function'>.
Consider: class A: x = 5. What is A.x and how can it be modified?
A
Instance variable, modify via obj.x
B
Class variable, modify via A.x or obj.x
C
Local variable, cannot be modified
D
Error
Correct Answer:
B. Class variable, modify via A.x or obj.x
EXPLANATION
x = 5 is a class variable. It can be accessed via A.x and modified via A.x = new_value or instance modification.
What will be the output of: print({x: x**2 for x in range(3)})?
A
{0: 0, 1: 1, 2: 4}
B
{0: 1, 1: 2, 2: 3}
C
{1: 1, 2: 4}
D
Error
Correct Answer:
A. {0: 0, 1: 1, 2: 4}
EXPLANATION
Dictionary comprehension creates {0: 0, 1: 1, 2: 4} where keys are from range(3) and values are their squares.
Consider the code: with open('file.txt') as f: data = f.read(). What happens when the block exits?
A
File remains open
B
File is automatically closed
C
Error occurs
D
data is deleted
Correct Answer:
B. File is automatically closed
EXPLANATION
The 'with' statement ensures the file is automatically closed when the block exits, even if an error occurs.
What is the output of: print(eval('2+3*4'))?
EXPLANATION
eval() evaluates the string as Python code following operator precedence. 2+3*4 = 2+12 = 14.