Function
Function
1. What is a function in C?
o Answer: A function is a block of code that performs a specific task. It is called
by its name and can take arguments and return a value.
2. What is the syntax for declaring a function in C?
o Answer:
return_type function_name(parameters) {
// Code
}
3. What is the difference between void and int return types in functions?
o Answer: A void function does not return a value, while an int function
returns an integer value.
4. Write a function to calculate the square of a number.
o Code:
int main() {
printf("%d", add(5, 3));
return 0;
}
o Output: 8.
Medium Questions
c
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
printf("%d", factorial(5));
return 0;
}
o Output: 120.
8. Explain the concept of pass-by-value in C.
o Answer: In C, function arguments are passed by value. Changes to the
parameter inside the function do not affect the original argument.
9. Write a function to check if a number is even or odd.
o Code:
int main() {
printf(isEven(10) ? "Even" : "Odd");
return 0;
}
o Output: Even.
10. How do you declare and call a function with no parameters?
o Answer:
void myFunction() {
printf("Hello");
}
int main() {
myFunction();
return 0;
}
o Output: Hello.
11. Write a program to calculate the GCD of two numbers using a function.
o Code:
int main() {
printf("%d", gcd(24, 36));
return 0;
}
o Output: 12.
Hard Questions
12. Write a program to generate the Fibonacci series up to n terms using recursion.
o Code:
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
for (int i = 0; i < 5; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
o Output: 0 1 1 2 3.
13. What is the difference between a function declaration and a function definition in C?
o Answer:
Function Declaration: Tells the compiler about a function's name,
return type, and parameters.
Function Definition: Contains the actual implementation of the
function.
14. Write a program to reverse a string using a function.
o Code:
int main() {
char str[] = "Hello";
reverse(str);
printf("%s", str);
return 0;
}
o Output: olleH.
15. How is memory allocated for function calls in C?
o Answer: Memory for function calls is allocated on the stack. Each call creates
a new stack frame.
16. Write a function to find the maximum of three numbers.
o Code:
int main() {
printf("%d", maxOfThree(10, 20, 15));
return 0;
}
o Output: 20.
Conceptual Questions
Programming Questions
Output Questions
31. Predict the output of:
void test() {
printf("Test");
}
int main() {
test();
return 0;
}
o Output: Test.
32. Predict the output:
int test() {
return 5;
}
int main() {
printf("%d", test());
return 0;
}
o Output: 5.
Conceptual Questions
Descriptive Questions
int main() {
int arr[] = {1, 2, 3};
printArray(arr, 3);
return 0;
}
int fun(int n) {
if (n == 1) return 0;
return 1 + fun(n / 2);
}
int main() {
printf("%d", fun(16));
return 0;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("%d %d", x, y);
return 0;
}
o Output: 20 10.
3. c
void test(int x) {
printf("%d ", x);
if (x > 1) test(x - 1);
}
int main() {
test(3);
return 0;
}
o Output: 3 2 1.
4. c
int main() {
printf("%d", add(3, 2));
return 0;
}
o Output: 5.
5. c
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
int main() {
printf("%d", fib(4));
return 0;
}
o Output: 3.
3. Check Palindrome:
4. Bubble Sort:
5. Power of a Number:
You said:
ChatGPT
Here are some function-to-array type questions with varying difficulty, including
conceptual, descriptive, and coding questions with answers:
Conceptual Questions
Descriptive Questions
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
printMatrix(matrix, 2);
return 0;
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
modifyArray(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]); // Output: 2 3 4 5 6
}
return 0;
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Sum: %d", sumArray(arr, 5));
return 0;
}
Output: Sum: 15
int main() {
int arr[5] = {10, 20, 5, 40, 25};
printf("Max: %d", maxElement(arr, 5));
return 0;
}
Output: Max: 40
int main() {
int arr[5] = {1, 2, 3, 4, 5};
reverseArray(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]); // Output: 5 4 3 2 1
}
return 0;
}
int main() {
int arr[6] = {1, 2, 3, 2, 5, 2};
printf("Occurrences: %d", countOccurrences(arr, 6, 2));
return 0;
}
Output: Occurrences: 3
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int
result[]) {
for (int i = 0; i < size1; i++) result[i] = arr1[i];
for (int i = 0; i < size2; i++) result[size1 + i] = arr2[i];
}
int main() {
int arr1[3] = {1, 2, 3};
int arr2[3] = {4, 5, 6};
int result[6];
mergeArrays(arr1, 3, arr2, 3, result);
for (int i = 0; i < 6; i++) {
printf("%d ", result[i]); // Output: 1 2 3 4 5 6
}
return 0;
}
int main() {
int arr[4] = {10, 20, 30, 40};
printf("Average: %.2f", averageArray(arr, 4));
return 0;
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
rotateLeft(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]); // Output: 2 3 4 5 1
}
return 0;
}
int main() {
int arr[6] = {1, 2, 2, 3, 3, 3};
frequencyArray(arr, 6);
return 0;
}
Output:
bash
int main() {
int arr[] = {1, 2, 3, 4};
func(arr + 1, 3);
for (int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output: 1 4 6 8
Explanation: func operates on arr + 1, doubling elements from index 1 onward.
2. c
Copy code
void swap(int *a, int *b) {
int *temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d", x, y);
return 0;
}
Output: 5 10
Explanation: The pointers a and b are swapped, not the values they point to. This has
no effect on the original variables.
3. c
Copy code
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
*(p + 2) = 100;
printf("%d %d", arr[2], *(arr + 2));
return 0;
}
4. c
Copy code
int main() {
int a = 5, b = 10;
int *p1 = &a, *p2 = &b;
*p1 += *p2;
*p2 = *p1 - *p2;
printf("%d %d", a, b);
return 0;
}
Output: 15 5
Explanation: *p1 += *p2 makes a = 15. Then *p2 = *p1 - *p2 assigns b = 5.
5. c
Copy code
int fun(int n) {
if (n % 2 == 0)
return n / 2;
else
return 3 * n + 1;
}
int main() {
int x = 6;
for (int i = 0; i < 3; i++) {
x = fun(x);
printf("%d ", x);
}
return 0;
}
Output: 3 10 5
Explanation:
o Iteration 1: fun(6) → 6 / 2 = 3
o Iteration 2: fun(3) → 3 * 3 + 1 = 10
o Iteration 3: fun(10) → 10 / 2 = 5
6. c
Copy code
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr + 1;
*(p++) = 10;
*(p + 1) = 20;
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output: 1 10 3 20 5
Explanation:
int main() {
int arr[] = {1, 2, 3, 4, 5};
modify(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output: 1 3 5 7 9
Explanation: Each element in the array is incremented by its index.
8. c
Copy code
int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("%d %d", arr[1][1], *(*(arr + 2) + 2));
return 0;
}
Output: 5 9
Explanation:
int main() {
int arr[] = {1, 2, 3, 4};
for (int i = 0; i < 4; i++) {
printf("%d ", fun(arr[i]));
}
return 0;
}
Output: 2 1 6 2
Explanation: The fun function doubles odd numbers and halves even numbers.
10.
c
Copy code
int main() {
char str[] = "Hello";
char *p = str;
*p = 'Y';
printf("%s", str);
return 0;
}
Output: Yello
Explanation: The first character of the string is modified to 'Y'.