Machine-translated by Google. Questions are authored in English.
Top

Structures, Unions, Enums Questions and Answers

1.

Consider the following C program.

#include\(\lt\)stdio.h\(\gt\)
struct Ournode{
    char x,y,z;
};

int main(){
    struct Ournode p = {'1', '0', 'a'+2};
    struct Ournode *q = &p;
    printf ("%c, %c", *((char*)q+1), *((char*)q+2));
    return 0;
}

The output of this program is:

Exam Year(s)

GATE CS - 2018,

Answer: A

The program prints 0, c.

The structure holds three chars in order, x = '1', y = '0' and z = 'a'+2. Since 'a'+2 is evaluated as a character code, z holds 'c'. Casting q to char* and adding 1 and 2 points at y and z, so the two values printed are '0' and 'c'.

The format %c prints just the character, without quotes, so the options with quotes are wrong. 'a+2' would need the expression to be stored as text, but it is computed when the structure is initialised.

Please log in or register to report this question.

2.

What will be the size of the following structure?

struct demo{
  int a;
  char b;
  float c;
}

 

Answer: A

Correct answer: 12

Members are laid out with padding so that each is properly aligned:

int a4 bytes
char b1 byte + 3 padding
float c4 bytes
total12 bytes

The three padding bytes exist so that the float begins on a 4-byte boundary. Without alignment the total would be 9.

Please log in or register to report this question.

3.

Which of the following operators can be applied on structure variables?

Answer: B

Correct answer: Assignment ( = )

A structure may be assigned as a whole, which copies it member by member. It may not be compared with ==: the compiler could not simply compare the bytes, because padding between members holds unspecified values, and comparing member by member is left to the programmer.

Please log in or register to report this question.

4.

How many bytes does the structure below occupy?

#include <stdio.h>
struct test
{
  int k;
  char c;
};

 

Answer: A

Correct answer: Multiple of integer size

The members are an int and a char, but the compiler adds padding so that the whole structure aligns to its strictest member. The size therefore comes out a multiple of the int size - 8 bytes on a typical machine, not the 5 the members occupy. Padding is what makes sizeof a structure unpredictable from its members alone.

Please log in or register to report this question.

5.

What will be the output of the following C code?

#include <stdio.h>
#define MAX 4
enum sanfoundry
{
    a,b=3,c
};
main()
{
    if(MAX!=c)
        printf("hello");
    else
        printf("welcome");
}

 

Answer: C

Correct answer: welcome

The enumerators are a = 0, b = 3 and therefore c = 4 - values continue from the last one given. MAX is also 4, so the condition MAX != c is false and the else branch prints welcome.

Please log in or register to report this question.

6.

What will be the output of the following C code if input given is 2?

#include <stdio.h>
enum day
{
    a,b,c=5,d,e
};
main()
{
    printf("Enter the value for a");
    scanf("%d",a);
    printf("%d",a);
}

 

Answer: C

The code shown above results in an error. This is because memory is not allocated for constants in enum. Thereore ampersand(&) cannot be used with them. Also, a value has already been assigned to ‘a’, which cannot be changed.

Please log in or register to report this question.

Answer: A

Correct answer: An array, each element of which is a pointer to a structure of type node

Subscript binds tighter than the dereference, so struct node *s[10] groups as struct node *(s[10]) - an array of 10 pointers, each to a struct node. A pointer to an array would need parentheses in the other place: struct node (*s)[10].

Please log in or register to report this question.

9.

What is the output of this program?

#include <stdio.h>         
void main()
{
struct demo{
    char * a;
    int n;
};

struct demo p = {"hello", 2015};
struct demo q = p;
printf("%d", printf("%s",q.a));
}

 

Answer: C

Correct answer: hello5

The inner printf runs first, writing hello and returning 5 - the number of characters it wrote. The outer printf then prints that return value. So the output is hello5, in that order. (The assignment q = p copies the structure, including the pointer, so both point at the same string.)

Please log in or register to report this question.

10.

What will be the output of the program if it is executed like below?
cmd> sample

/* sample.c */
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%s\n", argv[argc-1]);
    return 0;
}

 

Answer: A

Correct answer: sample

argv[0] is the program name as invoked, and with no further arguments argc is 1 - so argv[argc-1] is argv[0], which prints sample. The array always has argc entries plus a null pointer at argv[argc].

Please log in or register to report this question.

Loading…
Tags: Structures, Unions, Enums Questions and Answers || Structures, Unions, Enums MCQ Questions and Answers || Structures, Unions, Enums GK Questions and Answers || Structures, Unions, Enums GK MCQ Questions || Structures, Unions, Enums Multiple Choice Questions and Answers || Structures, Unions, Enums GK || GK on Structures, Unions, Enums || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming