Welcome to C Programming Quiz, Advanced Level !!

Question 1. 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)
      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 2. 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



wont print anything

Question 3. What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
    int x=10, y=20;
    SWAP(x, y, int);
    printf("%d %d\n", x, y);
    return 0;

it compiles

compiles with a warning

not compile

compiles and prints nothing

Question 4. The following program releases memory from a link list..which of the following is true?
struct node *nPtr, *sPtr;    /* pointers for a linked list. */ 
for (nPtr=sPtr; nPtr; nPtr=nPtr->next)

It will work correctly since the for loop covers the entire list.

It may fail since each node "nPtr" is freed before its next address can be accessed.

In the for loop, the assignment "nPtr=nPtr->next" should be changed to "nPtr=nPtr.next".

The loop will never end.

Question 5. 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 [] = { 
  "Dopey" "Doc", 
  "Grumpy" "Sneezy", 

Compilation Error




Question 6. Consider the function increment_ints(), defined below. Despite its significant inline commentary, it contains an error. Which one of the following correctly assesses it?
/* Increment each integer in the array 'p' of * size 'n'. */ 
void increment_ints (int p [/*n*/], int n) 
  assert(p != NULL);  /* Ensure that 'p' isn't a null pointer. */ 
  assert(n >= 0);  /* Ensure that 'n' is nonnegative. */ 
  while (n)  /* Loop over 'n' elements of 'p'. */ 
    *p++  ;          /* Increment *p. */ 
     p++  , n--;      /* Increment p, decrement n. */ 

*p++ causes p to be incremented before the dereference is performed, because both operators have equal precedence and are right associative.

An array is a nonmodifiable lvalue, so p cannot be incremented directly. A navigation pointer should be used in conjunction with p.

*p++ causes p to be incremented before the dereference is performed, because the autoincrement operator has higher precedence than the indirection operator.

The condition of a while loop must be a Boolean expression. The condition should be n != 0.

Question 7. what will be the output of the following program
int fun(int **ptr);

int main()
    int i=10;
    const int *ptr = &i;
    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


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

garbage value

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





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

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



compilation error

undefined behavior

Question 10. What does y in the sample code evaluated to?
int x[] = { 1, 4, 8, 5, 1, 4 }; 
int *ptr, y; 
ptr  = x +  4; 
y = ptr - x;



4 + sizeof( int )