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

Structures, Unions, Enums Questions and Answers

91.

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.

92.

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.

93.

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.

94.

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.

95.

#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.

96.

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.

97.

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.

98.

#include "stdio.h"

typedef enum{
    Male=5, Female
}SEX;

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

}

 

Answer: D

Correct answer: 5 6

Male is set to 5, and Female, having no value of its own, continues from it at 6. Enumerators always resume counting from the last value given.

Please log in or register to report this question.

99.

#include <stdio.h>
int main()
{
    struct site
    {
        char name[] = "GeeksQuiz";
        int no_of_pages = 200;
    };
    struct site *ptr;
    printf("%d ", ptr->no_of_pages);
    printf("%s", ptr->name);
    getchar();
    return 0;
}

 

Answer: D

Correct answer: Compiler Error

A structure declaration describes a type, not an object, so its members cannot carry initialisers - both char name[] = "GeeksQuiz"; and int no_of_pages = 200; are rejected. A member array also cannot have an unspecified size. The program never compiles, so the uninitialised ptr never becomes an issue.

Please log in or register to report this question.

100.

What will be the output of the following C code?

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

 

Answer: D

Correct answer: 101

Take each in turn. s1 = c, and in sanfoundry1 c is 0. s = a, and a is 1. s2 = d, which is 1. The prints are in the order s, s1, s2, giving 101. Note that C does not object to assigning a value of one enumeration to a variable of another - they are all just ints.

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