NCERT Computer Science Class #11 – Chapter #7 Solutions – Q #1

Chapter #7 – Functions

NCERT Computer Science Class #11 – Chapter #7 Solutions – Q #1

(a) Observe the following program carefully, and identify the error:

def create (text, freq):
     for i in range (1, freq):
          print text
 create(5)          #function call

Issues in the Code



Incorrect Function Call: The function create is defined to take two parameters (text and freq), but it is called with only one argument (5). This will raise a TypeError.

Print Syntax: If you are using Python 3, the print statement should be a function call with parentheses. The correct syntax is print(text) instead of print text.

Loop Range: The loop in the function starts from 1 and goes up to freq - 1. If you want to print the text freq times, you should adjust the range to include freq.

Corrected Code

def create(text, freq):
    for i in range(freq):  # This will iterate freq times
        print(text)        # Use parentheses for print in Python 3

# Correct function call with two arguments
create("Hello, World!", 5)

(b) Observe the following program carefully, and identify the error:

from math import sqrt,ceil
 def calc():
     print cos(0)
 calc()             #function call

Issues in the Code:

Missing Import for cos: The cos function is not imported from the math module. You need to include it in your import statement.

Print Syntax: If you are using Python 3, the print statement should be a function call with parentheses. The correct syntax is print(cos(0)).

Corrected Code

from math import sqrt, ceil, cos  # Importing cos

def calc():
    print(cos(0))  # Using parentheses for print

calc()  # Function call

(c) Observe the following program carefully, and identify the error:

mynum = 9
 def add9():
     mynum = mynum + 9
     print mynum
 add9()              #function call

Issues in the Code:

Local Variable Reference: In the function add9, you are trying to modify the variable mynum, which is defined outside the function. In Python, this will raise an Error because Python treats mynum as a local variable within the function due to the assignment statement.

Print Syntax: If you are using Python 3, the print statement should use parentheses. The correct syntax is print(mynum).

Corrected Code:

mynum = 9

def add9():
    global mynum  # Declare mynum as a global variable
    mynum = mynum + 9
    print(mynum)  # Use parentheses for print in Python 3

add9()  # Function call

(d) Observe the following program carefully, and identify the error:

def findValue( vall = 1.1, val2, val3):
     final = (val2 + val3)/ vall
     print(final)
 findvalue()          #function call

Issues in the Code:

Default Argument Placement: In Python, default arguments must be placed after non-default arguments in the function definition. In this case, vall has a default value, but val2 and val3 do not, which is not allowed.

Function Call: The function is called as findvalue(), but the correct name is findValue() (note the capitalization). This will raise a NameError.

Missing Arguments: The function findValue requires at least two arguments (val2 and val3) when called, but none are provided in the function call.

Corrected Code:

def findValue(val2, val3, vall=1.1):  # Default argument must come last
    final = (val2 + val3) / vall
    print(final)

# Correct function call with two required arguments
findValue(5, 10)  # Example call with val2=5 and val3=10

(e) Observe the following program carefully, and identify the error:

def greet():
     return("Good morning")
 greet() = message  #function call

Issues in the Code:

Assignment Syntax: The line greet() = message is incorrect. In Python, you cannot assign a value to a function call directly. Instead, you should assign the result of the function call to a variable.

Corrected Code:

def greet():
    return "Good morning"  # No need for parentheses around the string

# Correct assignment of the function's return value to a variable
message = greet()

NCERT Computer Science Class 11 Chapter 7 Solutions Q 1

Posts created 29

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top