1.
What does `len()` function return?
Correct Answer
A. Length
Explanation
The len() function in Python is used to determine the number of items in an object. When used on a string, list, tuple, or dictionary, it returns the count of elements in these data structures, provided as an integer. This function is integral for looping, conditionals, and sizing checks in Python programming, offering a straightforward way to assess the length or size of various iterable and non-iterable objects.
2.
Which data type is mutable?
Correct Answer
B. List
Explanation
In Python, lists are mutable, meaning their elements can be changed after the list has been created. Unlike tuples or strings, which are immutable and cannot be altered once formed, lists allow modifications such as adding, removing, or changing elements. This makes lists highly versatile for programs that require the storage of items where changes to the stored data might be necessary.
3.
How do you start a comment in Python?
Correct Answer
C. #
Explanation
In Python, comments are marked by the # symbol. Anything following this symbol on the same line is ignored by the Python interpreter. Comments are used to leave notes and explanations in the code, or to temporarily disable certain lines of code without deleting them. This is crucial for both code maintenance and collaboration, allowing programmers to understand the purpose or logic behind sections of code without executing them.
4.
What operator is used for exponentiation?
Correct Answer
B. **
Explanation
The ** operator in Python is used for exponentiation, which involves raising a number to the power of another. For example, x**y calculates x raised to the power y. This operator is a more readable and concise alternative to using the pow() function, streamlining mathematical operations involving powers in coding tasks.
5.
Which method adds an item to the end of a list?
Correct Answer
A. Append()
Explanation
The append() method in Python is used to add an item to the end of a list. This method takes a single argument, which is the item you want to add to the list, and appends it to the end. It modifies the original list and does not return any value. This is fundamental for dynamically building lists in Python, especially when the number of elements is not known in advance.
6.
What does the `pop()` method do?
Correct Answer
B. Remove
Explanation
The pop() method in Python removes the item at a given position in the list, and returns it. If no index is specified, pop() removes and returns the last item in the list. This method is useful for both retrieving an item and altering the list by removing that item, making it a valuable tool for managing lists where elements need to be processed and simultaneously removed.
7.
How do you create a dictionary?
Correct Answer
A. {}
Explanation
Dictionaries in Python are created using curly braces {} with pairs of keys and values. A dictionary is a collection which is unordered, changeable, and indexed. They are particularly useful for efficient data lookup, as values are associated with unique keys, allowing for fast retrieval, addition, and deletion of data based on the key.
8.
Which loop is used to iterate over a sequence?
Correct Answer
C. For
Explanation
The for loop in Python is used to iterate over items of any sequence, such as a list or a string, in the order that they appear in the sequence. This makes the for loop particularly useful for tasks that require each element in a sequence to be processed one at a time, utilizing a simple syntax that enhances readability and ease of use.
9.
What keyword is used to define a function?
Correct Answer
B. Def
Explanation
In Python, the def keyword is used to define a function. Functions are defined with a block of code that only runs when it is called. The def keyword is followed by the function name and parentheses which may include parameters. Functions organize code into manageable blocks, allowing for code reuse, better modularity, and a reduction in redundancy.
10.
What is the output of `print(8 // 3)`?
Correct Answer
B. 2
Explanation
When using the // operator in Python, the operation performed is called floor division. Unlike regular division that returns a precise float value, floor division returns the largest possible integer that is less than or equal to the division result. For 8 // 3, it divides 8 by 3 which equals 2.666..., but floor division truncates the decimal part and just returns 2. This operator is useful in scenarios where you need the division result as an integer, particularly in loop counters and index calculations.