1. 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("%dn", p.x);
-
}
a) 0
b) Compile time error
c) Undefined behaviour
d) Depends on the standard
Answer
Answer: a [Reason:] None.
2. 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("%dn", u.p.x);
-
}
a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 2
Answer
Answer: a [Reason:] None.
3. 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("%dn", u.p.x);
-
}
a) Compile time error
b) 2
c) Undefined behaviour
d) Depends on the standard
Answer
Answer: b [Reason:] None.
4. 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("%dn", u.p.x);
-
}
a) Compile time error
b) 2
c) Depends on the compiler
d) Depends on the standard
Answer
Answer: a [Reason:] None.
5. 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("%dn", u.p.x);
-
}
a) Compile time error
b) 2
c) Depends on the standard
d) None of the mentioned
Answer
Answer: b [Reason:] None.
6. 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("%dn", u.p.x);
-
}
a) Compile time error
b) 2
c) Undefined behaviour
d) None of the mentioned
Answer
Answer: a [Reason:] None.
7. 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("%dn", sizeof(p));
-
}
a) Compile time error
b) Depends on the compiler
c) 2
d) 4
Answer
Answer: d [Reason:] None.
8. 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("%dn", p.y);
-
}
a) 0
b) 4
c) Depends on the compiler
d) 2
Answer
Answer: a [Reason:] None.
9. 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("%dn", p.x);
-
}
a) Compile time error
b) 110
c) Depends on the standard
d) None of the mentioned
Answer
Answer: b [Reason:] None.
10. 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("%dn", p.y);
-
}
a) 1
b) 2
c) 0
d) Depends on the compiler
Answer
Answer: c [Reason:] None.