Which data type is immutable in Python?
Tuples are immutable sequences in Python. Once created, their elements cannot be modified, unlike lists.
Which operator is used for integer division in Python 3?
The '//' operator performs floor division and returns an integer, while '/' returns a float in Python 3.
Which of the following will create an empty dictionary?
{} and dict() both create empty dictionaries. {} is more concise while dict() is explicit.
Which method is used to add an element to a Python list?
append() adds an element at the end, while insert() adds at a specific position. Both are valid methods.
What will be the output of: 'Hello' * 2?
String multiplication repeats the string. 'Hello' * 2 produces 'HelloHello'.
Advertisement
Which of the following is a mutable data type in Python?
Lists are mutable, meaning their elements can be changed after creation. Strings, integers, and tuples are immutable.
What will be the output of: 'a' in 'apple'?
The 'in' operator checks for substring presence. 'a' is in 'apple', so it returns True.
What will be the output of: x = 5; y = x; y = 10; print(x)?
x and y are separate variables. Assigning y = 10 doesn't change x, which remains 5.
What will be the output of: print(type({}))?
Empty curly braces {} create an empty dictionary, not a set. To create an empty set, use set().
What will be the output of: print(abs(-5) + abs(3))?
abs(-5) = 5 and abs(3) = 3. Therefore, 5 + 3 = 8.
Which of the following is a valid variable name in Python?
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(max([3, 1, 4, 1, 5, 9, 2, 6]))?
The max() function returns the largest element in the list, which is 9.
What will be the output of: x = [1, 2, 3]; x.append(4); print(len(x))?
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('a' * 3 + 'b' * 2)?
'a' * 3 produces 'aaa' and 'b' * 2 produces 'bb'. Concatenating gives 'aaabb'.
What will be the output of: x = '10'; y = int(x); print(y + 5)?
int(x) converts the string '10' to integer 10. Adding 5 gives 15 (integer addition).
Which of the following code snippets will execute without raising a NameError?
Only option B defines x before using it. Other options reference undefined variables, causing NameError.
What will be the output of: print(all([True, True, True]))?
all() returns True if all elements are True. Since all three elements are True, the output is True.
What is the output of: x = '25'; y = int(x); print(y + 5)
int('25') converts the string to integer 25, then 25 + 5 = 30.
What will be the output of: lst = [1, 2, 3]; lst.extend([4, 5]); print(lst)
extend() adds each element of the iterable to the list, resulting in [1, 2, 3, 4, 5].
Consider the code: x = [10, 20, 30]; y = x; y[0] = 99; print(x[0]). What is the output?
y = x creates a reference to the same list, not a copy. Modifying y also modifies x.