This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1fact (N)
if (N equal 0)
return 1
else
return N * fact (N-1)
The above algorithm is a specific example of ______________.
Option C
The given algorithm calculates the factorial of a number N using a function that calls itself with N−1, a clear example of Recursion.
Q: 2If the following function is called using func(5), how many times will the printf statement be executed:
int func(int n)
{
if(n==0)
return 0;
printf(“%d ”,n*n);
return(func(n-1)+n*n);
}
Option B
The printf statement is executed once for each recursive call before reaching the base case (n==0). Therefore, it executes 5 Times.
Q: 3Choose the correct statement respect to function.
Option D
A function in C language is a block of code that performs a specific task. Functions help in dividing a large program into smaller, manageable parts, which makes the program easier to understand, write, and maintain.
Functions are executed only when they are called and help in better memory utilization because the same function code can be reused multiple times instead of writing the same code again.
Q: 4How are arrays passed to a function in C?
Option B
In C, when an array is passed to a function, the base address of the array is passed, not a copy of all its elements.
The array name itself represents the address of the first element, so any modification made to the array elements inside the function affects the original array. Because the function receives the address and works directly on the original data.
#include<stdio.h>
void changeValue(int crr[])
{
int i;
for(i=0;i<3;i++)
crr[i]=crr[i]+5;
}
void main()
{
int i;
int arr[3]={10,20,30};
changeValue(arr);
for(i=0;i<3;i++)
printf("%d ",arr[i]);
}
OUTPUT
15 25 35
Q: 5Which of the following is false
Option A
In programming, when a function is called, its local variables and function calls are stored in the Stack memory, not in the Heap.
Q: 6What is the output of the following C program?
#include<stdio.h>
int f(int n)
{
static int r=0;
if(n<=0)
return(1);
if(n>3)
{
r=n;
return(f(n-2)+2);
}
return(f(n-1)+r);
}
void main()
{
int y;
y=f(5);
printf("%d",y);
}
Option B
The program uses a recursive function f() with a static variable r, which retains its value across all recursive calls.
When f(5) is called, the condition n>3 is true, so r is assigned the value 5, and the function returns f(3)+2.
For f(3), the condition n>3 is false and n<=0 is also false, so the function executes the last return statement f(n-1)+r. Since r is static and already set to 5, it remains unchanged in all subsequent calls.
The recursion continues as f(3) = f(2)+5, f(2)=f(1)+5, and f(1)=f(0)+5. When f(0) is reached, the condition n<=0 becomes true and the function returns 1.
The return values are then calculated as follows:
Therefore, the output printed by the program is 18.
Q: 7Which of the following is/are mandatory in function declaration?
Option A
In C/C++, a Function Declaration (Prototype) is used to inform the compiler about the function before its actual use. The essential components required in a function declaration are the return type and the function name.
E.g.:
Q: 8Study the given program and choose the best option for output.
#include<stdio.h>
void f1();
void main()
{
void f1(int);
f1();
}
void f1()
{
printf(“2”);
}
Option C
In C language, function declarations or prototypes and definitions must be consistent in terms of parameters. If a function is declared with parameters, then its definition must also match the same parameter list.
Inside main(), the function f1 is declared as void f1(int), which means it expects one integer argument. However, the function is called as f1() without passing any argument, and its actual definition is also without parameters. This creates a mismatch between declaration, definition, and function call, which leads to a Compile-Time Error.
Note:
In C language, a function can be declared multiple times, but all declarations must be consistent (same return type and same parameters).
Multiple declarations do not create any problem as long as they are identical. The compiler simply treats them as repeated information about the same function.
E.g.:
void f1();
void f1(); // Repeated Declaration (Allowed)
int main()
{
f1();
}
void f1()
{
printf("Hello, Suresh");
}
| EXAMPLE | RESULT | REASON |
|---|---|---|
| void f1(); void f1(); | Valid. No Error. | Same function declared multiple times with identical signature. |
| int sum(int, int); int sum(int, int); | Valid. No Error. | Return type and parameters are exactly same. |
| void f1(int); void f1(); | Invalid. Compile Time Error. | Parameter mismatch, one has argument, other does not. |
| int f1(); float f1(); | Invalid. Compile Time Error. | Return type mismatch. |
| int sum(int, int); int sum(int); | Invalid. Compile Time Error. | Number of parameters differ. |
Q: 9Use of function
(1) Achieve modularization.
(2) Better memory utilization.
(3) Avoiding rewriting of code.
(4) Makes debugging easier.
Option A
A function is a block of code written to perform a specific task. Functions help in organizing a program into smaller parts, which makes the program clear, reusable, and easy to manage.
Q: 10What will be the output of the following code?
#include<stdio.h>
void modify(int a, int b)
{
a += 10;
b += 20;
printf(“Inside modify: %d %d”,a,b);
}
int main()
{
int x =5, y = 7;
modify(x,y);
printf(“Inside main : %d %d”,x,y);
return 0;
}
Option A
This program demonstrates call by value in C. When modify(x,y) is called, the values of x and y are copied into local variables a and b. Inside the function, a becomes 15 and b becomes 27, which are printed. However, changes to a and b do not affect the original variables in main, so x and y remain 5 and 7.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.