Vidyalelo
C Programming · all questions

Structure and Union
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

190

Questions

9/10

Page

Pick an option on a question to see the right answer and solution.

Which of the following is an incorrect syntax to pass by reference a member of a structure in a function?
(Assume: struct temp{int a;}s;)

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
struct student
{
};
void main()
{
    struct student s[2];
    printf("%d", sizeof(s));
}

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
struct point
{
    int x;
    int y;
} p[] = {1, 2, 3, 4, 5};
void foo(struct point*);
int main()
{
    foo(p);
}
void foo(struct point p[])
{
    printf("%d %d\n", p->x, p[2].y);
}

Select an option to see the answer and solution.

Which of the following is not possible under any scenario?

Select an option to see the answer and solution.

Calculate the % of memory saved when bit-fields are used for the following C structure as compared to with-out use of bit-fields for the same structure? (Assuming size of int = 4)
struct temp
{
    int a : 1;
    int b : 2;
    int c : 4;
    int d : 4;
}s;

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
typedef struct student
{
    char *a;
}stu;
void main()
{
    struct stu s;
    s.a = "hi";
    printf("%s", s.a);
}

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
void main()
{
    struct student
    {
        int no;
        char name[20];
    };
    struct student s;
    s.no = 8;
    printf("%d", s.no);
}

Select an option to see the answer and solution.

Which of the following statement is true?

Select an option to see the answer and solution.

What will be the output of the following 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);
}

Select an option to see the answer and solution.

Which of the given option is the correct method for initialization?
typedef char *string;

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
struct p
{
    int x;
    char y;
};
int main()
{
    struct p p1[] = {1, 92, 3, 94, 5, 96};
    struct p *ptr1 = p1;
    int x = (sizeof(p1) / 3);
    if (x == sizeof(int) + sizeof(char))
        printf("%d\n", ptr1->x);
    else
        printf("falsen");
}

Select an option to see the answer and solution.

Which of the following will stop the loop at the last node of a linked list in the following C code snippet?
struct node
{
    struct node *next;
};

Select an option to see the answer and solution.

In the following declaration of bit-fields, the constant-expression must be . . . . . . . .
struct-declarator:
declarator
type-specifier declarator opt : constant-expression

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
struct p
{
    int x;
    int y;
};
int main()
{
    struct p p1[] = {1, 2, 3, 4, 5, 6};
    struct p *ptr1 = p1;
    printf("%d %d\n", ptr1->x, (ptr1 + 2)->x);
}

Select an option to see the answer and solution.

What would be the size of the following union declaration? (Assuming size of double = 8, size of int = 4, size of char = 1)
#include <stdio.h>
union uTemp
{
    double a;
    int b[10];
    char c;
}u;

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
union p
{
    int x;
    float y;
};
int main()
{
    union p p, b;
    p.x = 10;
    printf("%f\n", p.y);
}

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
struct student fun(void)
{
    struct student
    {
        char *name;
    };
    struct student s;
    s.name = "alan";
    return s;
}
void main()
{
    struct student m = fun();
    printf("%s", m.name);
}

Select an option to see the answer and solution.

In what situation, install function returns NULL?

Select an option to see the answer and solution.

What will be the output of the following C code?
#include <stdio.h>
typedef struct p *q;
struct p
{
    int x;
    char y;
    q ptr;
};
int main()
{
    struct p p = {1, 2, &p};
    printf("%d\n", p.ptr->ptr->x);
    return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C code according to C99 standard?
#include <stdio.h>
struct p
{
    int k;
    char c;
    float f;
};
int main()
{
    struct p x = {.c = 97};
    printf("%f\n", x.f);
}

Select an option to see the answer and solution.