Showing 81–90 of 119 questions
What will be the output of: x = 5; y = 10; print(x == y)
A
True
B
False
C
Error
D
None
EXPLANATION
The comparison operator == checks if x equals y. Since 5 ≠ 10, the result is False.
Which of the following statements about Python tuples is correct?
A
Tuples are mutable and can be modified after creation
B
Tuples are immutable and cannot be modified after creation
C
Tuples use square brackets for declaration
D
Tuples automatically convert to lists when assigned
Correct Answer:
B. Tuples are immutable and cannot be modified after creation
EXPLANATION
Tuples are immutable sequences in Python, meaning their contents cannot be changed once created.
What is the correct way to create a dictionary in Python?
A
d = {1: 'a', 2: 'b'}
B
d = [1: 'a', 2: 'b']
C
d = (1: 'a', 2: 'b')
D
d = {1; 'a'; 2; 'b'}
Correct Answer:
A. d = {1: 'a', 2: 'b'}
EXPLANATION
Dictionaries in Python use curly braces with key-value pairs separated by colons.
What will be the output of: print({1, 2, 3} | {3, 4, 5})?
A
{1, 2, 3, 3, 4, 5}
B
{1, 2, 3, 4, 5}
C
{3}
D
{1, 2, 4, 5}
Correct Answer:
B. {1, 2, 3, 4, 5}
EXPLANATION
The | operator performs set union, combining all unique elements from both sets. Result is {1, 2, 3, 4, 5}.
Consider the following code:
x = [1, 2, 3]
y = x[::-1]
print(y)
What will be the output?
A
[1, 2, 3]
B
[3, 2, 1]
C
[3, 1, 2]
D
Error
Correct Answer:
B. [3, 2, 1]
EXPLANATION
The slice notation [::-1] reverses the list. [1, 2, 3] reversed is [3, 2, 1].
What will be the output of: print(all([True, True, True]))?
A
True
B
False
C
1
D
None
EXPLANATION
all() returns True if all elements are True. Since all three elements are True, the output is True.
Which of the following code snippets will execute without raising a NameError?
A
print(undefined_variable)
B
x = 5; print(x)
C
print(y) where y is not defined
D
print(2 + undefined)
Correct Answer:
B. x = 5; print(x)
EXPLANATION
Only option B defines x before using it. Other options reference undefined variables, causing NameError.
What will be the output of: x = '10'; y = int(x); print(y + 5)?
A
'15'
B
15
C
'105'
D
Error
EXPLANATION
int(x) converts the string '10' to integer 10. Adding 5 gives 15 (integer addition).
What will be the output of: print('a' * 3 + 'b' * 2)?
A
aaabb
B
aabbb
C
ababab
D
ab ab
EXPLANATION
'a' * 3 produces 'aaa' and 'b' * 2 produces 'bb'. Concatenating gives 'aaabb'.
What will be the output of: x = [1, 2, 3]; x.append(4); print(len(x))?
EXPLANATION
append() adds an element to the list. After adding 4, the list has 4 elements, so len(x) = 4.