Sail E0 Webinar

MCQs

Total Questions : 43 | Page 4 of 5 pages
Question 31.


What will be the output of the program?


#include<stdio.h>
#define SWAP(a, b) int t; t=a, a=b, b=t;
int main()
{
int a=10, b=12;
SWAP(a, b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
  1.    a = 10, b = 12
  2.    a = 12, b = 10
  3.    Error: Declaration not allowed in macro
  4.    Error: Undefined symbol 't'
 Discuss Question
Answer: Option B. -> a = 12, b = 10

The macro SWAP(a, b) int t; t=a, a=b, b=t; swaps the value of the given two variable.

Step 1: int a=10, b=12; The variable a and b are declared as an integer type and initialized 

to 10, 12 respectively.

Step 2: SWAP(a, b);. Here the macro is substituted and it swaps the value to variable a and b.

Hence the output of the program is 12, 10.


Question 32.


What will be the output of the program?


#include<stdio.h>
#define PRINT(int) printf("int=%d, ", int);
int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}
  1.    int=2, int=3, int=4
  2.    int=2, int=2, int=2
  3.    int=3, int=3, int=3
  4.    int=4, int=4, int=4
 Discuss Question
Answer: Option A. -> int=2, int=3, int=4

The macro PRINT(int) print("%d,", int); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized

 to 2, 3, 4 respectively.

Step 2: PRINT(x); becomes printf("int=%d,",x). Hence it prints 'int=2'.

Step 3: PRINT(y); becomes printf("int=%d,",y). Hence it prints 'int=3'.

Step 4: PRINT(z); becomes printf("int=%d,",z). Hence it prints 'int=4'.

Hence the output of the program is int=2, int=3, int=4.


Question 33.


What will be the output of the program?


#include<stdio.h>#define CUBE(x) (x*x*x)
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
  1.    9, 4
  2.    27, 4
  3.    27, 6
  4.    Error
 Discuss Question
Answer: Option C. -> 27, 6

The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.)

Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id

 initialized to 3.

Step 2: a = CUBE(b++); becomes

=> a = b++ * b++ * b++;

=> a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in

 this statement.

=> a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented 

by 3. (ie: b=6)

Step 3: printf("%d, %d`setminus`n", a, b); It prints the value of variable a and b.

Hence the output of the program is 27, 6.



Question 34.


What will be the output of the program?


#include<stdio.h>
#define SQR(x)(x*x)
int main()
{
int a, b=3;
a = SQR(b+2);
printf("%d\n", a);
return 0;
}
  1.    25
  2.    11
  3.    Error
  4.    Garbage value
 Discuss Question
Answer: Option B. -> 11

The macro function SQR(x)(x*x) calculate the square of the given number 'x'. (Eg: 102)

Step 1: int a, b=3; Here the variable a, b are declared as an integer type and the variable b

 is initialized to 3.

Step 2: a = SQR(b+2); becomes,

=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .

=> a = 3+2 * 3+2;

=> a = 3 + 6 + 2;

=> a = 11;

Step 3: printf("%d`setminus`n", a); It prints the value of variable 'a'.

Hence the output of the program is 11



Question 35.


What will be the output of the program?


#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
float s=10, u=30, t=2, a;
a = 2*(s-u*t)/SQUARE(t);
printf("Result = %f", a);
return 0;
}
  1.    Result = -100.000000
  2.    Result = -25.000000
  3.    Result = 0.000000
  4.    Result = 100.000000
 Discuss Question
Answer: Option A. -> Result = -100.000000

The macro function SQUARE(x) x*x calculate the square of the given number 'x'. (Eg: 102)

Step 1: float s=10, u=30, t=2, a; Here the variable s, u, t, a are declared as an floating point 

type and the variable s, u, t are initialized to 10, 30, 2.

Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,

=> a = 2 * (10 - 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .

=> a = 2 * (10 - 30 * 2) / 2 * 2;

=> a = 2 * (10 - 60) / 2 * 2;

=> a = 2 * (-50) / 2 * 2 ;

=> a = 2 * (-25) * 2 ;

=> a = (-50) * 2 ;

=> a = -100;

Step 3: printf("Result=%f", a); It prints the value of variable 'a'.

Hence the output of the program is -100


Question 36.


What will be the output of the program?


#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);
int main()
{
int i=10, j=5, k=0;
k = MAN(++i, j++);
printf("%d, %d, %d\n", i, j, k);
return 0;
}
  1.    12, 6, 12
  2.    11, 5, 11
  3.    11, 5, Garbage
  4.    12, 6, Garbage
 Discuss Question
Answer: Option A. -> 12, 6, 12

The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.

Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to

 value 10, 5, 0 respectively.

Step 2: k = MAN(++i, j++); becomes,

=> k = ((++i)>(j++)) ? (++i):(j++);

=> k = ((11)>(5)) ? (12):(6);

=> k = 12

Step 3: printf("%d, %d, %d`setminus`n", i, j, k); It prints the variable i, j, k.

In the above macro step 2 the variable i value is increemented by 2 and variable j value is 

increemented by 1.

Hence the output of the program is 12, 6, 12



Question 37.

In which stage the following code 
#include<stdio.h> 
gets replaced by the contents of the file stdio.h


  1.    During editing
  2.    During linking
  3.    During execution
  4.    During preprocessing
 Discuss Question
Answer: Option D. -> During preprocessing

The preprocessor replaces the line #include <stdio.h> with the system header file of 

that name. More precisely, the entire text of the file 'stdio.h' replaces the #include directive.


Question 38.


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;
}
  1.    It compiles
  2.    Compiles with an warning
  3.    Not compile
  4.    Compiles and print nothing
 Discuss Question
Answer: Option C. -> Not compile

The code won't compile since declaration of t cannot occur within parenthesis.


Question 39.


What will be the output of the program?


##include<stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
  1.    9, 4
  2.    27, 4
  3.    27, 6
  4.    Error
 Discuss Question
Answer: Option C. -> 27, 6


The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.)



Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3.



Step 2: a = CUBE(b++); becomes


=> a = b++ * b++ * b++;


=> a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement.


=> a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6)



Step 3: printf("%d, %dn", a, b); It prints the value of variable a and b.


Hence the output of the program is 27, 6.




Question 40.

What would be output of the program ?

#include<stdio.h>
#define X 5+2
void main()
{
int i;
i = X*X*X;
printf("%d",i);
}


  1.    343
  2.    27
  3.    133
  4.    Compiler Error
  5.    None of the above
 Discuss Question
Answer: Option B. -> 27


As we know #define is a token pasting preprocessor it only paste the value of micro constant in the program, before the actual compilation start.
So pasting 5+2 in place of X, we have
i = 5+2*5+2*5+2
 = 5+10+10+2
= 27


Latest Videos

Latest Test Papers