Showing 61–70 of 100 questions
in Basics & Syntax
What does len() function return when applied to a string?
A
The ASCII value of first character
B
The number of characters in the string
C
The memory size of the string
D
The index of last character
Correct Answer:
B. The number of characters in the string
EXPLANATION
The len() function returns the number of characters (length) in a string.
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'.