C Programming

C Programming

Structures, Unions, Enums has 53 Questions
How will you free the allocated memory ?

remove(var-name);
free(var-name);
delete(var-name);
dalloc(var-name);
What will be the output of the program ?
#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u;
    u.ch[0]=3;
    u.ch[1]=2;
    printf("%d, %d, %d", u.ch[0], u.ch[1], u.i);
    return 0;
}

3, 2, 515
515, 2, 3
3, 2, 5
515, 515, 4
Point out the error in the program?
struct emp
{
    int ecode;
    struct emp *e;
};

Error: in structure declaration
Linker Error
No Error
None of above
Which of the following statements correct about the below program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float sal;
    };
    struct emp e[2];
    int i=0;
    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, &e[i].age, &e[i].sal);

    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
    return 0;
}

Error: scanf() function cannot be used for structures elements.
The code runs successfully.
Error: Floating point formats not linked Abnormal program termination.
Error: structure variable must be initialized.
A union cannot be nested in a structure

True
False
If the following structure is written to a file using fwrite(), can fread() read it back successfully?
struct emp
{
    char *n;
    int age;
};
struct emp e={"ASTIWZ", 15};
FILE *fp;
fwrite(&e, sizeof(e), 1, fp);

Yes
No
What is the similarity between a structure, union and enumeration?

All of them let you define new values
All of them let you define new data types
All of them let you define new pointers
All of them let you define new structures
What will be the output of the program ?
#include<stdio.h>

int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d", v.a);
    return 0;
}

10
20
30
0
Point out the error in the program?
typedef struct data mystruct;
struct data
{
    int x;
    mystruct *b;
};

Error: in structure declaration
Linker Error
No Error
None of above
Which of the following statements correct about the below program?
#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u1 = {512};
    union a u2 = {0, 2};
    return 0;
}
1: u2 CANNOT be initialized as shown.
2: u1 can be initialized as shown.
3: To initialize char ch[] of u2 '.' operator should be used.
4: The code causes an error 'Declaration syntax error'

1, 2
2, 3
1, 2, 3
1, 3, 4
Nested unions are not allowed

True
False
size of union is size of the longest element in the union

Yes
No
What will be the output of the program ?
#include<stdio.h>

int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit={1, 2, 13};

    printf("%d, %d, %d", bit.bit1, bit.bit3, bit.bit4);
    return 0;
}

1, 2, 13
1, 4, 4
-1, 2, -3
-1, -2, -13
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct a
    {
        float category:5;
        char scheme:4;
    };
    printf("size=%d", sizeof(struct a));
    return 0;
}

Error: invalid structure member in printf
Error in this float category:5; statement
No error
None of above
Which of the following statements correctly assigns 45 to month using pointer variable pdt?
#include<stdio.h>

    struct date
    {
        int day;
        int month;
        int year;
    };
int main()
{
    struct date d;
    struct date *pdt;
    pdt = &d;
    return 0;
}

pdt.month = 12
&pdt.month = 12
d.month = 12
pdt->month = 12
Bit fields CANNOT be used in union.

True
False
The elements of union are always accessed using & operator

Yes
No
What will be the output of the program in 16 bit platform (Turbo C under DOS) ?
#include<stdio.h>

int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit;
    printf("%d", sizeof(bit));
    return 0;
}

1
2
4
9
Point out the error in the program?
struct emp
{
    int ecode;
    struct emp e;
};

Error: in structure declaration
Linker Error
No Error
None of above
Which of the following statements correct about the below code?
maruti.engine.bolts=25;

Structure bolts is nested within structure engine.
Structure engine is nested within structure maruti.
Structure maruti is nested within structure engine.
Structure maruti is nested within structure bolts.
one of elements of a structure can be a pointer to the same structure.

True
False
Will the following code work?
#include<stdio.h>
#include<alloc.h>

struct emp
{
    int len;
    char name[1];
};
int main()
{
    char newname[] = "Rahul";
    struct emp *p = (struct emp *) malloc(sizeof(struct emp) -1 +
                    strlen(newname)+1);

    p->len = strlen(newname);
    strcpy(p -> name, newname);
    printf("%d %s", p->len, p->name);
    return 0;
}

Yes
No
What will be the output of the program ?
#include<stdio.h>

int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d", MON, TUE, WED, THU, FRI, SAT);
    return 0;
}

-1, 0, 1, 2, 3, 4
-1, 2, 6, 3, 4, 5
-1, 0, 6, 2, 3, 4
-1, 0, 6, 7, 8, 9
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}

Error: invalid structure member
Error: Floating point formats not linked
No error
None of above
A structure can be nested inside another structure.

True
False
A pointer union CANNOT be created

Yes
No
What will be the output of the program ?
#include<stdio.h>

int main()
{
    enum status {pass, fail, absent};
    enum status stud1, stud2, stud3;
    stud1 = pass;
    stud2 = absent;
    stud3 = fail;
    printf("%d %d %d", stud1, stud2, stud3);
    return 0;
}

0, 1, 2
1, 2, 3
0, 2, 1
1, 3, 2
Point out the error in the program?
#include<stdio.h>
#include<string.h>
void modify(struct emp*);
struct emp
{
    char name[20];
    int age;
};
int main()
{
    struct emp e = {"Sanjay", 35};
    modify(&e);
    printf("%s %d", e.name, e.age);
    return 0;
}
void modify(struct emp *p)
{
     p ->age=p->age+2;
}

Error: in structure
Error: in prototype declaration unknown struct emp
No error
None of above
Which of the following statement is True?

User has to explicitly define the numeric value of enumerations
User has a control over the size of enumeration variables.
Enumeration can have an effect local to the block, if desired
Enumerations have a global effect throughout the file.
Is there easy way to print enumeration values symbolically?

Yes
No
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i=4, j=8;
    printf("%d, %d, %d", i|j&j|i, i|j&j|i, i^j);
    return 0;
}

12, 12, 12
112, 1, 12
32, 1, 12
-64, 1, 12
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct bits
    {
        int i:40;
    }bit;

    printf("%d", sizeof(bit));
    return 0;
}

4
2
Error: Bit field too large
Error: Invalid member access in structure
The '.' operator can be used access structure elements using a structure variable.

True
False
By default structure variable will be of auto storage class

Yes
No
What will be the output of the program in Turbo C (under DOS)?
#include<stdio.h>

int main()
{
    struct emp
    {
        char *n;
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    strupr(e2.n);
    printf("%s", e1.n);
    return 0;
}

Error: Invalid structure assignment
DRAVID
Dravid
No output
Point out the error in the program?
#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a z1 = {512};
    union a z2 = {0, 2};
    return 0;
}

Error: invalid union declaration
Error: in Initializing z2
No error
None of above
Union elements can be of different sizes.

True
False
Is it necessary that the size of all elements in a union should be same?

Yes
No
What will be the output of the program in 16-bit platform (under DOS)?
#include<stdio.h>

int main()
{
    struct node
    {
        int data;
        struct node *link;
    };
    struct node *p, *q;
    p = (struct node *) malloc(sizeof(struct node));
    q = (struct node *) malloc(sizeof(struct node));
    printf("%d, %d", sizeof(p), sizeof(q));
    return 0;
}

2, 2
8, 8
5, 5
4, 4
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char n[20];
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    if(e1 == e2)
        printf("The structure are equal");
    return 0;
}

Prints: The structure are equal
Error: Structure cannot be compared using '=='
No output
None of above
A structure can contain similar or dissimilar elements

True
False
Can we have an array of bit fields?

Yes
No
What will be the output of the program ?
#include<stdio.h>

int main()
{
    struct byte
    {
        int one:1;
    };
    struct byte var = {1};
    printf("%d", var.one);
    return 0;
}

1
-1
0
Error
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct bits
    {
        float f:2;
    }bit;

    printf("%d", sizeof(bit));
    return 0;
}

4
2
Error: cannot set bit field for float
Error: Invalid member access in structure
The '->' operator can be used to access structures elements using a pointer to a structure variable only

True
False
Will the following declaration work?
typedef struct s
{
    int a;
    float b;
}s;

Yes
No
What will be the output of the program ?
#include<stdio.h>

int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d", ++MON, TUE, WED, THU, FRI, SAT);
    return 0;
}

-1, 0, 1, 2, 3, 4
Error
0, 1, 6, 3, 4, 5
0, 0, 6, 7, 8, 9
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float bs;
    };
    struct emp e;
    e.name = "Suresh";
    e.age = 25;
    printf("%s %d", e.name, e.age);
    return 0;
}

Error: Lvalue required/incompatible types in assignment
Error: invalid constant expression
Error: Rvalue required
No error, Output: Suresh 25
It is not possible to create an array of pointer to structures.

True
False
Can a structure can point to itself?

Yes
No
What will be the output of the program ?
#include<stdio.h>

    struct course
    {
        int courseno;
        char coursename[25];
    };
int main()
{
    struct course c[] = { {102, "Java"}, 
                          {103, "PHP"}, 
                          {104, "DotNet"}     };

    printf("%d", c[1].courseno);
    printf("%s", (*(c+2)).coursename);
    return 0;
}

103 Dotnet
102 Java
103 PHP
104 DotNet
If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will the following structure always occupy 7 bytes?
struct ex
{
    char ch;
    int i;
    long int a;
};

Yes
No
What will be the output of the program given below in 16-bit platform ?
#include<stdio.h>

int main()
{
    enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;
    printf("%d", sizeof(var));
    return 0;
}

1
2
4
10