0% found this document useful (0 votes)
37 views2 pages

Advanced Python Quiz Questions

The document contains 20 questions about advanced Python topics testing knowledge of features like decorators, generators, exceptions, classes and more. It covers concepts like modifying function behavior with decorators, using yield for generators, len() to get dictionary length, and try/except for exception handling.

Uploaded by

Raghav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

Advanced Python Quiz Questions

The document contains 20 questions about advanced Python topics testing knowledge of features like decorators, generators, exceptions, classes and more. It covers concepts like modifying function behavior with decorators, using yield for generators, len() to get dictionary length, and try/except for exception handling.

Uploaded by

Raghav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

### Advanced Python Questions

11. What is the output of the following code?


```python
def foo(x=[]):
[Link](1)
return x

print(foo())
print(foo())
```
- A) [1], [1]
- B) [1], [1, 1]
- C) [1], []
- D) [1, 1], [1, 1]
- Answer: B) [1], [1, 1]

12. Which keyword is used to de ne a generator in Python?


- A) yield
- B) generate
- C) return
- D) generator
- Answer: A) yield

13. What is the output of the following code?


```python
print(bool('False'))
```
- A) True
- B) False
- C) 'False'
- D) Error
- Answer: A) True

14. Which of the following functions is used to get the length of a dictionary?
- A) len()
- B) count()
- C) size()
- D) length()
- Answer: A) len()

15. What is the output of the following code?


```python
a = [1, 2, 3]
b=a
[Link](4)
print(b)
```
- A) [1, 2, 3]
- B) [1, 2, 3, 4]
- C) [4]
- D) Error
- Answer: B) [1, 2, 3, 4]

### Expert Python Questions

16. Which of the following is true about Python decorators?


- A) They are used to modify the behavior of a function or class
- B) They are de ned using the `def` keyword
- C) They are called before de ning a function
fi
fi
fi
- D) They cannot be nested
- Answer: A) They are used to modify the behavior of a function or class

17. What will be the output of the following code?


```python
a = {1, 2, 3}
b = {3, 4, 5}
print(a & b)
```
- A) {1, 2, 3, 4, 5}
- B) {1, 2}
- C) {3}
- D) {1, 2, 3}
- Answer: C) {3}

18. What does the `__init__` method do in a class?


- A) It initializes the class attributes
- B) It is called automatically when an instance is created
- C) It is a constructor method
- D) All of the above
- Answer: D) All of the above

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


```python
def foo():
try:
return 1
nally:
return 2

print(foo())
```
- A) 1
- B) 2
- C) Error
- D) None
- Answer: B) 2

20. Which of the following is used to handle exceptions in Python?


- A) try/except
- B) try/catch
- C) handle/except
- D) catch/handle
- Answer: A) try/except

These questions cover various aspects of Python and are designed to test fundamental as well as
advanced understanding of the language.
fi

Common questions

Powered by AI

The `__init__` method in a Python class, often referred to as the constructor, is executed upon the creation of a new class instance. It initializes class attributes and sets up the initial state of the object. This method is automatically called when an instance is created and allows for any necessary parameters to customize the instance's initial configuration.

The `len()` function, when used with dictionaries in Python, returns the number of key-value pairs in the dictionary. It provides a count of how many elements (pairs) are contained in the dictionary. This is useful for quickly determining the size of a dictionary.

The `yield` keyword is used in Python to define a generator function, which returns an iterator. Unlike `return`, which exits the function completely, `yield` pauses the function saving its state, and resumes where it left off when called again. This allows the function to produce a series of values over time rather than computing them all at once and sending them back. This makes `yield` more memory efficient than `return` when dealing with large datasets.

Two variables point to the same list in Python when one is assigned to the other without copying, meaning they reference the same memory location. Modifications to the list via one variable will reflect in the other. For example: ```python a = [1, 2, 3] b = a a.append(4) ``` Here, both a and b refer to the same list object, so appending 4 to a also modifies b, resulting in [1, 2, 3, 4]

Decorators in Python are a form of metaprogramming that can modify or wrap a function or class to change or enhance its behavior without altering its source code. Common use cases include logging, enforcing access control, imposing restrictions, instrumentation, and memoization. Decorators provide a convenient, readable way to add functionality to existing code.

Python uses `try` and `except` blocks to handle exceptions, allowing for clean exception management by catching errors and continuing program execution. Unlike Java's `try` and `catch`, Python omits the need for exception declarations or checked exceptions, providing flexibility but requiring careful management to avoid runtime errors.

The printed result of the operation is `{3}`, which is the intersection of sets `a` and `b`. The `&` operator calculates the intersection, resulting in a set containing only elements that are present in both `a` and `b`. In this case, the number 3 is the only common element.

The output of the code snippet is [1], [1, 1]. This occurs because the default value for the parameter x in the function foo is a mutable list. When foo() is called for the first time, the list [1] is returned and stored as the default value. On subsequent calls, this same list is used and modified, resulting in the list containing [1, 1] on the second call.

In Python, the `bool()` function returns `True` for any non-empty string, regardless of its content. Since 'False' is a non-empty string, it evaluates to `True` when converted to a boolean with `bool()`. It's important to note that `bool()` does not consider the content of the string but only its emptiness.

The `finally` block in Python is designed to always execute after the `try` block, even if a return statement is hit within `try`. In the code snippet, although `try` specifies `return 1`, the `finally` block overrides it with `return 2`. `finally` ensures clean-up code runs, hence why it commits the function to return 2, not 1.

You might also like