What will be the output of: print(10 // 3)?
The floor division operator (//) returns the quotient without decimal places. 10 // 3 = 3.
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.
Advertisement
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: try: int('abc'); except: print('error'). What will be printed?
int('abc') raises a ValueError. The except block catches it and prints 'error' (without quotes).
What is the output of: print(*[1, 2, 3])?
The * operator unpacks the list. print(*[1, 2, 3]) becomes print(1, 2, 3) which prints '1 2 3' with spaces.
Consider: f = lambda x: x**2. What is f(5)?
Lambda creates an anonymous function. f = lambda x: x2 creates a function that returns x squared. f(5) = 52 = 25.
What will be the result of: set([1, 2, 2, 3, 3, 3])?
Sets remove duplicate elements. set([1, 2, 2, 3, 3, 3]) returns {1, 2, 3}.
Consider: *a, b = [1, 2, 3]. What is a?
The * operator in unpacking collects remaining elements. a gets [1, 2] and b gets 3.
What is the output of: print(eval('2+3*4'))?
eval() evaluates the string as Python code following operator precedence. 2+3*4 = 2+12 = 14.