Welcome to C Programming Quiz, Advanced Level !!

Question 1. what is the output of the following 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};
    printf("%d", f(a, 6));
    return 0;
}

-9

5

15

19

Question 2. How many elements does the array dwarves (declared below) contain? Assume the C compiler employed strictly complies with the requirements of Standard C.
char * dwarves [] = { 
  "Sleepy", 
  "Dopey" "Doc", 
  "Happy", 
  "Grumpy" "Sneezy", 
  "Bashful", 
};

Compilation Error

5

6

7

Question 3. The function below has a flaw that may result in a serious error during some invocations. Which one of the following describes the deficiency illustrated above?
int fibonacci (int n)
{ 
 switch (n)
 { 
  default: 
      return (fibonacci(n - 1) +  fibonacci(n - 2)); 
  case 1: 
  case 2: 
 } 
  return 1; 
}

For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.

An error in the algorithm causes unbounded recursion for all values of n.

A break statement should be inserted after each case. Fall-through is not desirable here.

Since the default case is given first, it will be executed before any case matching n.

Question 4. The function crash(), defined below, triggers a fault in the memory management hardware for many architectures. Which one of the following explains why "got here" may NOT be printed before the crash?
void crash (void) 
{ 
 printf("got here"); 
 *((char *) 0) = 0; 
}

The C standard says that dereferencing a null pointer causes undefined behavior. This may explain why printf() apparently fails.

printf() always buffers output until a newline character appears in the buffer. Since no newline was present in the format string, nothing is printed.

If the standard output stream is buffered, the library buffers may not be flushed before the crash occurs.

printf() expects more than a single argument. Since only one argument is given, the crash may actually occur inside printf(), which explains why the string is not printed. puts() should be used instead.

Question 5. What will be the output of the program?
int main(void) {

                int i=3, j;
                i++ + i++ + i++;
                printf("%d", j);
                return 0;
}

9

15

compilation error

undefined behavior

Question 6. Explain the following C Declaration:
typedef int *(*(*fp[])())()

fp is pointer to function returning array of pointer to int

fp is function returning pointer to array of pointer to function returning pointer to int

fp is array of pointer to function returning pointer to function returning pointer to int

fp is array of pointer to function returning pointer to pointer to int

Question 7. what is the output of the following code sample?
#include <stdio.h>
int i; 
void increment( int i ) 
{ 
   i++  ; 
} 

int main() 
{ 
   for( i = 0; i < 10; increment( i ) ) 
   { 
   } 
   printf("i=%d\n", i); 
   return 0; 
}

it wont compile at all

i=9

i=10

wont print anything

Question 8. what will be the output of the following program
#include<stdio.h>
int fun(int **ptr);

int main()
{
    int i=10;
    const int *ptr = &i;
    fun(&ptr);
    return 0;
}
int fun(int **ptr)
{
    int j = 223;
    int *temp = &j;
    printf("Before changing ptr = %5x\n", *ptr);
    const *ptr = temp;
    printf("After changing ptr = %5x\n", *ptr);
    return 0;
}

address of i
     address of j

10
     223

Error: cannot convert parameter 1 from 'const int **' to 'int **'

garbage value

Question 9. What number will z in the following code sample
int z,x=5,y=-10,a=4,b=2;
z = x++ - --y * b / a;

5

6

10

11

Question 10. what is the value of x when the following sample code is executed
int x = 3; 
if( x == 2 );
  x = 0; 
if( x == 3 )
 x++; 
else x += 2;

4

2

4

0