0% found this document useful (0 votes)
3 views

Python_Questions_Answers

The document contains a series of Python questions with multiple-choice answers and explanations. It covers topics such as class attribute naming conventions, thread management, and the differences between `__repr__()` and `__str__()` methods. Each question is designed to test knowledge of Python programming concepts and best practices.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_Questions_Answers

The document contains a series of Python questions with multiple-choice answers and explanations. It covers topics such as class attribute naming conventions, thread management, and the differences between `__repr__()` and `__str__()` methods. Each question is designed to test knowledge of Python programming concepts and best practices.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Python Questions with Answers and Explanations

---

### **Question 1 of 30**


**Problem**:
You need to define a Python class named Widget in your application with an
attribute named `value`, a fairly common name. In which of the scenarios below, is
it better to rename `self.value` to `self.__value` (with a double underscore)?

**Choices**:
- **A**: Widget is defined as part of a public API, and we do not want any class
methods of Widget to access value.
- **B**: The widget is internal to your application and is the base class of a
diamond-shaped inheritance hierarchy, and we do not want derived class methods to
access value.
- **C**: The widget is internal to your application, and we do not want any class
methods of the Widget class to access value.
- **D**: The widget is internal to your application and is the base class of an
inheritance hierarchy, and we do not want derived class methods to access value.
- **E**: Widget is defined as part of a public API, and the clients of this API are
expected to subclass Widget in their own inheritance hierarchies. We do not want
methods of these subclasses to access value.

**Answer**: **D**
**Explanation**: Using `__value` invokes name mangling, which ensures that the
attribute is harder to access or override accidentally in derived classes. This is
particularly useful in inheritance hierarchies to protect the attribute.

---

### **Question 2 of 30**


**Problem**:
You are working on a Python 3 program that uses threading. The code snippet
schedules five threads every 10 seconds. If you want tighter control over the
number of threads, what would you do?

**Choices**:
- **A**: Use multiple schedulers and make them run serially.
- **B**: Use a thread pool.
- **C**: None is correct.
- **D**: Use only jobQueue.queue that will queue relevant threads as and when
added.

**Answer**: **B**
**Explanation**: A thread pool allows you to control the number of threads,
ensuring efficient resource usage while preventing excessive thread creation.

---

### **Question 3 of 30**


**Problem**:
What is the difference between the implementations of the methods `__repr__()` and
`__str__()` in a user-defined class in Python?

**Choices**:
- **A**: If we do not implement the `__str__()` function, then a call to `str()` on
an object invokes `__repr__()`.
- **B**: An invocation of `__str__()` returns a user-friendly printable string that
can also be used by a debugger to reconstruct a representation of the original
object.
- **C**: A call to `repr()` invokes both `__repr__()` and `__str__()`, whereas a
call to `str()` invokes just `__str__()`.
- **D**: An invocation of `__repr__()` returns a developer-friendly printable
string that can be used by a debugger to reconstruct a representation of the
original object.
- **E**: If we do not implement `__repr__()`, then a call to `repr()` on an object
invokes `__str__()`.

**Answer**: **A, B, D**


**Explanation**:
- `__repr__()` is meant for developers and returns an unambiguous string that can
recreate the object.
- `__str__()` is for end-users, providing a readable or printable representation.
- If `__str__()` is missing, Python falls back to `__repr__()`.

---

... (content continues for Questions 4 through 30)

You might also like