Welcome to C Programming Quiz, Advanced Level !!

Question 1. what will be the output of the following program
#include<stdio.h>
#include<stdarg.h>
void display(int num, ....);

int main()
{
    display(4, 'A', 'B', 'C', 'D');
    return 0;
}
void display(int num, ...)
{
    char c, c1; int j;
    va_list ptr, ptr1;
    va_start(ptr, num);
    va_start(ptr1, num);
    for(j=1; j<=num; j++)
    {
        c = va_arg(ptr, int);
        printf("%c", c);
        c1 = va_arg(ptr1, int);
        printf("%d\n", c1);
    }
}

A, A
     B, B
     C, C
     D, D

A, a
     B, b
     C, c
     D, d

A, 65
     B, 66
     C, 67
     D, 68

A, 0
     B, 0
     C, 0
     C, 0

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. 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 4. 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 5. 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 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 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. 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 9. what is the output of the following code sample
int main(void) {
                char *p="abc";
                char *q="abc123";
                while(*p=*q)
                {
                                print("%c %c",*p,*q);
                }
}

aabbcc123

abcabc123

infinite loop

segmentation fault

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