Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Students can Download Computer Science Chapter 7 Python Functions Questions and Answers, Notes Pdf, Samacheer Kalvi 12th Computer Science Book Solutions Guide Pdf helps you to revise the complete Tamilnadu State Board New Syllabus and score more marks in your examinations.

Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Samacheer Kalvi 12th Computer Science Python Functions Text Book Back Questions and Answers

PART – I
I. Choose The Best Answer

Question 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
Answer:
(c) Function

Question 2.
A Function which calls itself is called as …………………………
(a) Built – in
(b) Recursion
(c) Lambda
(d) Return
Answer:
(b) Recursion

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 3.
Which function is called anonymous un – named function?
(a) Lambda
(b) Recursion
(c) Function
(d) Define
Answer:
(a) Lambda

Question 4.
Which of the following keyword is used to begin the function block?
(a) Define
(b) For
(c) Finally
(d) Def
Answer:
(d) Def

Question 5.
Which of the following keyword is used to exit a function block?
(a) Define
(b) Return
(c) Finally
(d) Def
Answer:
(b) Return

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 6.
While defining a function which of the following symbol is used.
(a) ; (semicolon)
(b) . (dot)
(c) : (colon)
(d) $ (dollar)
Answer:
(c) : (colon)

Question 7.
In which arguments the correct positional order is passed to a function?
(a) Required
(b) Keyword
(c) Default
(d) Variable – length
Answer:
(a) Required

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 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
Answer:
(a) (I) is correct and (II) is wrong

Question 9.
Pick the correct one to execute the given statement successfully.
if_: print (x,” is a leap year”)
(a) x % 2 = 0
(b) x % 4 = = 0
(c) x / 4 = 0
(d) x % 4 = 0
Answer:
(b) x % 4 = = 0

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 10.
Which of the following keyword is used to define the function testpython():?
(a) Define
(b) Pass
(c) Def
(d) While
Answer:
(c) Def

PART – II
II. Answer The Following Questions

Question 1.
What is function?
Answer:
Functions are named blocks of code that are designed to do specific job. If you need to perform that task multiple times throughout your program, you just call the function dedicated to handling that task.

Question 2.
Write the different types of function?
Answer:

  1. User – defined Functions
  2. Built – in Functions
  3. Lambda Functions
  4. Recursion Functions

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 3.
What are the main advantages of function?
Answer:
Main advantages of functions are:

  1. It avoids repetition and makes high degree of code reusing.
  2. It provides better modularity for your application.

Question 4.
What is meant by scope of variable? Mention its types?
Answer:
Scope of variable refers to the part of the program, where it is accessible, i.e., area where you can refer (use) it. We can say that scope holds the current set of variables and their values. The two types of scopes are: local scope and global scope.

Question 5.
Define global scope?
Answer:
A variable, with global scope can be used anywhere in the program. It can be created by defining a variable outside the scope of any function/block.

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 6.
What is base condition in recursive function?
Answer:
The condition that is applied in any recursive function is known as base condition. A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop.

Question 7.
How to set the limit for recursive function? Give an example?
Answer:
Python also allows you to change the limit using sys.setrecursionlimit (limit value).
Example:
import sys
sys.setrecursionlimit(3000)
def fact (n):
if n = = 0:
return 1
else:
return n * fact (n – 1)
print (fact (2000))

PART – III
III. Answer The Following Questions

Question 1.
Write the rules of local variable?
Answer:
Rules of local variable:

  1. A variable with local scope can be accessed only within the function/block that it is created in.
  2. When a variable is created inside the function/block, the variable becomes local to it.
  3. A local variable only exists while the function is executing.
  4. The formate arguments are also local to function.

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 2.
Write the basic rules for global keyword in python?
Answer:
The basic rules for global keyword in Python are:

  1. When we define a variable outside a function, it’s global by default. You don’t have to use global keyword.
  2. We use global keyword to read and write a global variable inside a function.
  3. Use of global keyword outside a function has no effect

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 3.
What happens when we modify global variable inside the function?
Answer:
It will change the global variable value outside the function also.

Question 4.
Differentiate ceil ( ) and floor ( ) function?
Answer:
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 5.
Write a Python code to check whether a given year is leap year or not?
Leap year or not:
Program code:
Answer:
n = int (input(“Enter any year”))
if (n % 4 = = 0):
print “Leap year”
else:
print “Not a Leap year”
Output:
Enter any year 2001
Not a Leap year

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 6.
What is composition in functions?
Answer:
The value returned by a function may be used as an argument for another function in a nested manner. This is called composition. For example, if we wish to take a numeric value or an expression as a input from the user, we take the input string from the user using the function input ( ) and apply eval ( ) function to evaluate its value.

Question 7.
How recursive function works?
Answer:

  1. Recursive function is called by some external code.
  2. If the base condition is met then the program gives meaningful output and exits.
  3. Otherwise, function does some required processing and then calls itself to continue recursion.

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 8.
What are the points to be noted while defining a function?
Answer:

  1. Function blocks begin with the keyword “def ” followed by function name and parenthesis ( ).
  2. Any input parameters or arguments should be placed within these parentheses when you define a function.
  3. The code block always comes after a colon (:) and is indented.
  4. The statement “return [expression]” exits a function, optionally passing back an expression to the caller. A “return” with no arguments is the same as return None

PART – IV
IV. Answer The Following Questions

Question 1.
Explain the different types of function with an example?
Answer:
Types of Functions:
Basically, we can divide functions into the following types:

  1. User – defined Functions
  2. Built – in Functions
  3. Lambda Functions
  4. Recursion Functions

Functions:
User – defined functions
Built – in functions
Lambda functions
Recursion functions

Description:
Functions defined by the users themselves.
Functions that are inbuilt with in Python.
Functions that are anonymous un-named function.
Functions that calls itself is known as recursive.

(I) Syntax for User defined function
def <function_name ( [parameter1, parameter!…] ) > :
<Block of Statements> return <expression /None>
Example:
def hello ( ):
print (“hello – Python”)
return

Advantages of User – defined Functions:

  1. Functions help us to divide a program into modules. This makes the code easier to manage.
  2. It implements code reuse. Every time you need to execute a sequence of statements, all you need to do is to call the function.
  3. Functions, allows us to change functionality easily, and different programmers can work on different functions.

(II) Anonymous Functions:
In Python, anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called as lambda functions.

The use of lambda or anonymous function:

  1. Lambda function is mostly used for creating small and one-time anonymous function.
  2. Lambda functions are mainly used in combination with the functions like filter ( ), map ( ) and reduce ( ).

Syntax of Anonymous Functions
The syntax for anonymous functions is as follows:
lambda [argument(s)] expression
Example:
sum = lambda argl, arg2: argl + arg2
print (‘The Sum is sum (30, 40))
print (‘The Sum is :’, sum (-30, 40))
Output:
The Sum is: 70
The Sum is: 10
The above lambda function that adds argument argl with argument arg2 and stores the result in the variable sum. The result is displayed using the print ( ).

(III) Functions using libraries:
Built – in and Mathematical functions
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

(IV) Recursive functions:
When a function calls itself is known as recursion. Recursion works like loop but sometimes 1 ’ it makes more sense to use recursion than loop. You can convert any loop to recursion.
Example:
def fact(n):
if n = = 0:
return 1
else:
return n * fact (n – 1)
print (fact (0))
print (fact (5))
Output:
1
120

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 2.
Explain the scope of variables with an example?
Answer:
Scope of Variables:
Scope of variable refers to the part of the program, where it is accessible, i.e., area where you can refer (use) it. We can say that scope holds the current set of variables and their values.
The two types of scopes are local scope and global scope.

(I) Local scope:
A variable declared inside the function’s body or in the local scope is called as local variable.

Rules of local variable:

  1. A variable with local scope can be accessed only within the function/block that it is created in.
  2. When a variable is created inside the function/block; the variable becomes local to it.
  3. A local variable only exists while the function is executing.
  4. The formate arguments are also local to function.

Example: Create a Local Variable
def loc ( ):
y = 0 # local scope
print (y)
loc ( )
Output:
0
(II) Global Scope:
A variable, with global scope can be used anywhere in the program. It can be created by defining a variable outside the scope of any function/block.

Rules of global Keyword:
The basic rules for global keyword in Python are:

  1. When we define a variable outside a function, it’s global by default. You don’t have to useglobal keyword.
  2. We use global keyword to read and write a global variable inside a function.
  3. Use of global keyword outside a function has no effect

Example: Global variable and Local variable with same name
x = 5 def loc ( ):
x = 10
print (“local x:”, x)
loc ( )
print (“global x:”, x)
Output:
local x: 10
global x: 5
In above code, we used same name ‘x’ for both global variable and local variable. We get different result when we print same variable because the variable is declared in both scopes, i.e. the local scope inside the function loc() and global scope outside the function loc ( ).
The output:- local x: 10, is called local scope of variable.
The output: – global x: 5, is called global scope of variable.

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 3.
Explain the following built-in functions?
(a) id ( )
(b) chr ( )
(c) round ( )
(d) type ( )
(e) pow ( )
Answer:
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 4.
Write a Python code to find the L.C.M. of two numbers?
Answer:
Method I using functions:
def lcm (x, y):
if x > y:
greater = x
else:
greater = y while (true):
if ((greater % x = = 0) and (greater % y = = 0)):
lcm = greater break
greater + = 1
return lcm
num 1 = int (input(“Enter first number : “))
num 2 = int (input(“Enter second number : “))
print (“The L.C.M of”, numl, “and”, num, “is”, lcm(num1, num2))

Method II
(without using functions)
a = int (input (“Enter the first number :”))
b = int (input (“Enter the second number :”))
if a > b:
mini = a
else:
min 1 = b
while(1):
if (min 1 % a = = 0 and mini 1 % b = = 0):
print (“LCM is:”, mini)
break
mini = min 1 + 1
Output:
Enter the first number: 15
Enter the second number: 20
LCM is: 60

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 5.
Explain recursive function with an example?
Answer:
Python recursive functions
When a function calls itself is known as recursion. Recursion works like loop but sometimes it makes more sense to use recursion than loop. You can convert any loop to recursion.
A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition! Such a process is known as infinite iteration. The condition that is applied in any recursive function is known as base condition. A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop.

Working Principle:

  1. Recursive function is called by some external code.
  2. If the base condition is met then the program gives meaningful output and exits.
  3. Otherwise, function does some required processing and then calls itself to continue recursion. Here is an example of recursive function used to calculate factorial.

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

Practice Programs

Question 1.
Try the following code in the above program?
Answer:
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions
Output:
1. Error

2. Name: Sri
Salary: 3500
Salary: 3500

3. Name: Balu
Salary: 3500

4. Name: Jose
Salary: 1234

5. Name:
Salary: 1234

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 2.
Evaluate the following functions and write the output?
Answer:
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions
Output:

  1. 30
  2. 9
  3. 8
  4. 9

Question 3.
Evaluate the following functions and write the output?
Answer:
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions
Output:
(i) 1. 13
2. 3.2

(ii) 1. 50
2. 36

(iii) <class ‘str’>

(iv) 0b10000

(v). 1. CR (carriage return)
2. It moves the cursor to the beginning of same line

(vi) 1. 8.2
2. 18.0
3. 0.510
4. 0.512

(vii) 1. B
2. a
3. A
4. 6
5. 10

(viii) 1. 0.125
2. 8.0
3. 1

Samacheer kalvi 12th Computer Science Python Functions Additional Questions and Answers

PART – 1
I. Choose The Best Answer

Question 1.
Name of the function is followed by ………………………….
(a) ( )
(b) [ ]
(c) <>
(d) { }
Answer:
(a) ( )

Question 2.
A …………………………. is one or more lines of code, grouped together.
(a) Code –
(b) Block
(c) Function
(d) Arguments
Answer:
(b) Block

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 3.
A block of code begins when a line is indented by ……………………. spaces usually.
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(c) 4

Question 4.
A block within a block is called …………………………… block.
Answer:
Nested

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 5.
If the return has no argument, …………………………….. will be displayed as the last statement of the output.
(a) No
(b) None
(c) Nothing
(d) No value
Answer:
(b) None

Question 6.
How many types of functions arguments are there?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(c) 4

Question 7.
The arguments can be given in improper order in ………………………. arguments.
(a) Required
(b) Keyword
(c) Default
(d) Variable – length
Answer:
(b) Keyword

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 8.
What is the symbol used to denote variable – length arguments?
(a) +
(b) *
(c) &
(d) ++
Answer:
(b) *

Question 9.
How many methods of arguments passing are there in variable length method.
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(a) 2

Question 10.
Non – keyword variable arguments are called ……………………………….
Answer:
Tuples

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 11.
In python’s ……………………….. function supports variable length arguments.
(a) Input
(b) Write
(c) Output
(d) Print
Answer:
(d) Print

Question 12.
Lambda functions cannot be used in combination with ………………………….
(a) Filter
(b) Map
(c) Print
(d) Reduce
Answer:
(c) Print

Question 13.
Lambda function can only access ……………………….. variables.
(a) Local
(b) Function
(c) Global
(d) Nested
Answer:
(c) Global

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 14.
How many types of scopes are there?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(a) 2

Question 15.
Find the correct one:
(a) Global keyword outside the function has no effect
(b) Global keyword outside the function has effect
Answer:
(a) Global keyword outside the function has no effect

Question 16.
Read the following statement and choose the wrong statements
(a) Without using the global keyword, we cannot modify the global variable
(b) Using global keyword we can modify the global variable
(c) Without global keyword, we can modify the global variable
Answer:
(c) Without global keyword, we can modify the global variable

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 17.
Find the correct statement.
(a) Local and global variables cannot be used in the same code
(b) Local and global variables can be used in the same code
Answer:
(b) Local and global variables can be used in the same code

Question 18.
The ……………………… function is the inverse of chr ( ) function.
(a) Ord ( )
(b) Abs ( )
(c) Chr ( )
(d) Bin ( )
Answer:
(a) Ord ( )

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 19.
…………………………. function is the alternative for bin ( ) function.
(a) Ord ( )
(b) Format ( )
(c) Binary ( )
(d) Ord ( )
Answer:
(b) Format ( )

Question 20.
bin ( ) returns the binary string prefixed with ………………………… for the given integer number
(a) b
(b) ob
(c) obin
(d) bin
Answer:
(b) ob

Question 21.
Find the output.
d = 43
print(‘A =ord(d))
(a) 67
(b) 95
(c) 97
(d) 65
Answer:
(d) 65

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 22.
Find the output,
d = 43
print (chr(d))
(a) –
(b) +
(c) *
(d) /
Answer:
(b) +

Question 23.
The default precision for fixed point number is ………………………….
(a) 2
(b) 4
(c) 6
(d) 8
Answer:
(c) 6

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 24.
How many formats are there for the format ( ) functions?
(a) 12
(b) 5
(c) 3
(d) 1
Answer:
(c) 3

Question 25.
………………………. function returns the smallest integer greater than or equal to x
(a) Sqrt
(b) Flow
(c) Floor
(d) Cell
Answer:
(d) Cell

Question 26.
………………………. function is used to evaluate the input value.
(a) Input
(b) Valuate
(c) Eval
(d) Val
Answer:
(c) Eval

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 27.
Any Loop can be converted to recursive functions.
True / False
Answer:
True

Question 28.
Find true statement
(a) Recursive function call itself
(b) Recursive function have to be called externally
Answer:
(a) Recursive function call itself

PART – II
II. Answer The Following Questions.

Question 1.
Define nested blocks?
Answer:
Nested Block:
A block within a block is called nested block. When the first block statement is indented by a single tab space, the second block of statement is indented by double tab spaces.

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 2.
Give the syntax for passing parameters in functions?
Answer:
Parameters or arguments can be passed to functions
def function _ name (parameter (s) separated by comma):

Question 3.
Differentiate parameters and arguments?
Answer:
Parameters are the variables used in the function definition whereas arguments are the values we pass to the function parameters.

Question 4.
Classify Function Arguments?
Function Arguments:

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable – length arguments

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 5.
Define default arguments?
Answer:
Default Arguments
In Python the default argument is an argument that takes a default value if no value is provided in the function call. The following example uses default arguments, that prints default salary when no argument is passed, def printinfo(sal=3500):

Question 6.
What are the two methods of passing arguments in variable length arguments?
Answer:
In Variable Length arguments we can pass the arguments using two methods.

  1. Non keyword variable arguments
  2. Keyword variable arguments

PART – III
III. Answer The Following Questions.

Question 1.
Write the output for the program given below?
Answer:
Program:
def printdata (name, age):
print (“Example – 3 Keyword arguments”)
print (“Name :”,name)
print (“Age age)
return
# Now you can call printdata ( ) function
printdata (age = 25, name = “Gshan”)
Output:
Example – 3 Keyword arguments
Name: Gshan
Age: 25

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 2.
Give the syntax for defining variable – length arguments.?
Syntax – Variable – Length Arguments:
Answer:
def function _name (*args):
function_body
return_statement

Question 3.
Write note on return statement?
Answer:
The return Statement
1. The return statement causes your function to exit and returns a value to its caller. The point of functions in general is to take inputs and return something.

2. The return statement is used when a function is ready to return a value to its caller. So, only one return statement is executed at run time even though the function contains multiple return statements.

3. Any number of ‘return’ statements are allowed in a function definition but only one of them is executed at run time.

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 4.
Find the output?
Answer:
Program:
Answer:
x = 0 # global variable
def add ( ):
global x
x = x + 5 # increment by 2
print (“Inside add ( ) function x value is:”, x)
add ( )
print (“In main x value is x)
Output:
Inside add ( ) function x value is: 5.
In main x value is: 5

Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Question 5.
Write note on format function?
Answer:
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

PART – IV
IV. Answer The Following Questions.

Question 1.
Write any 5 built in functions?
Answer:
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions
Samacheer Kalvi 12th Computer Science Solutions Chapter 7 Python Functions

Leave a Comment

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