Lecture 9
Lecture 9
2. Which of the following is the correct syntax for declaring a function in C++?
o A) return_type function_name(parameters);
o B) function_name return_type(parameters);
o C) return_type function_name(parameters) { // body }
o D) function_name(parameters) { return_type; }
4. What is the correct syntax for calling a function named square that takes an integer
as an argument in C++?
o A) square(5);
o B) square(5) = result;
o C) square(int 5);
o D) result = square(5);
Answer: B) A function prototype informs the compiler about the function’s name, return
type, and parameters.
6. Which of the following functions is an example of a function that does not return
any value?
o A) int square(int);
o B) double raiseToPow(double, int);
o C) void printMessage();
o D) float areaOfCircle(double);
8. In C++, what is the correct way to handle a function that calculates the square of a
number?
o A) A function that takes an integer and returns an integer.
o B) A function that returns a string representation of the square.
o C) A function that prints the square directly without returning it.
o D) A function that multiplies two numbers and returns the result.
cpp
Copy code
int square(int number) {
return number * number;
}
o A) 4
o B) 16
o C) 12
o D) 8
Answer: B) 16
Answer: A) Breaking the program into functions that handle smaller sub-tasks.
Answer: B) The function works with a copy of the variable, and changes do not affect
the original.
12. Which function declaration is correct for a function that takes a double and an
integer as arguments and returns a double?
o A) double power(double, int);
o B) int power(double, int);
o C) void power(double, int);
o D) double power(int, double);
13. Which of the following is the correct way to declare and define a function in
separate files?
o A) Define the function first, then declare it in the header file.
o B) Declare the function in the header file, and define it in a separate
implementation file.
o C) Declare the function and define it both in the same file.
o D) It is not possible to declare and define functions separately in C++.
Answer: B) Declare the function in the header file, and define it in a separate
implementation file.
cpp
Copy code
int isEven(int number) {
if (number % 2 == 0)
return 1;
else
return 0;
}
int main() {
int number;
cin >> number;
if (isEven(number))
cout << "Even";
else
cout << "Odd";
return 0;
}
15. Which of the following statements is true about the return statement in a function?
o A) A function with a void return type must always use a return statement.
o B) A return statement terminates the execution of the function and returns
control to the calling function.
o C) A function with a return statement does not need to have a return type.
o D) The return statement can only return an integer.
Answer: B) A return statement terminates the execution of the function and returns
control to the calling function.
16. What will happen if a function with a return type is missing the return statement?
o A) The program will compile but may not return a value.
o B) The program will crash.
o C) The program will give a compile-time error.
o D) The program will give a runtime error.
Answer: B) To inform the compiler about the function's name, return type, and
parameters before it is used in the program.
18. Which of the following is a correct way to declare a function that takes two integers
and returns their sum as an integer?
o A) int addNumbers(int, int);
o B) void addNumbers(int, int);
o C) int addNumbers();
o D) void addNumbers();
Answer: C) The arguments passed to a function must match the type of the function's
parameters.
20. How would you modify the function raiseToPow to handle negative exponents
correctly?
o A) Return the result as a positive number.
o B) Return 1 divided by the result raised to the positive exponent.
o C) Throw an error when the exponent is negative.
o D) Ignore the negative exponent.
21. What is the correct syntax to define a function that takes no arguments and returns
a double?
o A) double function() { // body }
o B) double function(void) { // body }
o C) function() -> double { // body }
o D) void function() -> double { // body }
22. What is the result when you use a function call multiple times with different
arguments?
o A) The function is executed only once with the last argument.
o B) The function executes multiple times with each argument, and returns the
result of the last execution.
o C) The function executes multiple times with each argument, and their results are
combined.
o D) The function will give an error when called multiple times.
Answer: B) The function executes multiple times with each argument, and returns the
result of the last execution.
23. Which of the following is true about a function with a void return type?
o A) It can only perform calculations and not display any output.
o B) It can return a value, but the value is ignored by the calling function.
o C) It cannot take any parameters.
o D) It performs an action but does not return any value to the calling function.
Answer: D) It performs an action but does not return any value to the calling function.
25. In a C++ function definition, which keyword is used to indicate that the function
does not return any value?
o A) int
o B) double
o C) void
o D) return
Answer: C) void
26. What will happen if you try to call a function without declaring its prototype when
it is defined after the main function?
o A) The compiler will throw an error because it does not know about the function.
o B) The program will run normally.
o C) The compiler will automatically generate the function's prototype.
o D) The function will be called but will not return a value.
Answer: A) The compiler will throw an error because it does not know about the
function.
Answer: B) Function overloading allows functions with the same name but different
parameter lists.
31. What will happen if you forget to include a return statement in a function that has a
non-void return type?
o A) The function will return 0 by default.
o B) The program will throw a compile-time error.
o C) The program will execute, but the return value will be undefined.
o D) The function will print an error message.
32. Which of the following is correct for the function circleArea that calculates the
area of a circle given the radius?
o A) double circleArea(int radius);
o B) double circleArea(double radius);
o C) int circleArea(double radius);
o D) void circleArea(double radius);
33. Which keyword in C++ is used to declare a function that takes parameters but does
not return any value?
o A) void
o B) return
o C) function
o D) int
Answer: A) void
cpp
Copy code
int cube(int number) {
return number * number * number;
}
int main() {
cout << cube(3);
return 0;
}
o A) 9
o B) 6
o C) 27
o D) 30
Answer: C) 27