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.
Q: 11Consider the following recursive code for ak(m,n)
int ak(m,n)
{
if(m==0) then
return(n+1)
else if(n==0) then
return(ak(m-1,1))
else
return(ak(m-1,ak(m,n-1)))
}
The value returned when the code is invoked with ak(1,1) is
Option B
The given function is a form of the Ackermann Function, which is a well-known recursive function with multiple recursive calls.
The function is defined as:
| Step | Expression | Rule Used | Result |
|---|---|---|---|
| 1 | ak(1,1) | m!=0 and n!=0, so use ak(m-1, ak(m,n-1)) | ak(0, ak(1,0)) |
| 2 | ak(1,0) | n==0, so use ak(m-1,1) | ak(0,1) |
| 3 | ak(0,1) | m==0, so return n+1 | 2 |
| 4 | ak(1,0) | Substitute value from above | 2 |
| 5 | ak(1,1) | Substitute value back | ak(0,2) |
| 6 | ak(0,2) | m==0, so return n+1 | 3 |
The final value obtained after all recursive substitutions is 3.
Q: 12Consider the following C function, what will be the output when myFunction(5) is called?
int myFunction(int x)
{
int sum=0;
for(int i=1;i<=x;i++)
{
sum=sum+i;
}
printf(“%d”,sum);
}
Option C
When myFunction(5) is called, the variable sum is initially set to 0. The for loop runs from i=1 to i=5, and in each iteration, the value of i is added to sum. So, the calculation becomes, 1+2+3+4+5 = 15. After the loop completes, the function prints the value of sum, which is 15.
Q: 13What would be output of the following program?
int num=35;
void main()
{
int num=45;
printf("%d %d",num,num);
}
Option B
In this program, there are two variables with the same name num. One is a global variable (num = 35), and the other is a local variable inside main() (num = 45).
When a local and global variable have the same name, the local variable gets priority inside the function. It means the global variable is hidden inside the function.
So inside main(), when printf("%d %d", num, num); runs, it uses the local variable both times. The local variable has the value 45, so both %d will print 45.
Q: 14Find output of the code given below
#include<stdio.h>
void func(int *p, int *q)
{
p=q;
*p=2;
}
int main()
{
int i=0, j=1;
func(&i, &j);
printf("%d,%d", i, j);
return 0;
}
Option B
In the main function, i=0 and j= . When func(&i,&j) is called, the pointer p receives the address of i, and q receives the address of j.
Inside the function, the statement p=q; makes the local pointer p point to the same location as q, i.e., the address of j. This change affects only the local copy of p, not the original pointer in main.
Next, the statement *p=2; modifies the value at the address where p is pointing. Since p now points to j, the value of j becomes 2. The value of i remains unchanged.
Therefore, the value of i remains 0 and the value of j becomes 2, so the output printed is 0, 2.
Q: 15What would be output of the following program
void main()
{
int num=20;
{
int num=10;
}
printf("%d ",num);
}
Option B
In this program, a variable named num is first declared in the main() function with the value 20. Inside the next block, another variable with the same name num is declared and given the value 10. This inner variable exists only within its block, and once the block ends, this inner num is no longer accessible. The outer num with value 20 remains unchanged.
When the printf("%d", num); statement executes, it refers to the outer variable because the inner one has already gone out of scope. Therefore, the program prints 20.
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.