Unit-II - Python
Unit-II - Python
Explanation: The `range()` function in Python returns a generator object that produces a sequence of
numbers.
**Answer: A) True**
Explanation: The expression `x > 3 and x < 10` evaluates to `True` because both conditions are true.
**Answer: B) float**
4. **What is the correct way to open a file named "data.txt" for writing in Python?**
- A) `file = open("data.txt", "r")`
- B) `file = open("data.txt", "w")`
- C) `file = open("data.txt", "a")`
- D) `file = open("data.txt", "x")`
Explanation: Opening a file in write mode `"w"` will truncate the file if it exists or create a new file
if it does not exist.
Explanation: List comprehension is used to create a new list containing squares of even numbers
from the original list.
**Answer: B) Error**
Explanation: The `pass` statement in Python is a null operation and does nothing when executed.
print(func(2))
```
- A) 2
- B) 5
- C) 7
- D) Error
**Answer: C) 7**
Explanation: The function `func` adds its two parameters, where `b` has a default value of `5`.
Explanation: The `__init__` method is called when a new instance of a class is created and is used to
initialize its attributes.
Explanation: The `break` statement is used to exit the current loop prematurely.
**Answer: A) 5.0**
Explanation: In Python 3.x, division `/` always returns a float, even if the result is a whole number.
Explanation: Python does not support `//` for single-line comments; it's used for floor division.
greet()
```
- A) Hello, World!
- B) Hello, !
- C) Error
- D) None
Explanation: The default value of `name` is "World", so if no argument is provided, it will greet
"World".
**Answer: B) nohtyP**
Explanation: Slicing with `[::-1]` reverses the string.
Explanation: Python dictionaries can contain duplicate values but not duplicate keys.
**Answer: C) Error**
Explanation: You can't concatenate a string with an integer directly without converting it.
**Answer: C) <>**
Explanation: In Python, the not equal operator is `!=`, and there's no `<>` operator.
Explanation: Slicing `x[:]` creates a shallow copy of the list, so modifying `y` does not affect `x`.
**Answer: A) 5**
Explanation: The `or` operator returns the first truthy value it encounters.
**Answer: B) Skips the rest of the loop and starts the next iteration**
Explanation: The `continue` statement skips the rest of the loop's body and proceeds with the next
iteration.
**Answer: C) 25**
Explanation: `**` represents exponentiation in Python. So, `x ** y` equals 5 raised to the power of
2, which is 25.
22. **Which of the following is the correct way to check if a key exists in a dictionary in Python?**
- A) `if key in dict:`
- B) `if dict[key]:`
- C) `if key.exists() in dict:`
- D) `if dict.contains(key):`
Explanation: Since `y` references the same list as `x`, modifying `y` also affects `x`.
**Answer: C) decimal**
Explanation: While Python does have support for decimals through the `decimal` module, it's not a
built-in data type like `int`, `long`, and `float`.
**Answer: B) False**
Explanation: In Python, string comparison is based on lexicographic order. Since "apple" comes
before "banana", the expression `"apple" > "banana"` evaluates to `False`.
27. **Which of the following is the correct way to open a file in Python using a context manager?**
- A) `file = open("data.txt", "r")`
- B) `file = open("data.txt", "w")`
- C) `with open("data.txt", "r") as file:`
- D) `file = open("data.txt", "a")`
Explanation: Using a context manager with the `with` statement ensures that the file is properly
closed after its suite finishes, even if an exception is raised.
**Answer: A) HelloHelloHello**
Explanation: The `__str__` method is called when the `str()` function is invoked on an object, and it
should return a string representation of the object.
**Answer: C) 2**
Explanation: The `>>` operator performs bitwise right shift operation, which is equivalent to
dividing the number by 2.
Certainly! Here are 10 more multiple-choice questions:
Explanation: The `append()` method in Python is used to add an element to the end of a list.
**Answer: C) "th"**
Explanation: Slicing `[2:4]` extracts characters from index 2 up to, but not including, index 4.
33. **Which of the following is NOT a valid way to create a tuple in Python?**
- A) `my_tuple = (1, 2, 3)`
- B) `my_tuple = 1, 2, 3`
- C) `my_tuple = [1, 2, 3]`
- D) `my_tuple = tuple([1, 2, 3])`
**Answer: A) Error**
35. **Which of the following is a correct way to comment out multiple lines of code in Python?**
- A) `// This is a comment`
- B) `# This is a comment`
- C) `/* This is a comment */`
- D) `''' This is a comment '''`
Explanation: Option D encloses multiline comments using triple single or double quotes.
**Answer: B) 1**
Explanation: `remove()` is used to remove the first occurrence of a specified value, not an index.
Explanation: List comprehension is used to create a new list containing squares of each number in
`nums`.
**Answer: A) 20 10**
Explanation: This code swaps the values of `x` and `y` using tuple unpacking.
**Answer: B) nohtyP**
41. **Which of the following is the correct way to define a function in Python?**
- A) `function my_function():`
- B) `def my_function():`
- C) `define my_function():`
- D) `func my_function():`
**Answer: A) True**
**Answer: D) list**
Explanation: Lists in Python are mutable, meaning their elements can be changed after creation.
Explanation: The `sorted()` function in Python returns a new sorted list from the elements of any
iterable.
**Answer: A) True**
**Answer: B) 3**
Explanation: `//` performs integer division, discarding any remainder.
47. **Which of the following is the correct way to access the value associated with the key "age" in
the dictionary `person`?**
```python
person = {"name": "John", "age": 30}
```
- A) `person["age"]`
- B) `person.get("age")`
- C) `person(1)`
- D) `person.key("age")`
**Answer: A) `person["age"]`**
Explanation: To access a value in a dictionary, you use square brackets `[]` with the key inside.
**Answer: B) False**
49. **Which of the following is NOT a valid way to create a set in Python?**
- A) `my_set = {1, 2, 3}`
- B) `my_set = set([1, 2, 3])`
- C) `my_set = set(1, 2, 3)`
- D) `my_set = set()`
Explanation: Option C is not a valid way to create a set. Sets must be initialized with an iterable.
Explanation: The `extend()` method in Python adds all the elements of an iterable to the end of the
list.