1

C Quiz - 110

Question 1
Suppose someone writes increment macro (i.e. which increments the value by one) in following ways:
#define INC1(a) ((a)+1)

#define INC2 (a) ((a)+1)

#define INC3( a ) (( a ) + 1)

#define INC4 ( a ) (( a ) + 1)
Pick the correct statement for the above macros.
A
Only INC1 is correct.
B
All (i.e. INC1, INC2, INC3 and INC4) are correct.
C
Only INC1 and INC3 are correct.
D
Only INC1 and INC2 are correct.
Macro & Preprocessor    C Quiz - 110    
Discuss it


Question 1 Explanation: 
In C, for macros with arguments, there can’t be any space between macro name and open parenthesis. That’s why only INC1 and INC3 are correct. Basically, “#define INC2 (a) ((a)+1)” results in “INC2” expansion to “(a) ((a)+1)” which is not the desired expansion.
Question 2
The following program won’t compile because there’re space between macro name and open parenthesis.
#include "stdio.h"

#define MYINC   (  a  )  (  ( a )  +  1 )

int main()
{

 printf("GeeksQuiz!");

 return 0;
}
A
TRUE
B
FALSE
Macro & Preprocessor    C Quiz - 110    
Discuss it


Question 2 Explanation: 
Please note that #define is a preprocessor directive i.e. it’s processed before actual compilation takes place. In the above program snippet MYINC isn’t used in the program anywhere. So even though MYINC doesn’t perform the intended behaviour i.e. it won’t increment a, but MYINC is a valid macro. If we had used MYINC anywhere in the program, it would have been replaced with “( a ) ( ( a ) + 1 )”. So above program will compile and run without any issue.
Question 3
Typically, library header files in C (e.g. stdio.h) contain not only declaration of functions and macro definitions but they contain definition of user defined data types (e.g. struct, union etc), typedefs and definition of global variables as well. So if we include the same header file more than once in a C program, it would result in compile issue because re-definition of many of the constructs of the header file would happen. So it means the following program will give compile error.
#include “stdio.h”
#include “stdio.h”
#include “stdio.h”

int main()
{
 printf(“Whether this statement would be printed?”)
 return 0;
}

A
TRUE
B
FALSE
Macro & Preprocessor    C Quiz - 110    
Discuss it


Question 3 Explanation: 
It’s okay to include library header files multiple times in a program. But actually the content of the header file is included only once. The way it’s achieved is due to usage of “#ifndef“, “#define” and “#endif”. That’s why it’s recommended to use these preprocesor macros even in user defined header files. For an example and usage of this, please check out the “Discuss it” of this question.
Question 4
The below program would give compile error because comma has been used after foo(). Instead, semi-colon should be used i.e. the way it has been used after bar(). That's why if we use semi-colon after foo(), the program would compile and run successfully while printing "GeeksQuiz"
#include "stdio.h"

void foo(void)
{
 printf("Geeks");
}
void bar(void)
{
 printf("Quiz");
}

int main()
{
 foo(), bar();
 return 0;
}
A
TRUE
B
FALSE
Operators    C Quiz - 110    
Discuss it


Question 4 Explanation: 
Here, comma is acting as an operator instead of separator. For a comma operator in C, first left operand is evaluated and then right operand is evaluated. That's why foo() would be called followed by bar(). There's no issue with the given program. It'll compile and print "GeeksQuiz" without any modification itself.
Question 5
In C, 1D array of int can be defined as follows and both are correct.
int array1D[4] = {1,2,3,4};
int array1D[] = {1,2,3,4};
But given the following definitions (along-with initialization) of 2D arrays
int array2D[2][4] = {1,2,3,4,5,6,7,8}; /* (i) */
int array2D[][4] = {1,2,3,4,5,6,7,8}; /* (ii) */
int array2D[2][] = {1,2,3,4,5,6,7,8}; /* (iii) */
int array2D[][] = {1,2,3,4,5,6,7,8}; /* (iv) */
Pick the correct statements.
A
Only (i) is correct.
B
Only (i) and (ii) are correct.
C
Only (i), (ii) and (iii) are correct.
D
All (i), (ii), (iii) and (iv) are correct.
Arrays    C Quiz - 110    
Discuss it


Question 5 Explanation: 
First of all, C language doesn’t provide any true support for 2D array or multidimensional arrays. A 2D array is simulated via 1D array of arrays. So a 2D array of int is actually a 1D array of array of int. Another important point is that array size can be derived from its initialization but that’s applicable for first dimension only. It means that 2D array need to have an explicit size of 2nd dimension. Similarly, for a 3D array, 2nd and 3rd dimensions need to have explicit size. That’s why only (i) and (ii) are correct. But array2D[2][] and array2D[][] are of incomplete type because their complete size can’t derived even from the initialization.
Question 6
Which one of the choices given below would be printed when the following program is executed?
#include
void swap (int *x, int *y)
{
    static int *temp;
    temp = x;
    x = y;
    y = temp;
}
void printab ()
{
    static int i, a = -3, b = -6;
    i = 0;
    while (i <= 4)
    {
        if ((i++)%2 == 1) continue;
        a = a + i;
        b = b + i;
    }
    swap (&a, &b);
    printf("a =  %d, b = %d\n", a, b);
}
main()
{
    printab();
    printab();
} 
A
a = 0, b = 3
a = 0, b = 3
B
a = 3, b = 0
a = 12, b = 9
C
a = 3, b = 6
a = 3, b = 6
D
a = 6, b = 3
a = 15, b = 12
C Quiz - 110    GATE IT 2006    
Discuss it


Question 6 Explanation: 
  Things to ponder:swap function doesn’t actually swaps two variables, rather just swaps their addresses in local variables x and y - which is effectively nothing once swap function returns.printab function adds 9 to static variables a and b. The number 9 comes from the fact that the while loop executes those arithmetic statements only when i = 1, 3, 5. Hence, when printab is called for the first time, these are the local variable values: a = −3 + 9 and b = −6 + 9. On the second time, a = −3 + 9 + 9 and b = −6 + 9 + 9 are the desired values. Therefore, correct answer would be (D) a = 6, b = 3; a = 15, b = 12.   This solution is contributed by vineet purswani .
Question 7
The following function computes the value of mCn correctly for all legal values m and n (m≥1,n≥0 and m>n)
 int func(int m, int n)
{
    if (E) return 1;
    else return(func(m -1, n) + func(m - 1, n - 1));
}
In the above function, which of the following is the correct expression for E?
A
(n = = 0) || (m = = 1)
B
(n = = 0) && (m = = 1)
C
(n = = 0) || (m = = n)
D
(n = = 0) && (m = = n)
C Quiz - 110    GATE IT 2006    
Discuss it


Question 7 Explanation: 
Make a recursion tree of the given function recursion.
There are 7 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice