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)
 { 
  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 2. 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 3. What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?
#include<stdio.h>
#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. 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;

-3

4

4 + sizeof( int )

0

Question 5. 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 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 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

Question 8. 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)
{    
    free(nPtr);
}

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 9. 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 10. 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