Which of the following line(s) will cause a compilation error?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
struct book
{
int b1;
unsigned float b2; //Line 1
!b2;
union notebook
{
static int n1; //Line 2
short int n2; //Line 3
}n;
printf("%d", sizeof(b) + sizeof(n)); //Line 4
return 0;
}
Which of the following line(s) will cause a compilation error?
Show answer & explanation
Line 1 is invalid because unsigned float is not a valid type qualifier combination in C (only unsigned applies to integral types, not floating-point). Line 2 is invalid because static cannot be used for members inside a union or struct—it only applies to file scope or function scope variables. Line 3 is valid C syntax. Line 4 would fail for a different reason (undefined identifiers b and n in a struct definition context), but the primary compilation errors occur at Lines 1 and 2.
Step-by-step Derivation:
Step-by-step analysis of each line:
Line 1:
unsigned float b2;— COMPILATION ERROR- In C,
unsignedis a type qualifier for integral types (char, short, int, long) - Floating-point types (float, double) do not support the
unsignedqualifier - This violates C type rules and will cause a compile-time error
- In C,
Line 2:
static int n1;— COMPILATION ERROR- The
staticstorage class specifier cannot be used for struct or union members staticis only valid for file-scope variables, function-scope variables, or function declarations- Members inside a struct/union must use no storage class or alignment specifiers like
_Alignas - This causes a compilation error
- The
Line 3:
short int n2;— VALID- This is syntactically correct;
short intis a valid integral type for union members
- This is syntactically correct;
Line 4:
printf("%d", sizeof(b) + sizeof(n));— Would be an error for different reasons- Even if Lines 1–2 were fixed, this line has issues:
bandnare not defined outside the struct scope - However, the question asks specifically about compilation errors, and Lines 1 and 2 are the primary syntax violations
- Even if Lines 1–2 were fixed, this line has issues:
Answer: C) Line 1 and Line 2