Anjeev Singh Academy

Question – 7: Predict the output of the following code :

Answer: Output:-

y = 5 a = 10

y = 10 a = 2

a + y = 12

y = 5 a = 10

Question – 8: What is wrong with the following function definition?
def addEm(x, y, z):
      return x + y + z
      print("the answer is", x + y + z)

Answer: The control can not reach the print( ) statement. It should be written before return statement.

def addEm(x, y, z):
       print("the answer is", x + y + z)
       return x + y + z

Question – 9: Create a Function that takes no parameter and returns None.

Answer:

def fun():
     return None

Question – 10: Consider the code given (for code refer to book) and answer the questions –

(i) When the code is above executed, what prints out?

Answer: Output:- 5 times 5 = 25

(ii) What is the variable output equal to after the code is executed?

Answer: The value of variable output is 25

Question – 11: Same as Q 10

Answer:

Question – 12: Find the errors [for code refer to book]-

(a)

Answer: – (a) Missing of colon , in function header. It should be –

def minus (total, decrement ) :

(b) and (c)

Answer: – (b) Error i -> In place of define write def in function header. Error ii-> Missing of colon , in function header. Error iii -> N is string can not be used in arithmetic expression. Error iv -> Return should be return.

Corrected code is:-

def check( ):    # Error (i) & (ii)
     N = input('Enter N :')
     i = 3
     answer = 1 + i ** 4 / int(N)    #Error (iii)
     return answer     #Error (iv)

(c)

Answer: – (c) Error i -> In function alpha, return statement used two times. It should be one time.

Error ii -> In function beta, Name Error – name ‘n’ is not defined.

Error iii -> Invalid use of : inside the print() .

Error iv -> Invalid use of : inside the print() .

Question – 13: Draw the environment variable

Answer: uploading soon

Question – 14: Draw flow of execution of above code

Answer: 12 -> 1 -> 2 -> 3 -> 4 -> 12 -> 6 -> 7 -> 12 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 10 -> 6 -> 7 -> 10 -> 12

Question – 15: In the given code, which variables are in the same scope?

Answer: a & b, and c & d

Question – 16: Write a program with a function that takes an integer and prints the number that follows after it. Call the function with these arguments.

4, 6, 8, 2 + 1, 4 – 3 * 2, -3 -2

Answer:

def printfollow(n):
    print(n+1)
printfollow(4)
printfollow(6)
printfollow(8)
printfollow(2+1)
printfollow(4-3*2)
printfollow(-3-2)

Output:

5
7
9
4
-1
-4

Question – 17: Write a program with non-void version of above function and then write the flow of execution for both the programs.

Answer:

def printfollow(n):
    retutn (n+1)
print(printfollow(4))
print(printfollow(6))
print(printfollow(8))
print(printfollow(2+1))
print(printfollow(4-3*2))
print(printfollow(-3-2))

Output

5
7
9
4
-1
-4

Flow of control of both program Q-16 & Q-17 are :

3->1->2->4->1->2->51->2->6->1->2->7->1->2->8->1->2->End of program

Question – 18: What is the output of following code fragments

(a)

def increment(n):
    n.append([4])
    return n

L = [1, 2, 3]
M = increment(L)
print(L, M)

Answer: Output:

[1, 2, 3, [4]] [1, 2, 3, [4]]

Question – 18: What is the output of following code fragments

(b)

def increment(n):
    n.append([49])
    return n[0], n[1], n[2], n[3]

L = [23, 35, 47]
m1, m2, m3, m4 = increment(L)
print(L)
print(m1, m2, m3, m4)
print(L[3] == m4)

Answer: Output:

[23, 35, 47, [49]]
23 35 47 [49]
True

Chapter 3 – Working with Functions

Type C: Programming Practice / Knowledge based Questions

Question – 1: Write a function that takes amount-in-dollars and dollar-to-rupee conversion price; it then returns the amount converted to rupees. Create the function in both void and non-void forms.

Answer: #Dollar to Rupees Conversion

#Que - 1 Dollar To Rupees

#Non-void Function
def dollar_to_rupee(amount):
    return amount * 75.45

#Void Function
def dollar_to_rup(amount):
    print(amount * 75.45)

print("Dollar to Rupee :: ", dollar_to_rupee(5))
dollar_to_rup(10)

Output:

Dollar to Rupee :: 377.25
754.5

Question – 2: Write a function to calculate volume of a box with appropriate default values for its parameters. You function should have the following input parameters.
  • (a) length of box;
  • (b) width of box;
  • (c) height of box.

Test it by writing complete program to invoke it.

Answer: #Function to calculate volume of box with default value

#Que - 2 : Function to calculate Volume of a box

def calculate_box(length = 3, width = 4, height = 5):
    return length * width * height

print("calculate_box()=>",calculate_box())
print("calculate_box(10, 20, 15)=>",calculate_box(10, 20, 15))
print("calculate_box(height = 12) =>",calculate_box(height = 12))
print("calculate_box(10)=>",calculate_box(10))

Output:-

calculate_box()=> 60
calculate_box(10, 20, 15)=> 3000
calculate_box(height = 12) => 144
calculate_box(10)=> 200

Question – 3: Write a program to have following functions :
  • (i) a function that takes a number as argument and calculates cube for it. The function does not return a value. If there is no value passed to the function in function call, the function should calculate cube of 2.
  • (ii) a function that takes two char arguments and returns True if both the arguments are equal otherwise False.

Answer: #Function to calculate (i) Cube of a number and (ii) Compare two chars

#(i) Cube of a number

def cube(n = 2):
    print(n * n * n)

#(ii) Compare two chars

def compare(ch1, ch2):
    if ch1 == ch2:
        return True
    else:
        return False

print("cube() =>")
cube()
print("cube(5) =>")
cube(5)

print("compare('a', 'b') =>", compare('a', 'b'))
print("compare('a', 'a') =>",compare('a', 'a'))

Output:

cube() =>
8
cube(5) =>
125
compare(‘a’, ‘b’) => False
compare(‘a’, ‘a’) => True

Question – 4: Write a function that receives two numbers and generates a random number from that range . Using this function, the main program should be able to print three numbers randomly.

Answer: #Function to generate random number. Creates three random number using this function.

import random

def generate_random(a, b):
    return random.randint(a,b)

print("First Random Number :: ",generate_random(5,10))
print("Second Random Number :: ",generate_random(50,100))
print("Third Random Number :: ",generate_random(1, 25))

Output:

First Random Number :: 6
Second Random Number :: 75
Third Random Number :: 1

First Random Number :: 7
Second Random Number :: 57
Third Random Number :: 8

First Random Number :: 10
Second Random Number :: 93
Third Random Number :: 19

Sorry! You cannot copy content of this page. Please contact, in case you want this content.

Scroll to Top