What is the correct way to create a variable in Python?
Python variables are created using the assignment operator (=) with valid identifiers containing letters, digits, and underscores.
Which of the following is NOT a valid Python identifier?
Python identifiers cannot start with a digit. They must begin with a letter or underscore.
What will be the output of: print(type(5.0))?
5.0 is a floating-point number in Python. The type() function returns <class 'float'> for decimal numbers.
Which keyword is used to create a function in Python?
The 'def' keyword is used to define functions in Python. It is followed by the function name and parameters.
Which data type is immutable in Python?
Tuples are immutable sequences in Python. Once created, their elements cannot be modified, unlike lists.
Advertisement
What will be the output of: len('Python')?
The string 'Python' has 6 characters: P, y, t, h, o, n. The len() function returns the number of characters.
Which operator is used for integer division in Python 3?
The '//' operator performs floor division and returns an integer, while '/' returns a float in Python 3.
Which of the following will create an empty dictionary?
{} and dict() both create empty dictionaries. {} is more concise while dict() is explicit.
Which method is used to add an element to a Python list?
append() adds an element at the end, while insert() adds at a specific position. Both are valid methods.
What will be the output of: 'Hello' * 2?
String multiplication repeats the string. 'Hello' * 2 produces 'HelloHello'.
Which of the following is a mutable data type in Python?
Lists are mutable, meaning their elements can be changed after creation. Strings, integers, and tuples are immutable.
What will be the output of the following code?
x = [1, 2, 3]
x.append([4, 5])
print(len(x))
append() adds the entire list [4, 5] as a single element. So x becomes [1, 2, 3, [4, 5]] with length 4.
What is the output of: print(2 3 2)?
The operator is right-associative. So 2 3 2 = 2 (3 2) = 2 9 = 512.
What will be the output of:
x = [1, 2, 3]
y = x
x.append(4)
print(y)
y = x creates a reference to the same list, not a copy. When x is modified, y reflects the changes.
Which statement about Python comments is correct?
Python comments start with # and can appear anywhere. Multi-line comments use triple quotes or multiple # symbols.
What will be the output of: 'a' in 'apple'?
The 'in' operator checks for substring presence. 'a' is in 'apple', so it returns True.
Which of the following will raise an IndexError?
A list with 3 elements has valid indices 0, 1, 2. Index 3 is out of range and raises IndexError.
What will be the output of: print(type(25))?
The division operator (/) in Python 3 always returns a float, even if both operands are integers.
Which of the following keywords is used to create a function in Python?
The 'def' keyword is used to define a function in Python. Other options are not valid Python keywords.
What will be the output of: print("Python"[2:5])?
String slicing [2:5] extracts characters from index 2 to 4 (5 is exclusive). 'Python'[2:5] = 'yth'.