Showing 41–50 of 56 questions
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.
What will be the output of: print(max([3, 1, 4, 1, 5, 9, 2, 6]))?
EXPLANATION
The max() function returns the largest element in the list, which is 9.
Which of the following is a valid variable name in Python?
A
2variable
B
var-iable
C
var_iable
D
var iable
Correct Answer:
C. var_iable
EXPLANATION
Variable names can contain letters, digits, and underscores, but cannot start with a digit or contain hyphens or spaces.
What will be the output of: print(abs(-5) + abs(3))?
EXPLANATION
abs(-5) = 5 and abs(3) = 3. Therefore, 5 + 3 = 8.
What will be the output of: print(type({}))?
EXPLANATION
Empty curly braces {} create an empty dictionary, not a set. To create an empty set, use set().
What will be the output of: x = 5; y = x; y = 10; print(x)?
EXPLANATION
x and y are separate variables. Assigning y = 10 doesn't change x, which remains 5.
What will be the output of: 'a' in 'apple'?
A
True
B
False
C
1
D
Error
EXPLANATION
The 'in' operator checks for substring presence. 'a' is in 'apple', so it returns True.