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

Structures, Unions, Enums Questions and Answers

91.

What is the output of this program?

#include <stdio.h>
int main()
{
    enum days {MON=-1, TUE, WED=4, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d", MON, TUE, WED, THU, FRI, SAT);
    return 0;
}

 

Answer: A

Correct answer: -1 0 4 5 6 7

Enumerators continue from the last value given. MON is set to -1, so TUE is 0; WED is set to 4, so THU, FRI and SAT are 5, 6, 7. Values may be repeated or given out of order - the compiler only fills in what you leave out.

Please log in or register to report this question.

92.

Predict the output of following C program

#include <stdio.h>
struct Point
{
  int x, y, z;
};
 
int main()
{
  struct Point p1 = {.y = 0, .z = 1, .x = 2};
  printf("%d %d %d", p1.x, p1.y, p1.z);
  return 0;
}

 

Answer: B

Correct answer: 2 0 1

These are designated initialisers, introduced in C99: each value names the member it initialises, so their order in the braces is irrelevant. .x = 2, .y = 0, .z = 1 whatever order they are written in, and the program prints 2 0 1.

Please log in or register to report this question.

93.

What will be the output of the following C code?

#include <stdio.h>
enum sanfoundry
{
    a,b,c=5
};
int main()
{
    enum sanfoundry s;
    b=10;
    printf("%d",b);
}

 

Answer: A

Correct answer: Error

b is an enumerator - a constant - so b = 10; is an assignment to something that is not an lvalue, and the compiler rejects it. Only an enum variable can be assigned to.

Please log in or register to report this question.

94.

What will be the output of the following C code?

#include <stdio.h>
enum sanfoundry
{
    a=1,b,c,d,e
};
int main()
{
    printf("%d",b*c+e-d);
}

 

Answer: B

Correct answer: 7

Values run on from the one given: a = 1, so b = 2, c = 3, d = 4, e = 5. Then

\[ b \times c + e - d = 2 \times 3 + 5 - 4 = 7 \]

Please log in or register to report this question.

95.

What is the correct syntax to declare a function foo() which receives an array of structure in function?

Answer: A

Correct answer: void foo(struct *var);

An array argument decays to a pointer to its first element, so a function receiving an array of structures takes a pointer to structure. (In real code the tag must be named - void foo(struct tagname *var); - since struct alone is not a type.) Passing an array never copies it, so the length must be passed separately.

Please log in or register to report this question.

96.

What will be the output of the following C code?

main()
{
    enum resut {pass, fail};
    enum result s1,s2;
    s1=pass;
    s2=fail;
    printf("%d",s1);
}

 

Answer: A

Correct answer: error

The enumeration is declared as enum resut - a typo - and the variables are then declared as enum result, a tag that does not exist. The compiler reports an incomplete type.

Please log in or register to report this question.

97.

Which operator connects the structure name to its member name?

Answer: C

Correct answer: .

The dot operator selects a member of a structure variable: s.member. The arrow -> does the same through a pointer, and is exactly equivalent to (*p).member.

Please log in or register to report this question.

98.

#include <stdio.h>

typedef enum{
    Male, Female
}SEX;

int main()
{
  SEX var = Male;
  SEX var1 = Female;
  printf("%s %s",var, var1); 
  return 0;
}

 

Answer: C

Correct answer: Runtime error (Segmentation fault)

%s tells printf to treat its argument as a pointer to a string, but the arguments are the integers 0 and 1. printf then dereferences address 0 and address 1, and the program is killed. Nothing in the program remembers the names Male and Female - printing them requires an array of strings indexed by the enumerator.

Please log in or register to report this question.

99.

Which operator connects the structure name to its member name?

Answer: C

Correct answer: .

The dot connects a structure variable to its member: s.member. Through a pointer the arrow is used instead, p->member, which is shorthand for (*p).member.

Please log in or register to report this question.

100.

What will be output for the following code?

Answer: A

The code shown above results in an error, stating : storage size of t1 and t2 unknown. There is an error in the declaration of these variables in the statement: enum test1 t1,t2;

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