Question
Which of the following statements about parameter
passing in Python is TRUE?Solution
In Python, arguments are passed to functions using pass-by-object-reference , a hybrid approach where the behavior depends on the object’s mutability. For mutable objects (e.g., lists, dictionaries), functions can modify the object directly, as the reference points to the same memory location. For immutable objects (e.g., integers, strings), any attempt to modify them results in a new object being created, leaving the original object unchanged. For example: def modify_list(lst): lst.append(4) my_list = [1, 2, 3] modify_list(my_list) print(my_list) # Output: [1, 2, 3, 4] In the case of immutables: def modify_int(x): x += 1 num = 5 modify_int(num) print(num) # Output: 5 This illustrates that Python’s approach is neither traditional pass-by-value nor pass-by-reference. Why Other Options Are Incorrect:
- Option A: Python does not strictly follow pass-by-value. Instead, objects are passed as references, which may or may not result in modification based on their mutability.
- Option B: This incorrectly splits the behavior based on mutability. Both mutable and immutable objects are passed by reference; the difference lies in how modifications are handled.
- Option D: This is partially true in terms of scope but does not explain how references to objects work.
- Option E: Functions can indeed modify mutable arguments, as shown in the first example.
A person invests a sum of money in two schemes, P and Q. Scheme P offers simple interest, and Scheme Q offers compound interest. The amount invested in ...
In each of the following questions, a question is followed by information given in three statements. You have to study the question along with the stat...
What is the capacity of a cylindrical tank?
I. The radius of the base is half of its height, which is 42 meters.
II. The area of the base ...
What is the profit%/loss% incurred by selling an article for Rs. 44,000?
Statement I: The difference between the cost price and the selling pric...
What is the value of number x ?
I. The HCF of x and 16 is 8.
II. The LCM of ...
There are 7 people in a group. Average age of the group decreases by 5 when two more people join the group. Find the average age of the new group.
<...Determine the time taken by a pipe R to empty a water tank.
Statement I: Pipe P and pipe Q can fill a water tank in 6 hou...
How much profit did the company earn in the year 2016?
I) The company earned 45% more profit in the year 2017 than that in the year 2015.
...
A box contains red and blue marbles. What is the ratio of red marbles to blue marbles in the box?
Statements I: The number of red marbles is 4...
How many students in a class can write both French and Spanish?
I. In the class, 25% of the total students can write Spanish and two-fifth of the...