Functions and Procedures # MCQs Practice set

Q.1 Which of the following correctly defines a function in Python that returns the square of a number?

def square(x): return x*x
function square(x) { return x*x }
def square x: x*x
function square(x): x*x
Explanation - In Python, functions are defined using 'def' keyword followed by the function name, parameters in parentheses, and a colon. The return statement is used to return a value.
Correct answer is: def square(x): return x*x

Q.2 What is the output of the following Python code? def add(a, b=5): return a+b print(add(3))

3
5
8
Error
Explanation - The function 'add' has a default parameter b=5. Calling add(3) adds 3+5=8.
Correct answer is: 8

Q.3 In C language, what is the default return type of a function if none is specified?

void
int
float
double
Explanation - In C, if a return type is not specified, it is assumed to be 'int' by default (in older standards; modern standards require explicit declaration).
Correct answer is: int

Q.4 Which statement about a procedure is true?

It must always return a value
It cannot take parameters
It performs a task but may not return a value
It is the same as a loop
Explanation - A procedure is a block of code that performs a specific task. It may or may not return a value depending on the programming language.
Correct answer is: It performs a task but may not return a value

Q.5 What is recursion in programming?

A function that calls itself
A function that never returns
A function with no parameters
A function with multiple return statements
Explanation - Recursion occurs when a function calls itself either directly or indirectly to solve a problem.
Correct answer is: A function that calls itself

Q.6 In C++, which of the following is the correct syntax to declare a function prototype?

int add(int, int);
add(int a, int b);
int add(a,b) int a,b;
function int add(int, int);
Explanation - In C++, a function prototype includes the return type, function name, and parameter types followed by a semicolon.
Correct answer is: int add(int, int);

Q.7 Which of the following is NOT a valid characteristic of functions?

They help in code reusability
They improve readability
They execute automatically without being called
They can take parameters
Explanation - Functions do not execute automatically; they must be called explicitly unless they are called by another function.
Correct answer is: They execute automatically without being called

Q.8 Which keyword is used to exit a function in Python and return a value?

exit
break
return
stop
Explanation - In Python, 'return' is used to exit a function and optionally pass a value back to the caller.
Correct answer is: return

Q.9 What will be printed by the following Python code? def func(x): x += 1 return x print(func(5))

5
6
7
Error
Explanation - The function adds 1 to the input 5 and returns 6.
Correct answer is: 6

Q.10 Which of the following defines a procedure in Pascal?

procedure Sum(a, b: integer);
function Sum(a, b: integer): integer;
def Sum(a,b): return a+b
Sum(a,b) -> integer;
Explanation - In Pascal, a procedure is defined using the 'procedure' keyword and does not return a value.
Correct answer is: procedure Sum(a, b: integer);

Q.11 What type of function does not return a value in C?

int function
void function
float function
double function
Explanation - A 'void' function in C performs a task but does not return a value.
Correct answer is: void function

Q.12 What is the main advantage of using functions in programming?

Code reusability and modularity
Slower execution
More memory usage
Harder debugging
Explanation - Functions allow code to be reused in multiple places and help organize the program into modules.
Correct answer is: Code reusability and modularity

Q.13 What is a parameter in a function?

A variable inside a function used only internally
A value passed to a function when it is called
A function name
A global variable
Explanation - Parameters are values passed to a function when it is called, which the function uses to perform its task.
Correct answer is: A value passed to a function when it is called

Q.14 Which of the following statements about local variables is correct?

They are accessible outside the function
They are created each time the function is called
They retain their value between function calls automatically
They must be global
Explanation - Local variables exist only during the function execution and are recreated each time the function is called.
Correct answer is: They are created each time the function is called

Q.15 What is a default argument in a function?

A parameter with a predefined value if not provided
A parameter that must always be provided
A global variable
A function with no parameters
Explanation - Default arguments allow a function to be called without passing all parameters. The default value is used if no value is provided.
Correct answer is: A parameter with a predefined value if not provided

Q.16 In Python, can a function return multiple values?

Yes, as a tuple
No, only one value can be returned
Yes, but only as a list
Yes, but only as a string
Explanation - Python functions can return multiple values by returning them as a tuple.
Correct answer is: Yes, as a tuple

Q.17 Which of the following is an example of a recursive function?

def factorial(n): return 1 if n==0 else n*factorial(n-1)
def factorial(n): return n+1
def factorial(n): print(n)
def factorial(n): return n*2
Explanation - A recursive function calls itself to compute a value, as shown in factorial(n).
Correct answer is: def factorial(n): return 1 if n==0 else n*factorial(n-1)

Q.18 What is the difference between a function and a procedure?

A function returns a value; a procedure may not
A function cannot take parameters; a procedure can
A function is slower than a procedure
There is no difference
Explanation - The main difference is that functions return a value, while procedures perform tasks and may not return anything.
Correct answer is: A function returns a value; a procedure may not

Q.19 In Java, which of the following is the correct way to define a method that returns an integer?

int sum(int a, int b) { return a+b; }
sum(int a, int b) { return a+b; }
void sum(int a, int b) { return a+b; }
function int sum(a,b) { return a+b; }
Explanation - In Java, methods must have a return type specified. 'int' indicates the method returns an integer.
Correct answer is: int sum(int a, int b) { return a+b; }

Q.20 Which of the following will cause a stack overflow in a program?

A recursive function with no base case
A function returning an integer
A procedure without parameters
A function with default arguments
Explanation - Recursive functions without a base case call themselves indefinitely, eventually exhausting the stack memory.
Correct answer is: A recursive function with no base case

Q.21 In Python, which of the following statements about functions is FALSE?

Functions can be assigned to variables
Functions can be passed as arguments
Functions can return other functions
Functions must always have at least one parameter
Explanation - Functions in Python do not require any parameters; they can be defined without parameters.
Correct answer is: Functions must always have at least one parameter

Q.22 What is the output of the following Python code? def f(x): return x*2 def g(y): return f(y)+1 print(g(3))

6
7
5
Error
Explanation - f(3) returns 6; g adds 1 to it, giving 7.
Correct answer is: 7

Q.23 Which of the following is true about function overloading?

Multiple functions with the same name but different parameters
Functions can have only one version in a program
Functions must return the same type
Overloading is not allowed in C++
Explanation - Function overloading allows multiple functions with the same name but different parameter types or numbers in C++.
Correct answer is: Multiple functions with the same name but different parameters

Q.24 What is a higher-order function?

A function that can take other functions as arguments or return them
A function that only returns integers
A function that cannot take parameters
A function defined inside a class
Explanation - Higher-order functions operate on other functions, either by taking them as arguments or returning them.
Correct answer is: A function that can take other functions as arguments or return them

Q.25 What is the difference between call by value and call by reference?

Call by value passes a copy; call by reference passes the actual variable
Call by value passes the actual variable; call by reference passes a copy
Call by value is only in Python; call by reference is in C
There is no difference
Explanation - Call by value sends a copy of the argument to the function, so changes do not affect the original variable. Call by reference passes the actual variable, allowing modifications.
Correct answer is: Call by value passes a copy; call by reference passes the actual variable