What will be the output of: print(bool(0), bool(''), bool([]))
0, empty string '', and empty list [] are all falsy values in Python, so bool() returns False for each.
Which operator in Python performs integer division?
The // operator performs floor division, returning the integer quotient.
What is the output of: for i in range(3): print(i, end=' ')
range(3) generates 0, 1, 2. The end=' ' parameter prevents newline after each print.
Consider: s = 'Python'; print(s[2:5]). What will be the output?
String slicing s[2:5] includes indices 2, 3, 4. 'Python'[2:5] = 'tho'.
What will be the output of: x = 10; y = 20; x, y = y, x; print(x, y)
Python tuple unpacking allows simultaneous assignment, effectively swapping x and y.
Advertisement
Consider: s = 'Python'. What is s[-1]?
Negative indexing starts from the end. s[-1] gives the last character 'n'. s = 'Python' has characters indexed from 0 to 5.
Which of the following will correctly check if a key exists in a dictionary?
The 'in' operator checks for key existence in a dictionary. has_key() was removed in Python 3.
Consider the code: def func(a, b=5): return a + b. What is func(3)?
b has a default value of 5. When func(3) is called, a=3 and b=5 (default), so return 3+5=8.
What will be printed: for i in range(1, 4): print(i, end=' ')?
range(1, 4) generates numbers from 1 to 3 (4 is excluded). The end=' ' parameter prints with space separator.
Consider: lst = [1, 2, 3, 2, 1]. What will lst.count(2) return?
The count() method returns the number of occurrences of an element. 2 appears twice in the list.
What is the output of: print(5 == '5')?
The == operator compares values and types. 5 (integer) and '5' (string) are different types, so the result is False.
Consider the code: x = 5; y = x; x = 10. What is the value of y?
When y = x is executed, y gets the value 5. Later changing x to 10 does not affect y's value because integers are immutable.
What will be the output of: print(isinstance(5, int))?
isinstance() checks if an object is an instance of a class. 5 is an integer, so isinstance(5, int) returns True.
Consider: result = [x*2 for x in range(3) if x > 0]. What is result?
List comprehension iterates through range(3) [0,1,2], filters x>0 [1,2], and multiplies by 2: [2, 4].
What will be the result of: ' hello '.strip()?
The strip() method removes leading and trailing whitespace. ' hello '.strip() returns 'hello'.
Consider: d = {1: 'a', 2: 'b', 3: 'c'}. What is d.get(4, 'default')?
The get() method returns a value for a key. If the key doesn't exist, it returns the default value provided.
Consider: lst = [1, [2, 3], 4]. What is lst[1][0]?
lst[1] refers to the nested list [2, 3]. lst[1][0] refers to the first element of that nested list, which is 2.
What will be the output of: print('abc' * 3)?
String multiplication repeats the string. 'abc' * 3 produces 'abcabcabc' and print displays it without quotes.
Consider the code: x = [1, 2, 3]; y = x; y.append(4). What is x?
y = x creates a reference to the same list object. When y.append(4) is called, it modifies the original list. Both x and y point to [1, 2, 3, 4].
What will be printed: print(3 2 2)?
Exponentiation is right-associative. 3 2 2 = 3 (2 2) = 3 ** 4 = 81.