Question

In: Computer Science

Part 1 Here is some code: def foo(var): var = [] var.append("hello") var.append("world") list = ["bah"]...

Part 1

Here is some code:

def foo(var):
    var = []
    var.append("hello")
    var.append("world")

list = ["bah"]
foo(list)
print(list)

Which Option is correct?

A) ["hello","world"] is output because python passes the list by reference and the list is changed inside the function.

B) ["bah"] is output because python passes the list by value so changes to the list in the function are made to a separate list than the one that is passed in.

C) ["bah"] is output because python passes the list by reference but a new reference is created by the first line in the function so subsequent changes are made to a separate list than the one that is passed in.

Part 2

Consider this code:

list1 = []
list2 = list1

list2.append(1)
list2.append(2)
list1.append(3)

Which option is correct?

A) list1 contains [3] and list2 contains[1,2]

B) list1 contains [3] and list2 references the same list as list1

C) list1 contains [1,2,3] and list2 references the same list as list1

D) list2 contains [1,2,3] and list1 also contains [1,2,3] but the 3 is stored in a different location than list2


Solutions

Expert Solution

Part 1-

def foo(var):
    var = []
    var.append("hello")
    var.append("world")

list = ["bah"]
foo(list)
print(list)

ANSWER- C) ["bah"] is output because python passes the list by reference but a new reference is created by the first line in the function so subsequent changes are made to a separate list than the one that is passed in.

EXPLAINATION- Let's first understand what is a call by reference. In call by reference the reference or address of the variable or list is passed to the function or method. And hence, any change in the variable inside the function is reflected in our main variables or more specifically called actual arguments.

Now, in python list is by default passed as call by reference that means any change in the list inside the function will be reflected in the actual arguments. But then why the output is not ["hello","world"] here. That is because the new reference is created by the first line in the function so subsequent changes are made to a separate list than the one that is passed in.

Part 2-

list1 = []
list2 = list1

list2.append(1)
list2.append(2)
list1.append(3)

ANSWER-   list1 contains [1,2,3] and list2 references the same list as list1.

EXPLAINATION- In python the assignment operator makes the two list point to a single list. That means what list2=list1 does is it points the same list. So any change in either of them will be reflected in both the lists because the are referencing to same chunk of memory.

HAPPY LEARNING


Related Solutions

import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1...
import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1     print(var)     function2(var) def function2(myVar):     print(myVar)     var = myVar + 1     print(var)     function3(var) def function3(myVar):     print(myVar)     var = myVar + 1     print(var) def main(argv):     var = 10     print(var)     function1(var) if __name__=="__main__":     main(sys.argv) 1. As the program runs, what is the first line that will be interpreted by Python, and what action will...
Valuation of DEF Corp Here is some data about DEF Corp (in 000s): Sales for last...
Valuation of DEF Corp Here is some data about DEF Corp (in 000s): Sales for last year $560,000. Sales are predicted to grow at 8% for years 1-5 and 3% after that. The following are expressed as a % of sales: COGS % of Sales 42% Gr Assets % of Sales 26% Inv. % of Sales 16% Assume Inventory is the only working capital account Depreciation is 12% of Gross Fixed Assets The company’s tax rate is 22% DEF Corp....
Javascript Problem: List Reverse Given a list: var list = { value: 1, next: { value:...
Javascript Problem: List Reverse Given a list: var list = { value: 1, next: { value: 2, next: { value: 3, next: null } } }; Reverse the order of the list so that it looks like: var list = { value: 3, next: { value: 2, next: { value: 1, next: null } } }; Use the following shell if needed: //assignment1.js function reverseList(list) { // your code here ... return reversedList; } Example Test Case(s): Arguments: { value:...
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def;...
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def; ➢ Describe the procedure how much memory will be allocated when these three lines of codes will execute. ➢ Describe what will happened after execution of each of these line of code in term of memory allocation and data storage Qno.1 Part(B) A capacitor is constructed from two conductive metal plates 30cm x 50cm which are spaced 6mm apart from each other, and uses...
Hello: I am working on some homework problems for decision modeling. Here is the question: The...
Hello: I am working on some homework problems for decision modeling. Here is the question: The weekly deman for a slow moving product has the following probability mass function: Demand, x Probability f(x) 0 0.2 1 0.4 2 0.3 3 0.1 4 or more 0 Use VLOOKUP to generate 25 random variates from this distribution. I know i have to use RAND and VLOOKUP I just cannot get it to give me 25 random numbers. Thank you for any help...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
Modify the following source code so that five subsequent threads print the message “Hello World number,”...
Modify the following source code so that five subsequent threads print the message “Hello World number,” where number indicates the unique thread created; e.g. “Hello World 1” // Your_name_goes_here #include <pthread.h> #include <semaphore.h> sem_t semaphore; // also a global variable just like mutexes void *thread_function( void *arg ) { sem_wait( &semaphore ); // perform some task pthread_exit( NULL ); } int main() { int tmp; tmp = sem_init( &semaphore, 0, 0 ); // initialize pthread_create( &thread[i], NULL, thread_function, NULL );...
Here is an outline for some code I need to write. This class used Intro to...
Here is an outline for some code I need to write. This class used Intro to Java Edition 11, and we only got to Chapter 9. There is no set right way that this program has to be done. Feel free to interpret this in a way that you wish. Mrs. Quackenbush is back! She now has bought a beverage establishment, The Green Dragon Inn, and needs to have a way to insure the consistency of the drinks her employees...
Hello, Here is the question: Use the sample from Topic 2 DQ 1 and test that...
Hello, Here is the question: Use the sample from Topic 2 DQ 1 and test that the population mean for rolling a single die is 3.5. Here is my dice roll. 6,3,4,3,2,2,2,4,3,5,1,5,4,3,4,2,2,1,6,2,4,3,5,1,3,3,5,2,5,5,3,4,4,2,3,5,1,4,1,6,6,4,4,1,1,4,1,2,2,4 Mean: 3.2400 Median: 3.00 Mode: 4.00 Variance: 2.3494 Standard deviation: 1.5328
Hello Here are few questions from economics 1) What is the meaning of capital in economics?...
Hello Here are few questions from economics 1) What is the meaning of capital in economics? There are physical capital, human capitals, natural resources per worker, technological resources. What is the factor of production? Please provide examples 2) How can you produce capital? What is the meaning of accumulation of capital? Please provide example 3) What is the difference between consumption goods and capital goods? Please provide an example of each 4)What are the diminishing returns? Please provide example
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT