Yes / No Questions

Question 1 :

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={"IndiaParinam", 15};
FILE *fp;
fwrite(&e, sizeof(e), 1, fp);


A). Yes
B). No
Answer : Option B

Explanation :

Since the structure contain a char pointer while writing the structure to the disk using fwrite() only the value stored in pointer n will get written. so fread() fails to read.


Question 2 :

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


A). Yes
B). No
Answer : Option A

Question 3 :

The elements of union are always accessed using & operator


A). Yes
B). No
Answer : Option B

Question 4 :

Will the following code work?

#include < stdio.h >
#include < malloc.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\n", p->len, p->name);
    return 0;
}


A). Yes
B). No
Answer : Option A

Explanation :

The program allocates space for the structure with the size adjusted so that the name field can hold the requested name.


Question 5 :

A pointer union CANNOT be created


A). Yes
B). No
Answer : Option B

Question 6 :

Is there easy way to print enumeration values symbolically?


A). Yes
B). No
Answer : Option B

Explanation :

You can write a function of your own to map an enumeration constant to a string.


Question 7 :

By default structure variable will be of auto storage class


A). Yes
B). No
Answer : Option A

Question 8 :

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


A). Yes
B). No
Answer : Option B

Question 9 :

Can we have an array of bit fields?


A). Yes
B). No
Answer : Option B

Question 10 :

Will the following declaration work?

typedef struct s
{
    int a;
    float b;
}s;


A). Yes
B). No
Answer : Option A

Question 11 :

Can a structure can point to itself?


A). Yes
B). No
Answer : Option A

Explanation :

A structure pointing to itself is called self-referential structures.


Question 12 :

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;
};


A). Yes
B). No
Answer : Option B

Explanation :

A compiler may leave holes in structures by padding the first char in the structure with another byte just to ensures that the integer that follows is stored at an location. Also, there might be 2extra bytes after the integer to ensure that the long integer is stored at an address, which is multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.