The keyword used to define a function in Python is:
Functions — Important Questions
SUMMARY: This chapter covers the concept of functions in Python, including their definition, usage, and importance in programming.
KEY TOPICS: function definition, function call, parameters and arguments, return statement, scope of variables, recursion, built-in functions, lambda functions, function overloading, anonymous functions
Which of the following is a correct way to call a function add with two arguments 3 and 4?
Check answerHide answer
The number of values returned by a Python function with no return statement is:
Check answerHide answer
Which keyword in Python is used to indicate that a variable is a global variable inside a function?
Check answerHide answer
A function that calls itself is known as:
Check answerHide answer
Differentiate between actual parameters and formal parameters in Python functions.
Differentiate between local variable and global variable.
What is a default argument? Give one example.
Explain pass by value and pass by reference in Python with one example.
Differentiate between user-defined and built-in functions giving one example each.
Write a Python function that takes a list and returns a tuple containing maximum minimum and average of list elements.
Write a Python program using a recursive function to compute the factorial of a number.
Discuss different types of arguments in Python — positional default keyword and variable-length — with examples.
Explain the scope of variables in Python (local enclosing global built-in) with one example each.
Write a Python function that accepts a string and returns a dictionary containing the count of each character.
Differentiate between actual and formal parameters in Python functions in tabular form.
Assertion (A): Functions promote code reusability.
Reason (R): A function can be called multiple times from different places.
Show explanationHide explanation
Assertion (A): A function can return multiple values in Python.
Reason (R): The values are returned as a tuple.
Show explanationHide explanation
Assertion (A): Default arguments must come after non-default arguments.
Reason (R): Otherwise Python raises a SyntaxError.
Show explanationHide explanation
Assertion (A): Recursive functions need a base case.
Reason (R): Without it the recursion would never stop and cause a stack overflow.
Show explanationHide explanation
Assertion (A): The global keyword allows modifying a global variable inside a function.
Reason (R): Without it the variable becomes local to the function.
Show explanationHide explanation
Statement 1: A function definition starts with the def keyword.
Statement 2: The function body is indented.
Show answerHide answer
Statement 1: Lambda functions are anonymous functions.
Statement 2: They are defined using the lambda keyword.
Show answerHide answer
Statement 1: A function can call other functions.
Statement 2: It can also call itself (recursion).
Show answerHide answer
Statement 1: Functions help in modular programming.
Statement 2: Each function does one specific task.
Show answerHide answer
Statement 1: Keyword arguments improve readability.
Statement 2: The argument name is specified explicitly during the function call.
Show answerHide answer
-
Which keyword is used to define a function in Python?AfunctionBdefCfunDdefine
-
Which scope does a variable defined inside a function have?ALocalBGlobalCBuilt-inDClass
-
Write a Python program implementing the calculator using one function per operation.
Show answersHide answers
For each function call predict its return value.
| Call to f(a, b=10) | Return value |
|---|---|
| f(5) | ? |
| f(5, 7) | ? |
| f(b=4, a=3) | ? |
| f(*[2, 3]) | ? |
| f() | ? |
For each function call identify if it is recursive or iterative.
| Function | Type (recursive/iterative) |
|---|---|
| factorial via self-call | ? |
| factorial via for loop | ? |
| fibonacci via self-call | ? |
| even() returning n%2==0 | ? |
| sum_list via self-call | ? |
Study the table on Python function arguments and answer:
| Type | Description | Example |
|---|---|---|
| Positional | Order matters | f(1, 2) |
| Keyword | Name = value | f(a=1, b=2) |
| Default | Default if absent | def f(a, b=10) |
| Variable-length | *args / **kwargs | def f(*args) |
-
Which type of argument has a default value if not provided?APositionalBKeywordCDefaultDVariable-length
-
Which is used for variable-length keyword arguments?A*argsB**kwargsCBothD
-
Discuss the four types of arguments in Python with one example each.
Show answersHide answers
Study the function call and return diagram and answer:
-
The arguments passed to a function during a call are called:AActual parametersBFormal parametersCDefault parametersDVariable parameters
-
A function with no return statement returns:A0B1CNone (None object)Dundefined
-
Differentiate between actual and formal parameters and explain function return mechanism.
Show answersHide answers
Make a full Computer Science paper on Functions.
Pick the question mix, set the marks, hit generate. You get a ready-to-print paper with an answer key.
Generate your paper — free