PRIP 3.3 Sumita Arora Solutions | Class 12 Computer Science

Here are PRIP 3.3 Sumita Arora Solutions for class 12 Computer Science. To view Sumita Arora solutions for all chapters, visit here.

Q.1: Consider the following code. Identify the types and values of arguments being passed for each function call.
def func( a, b = 5, c = 10 ):
    poly = 2*a**2 - 5*b + c
    print("with values", a, b, c) 
    print(poly)

func( 3,7)                             # function call 1
func(25, c=24)                    # function call 2
func(c=50, a=100)             # function call 3

Answer:

Function call 1

Types of Arguments:

  
 

a gets     3

b gets      7

c gets        10

Function call 2

Types of Arguments:

  
 

a gets     25

b gets      5

c gets        24

Function call 3

Types of Arguments:

  
 

a gets     100

b gets      5

c gets        50

Q.2: What will be the output produced by above program (given in question 1)?

Answer:

Code:
def func( a, b = 5, c = 10 ):
    poly = 2*a**2 - 5*b + c
    print("with values", a, b, c) 
    print(poly)
func( 3,7)                             # function call 1
func(25, c=24)                    # function call 2
func(c=50, a=100)             # function call 3


Output:
with values 3 7 10
-7
with values 25 5 24
1249
with values 100 5 50
20025

Q.3: Consider following function header
def robot(voltage, state = 'a stiff', action='voon', type = 'Norwegian Blue'):
For the above function, some function calls are being given below. Find the errors in these function call statements :

Answer:

 

Function call

State reason(s) for these calls being invalid

(a)

robot()

voltage is required argument calling robot

Error :
TypeError: robot() missing 1 required positional argument: ‘voltage’
Valid calls:
robot(5.0)
robot(voltage= 5.0) 

(b)

robot(voltage=5.0, ‘dead’)

while function call positional arguments should appear before keyword arguments
in example even we specify voltage=5.0 ,‘dead’ also considered as parameter for voltage

Error:
#SyntaxError: positional argument follows keyword argument
valid calls:
robot(voltage = ‘dead’)
robot(‘dead’)
robot(5.0)
robot(voltage = 5.0) 

(c)

robot(110, voltage = 220)

110 is parameter for positional argument voltage
In function call 2 values are passed for voltage that’s why
python interpreter gives error:
TypeError: robot() got multiple values for argument ‘voltage’
Valid calls:
robot(100)
robot(voltage= 220)

(d)

robot(actor = ‘ Harrison Ford ’)

In function definition there is no argument named actor
So python interpreter gets argument which is not expected and gives error:
TypeError: robot() got an unexpected keyword argument ‘actor’

Q.4: What is wrong with following function code? Find reason and correct it.
def addThree(n1, n2, n3):
    return n1 + n2 + n3 
    print("the answer is", n1 + n2 + n3)

Answer:

Reason

function body return addition of n1,n2,n3 first and then prints statement
Due to return statement control of execution transfers from where function is called

Correct Code

def addThree(n1, n2, n3):
    print("the answer is", n1 + n2 + n3)
    return n1 + n2 + n3 
Q.5: Following code is giving an error at line number 6. Why is this error occurring ? What is the solution?
1. def c_to_f(c):
2.     result = c/5.0*9+ 32
3.     return result
4.
5. tempf = c_to_f(19)
6. print(result)

Answer:

Reason

Error raised by code is:
NameError: name ‘result’ is not defined

Reason:
result is a local variable i.e. it has scope limited to only function c_to_f
to print result print statement should be in c_to_f function
or declare result as global variable

Correct Code

Solution 1:
def c_to_f(c):
    result = c/5.0*9+ 32
    print(result)
    return result
tempf = c_to_f(19)


Solution 2:
def c_to_f(c):
    global result
    result = c/5.0*9+ 32
    return result
tempf = c_to_f(19)
print(result)
Q.6: Given some programs below. Write their flow of execution.

Answer:

Program CodeFlow of Execution
1. def square(x):
2.     return x*x3.4. square(5)
1 -> 4 -> 1 -> 2 -> 4
1. def square(x):
2.     return x*x
3.
4. print(square(5) + square(6))
1 -> 4 -> 1 -> 2 -> 4 -> 1 -> 2 -> 4
1. def square(x):
2.     return x*x
3.
4. def sum_of_squares (x, y):
5.     return square(x) + square(y)
6.
7. k = sum_of_squares(2, 3)
8. print(“sum 1s”, k)
1 -> 4 -> 7 -> 4 -> 5 -> 1 -> 2 -> 5 -> 1 -> 2 -> 5 -> 7 -> 8
Q.7: From the program given below, identify the scope of each variable in the program
x = 10                               # Reference 1
pi = 3.14

def incr(x):                       # Reference 2
    y = x+1 
    return y

def area(r):
    n = incr(r)
    ar = pi * n * n
    return ar

incr(5)
print(x)
area(4)

Answer:

Variable Name

Scope

x (Reference 1)

Scope of x is in whole program, including every function 

x (Reference 2)

Scope of x is in incr() function only

pi

Scope of pi is in whole program, including every function

y

Scope of y is in incr() function only

n

Scope of n is in area() function only

ar

Scope of ar is in area() function only

r

Scope of r is in area() function only

Q.8: Write a function that takes a character (i.e., a string of length 1) and returns True if it i a vowel, False otherwise. (Hint: Make use of logical operators.) 
Make four calls to this function for testing letters ‘u’,’k’,’m’,’i’.

Answer:

Code:
def isVowel(str):
    if(str == 'a' or str == 'e' or str == 'i' or str == 'o' or str == 'u'):  
        return True                  # if str is equal to any one of 'a', 'e', 'i', 'o', 'u'
                                      # It will return True
    return False 
print(isVowel('u'))
print(isVowel('k'))
print(isVowel('m'))
print(isVowel('i'))


Output:
True
False
False
True

Q.9: A Mersenne number is a number in the form 2^n-1 i.e.,
1st Mersenne number is 2^1-1=1
2nd Mersenne number is 2^2-1=3
3rd Mersenne number is 2^3-1=7
and so on.
Write a program that passes value to a function mersenne() and the function returns nth Mersenne number.

Answer:

Code:
def mersenne(n):
    return 2**n - 1  # 2**n is for 2^n

print( mersenne(1))
print( mersenne(2))
print( mersenne(3))
print( mersenne(4))


Output:
1
3
7
15
Q.10: Write a void function that receives a 4 digit number and calculates the sum of squares of first 2 digits’ number and last two digits’ number, e.g. if 1233 is passed as argument then function should calculate 122 +332 ?

Answer:

Code:
def calculate(num):
    last = num%100   # to get last two digits
    first = num//100  # to get first two digits floor value
    ans = (first**2) + (last**2)
    print("Answer is: {} ".format(ans))

calculate(1233)
calculate(6531)
calculate(4238)
calculate(1263)
calculate(9333)


Output: 
Answer is: 1233 
Answer is: 5186 
Answer is: 3208 
Answer is: 4113 
Answer is: 9738
Q.11: Write a function that takes one argument (a positive integer) and reports if the argument is prime or not. Write a program that invokes this function.

Answer:

Code:   
def isPrime(num):
    if num > 1:  
        for i in range(2,int(num/2)): # check if num is divisible by any number between 2 to num/2
            if (num % i) == 0:         # if true num id not prime number
                return False
        else:                          # else executes after for loop completes it's execution   
            return True  
    else:                              # if num <= 1 it's not prime number
            return False
   
print(isPrime(0))
print(isPrime(1))
print(isPrime(2))
print(isPrime(3))
print(isPrime(4))
print(isPrime(5))
print(isPrime(6))


Output:
False
False
True
True
True
True
False

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

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You cannot copy content of this page