Functions are essential for writing organized, reusable code. In this section, we’ll explore how to define, call, and use functions, along with function arguments, return values, and modularization techniques.
Defining and Calling Functions
What is the correct syntax to define a function in most programming languages? a) function myFunction() b) def myFunction() c) create myFunction() d) func myFunction()
How do you call a function named greet? a) greet() b) call greet() c) greet; d) invoke greet()
What will happen if a function is called without the correct number of arguments? a) The program will crash immediately b) The function will throw an error c) The function will run with default arguments d) The program will continue with a warning
Which of the following is NOT a valid function declaration in Python? a) def greet(name): b) function greet(name) c) def greet(): d) def greet(name='Hello'):
What keyword is used to define a function in JavaScript? a) function b) define c) create d) method
Function Arguments
What is the correct way to pass an argument to a function in most programming languages? a) By value b) By reference c) Both a and b d) By memory
How can you specify default values for function parameters in Python? a) By using default keyword b) By assigning a value directly in the parameter list c) By using the set function d) By using the default() function
Which of the following allows a function to accept an arbitrary number of arguments in Python? a) *args b) **kwargs c) varargs d) anyargs
What is the term for passing a function as an argument in Python? a) Function passing b) Higher-order function c) Function calling d) Function definition
What will the following Python function print? def greet(name='World', time='Morning'): print(f"Good {time}, {name}") a) Good Morning, World b) Good World, Morning c) Good name, time d) Good Morning, name
Return Values and Exit Codes
How do you return a value from a function in Python? a) exit() b) return value c) function(value) d) out(value)
What is an exit code in programming? a) A special code used for exiting loops b) A code indicating the success or failure of a program c) A code that forces the program to stop execution d) A return value for functions
What happens if a function does not include a return statement? a) The function will return None in Python b) The function will return undefined in JavaScript c) The function will run infinitely d) The program will crash
What value does a function return by default if no return statement is provided in C++? a) 0 b) null c) undefined d) None
How can you stop the execution of a function and return a value immediately in C? a) exit(value) b) return value; c) stop(value) d) end(value)
Which of the following is a valid return statement in C? a) return 10; b) exit 10; c) end 10; d) stop 10;
In which case would you use an exit code of 1 in a program? a) When the program runs successfully b) When there is an error or failure c) When there is a syntax warning d) When the program finishes without any output
Modularizing Code with Functions
What is the main purpose of using functions in a program? a) To store large amounts of data b) To divide the program into manageable sections c) To reduce the speed of execution d) To handle errors and exceptions
Which of the following is NOT an advantage of modularizing code using functions? a) Code reuse b) Easier debugging c) Larger program size d) Simplified code maintenance
How can you access a variable from the outer scope inside a function in Python? a) By using the global keyword b) By passing it as an argument c) By assigning a new value to it inside the function d) Both a and b
What is the key benefit of using function parameters instead of hardcoding values inside a function? a) Faster execution b) Increased modularity and flexibility c) Reduced code complexity d) More memory usage
How can you call a function defined in another file or module in Python? a) import function_name b) include function_name c) from module import function_name d) use function_name
Which of the following best describes a function that performs a specific task and does not return a value? a) Void function b) Returning function c) Lambda function d) Recursive function
How do you call a function that has been defined in a different module in JavaScript? a) require('function_name') b) import function_name from 'module_name' c) include function_name d) call function_name
What is the proper syntax to define a function with no parameters in C? a) void myFunction() b) myFunction() {} c) void myFunction[] d) function myFunction()
What type of function would you use to simplify complex code and break it into smaller parts? a) Recursive function b) Anonymous function c) Modular function d) Callback function
In which programming language can you use a return statement without a value in a void function? a) C b) Python c) Java d) JavaScript
What is the most common reason to split code into multiple functions? a) To make the program larger b) To make the program more complex c) To increase the readability and reusability of the code d) To decrease the speed of the program
In Java, how would you call a method that returns a value? a) By using the method name followed by parentheses b) By using return methodName() c) By calling the method and assigning its result to a variable d) By using call()
What is one of the main drawbacks of not using functions in programming? a) Increased code duplication b) Faster execution c) Easier debugging d) More flexible design
Answer Key
QNo
Answer (Option with text)
1
b) def myFunction()
2
a) greet()
3
b) The function will throw an error
4
b) function greet(name)
5
a) function
6
c) Both a and b
7
b) By assigning a value directly in the parameter list
8
a) *args
9
b) Higher-order function
10
a) Good Morning, World
11
b) return value
12
b) A code indicating the success or failure of a program
13
a) The function will return None in Python
14
a) 0
15
b) return value;
16
a) return 10;
17
b) When there is an error or failure
18
b) To divide the program into manageable sections
19
c) Larger program size
20
d) Both a and b
21
b) Increased modularity and flexibility
22
c) from module import function_name
23
a) Void function
24
b) import function_name from 'module_name'
25
a) void myFunction()
26
c) Modular function
27
a) C
28
c) To increase the readability and reusability of the code
29
c) By calling the method and assigning its result to a variable