MCQ TYPES
1. What would happen if
main() {
main();
}
is executed.
A. Compile time error.
B. The program will run forever and has to be manually terminated.
C. The stack would overflow and eventually the program will be killed by the operating system.
D. main() executes only once.
Answer : C
It executes until there is overflow in the call stack.
2. In a function, what is the maximum number of return statements possible
A. 1
B. 2
C. 4
D. None of the above
Answer : D
There is no restriction to the number of returns statements in a program.
3. What will be the output of the following program?
#include <stdio.h>
void demo();
int main()
{
void (*fun)();
fun = demo;
(*fun)();
fun();
return 0;
}
void demo()
{
printf("India ");
}
A. India
B. India India
C. India India India
D. Compiler Error
Answer : B
This is a simple program with function pointers. fun is assigned to point to demo. So the two
statements “(*fun)();” and “fun();” mean the same thing.
4. Express the return value of fun2( ) in terms of a and b.
int fun1(int x, int y)
{
if (x == 0) return 0;
return (y + fun1(x 1, y));
}
int fun2(int a, int b)
{
if (a == 0) return 1;
return fun1(fun2(a 1, b),b );
}
A. a*b
B. a+a*b
C. a^b
D. b^a
Answer : D
The function multiplies b to itself a times which is ba.
5. What is the running time of the program?
void fun(int n, int arr[])
{
int i = 0, j = 0;
for(; i < n; ++i)
while(j < n && arr[i] < arr[j])
j++;
}
A. O(n)
B. O(n^2)
C. O(nlogn)
D. O(log n)
Answer : A
Since the variable j is not initialized for each value of variable i. So, the inner loop runs at most n
times
6. Can you have a pointer that points to a function?
A. Yes
B. No
Answer : A
Yes,you can have a pointer to function in C language just like pointer to data types.
7. How many times the following loop runs for given value of n?
for(int i=2 ; i<=n ; i=i*4){
printf("%d",i);
}
A.O(n)
B.O(log n)
C.O(n^2)
D.O(sqrt(n))
E.None of above
Answer : B
This loop iterates as follows,
i=2=2*(4^0)
i=2*4=2*(4^1)
i=(2*4)*4=2*(4^2)
.....
......
i=2*(4^k) <= n
......
log2 + k*log 4 <= log n
k=log n
so, complexity=O(log n).
8. How many times the following loop runs for given value of n?
for(int i=2 ; i<=n ; i=pow(i,2)){
printf("%d",i);
}
A. O(n)
B. O(log n)
C. O(n^2)
D. O(sqrt(n))
E. None of above
Answer : E
This loop iterates as follows,
i=2
i=2^2
i=(2^2)^2
......
......
i=2^(2^k)<= n
k=log log n
so, complexity=O(log log n).
9. Which of the following function singatures are correct for passing a two dimensional array A of 10 rows
and 10 columns to a function xyz() in C? There should not be any compiler error or warning when you do
so.
A. xyz(int A[])
B. xyz(int *A[])
C. xyz(int A[][10])
D. xyz(int A[10][])
Answer : B,C
xyz(int *A[]) : Second dimension fixed
xyz(int A[][10]) :Passing by double pointe
10. Point out the line in the following code segment which would result in a compilation error:
#include<stdio.h> //line1
void f() //line2
{ //line3
printf("Hi"); //line4
} //line5
main() //line6
{ //line7
int a=10; //line8
a=f(); //line9
printf("\n%d",a); //line10
} //line11
Answer :Line 9
The function is a void function so it cannot return any value.
11. What does the following function do?
int f(int x,int y){
while(x!=y){
if(x>y)
return f(x y,y);
else
return f(x,y x);
}
return x;
}
A. Calculates Least Common Multiple (LCM)
B. Calculates Greatest Common Divisor (GCD)
C. Divides two numbers
D. Subtracts two numbers
Answer : B
This is a recursive way to calculate GCD.Euclidean algorithm
12. What is the output of the following program?
int i;
int fun();
int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}
int fun()
{
printf("Hi");
}
A. Hello
B. Hi Hello
C. No output
D. Infinite loop
Answer : A
As in int i; the variable i is declared as global,so by default it is initialized with value 0. So,while
condition will fail. Hence the output of the program is "Hello".
13. What will be the output of A(2,4) for the following recursive definition?
A(m,n)= (n+1) if(m=0)
A(m,n)= A(m 1,1) if(m>0 and n=0)
A(m,n)= A(m 1,A(m,n 1)) if(m>0 and n>0)
A. 5
B. 6
C. 9
D. 11
Answer : D
This is Ackermann function and answer of A(2,4) is 11.
14. You are given a set of n points on the number line. They are given in arbitrary order. The task is to find the
points that are closest to each other.
To solve the problem you decide to take one point and compute its distance to all other points and repeat
this process for all points. During the process you track the closest pair.
What is the running time of this algorithm ?
A. O(n)
B. O(n^2)
C. Runtime is independent of n.
D. O(log n)
Answer : B
Note that there are nC2 many pairs of points that the algorithm takes. This is equal to O(n^2)
running time.
15. int fact_comp(int n)
{
if (n == 1 || n == 0)
return 1;
else
return n*fact(n 1);
}
How many times is the function fact_comp() called when we want to compute fact_comp(n) ? (Assume: n is
a positive integer input)
A.. 1
B. n
C. 2n
Answer : B
The fact_comp function goes all the way from n to 1. Exactly n steps.
16. #include <stdio.h>
void strEdit(char *str)
{
str[0] = ‘A’;
}
int main()
{
char str[5] = “PDSA”;
strEdit(str);
printf(“%s\n”,str);
return 0;
}
What is the output of the program ?
A. PDSA
B. ADSA
C. Program doesn’t compile.
D. AAAA
Answer : B
The character array is passed by reference. The change in the function is reflected in the program
output.
17. int fun(int n)
{
static int i = 0;
if (n > 1){
n = n/2;
i = i+1;
fun(n);
}
else
return i;
}
What does fun(n) compute ?
A. Number of digits in n
B. Integer part of logarithm of n base 2
C. Function always returns 0
D. n/2in
Answer: B
The function computes the integer part of logarithm of n to the base 2.
Comments
Post a Comment