Q: 1 fact (N)
if (N equal 0)
return 1
else
return N * fact (N-1)
The above algorithm is a specific example of ______________.
Selection
Sorting
Recursion
Displacement
[ 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: 2 If 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);
}
1 Time
5 Time
4 Time
None of the above
[ 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: 3 Choose the correct statement respect to function.
Function is block of one or more statement that used to perform some specific tasks.
Functions are executed when they were called.
Function provides better memory utilization.
All of the above.
[ 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: 4 How are arrays passed to a function in C?
Call by Value
Call by Address
Either (A) or (B)
None of these
[ 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: 5 Which of the following is false
Recursive function stores its variable in heap.
Recursive function can cause stack overflow.
Iteration uses less memory than recursion.
None of the above
[ 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: 6 What 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);
}
5
18
7
9
[ 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: 7 Which of the following is/are mandatory in function declaration?
return type, function name only
return type, function name, parameters only
parameters, function name only
function name only
[ 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: 8 Study 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”);
}
2
Blank
Compile Time Error
Run Time Error
[ 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: 9 Use of function
(1) Achieve modularization.
(2) Better memory utilization.
(3) Avoiding rewriting of code.
(4) Makes debugging easier.
Only (1) (2) (3) (4) are correct
Only (2) (3) (4) are correct
Only (1) (2) (4) are correct
Only (3) (4) are correct
[ 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: 10 What 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;
}
Inside modify : 15 27
Inside main : 5 7
Inside modify : 15 27
Inside main : 15 27
Inside modify : 5 7
Inside main : 5 7
Inside modify : 5 7
Inside main : 15 27
[ 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: 11 Consider 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
5
3
4
2
[ 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: 12 Consider 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);
}
14
16
15
21
[ 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: 13 What would be output of the following program?
int num=35;
void main()
{
int num=45;
printf("%d %d",num,num);
}
35 35
45 45
35 45
Error occurred
[ 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: 14 Find 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;
}
0, 1
0, 2
2, 0
2, 1
[ 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: 15 What would be output of the following program
void main()
{
int num=20;
{
int num=10;
}
printf("%d ",num);
}
10
20
Compile time error
Run time error
[ 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.
Q: 16 What is the output of the following C program?
#include<stdio.h>
int f(int n)
{
static int i=1;
if(n>=5)
return n;
n=n+i;
i++;
return(f(n));
}
void main()
{
int y;
y=f(1);
printf("%d",y);
}
5
6
7
8
[ Option C ]
The function f() uses a static variable i, which means its value is preserved across recursive calls. Initially, i = 1.
The function is first called as f(1). Since condition n>=5 is false, the value of n is updated to n+i, so n becomes 2, and then i is incremented to 2. The function calls itself again with f(2).
In the next call, n becomes 2+2 = 4, and i is incremented to 3, followed by a recursive call f(4).
Again, since condition n>=5 is false, n becomes 4+3 = 7, and i becomes 4, and the function calls f(7).
Now the condition n>=5 is true, so the function returns 7. This value is returned through all previous recursive calls without any further modification. Therefore, the final value printed is 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.