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

Typedef Questions and Answers

31.

Array is preferred over linked list for the implementation of ________

Answer: C

When we try to implement binary search using linked list, the traversal steps per element increases in order to find the middle element. Thus, this process is slow and inefficient. This process is much faster using an array, hence it is preferable to use an array for the implementation of binary search.

Please log in or register to report this question.

32.

What will be the output of the following C code?

#include <stdio.h>
    typedef struct student
    {
        char *a;
    }stu;
    void main()
    {
        struct stu s;
        s.a = "hi";
        printf("%s", s.a);
    }

 

Answer: A

Correct answer: Compile time error

The declaration }stu; makes stu a type name, and its struct tag is student. So struct stu s; asks for a structure whose tag is stu, and no such tag exists. Either stu s; or struct student s; would compile; mixing the two forms is what fails.

Please log in or register to report this question.

Answer: B

Correct answer: typedef defined substitutes can be redefined again. (Eg: typedef char a; typedef int a;)

This is the false statement, and hence the answer. A typedef name may be repeated only if every declaration gives it the same type; redefining it as a different type in the same scope is an error, because typedef is handled by the compiler and subject to its rules. A #define macro, being pure text substitution, can be redefined freely.

Please log in or register to report this question.

34.

What will be the output of the following C code?

#include <stdio.h>
    typedef int integer;
    int main()
    {
        int i = 10, *ptr;
        float f = 20;
        integer j = i;
        ptr = &j;
        printf("%d\n", *ptr);
        return 0;
    }

 

Answer: D

Correct answer: 10

typedef int integer; makes integer an alias for int - the same type, not a distinct one. So integer j = i; copies 10 into an ordinary int, and ptr = &j; is a perfectly ordinary int * assignment with no conversion involved. Dereferencing it prints 10. The unused float f is there only as a distraction.

Please log in or register to report this question.

35.

What will be the output of the program?

#include <stdio.h>

typedef struct error {int warning, err, exception;} ERROR;
int main()
{
    ERROR e;
    e.err=1;
    printf("%d\n", e.err);
    return 0;
}

 

Answer: A

Correct answer: 1

The typedef gives the alias ERROR to a structure with three int members, so ERROR e; declares one and e.err = 1; sets the second member. Printing it gives 1. The other two members are left uninitialised, but neither is read, so no undefined behaviour arises.

Please log in or register to report this question.

36.

Which of the given option is the correct method for initialization?
typedef char *string;

Answer: B

Correct answer: string p = “Hello”;

The alias already carries the pointer, so the declaration needs no asterisk of its own - string p is exactly char *p. This is one of the small traps of pointer typedefs: the asterisk becomes invisible at the point of use, which is convenient but makes the declaration read as though p were a plain object.

Please log in or register to report this question.

37.

Consider this statement: typedef enum good {a, b, c} hello; Which of the following statements is correct about hello?

Answer: A

Correct answer: hello is a typedef of enum good

The declaration typedef enum good {a, b, c} hello; does two things: it defines an enumeration with tag good, and it gives that type the alias hello. So hello is a type name, not a variable and not a structure, and the statement itself is perfectly valid. You would then write hello h = a; to declare a variable of it.

Please log in or register to report this question.

38.

typedef which of the following may create problem in the program?

Answer: D

Correct answer: All of the mentioned.

Each of the three is a real source of difficulty. The terminating semicolon is easy to omit or misplace in a long declarator. An alias hides the true type, so the wrong printf or scanf conversion specifier is chosen. And arithmetic follows the underlying type, so a pointer alias produces pointer arithmetic where the reader may be expecting integer arithmetic.

Please log in or register to report this question.

Answer: B

Correct answer: Insertion and deletion of an element can be done at any position in a linked list

A linked list stores each element with a pointer to the next, so inserting or removing one is a matter of adjusting two pointers rather than shifting every following element as an array requires. The other options fail: both structures are linear, both hold homogeneous data, and it is the array whose size is fixed while the list grows as needed.

Please log in or register to report this question.

40.

What will be the output of the following C code?

#include <stdio.h>
    typedef struct p
    {
        int x, y;
    };
    int main()
    {
        p k1 = {1, 2};
        printf("%d\n", k1.x);
    }

 

Answer: A

Correct answer: Compile time error

Two things go wrong. The declaration typedef struct p { int x, y; }; supplies no new name at all, so the typedef declares nothing; and p k1 then uses the struct tag as though it were a type name, which C does not allow. Writing struct p k1, or giving the typedef a name, would fix it.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Typedef Questions and Answers || Typedef MCQ Questions and Answers || Typedef GK Questions and Answers || Typedef GK MCQ Questions || Typedef Multiple Choice Questions and Answers || Typedef GK || GK on Typedef || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming