What will the following code snippet print?
x = 5
y = 2 * x
print(y)
Explanation:
The code assigns 5 to 'x', then calculates 'y' as 2 times 'x' (which is 10), and finally prints the value of 'y'.
How many expressions can a lambda function have?
Explanation:
Lambda functions are limited to a single expression, which is evaluated and returned. This enforces their design for short, self-contained operations.
Can a Python function return multiple values?
Explanation:
Python allows returning multiple values by implicitly packaging them into a tuple. This tuple can then be unpacked into individual variables.
Which of the following data structures in Python is mutable?
Explanation:
Lists in Python are mutable, meaning their elements can be changed after creation. Tuples and strings are immutable.
What is the output of the following Python code?
my_list = [1, 2, 3, 4, 5]
print(my_list[1:3])
Explanation:
In Python slicing, the starting index is inclusive, and the ending index is exclusive. Therefore, my_list[1:3]
will return a new list containing elements from index 1 (the second element) up to, but not including, index 3.
What will the following code print?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.intersection(set2))
Explanation:
The 'intersection()' method returns a new set containing only the elements that are common to both sets.
What is the correct way to get user input and store it in a variable named 'name'?
Explanation:
The input()
function in Python takes an optional prompt argument and returns the user's input as a string. The input is then assigned to the variable 'name' using the '=' operator.
What is the output of the following Python code snippet?
print(type(5.0))
Explanation:
The type()
function in Python returns the data type of a given value. In this case, 5.0
is a floating-point number, hence the output is <class 'float'>
.
Which statement is true about the else
block in a while
loop?
Explanation:
In a while
loop, the else
block is executed only when the loop's condition evaluates to false, and not when the loop is terminated by a break
statement.
What will the variable 'x' hold after this code executes?
x = 5
x %= 2
Explanation:
The '%=' operator performs the modulo operation and then assigns the result back to 'x'. The remainder when 5 is divided by 2 is 1.
What will the following Python code snippet print?
x = 5
if x > 10:
print('A')
elif x > 3:
print('B')
else:
print('C')
Explanation:
The code checks if x
is greater than 10. Since it's not, it moves to the elif
condition. As x
is greater than 3, 'B' is printed.
What is the correct way to print 'Hello, World!' in Python?
Explanation:
Python uses the print()
function to display output. The text to be printed is enclosed in parentheses and single or double quotes.
Which comparison operator is used to check if two values are not equal?
Explanation:
The '!=' operator checks for inequality between two values. If the values are not equal, it returns True; otherwise, it returns False.
What is the output of the following Python code?
string = 'Hello, world!'
print(string[7:12])
Explanation:
In Python, string slicing uses a zero-based index. string[7:12]
extracts characters from index 7 (inclusive) to 12 (exclusive), resulting in the substring 'world'.