What will be the output of: print(type(3.14))
3.14 is a floating-point number, so type() returns <class 'float'>.
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.
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.
Advertisement
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.
Which of the following will raise a TypeError in Python?
int('10.5') raises TypeError because '10.5' is a string representation of a float, not a valid integer string.
What will be the output of: d = {'a': 1, 'b': 2}; print(list(d.items()))
dict.items() returns key-value pairs as tuples, converted to a list: [('a', 1), ('b', 2)].
Consider: x = [i**2 for i in range(4)]. What is x?
List comprehension: i**2 for i in [0,1,2,3] produces [0, 1, 4, 9].
What will be the output of: print(any([False, False, True]))
any() returns True if at least one element in the iterable is True.
In Python, which of the following is an immutable data type?
Tuples are immutable in Python, meaning their elements cannot be changed after creation. Lists, dictionaries, and sets are all mutable.
What will be the result of executing: print(type(5.0))?
5.0 is a floating-point number, so type(5.0) returns <class 'float'>.
What does the len() function return when applied to a string 'India'?
The string 'India' has 5 characters (I, n, d, i, a), so len('India') returns 5.
Consider the code: num = '123'. What will int(num) return?
int(num) converts the string '123' to the integer 123. The result is an integer type.
Which of the following is the correct syntax for a Python comment?
Python uses the hash symbol (#) for single-line comments. The other syntaxes are used in other programming languages.
What is the output of: print('Hello' + ' ' + 'World')?
String concatenation using + operator combines 'Hello', ' ', and 'World' into 'Hello World'.
Which method is used to add an element to a list in Python?
The append() method adds an element to the end of a list. insert() requires index specification, add() is for sets.