MCQs on Working with Functions | Shell Scripting

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

  1. 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()
  2. How do you call a function named greet?
    a) greet()
    b) call greet()
    c) greet;
    d) invoke greet()
  3. 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
  4. 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'):
  5. What keyword is used to define a function in JavaScript?
    a) function
    b) define
    c) create
    d) method

Function Arguments

  1. 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
  2. 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
  3. Which of the following allows a function to accept an arbitrary number of arguments in Python?
    a) *args
    b) **kwargs
    c) varargs
    d) anyargs
  4. 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
  5. 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

  1. How do you return a value from a function in Python?
    a) exit()
    b) return value
    c) function(value)
    d) out(value)
  2. 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
  3. 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
  4. What value does a function return by default if no return statement is provided in C++?
    a) 0
    b) null
    c) undefined
    d) None
  5. 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)
  6. Which of the following is a valid return statement in C?
    a) return 10;
    b) exit 10;
    c) end 10;
    d) stop 10;
  7. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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()
  9. 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
  10. 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
  11. 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
  12. 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()
  13. 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

QNoAnswer (Option with text)
1b) def myFunction()
2a) greet()
3b) The function will throw an error
4b) function greet(name)
5a) function
6c) Both a and b
7b) By assigning a value directly in the parameter list
8a) *args
9b) Higher-order function
10a) Good Morning, World
11b) return value
12b) A code indicating the success or failure of a program
13a) The function will return None in Python
14a) 0
15b) return value;
16a) return 10;
17b) When there is an error or failure
18b) To divide the program into manageable sections
19c) Larger program size
20d) Both a and b
21b) Increased modularity and flexibility
22c) from module import function_name
23a) Void function
24b) import function_name from 'module_name'
25a) void myFunction()
26c) Modular function
27a) C
28c) To increase the readability and reusability of the code
29c) By calling the method and assigning its result to a variable
30a) Increased code duplication

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top