Showing 101–110 of 119 questions
Which of the following keywords is used to create a function in Python?
A
function
B
def
C
func
D
define
EXPLANATION
The 'def' keyword is used to define a function in Python. Other options are not valid Python keywords.
What will be the output of: print(type(5 / 2))?
EXPLANATION
The division operator (/) in Python 3 always returns a float, even if both operands are integers.
Which of the following will raise an IndexError?
A
lst = [1, 2, 3]; lst[2]
B
lst = [1, 2, 3]; lst[3]
C
lst = [1, 2, 3]; lst[-1]
D
lst = [1, 2, 3]; lst[0]
Correct Answer:
B. lst = [1, 2, 3]; lst[3]
EXPLANATION
A list with 3 elements has valid indices 0, 1, 2. Index 3 is out of range and raises IndexError.
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.
Which statement about Python comments is correct?
A
Comments must be on their own line
B
Comments start with # and can be inline or on separate lines
C
Comments use // like in C++
D
Multiple line comments use /* */
Correct Answer:
B. Comments start with # and can be inline or on separate lines
EXPLANATION
Python comments start with # and can appear anywhere. Multi-line comments use triple quotes or multiple # symbols.
What will be the output of:
x = [1, 2, 3]
y = x
x.append(4)
print(y)
A
[1, 2, 3]
B
[1, 2, 3, 4]
C
Error
D
None
Correct Answer:
B. [1, 2, 3, 4]
EXPLANATION
y = x creates a reference to the same list, not a copy. When x is modified, y reflects the changes.
What is the output of: print(2 ** 3 ** 2)?
EXPLANATION
The operator is right-associative. So 2 3 2 = 2 (3 2) = 2 9 = 512.
What will be the output of the following code?
x = [1, 2, 3]
x.append([4, 5])
print(len(x))
EXPLANATION
append() adds the entire list [4, 5] as a single element. So x becomes [1, 2, 3, [4, 5]] with length 4.
Which of the following is a mutable data type in Python?
A
String
B
Integer
C
List
D
Tuple
EXPLANATION
Lists are mutable, meaning their elements can be changed after creation. Strings, integers, and tuples are immutable.
What will be the output of: 'Hello' * 2?
A
'HelloHello'
B
Error
C
'Hello' concatenated twice
D
HelloHello
Correct Answer:
D. HelloHello
EXPLANATION
String multiplication repeats the string. 'Hello' * 2 produces 'HelloHello'.