Algorithms - Online Test

Q1. Consider the following C program.
void f(int, short);
void main()
{
int i = 100; 
short s = 12; 
short *p = &s; 
____________;        // call to f()  
}
Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error? 
Answer : Option D
Explaination / Solution:

Here function f takes two arguments one is int and the other is short and its return type is void. So, in main function ‘P’ is a pointer to short and when we call f (i,*p) there won’t be any type checking error.

Q2. What will be the output of the following C program?
void count(int n){
static int d=1;
printf("%d ", n); printf("%d ", d); d++;
if(n>1) count(n-1);
printf("%d ", d);
}
void main(){
count(3);

Answer : Option A
Explaination / Solution:



Q3. Consider the following table:

Match the algorithms to the design paradigms they are based on.
Answer : Option C
Explaination / Solution:

Kruskal’s algorithm follows greedy approach in order to find MST of a connected graph. Quick sort follows divide and conquer strategy. Floyd Warshal algorithm is used to find the shortest path between every pair of vertices and it follows dynamic programming strategy.

Q4. Consider the C functions foo and bar given below:
int foo (int val ) {
int x = 0;
while (val > 0) {
x = x + foo ( val --);
}
return val ;
}
int bar (int val ) {
int x = 0;
while (val > 0) {
x = x + bar (val – 1) ;
}
return val ; 
}
Invocations of foo (3) and bar (3) will result in:  
Answer : Option B
Explaination / Solution:

Foo (3) calls foo (3) which in turn calls foo(3). This goes on infinite number of times which causes memory overflow and causes abnormal termination. Bar(3) → bar (2) →bar (1) →bar (0) (return 0) from here onwards bar (1) will call bar (0) and bar (0) will return 0 to bar (1) & this goes on forever without causing memory overflow.

Q5. Consider the following two functions. 
void funl (int n) {                        void fun2 (int n) {
if (n = =0 ) return;                            if (n = = 0) return ;
printf (“%d” , n);                               printf (“%d” , n);
fun2 (n - 2);                                     fun1(++n) ;
printf (“%d” , n);                               printf (“%d” , n);
}                                                       }
The output printed when fun1 (5) is called is  
Answer : Option A
Explaination / Solution:

In this the fun1() is calling fun2() after printing value and after returning from fun2(),it prints the same value. In the fun2() also the same thing happens So by looking options we can judge the correct sequence of output.

Q6. What is the value printed by the following C program?
#include <stdio.h> 
int f(int * a, int n)
{
if (n <= 0)return 0;
else if(*a % 2 == 0) return * a + f(a + 1,n - 1);
else return * a - f(a + 1, n - 1);
}
int main ( )
{
int a[ ] = {12, 7, 13, 4, 11, 6};
print f ("%d", f(a, 6));
return 0;
}
Answer : Option A
Explaination / Solution:
No Explaination.