Python Functions: book back answers

24 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.

← Notes and quiz for Python Functions

Choose the best answer: (1 Mark)

  1. A named blocks of code that are designed to do one specific job is called as

    • (a) Loop
    • (b) Branching
    • (c) Function
    • (d) Block
    Try it, then show the answer

    (c) Function

    Functions are named blocks of code designed to do a specific job.

    From the book: “Functions are named blocks of code that are designed to do specific job.”

  2. A Function which calls itself is called as

    • (a) Built-in
    • (b) Recursion
    • (c) Lambda
    • (d) return
    Try it, then show the answer

    (b) Recursion

    A function that calls itself is known as recursion.

    From the book: “When a function calls itself is known as recursion.”

  3. Which function is called anonymous un-named function

    • (a) Lambda
    • (b) Recursion
    • (c) Function
    • (d) define
    Try it, then show the answer

    (a) Lambda

    Anonymous functions are also called lambda functions.

    From the book: “Hence, anonymous functions are also called as lambda functions.”

  4. Which of the following keyword is used to begin the function block?

    • (a) define
    • (b) for
    • (c) finally
    • (d) def
    Try it, then show the answer

    (d) def

    A function block begins with the keyword def.

    From the book: “Function blocks begin with the keyword “def””

  5. Which of the following keyword is used to exit a function block?

    • (a) define
    • (b) return
    • (c) finally
    • (d) def
    Try it, then show the answer

    (b) return

    The return statement causes a function to exit and returns a value to its caller.

    From the book: “The return statement causes your function to exit and returns a value to its caller.”

  6. While defining a function which of the following symbol is used.

    • (a) ; (semicolon)
    • (b) . (dot)
    • (c) : (colon)
    • (d) $ (dollar)
    Try it, then show the answer

    (c) : (colon)

    The function code block comes after a colon.

    From the book: “The code block always comes after a colon (:) and is indented.”

  7. In which arguments the correct positional order is passed to a function?

    • (a) Required
    • (b) Keyword
    • (c) Default
    • (d) Variable-length
    Try it, then show the answer

    (a) Required

    Required arguments are passed in the correct positional order.

    From the book: ““Required Arguments” are the arguments passed to a function in correct positional order.”

  8. Read the following statement and choose the correct statement(s). (I) In Python, you don’t have to mention the specific data types while defining function. (II) Python keywords can be used as function name.

    • (a) I is correct and II is wrong
    • (b) Both are correct
    • (c) I is wrong and II is correct
    • (d) Both are wrong
    Try it, then show the answer

    (a) I is correct and II is wrong

    The chapter's function syntax does not require declaring parameter data types. It explicitly says Python keywords should not be used as function names.

    From the book: “Python keywords should not be used as function name.”

  9. Which of the following keyword is used to define the function testpython(): ?

    • (a) define
    • (b) pass
    • (c) def
    • (d) while
    Try it, then show the answer

    (c) def

    Function blocks begin with the keyword def followed by the function name.

    From the book: “Function blocks begin with the keyword “def” followed by function name and parenthesis ().”

Part - II Answer the following questions: (2 Marks)

  1. What is function?

    Try it, then show the answer

    A function is a group of related statements that performs a specific task.

    Functions are named code blocks dedicated to a particular job.

    From the book: “Functions are nothing but a group of related statements that perform a specific task.”

  2. Write the different types of function.

    Try it, then show the answer

    User-defined functions, built-in functions, lambda functions and recursion functions.

    The chapter divides Python functions into these four types.

    From the book: “Types of Functions are User defined, Built-in, lambda and recursion.”

  3. What are the main advantages of function?

    Try it, then show the answer

    Functions avoid repetition and enable code reuse. They provide better modularity for an application.

    Functions make programs easier to reuse and divide into manageable parts.

    From the book: “It avoids repetition and makes high degree of code reusing.”

  4. What is meant by scope of variable? Mention its types.

    Try it, then show the answer

    Scope of a variable is the part of a program where it is accessible. Its types are local scope and global scope.

    The chapter identifies local and global as the two types of scope.

    From the book: “Scope of variable refers to the part of the program, where it is accessible”

  5. Define global scope.

    Try it, then show the answer

    A variable with global scope can be used anywhere in the program and can be created outside the scope of any function.

    A variable defined outside a function has global scope by default.

    From the book: “A variable, with global scope can be used anywhere in the program.”

  6. What is base condition in recursive function

    Try it, then show the answer

    The condition applied in a recursive function is called the base condition. It must be present to prevent infinite execution.

    When the base condition is met, the program produces meaningful output and exits.

    From the book: “The condition that is applied in any recursive function is known as base condition.”

Part - III Answer the following questions: (3 Marks)

  1. Write the rules of local variable.

    Try it, then show the answer

    A local variable can be accessed only within the function where it is created. It exists only while that function is executing. Formal parameters are also local to the function.

    These are the rules listed for local scope in the chapter.

    From the book: “A variable declared inside the function's body is known as local variable.”

  2. Write the basic rules for global keyword in python.

    Try it, then show the answer

    A variable defined outside a function is global by default, so the global keyword is not needed to define it. Use global to modify a global variable inside a function. Using global outside a function has no effect.

    These are the three basic rules given for the global keyword.

    From the book: “We use global keyword to modify the value of the global variable inside a function.”

  3. What happens when we modify global variable inside the function?

    Try it, then show the answer

    Without using the global keyword, modifying a global variable inside a function causes an UnboundLocalError. With the global keyword, the function can modify the global variable, and the change is visible outside the function.

    The chapter demonstrates both the error without global and a successful modification with global.

    From the book: “Without using the global keyword we cannot modify the global variable inside the function”

  4. Differentiate ceil() and floor() function?

    Try it, then show the answer

    ceil(x) returns the smallest integer greater than or equal to x. floor(x) returns the largest integer less than or equal to x.

    Both are mathematical functions used through the math module.

    From the book: “Returns the largest integer less than or equal to x”

  5. What is composition in functions?

    Try it, then show the answer

    Composition is using the value returned by one function as an argument for another function in a nested manner.

    For example, the chapter describes using eval() on input obtained from input().

    From the book: “The value returned by a function may be used as an argument for another function in a nested manner.”

  6. How recursive function works?

    Try it, then show the answer

    An external part of the program calls the recursive function. If its base condition is met, it gives meaningful output and exits; otherwise, it processes the task and calls itself again.

    The base condition stops the recursive calls; without it, execution can continue like an infinite loop.

    From the book: “Otherwise, function does some required processing and then calls itself to continue recursion.”

  7. What are the points to be noted while defining a function?

    Try it, then show the answer

    A function block starts with def, followed by the function name and parentheses. Put any parameters inside the parentheses, follow the header with a colon, and indent the code block. Do not use Python keywords as function names.

    These are the function-definition requirements stated in the chapter.

    From the book: “The code block always comes after a colon (:) and is indented.”

Part - IV Answer the following questions: (5 Marks)

  1. Explain the following built-in functions. (a) id() (b) chr() (c) round() (d) type() (e) pow()

    Try it, then show the answer

    Built-in functions are functions inbuilt within Python. The following functions perform specific operations on the values supplied to them.

    1. id(): Returns the identity of an object, that is, its address in memory.
    2. chr(): Returns the Unicode character corresponding to the given ASCII value. It is the inverse of ord().
    3. round(): Returns the nearest integer to the given number. Its optional second argument, ndigits, specifies the number of decimal digits after rounding.
    4. type(): Returns the type of the given single object.
    5. pow(): Returns the result of raising the first argument to the power of the second argument, that is, a raised to the power of b.

    Example: For a = 5 and b = 2, pow(a,b) returns 25.

    These built-in functions return an object’s identity, a character, a rounded value, an object’s type, or a power calculation.

    From the book: “Returns the Unicode character for the given ASCII value.”

  2. Explain recursive function with an example.

    Try it, then show the answer

    Recursion is a method in which a function calls itself. A recursive function needs a base condition to stop it from continuing indefinitely.

    1. Meaning: When a function calls itself, it is known as recursion.
    2. Base condition: The condition applied in a recursive function is called the base condition. It is essential to prevent infinite execution.
    3. How recursion works: An external part of the program calls the recursive function. If the base condition is met, it produces a meaningful output and exits.
    4. Continued recursion: If the base condition is not met, the function performs the required processing and calls itself again.
    5. Use: Recursion works like a loop, and any loop can be converted to recursion.
    6. Factorial example: The chapter uses a recursive function to calculate factorial: it returns 1 when n is 0; otherwise, it returns n multiplied by the factorial of n−1.

    Example: def fact(n): if n == 0: return 1 else: return n * fact(n-1) print(fact(0)) # 1 print(fact(5)) # 120

    Thus, recursion solves a task by repeatedly calling the same function until its base condition is met.

    From the book: “A recursive function calls itself.”

Next chapterStrings and String Manipulation