POC Data Structures & File Handling 1 — Questions and Answers
Question 1: Which of the following is a mutable data type in Python?
- Tuple
- List (Correct answer)
- String
- Integer
Correct answer: List
A mutable data type is one whose value can be changed after it has been created. In Python, lists are mutable, meaning you can add, remove, or modify elements within a list. Tuples, strings, and integers are immutable; once created, their values cannot be altered without creating a new object.
Question 2: How do you add an item to a list in Python?
- list.add(item)
- list.insert(item)
- list.push(item)
- list.append(item) (Correct answer)
Correct answer: list.append(item)
The `append()` method is the standard way to add an item to the end of a list in Python. It modifies the list in-place by adding the new element as the last item. Other methods like `insert()` allow adding at a specific index, but `append()` is specifically for adding to the end of the list efficiently.
Question 3: Which data structure uses key-value pairs?
- List
- Tuple
- Set
- Dictionary (Correct answer)
Correct answer: Dictionary
A dictionary in Python is an unordered collection of data values, used to store data in key-value pairs. Each key must be unique and immutable, while values can be of any data type and can be duplicated. Dictionaries are highly efficient for retrieving values based on their associated keys, making them ideal for mapping data.
Question 4: What is the correct way to open a file for reading?
- open('file.txt', 'w')
- open('file.txt') (Correct answer)
- open('file.txt', 'x')
- open('file.txt', 'a')
Correct answer: open('file.txt')
When using the `open()` function in Python, if no mode is specified, it defaults to `'r'`, which stands for read mode. Therefore, `open('file.txt')` is the correct and most common way to open a file for reading its contents. Other modes like `'w'` (write), `'x'` (exclusive creation), and `'a'` (append) serve different purposes.
Question 5: Which method is used to read the entire content of a file?
- readline()
- read() (Correct answer)
- readall()
- fetch()
Correct answer: read()
The `read()` method in Python is specifically designed to read the entire content of a file as a single string. In contrast, `readline()` reads only one line at a time. The other options, `readall()` and `fetch()`, are not standard Python file methods for this purpose.
Question 6: What does the 'with' statement do when opening a file?
- It locks the file
- It writes to the file automatically
- It closes the file automatically (Correct answer)
- It encrypts the file
Correct answer: It closes the file automatically
The 'with' statement in Python creates a context manager, which ensures that resources are properly managed. When used with file operations, it guarantees that the file's `close()` method is automatically called once the block of code is exited, even if errors occur. This prevents resource leaks and simplifies file handling.
Question 7: Which data structure allows only unique elements?
- List
- Dictionary
- Tuple
- Set (Correct answer)
Correct answer: Set
A Set is a built-in Python data structure that stores an unordered collection of unique elements. It automatically handles the removal of duplicate values. Unlike Lists, Dictionaries, or Tuples, which can contain multiple identical items, a Set ensures that each element is distinct.
Question 8: How do you write to a file in Python?
- output()
- write() (Correct answer)
- put()
- printfile()
Correct answer: write()
To write content to a file in Python, you use the `write()` method on a file object that has been opened in a write mode (e.g., 'w', 'a', 'x'). This method takes a string argument and writes it to the file. Options like `output()`, `put()`, and `printfile()` are not standard Python file writing functions.
Question 9: Which method removes all items from a list?
- remove()
- delete()
- pop()
- clear() (Correct answer)
Correct answer: clear()
The `clear()` method is a list method that removes all elements from the list, effectively making it empty. While `remove()` deletes a specific value and `pop()` removes an element by index, `clear()` is the direct way to empty the entire list. `delete()` is not a standard list method.
Which of the following is a mutable data type in Python?