C Practice Questions on Functions
Multiple Choice Questions
1. What will be the output of the following program?
#include <stdio.h>
void func(int a, int b) {
a += b;
b += a;
int main() {
int x = 5, y = 10;
func(x, y);
printf("%d, %d", x, y);
return 0;
(a) 5, 10
(b) 15, 25
(c) 5, 15
(d) 15, 10
2. Consider the following code:
#include <stdio.h>
int f(int n) {
static int i = 1;
if (n >= 5) return n;
n += i;
i++;
return f(n);
int main() {
printf("%d", f(1));
return 0;
What is the output?
(a) 5
(b) 6
(c) 7
(d) 8
3. What does the following function compute?
int mystery(int arr[], int n) {
if (n == 1) return arr[0];
int x = mystery(arr, n - 1);
return (x > arr[n - 1]) ? x : arr[n - 1];
(a) Sum of array elements
(b) Maximum element in the array
(c) Minimum element in the array
(d) Product of array elements
4. What is the output of the following code?
#include <stdio.h>
void print(int n) {
if (n > 0) {
print(n - 1);
printf("%d ", n);
print(n - 1);
int main() {
print(3);
return 0;
(a) 1 2 1 3 1 2 1
(b) 1 2 3 1 2 1
(c) 3 2 1 1 2 3
(d) 1 1 2 1 1 3
5. Which of the following function declarations is correct for a function that returns a pointer to
an integer and takes two integers as arguments?
(a) int *func(int a, int b);
(b) int (*func)(int a, int b);
(c) *int func(int a, int b);
(d) int func*(int a, int b);
6. What will be the output?
#include <stdio.h>
int main() {
int i = 5;
printf("%d %d %d", i, i++, ++i);
return 0;
(a) 5 5 6
(b) 7 6 6
(c) 6 6 6
(d) Undefined behavior
7. Consider the following recursive function:
int foo(int n) {
return (n > 0) ? n - foo(n - 1) : 0;
What is the return value of foo(5)?
(a) 3
(b) 2
(c) 1
(d) 0
8. What does the following function do?
void func(char *s) {
if (*s) {
func(s + 1);
printf("%c", *s);
(a) Prints the string in reverse
(b) Prints the string as it is
(c) Prints the first character repeatedly
(d) Infinite recursion
9. What will be the output of the following program?
#include <stdio.h>
int x = 5;
void func() {
static int x = 10;
x++;
printf("%d ", x);
int main() {
func();
func();
printf("%d", x);
return 0;
(a) 11 12 5
(b) 11 12 12
(c) 6 7 5
(d) 6 7 7
10. Consider the following recursive function:
int foo(int a, int b) {
if (b == 0) return 1;
return a * foo(a, b - 1);
}
What does this function compute?
(a) a + b
(b) a × b
(c) aᵇ
(d) bᵃ
11. What is the output of the following code?
#include <stdio.h>
void change(int *p) {
*p = 20;
p = NULL;
int main() {
int x = 10;
int *ptr = &x;
change(ptr);
printf("%d %p", x, (void*)ptr);
return 0;
(a) 20 (nil)
(b) 10 (address of x)
(c) 20 (address of x)
(d) 10 (nil)
12. Which of the following correctly declares a function that takes an integer pointer and returns
a function pointer to a function that takes integer and returns float?
(a) float (*func(int*))(int);
(b) float *func(int*)(int);
(c) float (*)(int) func(int*);
(d) float (*func)(int*, int);
13. What does the following function do?
int mystery(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
return arr[n/2];
(a) Returns the maximum element
(b) Returns the median after sorting
(c) Returns the middle element after bubble sort
(d) Returns the average of array elements
1. Which of the following is the correct syntax to declare a function that takes two integers as
arguments and returns an integer?
A) int function(int, int)
B) function(int, int) int
C) function(int, int);
D) int function(int, int);
2. Which keyword is used to define a function that does not return any value?
A) int
B) void
C) return
D) char
3. What is the output of the following code?
#include <stdio.h>
float divide(int a, int b) {
return a / b;
}
int main() {
float result = divide(5, 2);
printf("%.2f\n", result);
return 0;
}
A) 2.500000
B) 2.000000
C) 2
D) Compiler Error
4. What is the output of the following code?
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int doubleIt(int x) {
return 2 * x;
}
int main() {
int res = doubleIt(add(3, 4));
printf("%d\n", res);
return 0;
}
A) 14
B) 11
C) 7
D) 6
5. What is the output of the following code?
#include <stdio.h>
int greet() {
printf("World!");
}
int main() {
greet();
printf("Hello");
return 0;
}
A) Hello
B) Hello World!
C) World! Hello
D) Compiler Error
Q6. Study the following code the correct answer for the following questions:
#include <stdio.h>
int doubleVal(int x) {
return x * 2;
}
int halfVal(float y) {
return y / 2;
}
int mix(int a, float b) {
int x = doubleVal(a);
int y = halfVal(b);
return x + y;
}
int main() {
int a = 5;
float b = 7.0;
int result = mix(a, b);
printf("Result = %d\n", result);
return 0;
}
a. What is the value of x inside mix()?
A) 5
B) 10
C) 2
D) Compiler Error
b. What is the value returned by halfVal(b)?
A) 3.5
B) 3
C) 4
D) Compiler Error
c. What is the return value of mix(a, b)?
A) 13
B) 10
C) 7
D) 14
d. What is the output printed in main()?
A) Result = 13
B) 13
C) Result = 10
D) Result = 3
e. What causes the decimal part of b to be lost in halfVal?
A) Compiler bug
B) Return type mismatch
C) Implicit conversion from float to int
D) No loss occurs
7. What is the output of the following code when a double is passed to a function expecting an
int?
#include <stdio.h>
void printInt(int x) {
printf("%d\n", x);
}
int main() {
double num = 5.7;
printInt(num);
return 0;
}
A) 5
B) 5.7
C) Error: Type mismatch
D) 6
8. What will be the output of the following code:
#include <stdio.h>
int roundOff(double x) {
return (int)x;
}
int main() {
double num = 9.99;
printf("%d\n", roundOff(num));
return 0;
}
A) 9
B) 9.99
C) 10
D) Compiler Error
9. What will be the output of the following code:
#include <stdio.h>
int foo(double x) {
return (int)(x + 0.5); // Round to nearest integer
}
int main() {
double num = 9.75;
printf("%d\n", foo(num));
return 0;
}
A) 9
B) 9.75
C) 10
D) 10.0
10. What will be the printed output when long is passed to a function expecting an int?
#include <stdio.h>
int doubleValue(int x) {
return x * 2;
}
int main() {
long num = 10000000000L;
printf("%d\n", doubleValue(num));
return 0;
}
A) 20000000000
B) Undefined behavior
C) 0
D) 10000000000
11. What will be the output of the following code:
#include <stdio.h>
void modifyString(char *str) {
str[0] = 'A';
}
int main() {
char str[] = "Test";
modifyString(str);
printf("%s\n", str);
return 0;
}
A) Test
B) Aest
C) Compilation Error
D) Undefined behaviour
Programming Problems
1. Write a function int isArmstrong(int n) that returns 1 if the number is an Armstrong number,
otherwise 0.
An Armstrong number is a number that is equal to the sum of its own digits each raised to the
power of the number of digits.
Example: 153 = 1³ + 5³ + 3³.
2. Write a recursive function int sumDigits(int n) that returns the sum of digits of a positive
integer n.
3. Write a function void rotateArray(int arr[], int n, int k) that rotates the array arr of size n by k
positions to the left.
Example:
Input: arr = [1,2,3,4,5], n=5, k=2
Output: arr = [3,4,5,1,2]
4. Write a function int secondLargest(int arr[], int n) that returns the second largest element in the
array.
Assume n >= 2.
5. Write a function void removeDuplicates(char str[]) that removes duplicate characters from the
string str in-place.
Example:
Input: "programming"
Output: "progamin"
6. Write a recursive function void decimalToBinary(int n) that prints the binary equivalent of a
positive integer n.
7. Write a function int isSorted(int arr[], int n) that returns 1 if the array is sorted in
non-decreasing order, otherwise 0.
8. Write a function void swap(int *a, int *b) without using a temporary variable.
9. Write a function int countPairs(int arr[], int n, int sum) that returns the number of pairs in the
array whose sum is equal to sum.
10. Write a function void printPascal(int n) that prints the first n rows of Pascal’s triangle.
11. Write a function void stringStats(char str[], int *vowels, int *consonants, int *digits) that
counts and stores the number of vowels, consonants, and digits in the given string.
12. Write a recursive function int countOccurrences(int arr[], int n, int key) that returns how
many times key appears in the array.
13. Write a function void matrixMultiply(int A[][3], int B[][3], int C[][3]) that multiplies two
3×3 matrices and stores the result in C.
14. Write a function int isPerfectNumber(int n) that returns 1 if the number is a perfect number,
otherwise 0.
A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding
itself).
Example: 6 = 1 + 2 + 3
15. Write a function void printPattern(int n) that prints the following pattern for given n:
text
n = 3:
23
456
n = 4:
23
456
7 8 9 10
16. What will be the output of this program?
#include <stdio.h>
int counter() {
static int count = 0;
count++;
return count;
int main() {
printf("%d ", counter());
printf("%d ", counter());
printf("%d ", counter());
return 0;
17. Write a function that uses function pointers:
#include <stdio.h>
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }
void executeOperation(int (*op)(int, int), int x, int y) {
printf("Result: %d\n", op(x, y));
// What would you call this in main?
18. What is wrong with this function and how would you fix it?
char* getMessage() {
char msg[] = "Hello World";
return msg;
19. Write a recursive function to calculate the factorial of a number:
long long factorial(int n) {
// Your code here
}
20. Write a function that demonstrates pass by value vs pass by reference:
void modifyValues(int a, int *b) {
a = a * 2;
*b = *b * 2;
// What will be the output if called as:
// int x = 5, y = 10;
// modifyValues(x, &y);
// printf("x = %d, y = %d", x, y);
21. Write a function in C that takes an array of integers and its size as parameters, and returns the
average of the array elements
22. Write a function in C to perform a circular left shift of a given integer by k positions
23. Write a function to calculate weekly pay
● Program should ask the user to enter the number of hours worked in a week
● Assumptions:
● Basic pay rate = $12.00 per hour
● Overtime (In excess of 40 hours) = time and a half
● Tax Rate:
■ 15% of the first $300
■ 20% of the next $150
■ 25% of the rest
● Formulas:
● grossPay = payrate * hours
● taxes = grossPay * taxRate (use different taxrates for the amount of earned)
● netPay = grossPay - taxes
● Utilize if/else statements, const keyword
24. Write a function to calculate Simple Interest
25. Write a function to calculate Compound Interest
Write a function to calculate total Compound Interest for a certain number of years, including
Compound Interest earned each year, using tree method
Total CI = CI of 1st Year + CI of 2nd Year = 6000 + 7000
CI for 1st year = SI earned on same principal and rate of interest
The above image illustrates the calculation of simple interest for a principal amount of 36000 at a
rate of 16.66% for 2 years.
26. Write a function to reverse a string
27. Write a function to revere an array of integers without using a 2nd array
28. Write a function to count the number of vowels and consonants in a given string, the function
should accept a character array, and 2 integer variables to store the number of consonants and
vowels in the given string
29. Write a function to calculate the sum of digits of a given number using recursion
30. Write a function to print Fibonacci series up to n terms using recursion
31. Write a function to print the first n prime numbers
32. Write a function to find the length of a given string without using strlen()
33. Write a function to find the character with the maximum occurrence in a given string
34. Write a function that removes duplicate elements from an integer array