Unit-II - Python

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

UNIT – II – Python

1. **What does the `range()` function in Python return?**


- A) A list of numbers
- B) A generator object
- C) A tuple of numbers
- D) An iterator object

**Answer: B) A generator object**

Explanation: The `range()` function in Python returns a generator object that produces a sequence of
numbers.

2. **What will be the output of the following code?**


```python
x=5
print(x > 3 and x < 10)
```
- A) True
- B) False
- C) Error
- D) None of the above

**Answer: A) True**

Explanation: The expression `x > 3 and x < 10` evaluates to `True` because both conditions are true.

3. **What is the output of `print(type(3.14))` in Python?**


- A) int
- B) float
- C) str
- D) None

**Answer: B) float**

Explanation: `type(3.14)` returns the type of the object, which is `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")`

**Answer: B) `file = open("data.txt", "w")`**

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.

5. **What will be the output of the following code?**


```python
nums = [1, 2, 3, 4, 5]
squares = [x ** 2 for x in nums if x % 2 == 0]
print(squares)
```
- A) [1, 4, 9, 16, 25]
- B) [4, 16]
- C) [1, 9, 25]
- D) [4, 8, 12, 16, 20]

**Answer: B) [4, 16]**

Explanation: List comprehension is used to create a new list containing squares of even numbers
from the original list.

6. **What is the output of `print("Hello" + 3)` in Python?**


- A) Hello3
- B) Error
- C) HelloHelloHello
- D) None

**Answer: B) Error**

Explanation: You can't concatenate a string with an integer directly.

7. **Which of the following statements is true about Python's `pass` statement?**


- A) It does nothing
- B) It stops the execution of the program
- C) It raises an exception
- D) It prints a message to the console

**Answer: A) It does nothing**

Explanation: The `pass` statement in Python is a null operation and does nothing when executed.

8. **What will be the output of the following code?**


```python
def func(a, b=5):
return a + b

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`.

9. **What is the purpose of the `__init__` method in Python classes?**


- A) To initialize the object's attributes
- B) To define a class constructor
- C) To perform cleanup tasks
- D) None of the above

**Answer: B) To define a class constructor**

Explanation: The `__init__` method is called when a new instance of a class is created and is used to
initialize its attributes.

10. **What does the `break` statement do in Python?**


- A) Exits the current loop
- B) Skips the rest of the loop and starts the next iteration
- C) Raises an exception
- D) None of the above

**Answer: A) Exits the current loop**

Explanation: The `break` statement is used to exit the current loop prematurely.

11. **What is the output of the following code?**


```python
print(10 / 2)
```
- A) 5.0
- B) 5
- C) 2.5
- D) Error

**Answer: A) 5.0**

Explanation: In Python 3.x, division `/` always returns a float, even if the result is a whole number.

12. **Which of the following is NOT a valid way to comment in Python?**


- A) `// This is a comment`
- B) `# This is a comment`
- C) `''' This is a comment '''`
- D) `""" This is a comment """`

**Answer: A) `// This is a comment`**

Explanation: Python does not support `//` for single-line comments; it's used for floor division.

13. **What will be the output of the following code?**


```python
def greet(name="World"):
print("Hello, " + name + "!")

greet()
```
- A) Hello, World!
- B) Hello, !
- C) Error
- D) None

**Answer: A) Hello, World!**

Explanation: The default value of `name` is "World", so if no argument is provided, it will greet
"World".

14. **What is the output of `print("Python"[::-1])` in Python?**


- A) Python
- B) nohtyP
- C) P
- D) Error

**Answer: B) nohtyP**
Explanation: Slicing with `[::-1]` reverses the string.

15. **Which of the following statements is true about Python dictionaries?**


- A) They are ordered collections
- B) They can contain duplicate keys
- C) They are accessed using indices
- D) They can only store homogeneous data

**Answer: B) They can contain duplicate keys**

Explanation: Python dictionaries can contain duplicate values but not duplicate keys.

16. **What will be the output of the following code?**


```python
print("1" + 2)
```
- A) 12
- B) 3
- C) Error
- D) None

**Answer: C) Error**

Explanation: You can't concatenate a string with an integer directly without converting it.

17. **Which of the following is NOT a valid comparison operator in Python?**


- A) ==
- B) !=
- C) <>
- D) >

**Answer: C) <>**

Explanation: In Python, the not equal operator is `!=`, and there's no `<>` operator.

18. **What will be the output of the following code?**


```python
x = [1, 2, 3]
y = x[:]
y.append(4)
print(x)
```
- A) [1, 2, 3]
- B) [1, 2, 3, 4]
- C) [1, 2, 3], [1, 2, 3, 4]
- D) Error

**Answer: A) [1, 2, 3]**

Explanation: Slicing `x[:]` creates a shallow copy of the list, so modifying `y` does not affect `x`.

19. **What will be the output of the following code?**


```python
print(5 or 3)
```
- A) 5
- B) 3
- C) True
- D) False

**Answer: A) 5**

Explanation: The `or` operator returns the first truthy value it encounters.

20. **What does the `continue` statement do in Python?**


- A) Exits the current loop
- B) Skips the rest of the loop and starts the next iteration
- C) Raises an exception
- D) None of the above

**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.

Absolutely! Here are 10 more multiple-choice questions:

21. **What will be the output of the following code?**


```python
x=5
y=2
print(x ** y)
```
- A) 7
- B) 10
- C) 25
- D) 10.25

**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):`

**Answer: A) `if key in dict:`**

Explanation: The `in` keyword is used to check if a key exists in a dictionary.

23. **What does the `strip()` method do in Python?**


- A) Removes leading and trailing whitespace from a string
- B) Removes all occurrences of a specified character from the beginning and end of a string
- C) Splits a string into a list of substrings
- D) Replaces all occurrences of a specified substring with another substring

**Answer: A) Removes leading and trailing whitespace from a string**


Explanation: The `strip()` method removes any leading (at the beginning) and trailing (at the end)
whitespace characters from a string.

24. **What will be the output of the following code?**


```python
x = [1, 2, 3]
y=x
y[0] = 4
print(x)
```
- A) [1, 2, 3]
- B) [1, 2, 3, 4]
- C) [4, 2, 3]
- D) Error

**Answer: C) [4, 2, 3]**

Explanation: Since `y` references the same list as `x`, modifying `y` also affects `x`.

25. **Which of the following is NOT a valid data type in Python?**


- A) int
- B) long
- C) decimal
- D) float

**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`.

26. **What is the output of `print("apple" > "banana")` in Python?**


- A) True
- B) False
- C) Error
- D) None

**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")`

**Answer: C) `with open("data.txt", "r") as file:`**

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.

28. **What will be the output of the following code?**


```python
print("Hello" * 3)
```
- A) HelloHelloHello
- B) 9
- C) Error
- D) None

**Answer: A) HelloHelloHello**

Explanation: The `*` operator is used for string repetition in Python.

29. **What is the purpose of the `__str__` method in Python classes?**


- A) To define a class constructor
- B) To convert an object to a string
- C) To compare two objects
- D) None of the above

**Answer: B) To convert an object to a string**

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.

30. **What will be the output of the following code?**


```python
num = 10
print(num >> 1)
```
- A) 5
- B) 20
- C) 2
- D) 0

**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:

31. **What does the `append()` method do in Python?**


- A) Adds an element to the beginning of a list
- B) Adds an element to the end of a list
- C) Removes an element from a list
- D) Replaces an element in a list

**Answer: B) Adds an element to the end of a list**

Explanation: The `append()` method in Python is used to add an element to the end of a list.

32. **What will be the output of the following code?**


```python
print("Python"[2:4])
```
- A) "Py"
- B) "th"
- C) "tho"
- D) "tho"

**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: C) `my_tuple = [1, 2, 3]`**

Explanation: Option C creates a list, not a tuple.

34. **What will be the output of the following code?**


```python
x=5
print("Hello" + x)
```
- A) Error
- B) Hello5
- C) Hello 5
- D) None

**Answer: A) Error**

Explanation: You can't concatenate a string with an integer directly.

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 '''`

**Answer: D) `''' This is a comment '''`**

Explanation: Option D encloses multiline comments using triple single or double quotes.

36. **What will be the output of the following code?**


```python
print(10 % 3)
```
- A) 3
- B) 1
- C) 0.333
- D) Error

**Answer: B) 1**

Explanation: The `%` operator returns the remainder of the division.

37. **Which of the following is NOT a valid method of a list in Python?**


- A) `append()`
- B) `pop()`
- C) `insert()`
- D) `remove()`
**Answer: D) `remove()`**

Explanation: `remove()` is used to remove the first occurrence of a specified value, not an index.

38. **What will be the output of the following code?**


```python
nums = [1, 2, 3]
squares = [x ** 2 for x in nums]
print(squares)
```
- A) [1, 4, 9]
- B) [1, 2, 3, 4, 9]
- C) [1, 2, 3, 1, 4, 9]
- D) Error

**Answer: A) [1, 4, 9]**

Explanation: List comprehension is used to create a new list containing squares of each number in
`nums`.

39. **What will be the output of the following code?**


```python
x = 10
y = 20
x, y = y, x
print(x, y)
```
- A) 20 10
- B) 10 20
- C) [10, 20]
- D) Error

**Answer: A) 20 10**

Explanation: This code swaps the values of `x` and `y` using tuple unpacking.

40. **What is the output of `print("Python"[::-1])` in Python?**


- A) Python
- B) nohtyP
- C) P
- D) Error

**Answer: B) nohtyP**

Explanation: Slicing with `[::-1]` reverses the string.


Sure, here are 10 more multiple-choice questions:

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: B) `def my_function():`**

Explanation: In Python, functions are defined using the `def` keyword.


42. **What will be the output of the following code?**
```python
print(5 == 5)
```
- A) True
- B) False
- C) 5
- D) Error

**Answer: A) True**

Explanation: `5 == 5` is a comparison that evaluates to `True`.

43. **Which of the following data types is mutable in Python?**


- A) int
- B) float
- C) str
- D) list

**Answer: D) list**

Explanation: Lists in Python are mutable, meaning their elements can be changed after creation.

44. **What does the `sorted()` function do in Python?**


- A) Sorts a list in ascending order
- B) Sorts a list in descending order
- C) Returns the length of a list
- D) Reverses the elements of a list

**Answer: A) Sorts a list in ascending order**

Explanation: The `sorted()` function in Python returns a new sorted list from the elements of any
iterable.

45. **What will be the output of the following code?**


```python
x = ["apple", "banana", "cherry"]
print("banana" in x)
```
- A) True
- B) False
- C) Error
- D) None

**Answer: A) True**

Explanation: `"banana"` is present in the list `x`.

46. **What is the output of `print(10 // 3)` in Python?**


- A) 3.333
- B) 3
- C) 3.0
- D) 0

**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.

48. **What will be the output of the following code?**


```python
x=5
print(x != 5)
```
- A) True
- B) False
- C) Error
- D) None

**Answer: B) False**

Explanation: `x != 5` evaluates to `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()`

**Answer: C) `my_set = set(1, 2, 3)`**

Explanation: Option C is not a valid way to create a set. Sets must be initialized with an iterable.

50. **What does the `extend()` method do in Python?**


- A) Adds a single element to the end of the list
- B) Adds multiple elements to the end of the list
- C) Removes an element from the list
- D) Replaces an element in the list

**Answer: B) Adds multiple elements to the end of the list**

Explanation: The `extend()` method in Python adds all the elements of an iterable to the end of the
list.

You might also like