Correct Answer: All of the above
Explanation: Functions in C help improve code readability by breaking it into smaller, manageable units, reduce redundancy by allowing code reuse, and modularize code for easier maintenance and debugging.
Correct Answer: A block of code that performs a specific task
Explanation: In C programming, a function is a block of code that performs a specific task. It can be called from anywhere in the program to execute its functionality.
Correct Answer: They are always passed by value.
Explanation: In C programming, function parameters are variables declared in the function definition and are always passed by value, meaning the function receives a copy of the value stored in the parameter.
Correct Answer: void
Explanation: In C programming, the void return type is used for functions that do not return any value. These functions are typically used for tasks that perform actions rather than calculate and return values.
Correct Answer: functionName();
Explanation: In C programming, a function is called by using its name followed by parentheses containing any required arguments. This syntax tells the compiler to execute the code within the function.
Correct Answer: There is no fixed limit
Explanation: In C programming, there is no fixed limit to the number of parameters a function can have. However, excessive parameters can make the code harder to read and maintain.
Correct Answer: They require a base case to terminate the recursion.
Explanation: Recursive functions in C call themselves to solve a problem by breaking it down into smaller subproblems. To prevent infinite recursion, a base case is needed to terminate the recursion.
Correct Answer: All of the above
Explanation: A function signature in C includes the function name, return type, and parameter types in the order they appear. It uniquely identifies the function and its interface.
Correct Answer: It will result in a compilation error.
Explanation: Calling a function with more arguments than declared in its definition in C will result in a compilation error because the compiler expects the correct number of arguments to match the function’s declaration.
Correct Answer: Local variables are accessible only within the function they are declared.
Explanation: In C programming, local variables are declared within a function and are accessible only within that function. They cannot be accessed from outside the function.
Correct Answer: A statement that declares the function’s return type, name, and parameters.
Explanation: A function declaration in C specifies the function’s return type, name, and parameters without providing the implementation. It serves as a prototype for the function.
Correct Answer: Before calling the function
Explanation: In C programming, a function declaration should appear before calling the function to inform the compiler about the function’s existence and interface.
Correct Answer: To inform the compiler about the function’s interface
Explanation: A function prototype in C informs the compiler about the function’s return type, name, and parameter types without providing the implementation. It allows the compiler to check the function calls for correctness.
Correct Answer: extern
Explanation: In C programming, the extern keyword is used to declare a function without providing its implementation. It indicates that the function is defined elsewhere in the program.
Correct Answer: returnType functionName(parameters);
Explanation: In C programming, a function declaration starts with the return type, followed by the function name and parameters enclosed in parentheses. The semicolon terminates the declaration.
Correct Answer: They must precede the function call.
Explanation: In C programming, function declarations must appear before calling the function to allow the compiler to understand the function’s interface before encountering its usage.
Correct Answer: To support forward referencing
Explanation: Separating function declaration and definition in C allows forward referencing, where functions can be declared before being defined, enabling functions to call each other in any order.
Correct Answer: Header files
Explanation: In C programming, function declarations are often placed in header files, which are included at the beginning of source files to provide function prototypes to other parts of the program.
Correct Answer: To provide function prototypes
Explanation: Header files in C programming contain function prototypes, type definitions, and other declarations needed to use external functions and libraries in the program.
Correct Answer: They can be located anywhere in the program.
Explanation: In C programming, function definitions can appear anywhere in the program. They include the actual implementation of the function’s functionality.
Correct Answer: By using the function’s name followed by parentheses and arguments
Explanation: To call a function in C from another function, you use the function’s name followed by parentheses containing any required arguments.
Correct Answer: Function calls can be nested within other function calls.
Explanation: In C programming, function calls can be nested within other function calls, allowing for complex program structures and logic.
Correct Answer: A memory area used to store local variables and function call information
Explanation: In C programming, the function call stack is a memory area used to store local variables, return addresses, and other function call information during program execution.
Correct Answer: To return a value to the calling function
Explanation: The return statement in a function call in C is used to return a value to the calling function. It can also be used to terminate the function execution prematurely.
Correct Answer: Arguments are always passed by value.
Explanation: In C programming, arguments are always passed by value to functions, meaning a copy of the argument’s value is passed to the function.
Correct Answer: The compiler will generate a warning.
Explanation: Calling a function with fewer arguments than declared in its definition in C will generate a compiler warning because the function expects a certain number of arguments.
Correct Answer: To inform the compiler about the function’s interface
Explanation: A function prototype in C informs the compiler about the function’s return type, name, and parameter types without providing the implementation. It allows the compiler to check function calls for correctness.
Correct Answer: Functions can be called recursively.
Explanation: In C programming, functions can call themselves recursively, allowing for elegant solutions to problems that exhibit recursive behavior.
Correct Answer: functionName(arguments);
Explanation: In C programming, to call a function with arguments, you use the function’s name followed by parentheses containing the arguments.
Correct Answer: Functions can be called from anywhere in the program.
Explanation: In C programming, functions can be called from anywhere in the program, including other functions, as long as their prototypes are visible.
#include <stdio.h>
void displayMessage() {
printf("Hello, world!\n");
}
int main() {
displayMessage();
return 0;
}
Correct Answer: Hello, world!
Explanation: The code defines a function `displayMessage()` that prints “Hello, world!” and calls it from the `main()` function. Therefore, the output will be “Hello, world!”.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 8
Explanation: The code defines a function `add()` that takes two integer arguments and returns their sum. In `main()`, it calls `add(3, 5)` and prints the result, which is 8.
#include <stdio.h>
void printNumbers(int n) {
for (int i = 1; i <= n; i++) {
printf("%d ", i);
}
printf("\n");
}
int main() {
printNumbers(5);
return 0;
}
Correct Answer: 1 2 3 4 5
Explanation: The code defines a function `printNumbers()` that prints numbers from 1 to n. In `main()`, it calls `printNumbers(5)`, so the output will be "1 2 3 4 5".
#include <stdio.h>
int multiply(int x, int y) {
return x * y;
}
int main() {
int result = multiply(4, 3) + multiply(2, 5);
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 26
Explanation: The code defines a function `multiply()` that takes two integer arguments and returns their product. In `main()`, it calls `multiply(4, 3)` and `multiply(2, 5)` and adds their results, which is 12 + 10 = 22.
#include <stdio.h>
void sayHello(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
char myName[] = "Alice";
sayHello(myName);
return 0;
}
Correct Answer: Hello, Alice!
Explanation: The code defines a function `sayHello()` that takes a character array as an argument and prints a greeting. In `main()`, it defines a character array `myName` with the value "Alice" and calls `sayHello(myName)`, resulting in "Hello, Alice!" being printed.
#include <stdio.h>
void printMessage() {
printf("Welcome to C programming!\n");
}
int main() {
printMessage();
return 0;
}
Correct Answer: Welcome to C programming!
Explanation: The code defines a function `printMessage()` that prints "Welcome to C programming!" and calls it from the `main()` function. Therefore, the output will be "Welcome to C programming!".
#include <stdio.h>
int power(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
int result = power(2, 3);
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 8
Explanation: The code defines a function `power()` that calculates the power of a number. In `main()`, it calls `power(2, 3)`, which computes 2^3 = 8, so the output will be "Result: 8".
#include <stdio.h>
void printPattern(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
int main() {
printPattern(4);
return 0;
}
* *
* * *
* * * *
**
***
****
Correct Answer: *
* *
* * *
* * * *
Explanation: The code defines a function `printPattern()` that prints a pattern of stars. In `main()`, it calls `printPattern(4)`, resulting in the output as described.
#include <stdio.h>
int sum(int arr[], int size) {
int total = 0;
for (int i = 0; i < size; i++) {
total += arr[i];
}
return total;
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int totalSum = sum(numbers, 5);
printf("Total Sum: %d\n", totalSum);
return 0;
}
Correct Answer: Total Sum: 15
Explanation: The code defines a function `sum()` that calculates the sum of elements in an array. In `main()`, it creates an array `numbers` and calls `sum(numbers, 5)`, which computes the sum of elements {1, 2, 3, 4, 5}, resulting in "Total Sum: 15".
#include <stdio.h>
void reverseString(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
for (int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
}
int main() {
char message[] = "Hello";
reverseString(message);
return 0;
}
Correct Answer: olleH
Explanation: The code defines a function `reverseString()` that reverses a given string. In `main()`, it defines a character array `message` with the value "Hello" and calls `reverseString(message)`, resulting in "olleH" being printed.
Correct Answer: By value
Explanation: By default, arguments are passed to functions in C by value, meaning a copy of the argument's value is passed to the function.
Correct Answer: It is unchanged
Explanation: When passed by value, the original variable remains unchanged within the function because only a copy of its value is passed.
Correct Answer: None of the above
Explanation: None of the mentioned operations can modify the original variable passed by value to a function in C because the function receives only a copy of the variable's value.
Correct Answer: It simplifies function calls
Explanation: Passing arguments by value simplifies function calls because it avoids potential side effects on the original variables and makes the function's behavior more predictable.
Correct Answer: Arrays are always passed by value.
Explanation: In C, arrays are always passed by value to functions, meaning a copy of the array's address is passed, not the entire array.
Correct Answer: The entire array is copied, leading to increased memory usage and performance issues.
Explanation: When a large array is passed by value to a function in C, the entire array is copied, which can lead to increased memory usage and performance issues, especially for large arrays.
Correct Answer: Pass a pointer to the array
Explanation: To avoid copying the entire array, a pointer to the array can be passed to the function, allowing the function to access the original array directly.
Correct Answer: By address
Explanation: When using pointers, arguments are passed to functions by their memory address, allowing the function to access and modify the original variable.
Correct Answer: It allows for modifying the original variables directly
Explanation: Passing arguments by reference in C allows functions to modify the original variables directly, making it useful for functions that need to update variable values.
Correct Answer: pointer
Explanation: In C, arguments can be passed by reference using pointers, which allow functions to access and modify the original variables directly.
#include <stdio.h>
void changeValue(int x) {
x = 10;
}
int main() {
int num = 5;
changeValue(num);
printf("Value after function call: %d\n", num);
return 0;
}
Correct Answer: Value after function call: 5
Explanation: The code passes the variable `num` to the function `changeValue()` by value, meaning a copy of `num` is modified within the function. Therefore, the original value of `num` remains unchanged, and the output will be "Value after function call: 5".
#include <stdio.h>
void changeValue(int *x) {
*x = 10;
}
int main() {
int num = 5;
changeValue(&num);
printf("Value after function call: %d\n", num);
return 0;
}
Correct Answer: Value after function call: 10
Explanation: The code passes the address of the variable `num` to the function `changeValue()` using a pointer. Inside the function, the value at the address pointed to by `x` is modified to 10. Therefore, the output will be "Value after function call: 10".
#include <stdio.h>
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
swap(x, y);
printf("x: %d, y: %d\n", x, y);
return 0;
}
Correct Answer: x: 5, y: 10
Explanation: The code tries to swap the values of `x` and `y` using a function `swap()`. However, since the arguments are passed by value, the swap operation occurs on copies of `x` and `y`, not on the original variables. Therefore, the output will be "x: 5, y: 10".
#include <stdio.h>
void increment(int *ptr) {
(*ptr)++;
}
int main() {
int num = 5;
increment(&num);
printf("Incremented value: %d\n", num);
return 0;
}
Correct Answer: Incremented value: 6
Explanation: The code passes the address of the variable `num` to the function `increment()` using a pointer. Inside the function, the value at the address pointed to by `ptr` is incremented by 1. Therefore, the output will be "Incremented value: 6".
#include <stdio.h>
void modifyArray(int arr[]) {
arr[0] = 10;
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
modifyArray(numbers);
printf("Modified array: %d\n", numbers[0]);
return 0;
}
Correct Answer: Modified array: 10
Explanation: The code passes the array `numbers` to the function `modifyArray()`, where the first element of the array is modified to 10. Since arrays are passed by reference (as pointers to their first elements), the modification affects the original array. Therefore, the output will be "Modified array: 10".
Correct Answer: To return a value to the calling function
Explanation: The return statement in a function in C is used to return a value to the calling function, allowing the function to provide a result or output.
Correct Answer: return
Explanation: In C programming, the return keyword is used to explicitly return a value from a function to the calling function.
#include <stdio.h>
int square(int num) {
return num * num;
}
int main() {
int result = square(5);
printf("Square: %d\n", result);
return 0;
}
Correct Answer: Square: 25
Explanation: The code defines a function `square()` that calculates the square of a number. In `main()`, it calls `square(5)`, which returns 25, so the output will be "Square: 25".
#include <stdio.h>
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
int result = max(8, 3);
printf("Max: %d\n", result);
return 0;
}
Correct Answer: Max: 8
Explanation: The code defines a function `max()` that returns the maximum of two numbers. In `main()`, it calls `max(8, 3)`, which returns 8, so the output will be "Max: 8".
#include <stdio.h>
int absolute(int num) {
if (num < 0) {
return -num;
} else {
return num;
}
}
int main() {
int result = absolute(-5);
printf("Absolute value: %d\n", result);
return 0;
}
Correct Answer: Absolute value: 5
Explanation: The code defines a function `absolute()` that returns the absolute value of a number. In `main()`, it calls `absolute(-5)`, which returns 5, so the output will be "Absolute value: 5".
#include <stdio.h>
void greet() {
printf("Hello, ");
}
int main() {
greet();
return 0;
}
Correct Answer: Hello,
Explanation: The code defines a function `greet()` that prints "Hello, ". In `main()`, it calls `greet()`, resulting in the output "Hello, ".
#include <stdio.h>
void printMessage() {
return;
printf("World!");
}
int main() {
printMessage();
printf("Hello, ");
return 0;
}
Correct Answer: Hello,
Explanation: The code defines a function `printMessage()` with a return statement before the printf statement. The code after the return statement will not be executed. Therefore, the output will be "Hello, ".
#include <stdio.h>
int calculate() {
return 5 * 5;
}
int main() {
int result = calculate();
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 25
Explanation: The code defines a function `calculate()` that returns the result of the expression 5 * 5. In `main()`, it calls `calculate()`, resulting in the output "Result: 25".
#include <stdio.h>
int getValue() {
return;
}
int main() {
int result = getValue();
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Compiler error
Explanation: The code defines a function `getValue()` that does not return any value despite being declared as an int function. Therefore, it will result in a compiler error.
#include <stdio.h>
int getRandom() {
return rand(); // Return a random integer
}
int main() {
int result = getRandom();
printf("Random number: %d\n", result);
return 0;
}
Correct Answer: Random number: (some random value)
Explanation: The code defines a function `getRandom()` that returns a random integer value using the `rand()` function. In `main()`, it calls `getRandom()`, resulting in the output "Random number: (some random value)" where `(some random value)` is the integer returned by `rand()` function.
Correct Answer: A function calling itself
Explanation: Recursion in C refers to the process where a function calls itself directly or indirectly.
Correct Answer: The terminating condition that stops the recursion
Explanation: The base case in recursion is the condition that stops the recursive calls, preventing infinite recursion.
Correct Answer: It causes infinite recursion
Explanation: Without a base case, a recursive function will continue to call itself indefinitely, leading to infinite recursion.
Correct Answer: Subproblem decomposition
Explanation: Recursion involves breaking down a problem into smaller instances of the same problem, known as subproblem decomposition.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int result = factorial(5);
printf("Factorial of 5: %d\n", result);
return 0;
}
Correct Answer: Factorial of 5: 120
Explanation: The code calculates the factorial of 5 using recursion. The factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
#include <stdio.h>
void countdown(int n) {
if (n == 0) {
printf("Go!\n");
} else {
printf("%d ", n);
countdown(n - 1);
}
}
int main() {
countdown(5);
return 0;
}
Correct Answer: 5 4 3 2 1 Go!
Explanation: The code implements a countdown function using recursion. It prints numbers from n to 1, followed by "Go!" when n reaches 0.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int result = fibonacci(6);
printf("Fibonacci of 6: %d\n", result);
return 0;
}
Correct Answer: Fibonacci of 6: 13
Explanation: The code calculates the 6th Fibonacci number using recursion. The 6th Fibonacci number is 8, which is the sum of the previous two Fibonacci numbers (5 and 3).
#include <stdio.h>
void printPattern(int n) {
if (n > 0) {
printf("%d ", n);
printPattern(n - 1);
printf("%d ", n);
}
}
int main() {
printPattern(3);
return 0;
}
Correct Answer: 3 2 1 1 2 3
Explanation: The code prints a pattern using recursion. It first prints numbers from n to 1, then from 1 to n.
#include <stdio.h>
int sum(int n) {
if (n == 0) {
return 0;
} else {
return n + sum(n - 1);
}
}
int main() {
int result = sum(4);
printf("Sum of first 4 natural numbers: %d\n", result);
return 0;
}
Correct Answer: Sum of first 4 natural numbers: 10
Explanation: The code calculates the sum of the first 4 natural numbers using recursion. The sum of the first n natural numbers is n * (n + 1) / 2.
#include <stdio.h>
int power(int base, int exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
}
int main() {
int result = power(2, 3);
printf("2 raised to the power of 3: %d\n", result);
return 0;
}
Correct Answer: 2 raised to the power of 3: 8
Explanation: The code calculates 2 raised to the power of 3 using recursion. The result is 8.
Correct Answer: A function that is declared with the inline keyword
Explanation: An inline function in C is a function that is declared with the inline keyword, suggesting the compiler to replace the function call with the function's body at the call site.
Correct Answer: To reduce function call overhead
Explanation: Inline functions in C are used to reduce function call overhead by replacing the function call with the function's body, potentially improving performance.
Correct Answer: They cannot be recursive
Explanation: One limitation of inline functions in C is that they cannot be recursive, meaning they cannot call themselves directly or indirectly.
Correct Answer: It automatically becomes a regular function
Explanation: If an inline function's definition exceeds a certain size or complexity, the compiler may choose to ignore the inline keyword, making it a regular function.
Correct Answer: inline void functionName() {}
Explanation: The syntax to declare an inline function in C is to prefix the function declaration with the inline keyword.
#include <stdio.h>
inline int square(int num) {
return num * num;
}
int main() {
int result = square(5);
printf("Square of 5: %d\n", result);
return 0;
}
Correct Answer: Square of 5: 25
Explanation: The code defines an inline function `square()` that calculates the square of a number. In `main()`, it calls `square(5)`, resulting in the output "Square of 5: 25".
#include <stdio.h>
inline int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 8
Explanation: The code defines an inline function `add()` that calculates the sum of two numbers. In `main()`, it calls `add(3, 5)`, resulting in the output "Result: 8".
#include <stdio.h>
inline int multiply(int x, int y) {
return x * y;
}
int main() {
int result = multiply(4, 3) + multiply(2, 5);
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 26
Explanation: The code defines an inline function `multiply()` that calculates the product of two numbers. In `main()`, it calls `multiply(4, 3)` and `multiply(2, 5)` and adds their results, which is 12 + 10 = 26.
#include <stdio.h>
inline int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
int result = max(8, 3);
printf("Max: %d\n", result);
return 0;
}
Correct Answer: Max: 8
Explanation: The code defines an inline function `max()` that returns the maximum of two numbers. In `main()`, it calls `max(8, 3)`, resulting in the output "Max: 8".
#include <stdio.h>
inline int absolute(int num) {
return (num < 0) ? -num : num;
}
int main() {
int result = absolute(-5);
printf("Absolute value: %d\n", result);
return 0;
}
Correct Answer: Absolute value: 5
Explanation: The code defines an inline function `absolute()` that returns the absolute value of a number. In `main()`, it calls `absolute(-5)`, resulting in the output "Absolute value: 5".
Correct Answer: Inline functions can have a variable number of arguments.
Explanation: Inline functions in C can have a variable number of arguments just like regular functions.
Correct Answer: Reduced function call overhead
Explanation: Inline functions in C help reduce function call overhead, leading to potential performance improvements.
Correct Answer: When the function is called frequently with small code
Explanation: Inline functions are most effective when they are called frequently with small code, as they reduce the overhead of function calls.
Correct Answer: Inline functions are automatically expanded at each call site by the compiler.
Explanation: Inline functions are expanded at each call site by the compiler, replacing the function call with the function's body.
Correct Answer: inline
Explanation: The `inline` keyword is used to declare an inline function in C.
#include <stdio.h>
inline int square(int num) {
return num * num;
}
int main() {
int result = square(5);
printf("Square of 5: %d\n", result);
return 0;
}
Correct Answer: Square of 5: 25
Explanation: The code defines an inline function `square()` that calculates the square of a number. In `main()`, it calls `square(5)`, resulting in the output "Square of 5: 25".
#include <stdio.h>
inline int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 8
Explanation: The code defines an inline function `add()` that calculates the sum of two numbers. In `main()`, it calls `add(3, 5)`, resulting in the output "Result: 8".
#include <stdio.h>
inline int multiply(int x, int y) {
return x * y;
}
int main() {
int result = multiply(4, 3) + multiply(2, 5);
printf("Result: %d\n", result);
return 0;
}
Correct Answer: Result: 26
Explanation: The code defines an inline function `multiply()` that calculates the product of two numbers. In `main()`, it calls `multiply(4, 3)` and `multiply(2, 5)` and adds their results, which is 12 + 10 = 26.
#include <stdio.h>
inline int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
int result = max(8, 3);
printf("Max: %d\n", result);
return 0;
}
Correct Answer: Max: 8
Explanation: The code defines an inline function `max()` that returns the maximum of two numbers. In `main()`, it calls `max(8, 3)`, resulting in the output "Max: 8".
#include <stdio.h>
inline int absolute(int num) {
return (num < 0) ? -num : num;
}
int main() {
int result = absolute(-5);
printf("Absolute value: %d\n", result);
return 0;
}
Correct Answer: Absolute value: 5
Explanation: The code defines an inline function `absolute()` that returns the absolute value of a number. In `main()`, it calls `absolute(-5)`, resulting in the output "Absolute value: 5".
#include <stdio.h>
void foo(int x) {
printf("%d ", x);
}
int bar(int y) {
return y * 2;
}
int main() {
int num = 5;
foo(bar(num));
return 0;
}
Correct Answer: 10
Explanation: The code calls function `bar()` with `num` as argument, which returns 10 (5 * 2). Then, the value 10 is passed to function `foo()` which prints it.
#include <stdio.h>
int compute(int x, int y) {
return x * y;
}
void display(int a, int b) {
printf("%d\n", a + b);
}
int main() {
int result = compute(3, 4);
display(result, 5);
return 0;
}
Correct Answer: 7
Explanation: The `compute()` function multiplies 3 and 4, resulting in 12. Then, `display()` function is called with arguments 12 and 5, printing their sum, which is 17.
#include <stdio.h>
void calculate(int x, int y, int *sum, int *product) {
*sum = x + y;
*product = x * y;
}
int main() {
int a = 3, b = 4, sum, product;
calculate(a, b, &sum, &product);
printf("Sum: %d, Product: %d\n", sum, product);
return 0;
}
Correct Answer: Sum: 7, Product: 12
Explanation: The `calculate()` function calculates the sum and product of `x` and `y` and assigns them to the variables pointed by `sum` and `product`. Then, in `main()`, these values are printed.
#include <stdio.h>
int mystery(int n) {
if (n <= 0)
return 0;
return n + mystery(n - 1);
}
int main() {
printf("%d\n", mystery(5));
return 0;
}
Correct Answer: 15
Explanation: The `mystery()` function calculates the sum of numbers from 1 to `n`. In this case, `mystery(5)` calculates 5 + 4 + 3 + 2 + 1 = 15.
#include <stdio.h>
int sum(int x, int y) {
return x + y;
}
int main() {
int (*func_ptr)(int, int) = ∑
printf("%d\n", (*func_ptr)(3, 4));
return 0;
}
Correct Answer: 7
Explanation: The code defines a function pointer `func_ptr` that points to the function `sum()`. Then, it dereferences and calls the function pointer with arguments 3 and 4, resulting in 7.