Tuesday, February 11, 2020

1.8 Union and Preprocessor directives in C


Session 8
o   Union
o   Preprocessor directives
1. #include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u;
    u.ch[0]=3;
    u.ch[1]=2;
    printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
    return 0;
}
  • A.   3, 2, 515
  • B.     515, 2, 3
  • C.     3, 2, 5
  • D.    515, 515, 4
Answer: a
Explanation:
The system will allocate 2 bytes for the union.
The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.

2. . Which of the following comment about Union is false?
[A] Union is a structure whose members share same memory area
[B] The compiler will keep track of what type of information is currently stored
[C] Only one of the members of union can be assigned a value at particular time
[D] Size allocated for Union is the size of its member needing the maximum storage
Answer: B. The compiler will keep track of what type of information is currently stored
Explanation:
Union is similar to structure the only difference is the way the memory allocated to structure and union
3. Size of a union is determined by size of the.
A. First member in the union
B. Last member in the union
C. Biggest member in the union
D. Sum of the sizes of all members
Answer: c
4. Comment on the following union declaration?

union temp
{
int a;
float b;
char c;
};
 union temp s = {1,2.5,’A'}; //REF LINE
     Which member of the union will be active after REF LINE?

A. a
B. b
C. c
D. Such declaration are illegal
Answer: a
5. What would be the size of the following union declaration?

union uTemp
{
double a;
int b[10];
char c;
}u;
(Assuming size of double = 8, size of int = 4, size of char = 1)
A. 4
B. 8
C. 40
D. 80
Answer: c
Explanation: int is declared as an array of size 10. So total size will be 10 x 4=40 which is the highest of all.
6. What type of data is holded by variable u int this C code?
1.       #include <stdio.h>
2.       union u_tag
3.       {
4.           int ival;
5.           float fval;
6.           char *sval;
7.       } u;
    The variable u here
a) Will be large enough to hold the largest of the three types;
b) Will be large enough to hold the smallest of the three types;
c) Will be large enough to hold the all of the three types;
d) None of the mentioned
Answer: a
Explanation: Size of the union is determined by the size of the largest member.
7.Members of a union are accessed as________________.
a) union-name.member
b) union-pointer->member
c) Both a & b
d) None of the mentioned
Answer:c
8.      What is the output of this C code?
1.       #include <stdio.h>
2.       struct
3.       {
4.           char *name;
5.           union
6.           {
7.               char *sval;
8.           } u;
9.       } symtab[10];
    the first character of the string sval by either of
a) *symtab[i].u.sval
b) symtab[i].u.sval[0]
c) You cannot have union inside structure
d) Both a & b
Answer:d


 9.What is the output of this C code(size of int and float is 4)?
1.       #include <stdio.h>
2.       union
3.       {
4.           int ival;
5.           float fval;
6.       } u;
7.       void main()
8.       {
9.           printf("%d", sizeof(u));
10.      }
a) 16
b) 8
c) 4
d) 32
Answer:c

10.   What is the output of this C code?
1.       #include <stdio.h>
2.       union stu
3.       {
4.           int ival;
5.           float fval;
6.       };
7.       void main()
8.       {
9.           union stu r;
10.          r.ival = 5;
11.          printf("%d", r.ival);
12.      }
a) 9
b) Compile time error
c) 16
d) 5
Answer: d
11.  What is the output of this C code?
1.       #include <stdio.h>
2.       union
3.       {
4.           int x;
5.           char y;
6.       }p;
7.       int main()
8.       {
9.           p.x = 10;
10.          printf("%d\n", sizeof(p));
11.      }
a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(int)
Answer: d

12.  What is the output of this C code?
1.       #include <stdio.h>
2.       union
3.       {
4.           int x;
5.           char y;
6.       }p;
7.       int main()
8.       {
9.           p.y = 60;
10.          printf("%d\n", sizeof(p));
11.      }
a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(char)
View Answer
Answer: c

13.  Bit feilds can be used in a union
A. true                         B. False
Answer: B

14. What is the output of this C code?
1.       #include <stdio.h>
2.       union p
3.       {
4.           int x;
5.           char y;
6.       }k = {.y = 97};
7.       int main()
8.       {
9.           printf("%d\n", k.y);
10.      }
a) Compile time error
b) 97
c) a
d) Depends on the standard
Answer: b

15. What is the output of this C code?
1.       #include <stdio.h>
2.       union p
3.       {
4.           int x;
5.           float y;
6.       }p;
7.       int main()
8.       {
9.           p.x = 10;
10.          printf("%f\n", p.y);
11.      }
a) Compile time error
b) Implementation dependent
C) 10.000000
d) 0.000000
Answer: b
Explanation: Depending on the compiler the value to be printed varies.

16. Which of the following share a similarity in syntax?
     1. Union, 2. Structure, 3. Arrays and 4. Pointers
a) 3 and 4
b) 1 and 2
c) 1 and 3
d) 1, 3 and 4
Answer: b

17. If we initialize one element of a union it also initializes other elements of a union.
A. true                         B. False
Answer: A

18.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d\n", v.a);
    return 0;
}
A. 10
B. 20
C. 30
D. 0
Answer: Option B
Explanation:
Because union can initialize only one variable at a time. It overwrites the memory with binary value of 20 where it was initialized with binary value of 10 before.
It takes the last initialized value of its member variables.
18.
What will be the output of the program ?
#include<stdio.h>
 
int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d\n", ++MON, TUE, WED, THU, FRI, SAT);
    return 0;
}
A.
-1, 0, 1, 2, 3, 4
B.
Error
C.
-1, 0, 6, 2, 3, 4
D.
-1, 0, 6, 7, 8, 9
Answer: B
Explanation: Because ++ or -- cannot be done on enum value.
19.
Point out the error in the program?
#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a z1 = {512};
    union a z2 = {0, 2};
    return 0;
}
[A].
Error: invalid union declaration
[B].
Error: in Initializing z2
[C].
No error
[D].
None of above
Answer: Option B

Explanation:
in this union has two members one is 'int' and another is 'char' .character must be placed in between double quotes hear z2 character initialization does not follow that rule .

20.
Which of the following statements correct about the below program?
#include<stdio.h>
 
int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u1 = {512};
    union a u2 = {0, 2};
    return 0;
}
1:
u2 CANNOT be initialized as shown.
2:
u1 can be initialized as shown.
3:
To initialize char ch[] of u2 '.' operator should be used.
4:
The code causes an error 'Declaration syntax error'
A.
1, 2
B.
2, 3
C.
1, 2, 3
D.
1, 3, 4
Answer: Option C

21. A union cannot be nested inside a structure
A. True          B. False
Answer: B
22. Nested unions are allowed
A. True         B. False
Answer: A
23. Union elements can be of different sizes
A. True           B. False
Answer: A
24.
Which of the following statement is True?
A.
User has to explicitly define the numeric value of enumerations
B.
User has a control over the size of enumeration variables.
C.
Enumeration can have an effect local to the block, if desired
D.
Enumerations have a global effect throughout the file.
Answer: Option C

25. Union variables can have local or global scope
A. True        B. False
Answer: A

26. A pointer to a union cannot be created.
A. True             B. False
Answer: B

27.  A elements of a union are always accessed using a & operator.
A. True         B. False
Answer: B

28.
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;
}
A.
It compiles
B.
Compiles with an warning
C.
Not compile
D.
Compiles and print nothing
Answer: Option C
Explanation:
The code won't compile since declaration of t cannot occur within parenthesis.




29.
In which stage the following code 
#include<stdio.h> 
gets replaced by the contents of the file stdio.h
A.
During editing
B.
During linking
C.
During execution
D.
During preprocessing
Answer: Option D
Explanation:
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.

30.
Will the program compile successfully?
#include<stdio.h>
#define X (4+Y)
#define Y (X+3)
 
int main()
{
    printf("%d\n", 4*X+2);
    return 0;
}
A.
Yes
B.
No
Answer: Option B
Explanation:
Reports an error: Undefined symbol 'X'
31.
Would the following typedef work?
typedef #include l;
A.
Yes
B.
No
Answer: Option B
Explanation:
Because typedef starts to work after preprocessing.



32.
Will the program compile successfully?
#include<stdio.h>
 
int main()
{
    printf("Hello" "World\n");
    return 0;
}
A.
Yes
B.
No
Answer: Option A
Explanation:
Yes, It prints "HelloWorld"
33.
It is necessary that a header files should have a .h extension?
A.
Yes
B.
No
Answer: Option B
Explanation:
No, the header files have any kind of extension. We can define fact.c, filename.c, #include<stdio.hdr>
#include<conio.exe>
in header file. 
34.
Will the program compile successfully?
#include<stdio.h>
 
int main()
{
    #ifdef NOTE
        int a;
        a=10;
    #else
        int a;
        a=20;
    #endif
    printf("%d\n", a);
    return 0;
}
A.
Yes
B.
No
Answer: Option A
Explanation:
Yes, this program will compile and run successfully and prints 20.
The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the #ifdef block statements. Here #ifdef condition fails because the Macro NOTE is nowhere declared.
Hence the #else block gets executed, the variable a is declared and assigned a value of 20.
printf("%d\n", a); It prints the value of variable a 20.

35.
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;
}
A.
12, 6, 12
B.
11, 5, 11
C.
11, 5, Garbage
D.
12, 6, Garbage
Answer: Option A
Explanation: k = MAN(++i, j++); becomes,
=> k = ((++i)>(j++)) ? (++i):(j++);
=> k = ((11)>(5)) ? (12):(6);
=> k = 12

36.
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;
}
A.
Result = -100.000000
B.
Result = -25.000000
C.
Result = 0.000000
D.
Result = 100.000000
Answer: Option A
Explanation: 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;

37.
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;
}
A.
9, 4
B.
27, 4
C.
27, 6
D.
Error
Answer: Option C
Explanation: Here we are using post increment so b++ is not incremented in the statement
a = b++ * b++ * b++

38.
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;
}
A.
26250.00 7500.00
B.
Nothing will print
C.
Error: Multiple declaration of si
D.
Garbage values
Answer: Option C
Explanation:
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



39.
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;
}
A.
Error: unexpected end of file because there is no matching #endif
B.
The number is odd
C.
Garbage values
D.
None of above
Answer: Option A
Explanation:
The conditional macro #if must have an #endif. In this program there is no #endif statement written.

40
Which of the following are correct preprocessor directives in C?
1:
#ifdef
2:
#if
3:
#elif
4:
#undef
A.
1, 2
B.
4
C.
1, 2, 4
D.
1, 2, 3, 4
Answer: Option D
Explanation:
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.


41.
Which of the following are correctly formed #define statements in C?
A.
#define CUBE (X) (X*X*X);
B.
#define CUBE(x) (X*X*X)
C.
#define CUBE(X)(X*X*X)
D.
#define CUBE(X) {X*X*X}
Answer: Option C

42.

If the file to be included doesn't exist, the preprocessor flashes an error message.
A.
True
B.
False
Answer: Option A
Explanation:
True, the included file does not exist it will generate the error.
43.
Preprocessor directive #undef can be used only on a macro that has been #define earlier
A.
True
B.
False
Answer: Option A
Explanation:
True, #undef can be used only on a macro that has been #define earlier
Example: #define PI 3.14
We can undefine PI macro by #undef PI

44.
There exists a way to prevent the same file from getting #included twice in the same program.
A.
True
B.
False
Answer: Option A
Explanation:
True, We can prevent the same file from getting included again by using a preprocessor directive called #ifndef (short for "if not defined") #ifndef XSTRING_H









45.
A preprocessor directive is a message from programmer to the preprocessor.
A.
True
B.
False
Answer: Option A
Explanation:
True, the programmer tells the compiler to include the preprocessor when compiling.

46.
Macro calls and function calls work exactly similarly.
A.
True
B.
False
Answer: Option B
Explanation:
False, A macro just replaces each occurrence with the code assigned to it. e.g. SQUARE(3) with ((3)*(3)) in the program.
A function is compiled once and can be called from anywhere that has visibility to the funciton.
47.
A macro must always be defined in capital letters.
A.
True
B.
False
Answer: Option B
Explanation:
FALSE, The macro is case insensitive.
48.
Every C program will contain at least one preprocessor directive.
A.
True
B.
False
Answer: Option B
Explanation:
False, the preprocessor directive is not mandatory in any c program.
49.

Macros with arguments are allowed
A.
True
B.
False
Answer: Option A
Explanation:
True, A macro may have arguments.
Example: #define CUBE(X)(X*X*X)



50.

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;
}
A.
2, 3, 4,
B.
2, 2, 2,
C.
3, 3, 3,
D.
4, 4, 4,
Answer: Option A
Explanation:
 PRINT(x); becomes printf("%d,",x). Hence it prints '2'.
 PRINT(y); becomes printf("%d,",y). Hence it prints '3'.
 PRINT(z); becomes printf("%d,",z). Hence it prints '4'.
Hence the output of the program is 2, 3, 4.






Previous            Next







No comments:

Post a Comment