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

Structures, Unions, Enums Questions and Answers

101.

#include <stdio.h>

typedef enum{
    Male, Female=-1
}SEX;

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

 

Answer: D

Correct answer: 0 -1

Male takes the default first value, 0, and Female is then explicitly set to -1. Explicit values may be given in any order and may be negative - the compiler only fills in the ones you leave out, continuing from the last value given.

Please log in or register to report this question.

102.

Which of the following are themselves a collection of different data types?

Answer: B

Correct answer: Structures

A structure groups members of different types under one name - an int, a float and a character array together. An array requires every element to be the same type, and a string is an array of characters.

Please log in or register to report this question.

103.

A user-defined data type can be created with ___________

Answer: D

Correct answer: all of the mentioned

All three create user-defined types. struct aggregates members of different types; enum names a set of integer constants; and typedef gives a new name to an existing type, which is how struct node * becomes NODEPTR. A union belongs on the same list.

Please log in or register to report this question.

104.

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.

105.

Which of the following cannot be a structure member?

Answer: B

Correct answer: Function

A structure member must be an object with storage, and a function is not - so a function cannot be a member of a C structure. What can be a member is a pointer to a function, which is how C programs implement dispatch tables and something resembling methods. Arrays and nested structures are ordinary members.

Please log in or register to report this question.

Answer: D

Correct answer: struct temp s; struct temp{}; main(){}

This one is an error. Declaring struct temp s; before the structure has been defined means declaring an object of an incomplete type - the compiler does not yet know how much storage to reserve. The other two are fine: the first defines the type and a variable together, and the second defines the type before using it. (A pointer to an incomplete type would be legal, since a pointer’s size is known.)

Please log in or register to report this question.

107.

What is the output of this C code?

#include <stdio.h>
    struct student
    {
        int no;
        char name[20];
    }
    void main()
    {
        struct student s;
        s.no = 8;
        printf("hello");
    }

 

Answer: A

Correct answer: Compile time error

The structure definition is missing its terminating semicolon after the closing brace. Without it the compiler reads on and tries to make sense of void main() as part of the declaration, and the program fails to compile.

Please log in or register to report this question.

108.

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…

Other Subjects

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