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 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: 3 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: 4 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: 5 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: 6 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: 7 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: 8 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.