C Programming Multiple Choice Questions and Answers on Bit-fields for Freshers
https://www.computersprofessor.com/2017/12/c-programming-multiple-choice-questions_31.html
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?
struct temp{int a : 13;
int b : 8;
int c : x;
}s;
a) 4
b) 8
c) 12
d) 32
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)
(Assuming size of int = 4, calculate the % using the memory that would be occupied without bit-fields)
struct temp{int a : 1;
int b : 2;
int c : 4;
int d : 4;
}s;
a) 25%
b) 33.3%
c) 50%
d) 75%
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
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
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
b) Legal, illegal
c) Illegal, illegal
d) Illegal, legal
Answer: c
11. What is the output of this C code?
#includestruct p{char x : 2;
int y : 2;
};
int main()
{struct p p;
p.x = 2;
p.y = 1;
p.x = p.x & p.y;
printf("%d\n", p.x);
}
a) 0
b) Compile time error
c) Undefined behaviour
d) Depends on the standard
b) Compile time error
c) Undefined behaviour
d) Depends on the standard
Answer: a
12. What is the output of this C code?
#includeunion u{struct p{unsigned char x : 2;
unsigned int y : 2;
};
int x;
};
int main()
{union u u;
u.p.x = 2;
printf("%d\n", u.p.x);
}
a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 2
b) Undefined behaviour
c) Depends on the standard
d) 2
Answer: a
13. What is the output of this C code?
#includeunion u{struct{unsigned char x : 2;
unsigned int y : 2;
}p;
int x;
};
int main()
{union u u;
u.p.x = 2;
printf("%d\n", u.p.x);
}
a) Compile time error
b) 2
c) Undefined behaviour
d) Depends on the standard
b) 2
c) Undefined behaviour
d) Depends on the standard
Answer: b
14. What is the output of this C code?
#includeunion u{struct{unsigned char x : 2;
unsigned int y : 2;
}p;
int x;
};
int main()
{union u u.p.x = 2;
printf("%d\n", u.p.x);
}
a) Compile time error
b) 2
c) Depends on the compiler
d) Depends on the standard
b) 2
c) Depends on the compiler
d) Depends on the standard
Answer: a
15. What is the output of this C code?
#includeunion u{struct{unsigned char x : 2;
unsigned int y : 2;
}p;
int x;
};
int main()
{union u u = {2};
printf("%d\n", u.p.x);
}
a) Compile time error
b) 2
c) Depends on the standard
d) None of the mentioned
b) 2
c) Depends on the standard
d) None of the mentioned
Answer: b
16. What is the output of this C code?
#includeunion u{struct{unsigned char x : 2;
unsigned int y : 2;
}p;
int x;
};
int main()
{union u u.p = {2};
printf("%d\n", u.p.x);
}
a) Compile time error
b) 2
c) Undefined behaviour
d) None of the mentioned
b) 2
c) Undefined behaviour
d) None of the mentioned
Answer: a
17. What is the output of this C code?
#includestruct p{unsigned int x : 2;
unsigned int y : 2;
};
int main()
{struct p p;
p.x = 3;
p.y = 1;
printf("%d\n", sizeof(p));
}
a) Compile time error
b) Depends on the compiler
c) 2
d) 4
b) Depends on the compiler
c) 2
d) 4
Answer: d
18. What is the output of this C code?
#includestruct p{unsigned int x : 2;
unsigned int y : 2;
};
int main()
{struct p p;
p.x = 3;
p.y = 4;
printf("%d\n", p.y);
}
a) 0
b) 4
c) Depends on the compiler
d) 2
b) 4
c) Depends on the compiler
d) 2
Answer: a
19. What is the output of this C code?
#includestruct p{unsigned int x : 7;
unsigned int y : 2;
};
int main()
{struct p p;
p.x = 110;
p.y = 2;
printf("%d\n", p.x);
}
a) Compile time error
b) 110
c) Depends on the standard
d) None of the mentioned
b) 110
c) Depends on the standard
d) None of the mentioned
Answer: b
20. What is the output of this C code?
#includestruct p{unsigned int x : 1;
unsigned int y : 1;
};
int main()
{struct p p;
p.x = 1;
p.y = 2;
printf("%d\n", p.y);
}
a) 1
b) 2
c) 0
d) Depends on the compiler
b) 2
c) 0
d) Depends on the compiler
Answer: c
