Welcome to C Programming Quiz, Advanced Level !!

Question 1. 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 2. 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 3. 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 4. what is the output of the following sample code?
void myFunc (int x) 
{ 
   if (x > 0)
   myFunc(--x); 
   printf("%d, ", x); 
} 
int main() 
{ 
   myFunc(5); 
   return 0; 
}

1, 2, 3, 4, 5, 5,

4, 3, 2, 1, 0, 0,

0, 0, 1, 2, 3, 4,

0, 1, 2, 3, 4, 5,

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 [] = { 
  "Sleepy", 
  "Dopey" "Doc", 
  "Happy", 
  "Grumpy" "Sneezy", 
  "Bashful", 
};

Compilation Error

5

6

7

Question 6. 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 7. 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 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 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 10. 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