OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

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?

Choose one option.
Show answer & explanation
Answer: C. Line 1 and Line 2

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:

  1. Line 1: unsigned float b2; — COMPILATION ERROR

    • In C, unsigned is a type qualifier for integral types (char, short, int, long)
    • Floating-point types (float, double) do not support the unsigned qualifier
    • This violates C type rules and will cause a compile-time error
  2. Line 2: static int n1; — COMPILATION ERROR

    • The static storage class specifier cannot be used for struct or union members
    • static is 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
  3. Line 3: short int n2; — VALID

    • This is syntactically correct; short int is a valid integral type for union members
  4. 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: b and n are not defined outside the struct scope
    • However, the question asks specifically about compilation errors, and Lines 1 and 2 are the primary syntax violations

Answer: C) Line 1 and Line 2