C Programming Multiple Choice Questions and Answers on Bit-fields for Freshers

1. What is the correct syntax to initialize bit-fields in an structure?
a) struct temp
    {
        unsigned int a : 1;
    }s;
b) struct temp
    {
        unsigned int a = 1;
    }s;
c) struct temp
    {
        unsigned float a : 1;
    }s;
d) None of the mentioned

Answer: a
2. Which of the following data types are accepted while declaring bit-fields?

a) char
b) float
c) double
d) none of the mentioned
Answer: a
3. Which of the following reduces the size of a structure?

a) union
b) bit-fields
c) malloc
d) none of the mentioned
Answer: b
4. For what minimum value of x in a 32-bit Linux OS would make the size of s equal to 8 bytes?
  1.     struct temp
  2.     {
  3.         int a : 13;
  4.         int b : 8;
  5.         int c : x;
  6.     }s;
a) 4
b) 8
c) 12
d) 32
Answer: c
5. Calculate the % of memory saved when bit-fields are used for the following structure.?
    (Assuming size of int = 4, calculate the % using the memory that would be occupied without     bit-fields)

  1.     struct temp
  2.     {
  3.         int a : 1;
  4.         int b : 2;
  5.         int c : 4;
  6.         int d : 4;
  7.     }s;
a) 25%
b) 33.3%
c) 50%
d) 75%
Answer: d
6. In the declaration of bit-fields,

    struct-declarator:
    declarator
    type-specifier declarator opt : constant-expression
    The constant-expression specifies
a) The width of the field in bits.
b) Nothing
c) The width of the field in bytes.
d) Error
Answer: a
7. In the declaration of bit-fields,

    struct-declarator:
    declarator
    type-specifier declarator opt : constant-expression
    The constant-expression must be
a) Any type
b) Nothing
c) Integer value
d) Nonnegative integer value
Answer: d
8. Which of the following is not allowed?

a) Arrays of bit fields
b) Pointers to bit fields
c) Functions returning bit fields
d) None of the mentioned
Answer: d
9. Bit fields can only be declared as part of a structure.

a) false
b) true
c) Nothing
d) Varies
Answer: b
10. The following declarations in order are
    short a : 17;
    int long y : 33;
a) Legal, legal
b) Legal, illegal
c) Illegal, illegal
d) Illegal, legal
Answer: c
11. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         char x : 2;
  5.         int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 2;
  11.         p.y = 1;
  12.         p.x = p.x & p.y;
  13.         printf("%d\n", p.x);
  14.     }
a) 0
b) Compile time error
c) Undefined behaviour
d) Depends on the standard
Answer: a
12. What is the output of this C code?
  1.     #include 
  2.     union u
  3.     {
  4.         struct p
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         };
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u;
  14.         u.p.x = 2;
  15.         printf("%d\n", u.p.x);
  16.     }
a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 2
Answer: a
13. What is the output of this C code?
  1.     #include 
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u;
  14.         u.p.x = 2;
  15.         printf("%d\n", u.p.x);
  16.     }
a) Compile time error
b) 2
c) Undefined behaviour
d) Depends on the standard
Answer: b
14. What is the output of this C code?
  1.     #include 
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u.p.x = 2;
  14.         printf("%d\n", u.p.x);
  15.     }
a) Compile time error
b) 2
c) Depends on the compiler
d) Depends on the standard
Answer: a
15. What is the output of this C code?
  1.     #include 
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u = {2};
  14.         printf("%d\n", u.p.x);
  15.     }
a) Compile time error
b) 2
c) Depends on the standard
d) None of the mentioned
Answer: b
16. What is the output of this C code?
  1.     #include 
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u.p = {2};
  14.         printf("%d\n", u.p.x);
  15.     }
a) Compile time error
b) 2
c) Undefined behaviour
d) None of the mentioned
Answer: a
17. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         unsigned int x : 2;
  5.         unsigned int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 3;
  11.         p.y = 1;
  12.         printf("%d\n", sizeof(p));
  13.     }
a) Compile time error
b) Depends on the compiler
c) 2
d) 4
Answer: d
18. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         unsigned int x : 2;
  5.         unsigned int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 3;
  11.         p.y = 4;
  12.         printf("%d\n", p.y);
  13.     }
a) 0
b) 4
c) Depends on the compiler
d) 2
Answer: a
19. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         unsigned int x : 7;
  5.         unsigned int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 110;
  11.         p.y = 2;
  12.         printf("%d\n", p.x);
  13.     }
a) Compile time error
b) 110
c) Depends on the standard
d) None of the mentioned
Answer: b
20. What is the output of this C code?
  1.     #include 
  2.     struct p
  3.     {
  4.         unsigned int x : 1;
  5.         unsigned int y : 1;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 1;
  11.         p.y = 2;
  12.         printf("%d\n", p.y);
  13.     }
a) 1
b) 2
c) 0
d) Depends on the compiler
Answer: c

Related

Multiple Choice Questions 6370994110872377192

Post a Comment

emo-but-icon

item