C Programming Questions and Answers – Bit-fields
C Programming Questions and Answers – Bit-fields
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) Both a and c.
View Answer
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
View Answer
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
View Answer
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
View Answer
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)
struct temp
{
int a : 1;
int b : 2;
int c : 4;
int d : 4;
}s;
a) 25%
b) 33.3%
c) 50%
d) 75%
View Answer
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
View Answer
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
View Answer
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
View Answer
Answer:d
9.Bit fields can only be declared as part of a structure.
a) false
b) true
c) Nothing
d) Varies
View Answer
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
View Answer
Answer:c
11.What is the output of this C code?
#include <stdio.h>
struct 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.
View Answer
Answer:a
12.What is the output of this C code?
#include <stdio.h>
union 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
View Answer
Answer:a
13.What is the output of this C code?
#include <stdio.h>
union 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
View Answer
Answer:b
14.What is the output of this C code?
#include <stdio.h>
union 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
View Answer
Answer:a
15.What is the output of this C code?
#include <stdio.h>
union 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
View Answer
Answer:b
16.What is the output of this C code?
#include <stdio.h>
union 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
View Answer
Answer:a
17.What is the output of this C code?
#include <stdio.h>
struct 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
View Answer
Answer:d
18.What is the output of this C code?
#include <stdio.h>
struct 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
View Answer
Answer:a
19.What is the output of this C code?
#include <stdio.h>
struct 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
View Answer
Answer:b
20.What is the output of this C code?
#include <stdio.h>
struct 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
View Answer
Answer:c