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.
In a store, there is an inventory of 4,800 caps. Black caps constitute 41(2/3) % of this total, with the rest being blue caps. The caps that have been s...
An integer 'p' is added to each of 11, 17, 29 and 41 so that the resulting numbers will be in proportion in given order only. Find the value of '(p + 4)'.
The ratio of number of boys and girls in a factory of 600 workers is 7:3. How many more girls should be joined to make the ratio 1:1?
The sum of three numbers is 189. If the ratio of the first to the second is 3:4 and that of second to the third is 5:7 then the second number is?
Ratio of male to female population of a town in 2010 was 13:12 and total population in 2010 was 25000. If the male population is ___ and female populat...
In a class, the ratio of number of boys to number of girls is 15:11. If 3 boys joined the class and 1 girl left the class, then the ratio becomes 7:5. ...
Monthly savings of ‘Q’ is 20% more than that of ‘P’ and is 44% less than monthly income of ‘P’. Monthly expenditure of ‘Q’ is Rs. 4760 a...
Some amount was divided among A, B and C in the ratio 3:5:8. If the share of A is 40 less than share of C, then find the 1/4th of total amount (in Rs).
- Find the fourth proportion of (k + 3), (2k - 6), and (4k - 9). (Note: 'k' is the smallest two-digit prime number.)
Rs. 7,20,000 is divided among 132 men and women. The ratio of total amount received by men and women is 5 : 4. But the ratio of money received by each m...