Here are PRIP 3.4 Sumita Arora Solutions for class 12 Computer Science. To view Sumita Arora solutions for all chapters, visit here.
Q.1: Consider following program code.
1. def myFunc(l, v):
2. l[0] += 2
3. v += 2
4. print("Values/variables within called function :", l, v)
5. return
6.# __main__
7. list1 = [1]
8. var1 = 1
9.print("Values/variables before function call :", list1, var1)
10. myFunc(list1, var1)
11. print("Values/variables after function call :", list1, var1)
2. l[0] += 2
3. v += 2
4. print("Values/variables within called function :", l, v)
5. return
6.# __main__
7. list1 = [1]
8. var1 = 1
9.print("Values/variables before function call :", list1, var1)
10. myFunc(list1, var1)
11. print("Values/variables after function call :", list1, var1)
(i) Identify flow of execution in the above program
Answer:
1 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 5 -> 10 -> 11
(ii) What will be the output of above program?
Answer:
Output:
Values/variables before function call: [1] 1
Values/variables within called function: [3] 3
Values/variables after function call: [3] 1
(iii) What is reason of this output?
Answer:
for var1: value after function call is not changed because int datatype is immutable
ex:
a=5 -> here a is pointing to memory location which holds 5
a=+1 -> instead of updating the memory location a new mwmory holds 6and now a points to that
Remember, 5 is still on that previous memory location without any variable representing it
for list1: values in list are updated because lists and dictionaries are mutable,
Changing them (even inside a function) changes the list or dictionary itself
Which isn’t the case for immutable data types.
Because Python stores variables from a function in a different memory location from global variables they are isolated. Thus, the variable var_1 can have one value (1) globally, and a different value (3) inside the function, where it is isolated.
Q.2: Consider following program code, which is similar to the previous program but the function myfunc() has some changes as shown below :
1. def myFunc(list1, var1):
2. list1[0] += 2
3. var1 += 2
4. print("Values/variables within called function :", list1, var1)
5. return
6.# __main__
7. list1 = [1]
8. var1 = 1
9.print("Values/variables before function call :", list1, var1)
10. myFunc(list1, var1)
11. print("Values/variables after function call :", list1, var1)
1. def myFunc(list1, var1):
2. list1[0] += 2
3. var1 += 2
4. print("Values/variables within called function :", list1, var1)
5. return
6.# __main__
7. list1 = [1]
8. var1 = 1
9.print("Values/variables before function call :", list1, var1)
10. myFunc(list1, var1)
11. print("Values/variables after function call :", list1, var1)
(i) Identify flow of execution in the above program
Answer:
1 -> 7 -> 8 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 5 -> 10 -> 11
(ii) What will be the output of above program?
Answer:
Output:
Values/variables before function call: [1] 1
Values/variables within called function: [3] 3
Values/variables after function call: [3] 1
(iii) What is reason of this output?
Answer:
Even the variable names are same list1and var1 are pointing to different locations
When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function i.e.
variable names are local to the function. This is called the scope of the variable.
for var1: value after function call is not changed because int datatype is immutable
ex:
a=5 -> here a is pointing to memory location which holds 5
a=+1 -> instead of updating the memory location a new memory holds 6and now a points to that
remember, 5 is still on that previous memory location without any variable representing it
for list1: values in list are updated because lists and dictionaries are mutable, changing them (even inside a function) changes the list or dictionary itself which isn’t the case for immutable data types. because Python stores variables from a function in a different memory location from global variables. They are isolated. Thus, the variable var_1 can have one value (1) globally, and a different value (3) inside the function, where it is isolated.
(iv) Try making memory environment(s) for above program
Answer:
This question is too ambiguous to answer. Hence, we are leaving this question unanswered and in case you've answer for it - we will be happy to add it for you, just write your answer to [email protected] and we'll be happy to give you credits :)
Q.3: Consider following program code
1. def myFunc(l, v):
2. l[0] += 2
3. v += 2
4. N = [2, 3, 4]
5. l=N
6. l.append(7)
7. print(1, v)
8. return
9. #__main__
10. list1 = [1]
11. var1 = 1
12. print("Values/variables before function call :", list1, var1)
13. myFunc(list1, var1)
14. print("Values/variables after function call : ", list1, var1)
1. def myFunc(l, v):
2. l[0] += 2
3. v += 2
4. N = [2, 3, 4]
5. l=N
6. l.append(7)
7. print(1, v)
8. return
9. #__main__
10. list1 = [1]
11. var1 = 1
12. print("Values/variables before function call :", list1, var1)
13. myFunc(list1, var1)
14. print("Values/variables after function call : ", list1, var1)
(i) Identify flow of execution in the above program
Answer:
1 -> 9 -> 10 -> 11 -> 12 -> 13 ->1 -> 2 -> 3 -> 4 -> 5 -> 6-> 7-> 8-> 13 -> 14
(ii) What will be the output of above program?
Answer:
Output:
Values/variables before function call: [1] 1
[2, 3, 4, 7] 3
Values/variables after function call: [3] 1
(iii) What is reason of this output?
Answer:
when list1 and var1 are passed as argument to myFunc() initially l holding reference of list1 and changes made in list l also changed in list1 because both pointing to same memory address when l is assigned to list N all changes made after that are not shown in list1 because list l is pointing where list N is pointing and not where list list1.
(iv) Try making memory environment(s) for above program
Answer:
This question is too ambiguous to answer. Hence, we are leaving this question unanswered and in case you've answer for it - we will be happy to add it for you, just write your answer to [email protected] and we'll be happy to give you credits :)
Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in