Sail E0 Webinar

MCQs

Total Questions : 43 | Page 3 of 5 pages
Question 21.

Which of the following are correctly formed #define statements in C?


  1.    #define CUBE (X) (X*X*X);
  2.    #define CUBE(x) (X*X*X)
  3.    #define CUBE(X)(X*X*X)
  4.    #define CUBE(X) {X*X*X}
 Discuss Question
Answer: Option C. -> #define CUBE(X)(X*X*X)


Question 22.

Which of the following are correct preprocessor directives in C?
1:  #ifdef
2:  #if
3:#elif
4:#undef


  1.    1, 2
  2.    4
  3.    1, 2, 4
  4.    1, 2, 3, 4
 Discuss Question
Answer: Option D. -> 1, 2, 3, 4

The macros #ifdef #if #elif are called conditional macros.

The macro #undef undefine the previosly declared macro symbol.

Hence all the given statements are macro preprocessor directives.


Question 23.


Point out the error in the program


#include<stdio.h>
int main()
{
int i;
#if A
printf("Enter any number:");
scanf("%d", &i);
#elif B
printf("The number is odd");
return 0;
}
  1.    Error: unexpected end of file because there is no matching #endif
  2.    The number is odd
  3.    Garbage values
  4.    None of above
 Discuss Question
Answer: Option A. -> Error: unexpected end of file because there is no matching #endif

The conditional macro #if must have an #endif. In this program there is no #endif

statement written.


Question 24.


Point out the error in the program


#include<stdio.h>
#define SI(p, n, r) float si; si=p*n*r/100;
int main()
{
float p=2500, r=3.5;
int n=3;
SI(p, n, r);
SI(1500, 2, 2.5);
return 0;
}
  1.    26250.00 7500.00
  2.    Nothing will print
  3.    Error: Multiple declaration of si
  4.    Garbage values
 Discuss Question
Answer: Option C. -> Error: Multiple declaration of si

The macro #define SI(p, n, r) float si; si=p*n*r/100; contains the error. To remove this error, 

we have to modify this macro to

#define SI(p,n,r) p*n*r/100


Question 25.


What will be the output of the program?


#include<stdio.h>
#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)
int main()
{
int x;
x = MAX(3+2, 2+7, 3+7);
printf("%d\n", x);
return 0;
}
  1.    5
  2.    9
  3.    10
  4.    3+7
 Discuss Question
Answer: Option C. -> 10

The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.

Step 1: int x; The variable x is declared as an integer type.

Step 2: x = MAX(3+2, 2+7, 3+7); becomes,

=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)

=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )

=> x = (5 >9 ? (10): (10) )

=> x = 10

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

Hence the output of the program is "10".



Question 26.


What will be the output of the program?


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

The macro PRINT(i) print("%d,", i); 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("%d,",x). Hence it prints '2'.

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

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

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


Question 27.


What will be the output of the program?


#include<stdio.h>
#define MESS junk
int main()
{
printf("MESS\n");
return 0;
}
  1.    junk
  2.    MESS
  3.    Error
  4.    Nothing will print
 Discuss Question
Answer: Option B. -> MESS

printf("MESS`setminus`n"); It prints the text "MESS". There is no macro calling inside the printf 

statement occured.



Question 28.


What will be the output of the program?


#include<stdio.h>
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply
int main()
{
char *opername = Xstr(oper);
printf("%s\n", opername);
return 0;
}
  1.    Error: in macro substitution
  2.    Error: invalid reference 'x' in macro
  3.    print 'multiply'
  4.    No output
 Discuss Question
Answer: Option C. -> print 'multiply'

The macro #define str(x) #x replaces the symbol 'str(x)' with 'x'.

The macro #define Xstr(x) str(x) replaces the symbol 'Xstr(x)' with 'str(x)'.

The macro #define oper multiply replaces the symbol 'oper' with 'multiply'.

Step 1: char *opername = Xstr(oper); The varible *opername is declared as an pointer

 to a character type.

=> Xstr(oper); becomes,

=> Xstr(multiply);

=> str(multiply)

=> char *opername = multiply

Step 2: printf("%s`setminus`n", opername); It prints the value of variable opername.

Hence the output of the program is "multiply"



Question 29.


What will be the output of the program?


#include<stdio.h>
#define MIN(x, y) (x 0)
printf("%d\n", z);
return 0;
}
  1.    3
  2.    4
  3.    0
  4.    No output
 Discuss Question
Answer: Option A. -> 3

The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.

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

 x, y are initialized to value 3, 4 respectively.

Step 2: z = MIN(x+y/2, y-1); becomes,

=> z = (x+y/2 < y-1)? x+y/2 : y - 1;

=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;

=> z = (3+2 < 4-1)? 3+2 : 4 - 1;

=> z = (5 < 3)? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the ifblock statements.

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

Hence the output of the program is 3



Question 30.


What will be the output of the program?


#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)
int main()
{
int x;
x = MAX(3+2, 2+7);
printf("%d\n", x);
return 0;
}
  1.    8
  2.    9
  3.    6
  4.    5
 Discuss Question
Answer: Option B. -> 9

The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.

Step 1 : int x; The variable x is declared as an integer type.

Step 2 : x = MAX(3+2, 2+7); becomes,

=> x = (3+2 > 2+7 ? 3+2 : 2+7)

=> x = (5 > 9 ? 5 : 9)

=> x = 9

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

Hence the output of the program is 9.



Latest Videos

Latest Test Papers