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.
Advertisement
What will be the output of: print(10 // 3)?
The floor division operator (//) returns the quotient without decimal places. 10 // 3 = 3.
Which of the following is NOT a valid Python variable name?
Variable names cannot start with a digit. '2nd_variable' is invalid because it begins with '2'. Valid names start with a letter or underscore.
What will be the data type of x after executing: x = 25?
In Python 3, the division operator (/) always returns a float, regardless of whether the operands are integers. 25 = 2.5, which is a float.
What is the output of: print(len('Python'))?
The string 'Python' has 6 characters: P-y-t-h-o-n. len() returns the number of characters in a string.
What will be the output of: print(10 if 5 > 3 else 20)?
This is a ternary conditional expression. Since 5 > 3 is True, the expression evaluates to 10 (the value before 'if').
What is the output of: print('a' in 'banana')?
The 'in' operator checks if 'a' is a substring of 'banana'. It is present (multiple times), so it returns True.
What will be the output of: x = 5; x += 3; print(x)?
x += 3 is equivalent to x = x + 3. Starting with x=5, after adding 3, x becomes 8.
What will be the output of the following code?
my_list = [1, 2, 3]
my_list.append(4)
print(len(my_list))
append() adds one element to the list. Initial length is 3, after append(4) it becomes 4.
Which of the following is NOT a valid Python data type?
In Python, the string data type is represented as 'str', not 'string'. 'string' is not a built-in data type.
What will be the type of x after executing: x = [1, 2.5, 'hello']?
Square brackets [] create a list object. Lists in Python can contain elements of different data types (heterogeneous).
Which method removes and returns the last element from a list?
pop() removes and returns the last element (or element at specified index). remove() only removes value, doesn't return it.
What will be the output?
my_list = [1, 2, 3, 4, 5]
print(my_list[1:3])
List slicing [1:3] returns elements from index 1 (inclusive) to index 3 (exclusive), which are elements at positions 1 and 2.