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

Typedef Questions and Answers

11.

We want to declare x, y and z as pointers of type int. The alias name given is: intpt The correct way to do this using the keyword typedef is:

Answer: D

Correct answer: typedef int* intptr; intptr x,y,z;

The keyword comes first, followed by the existing type and then the new name: typedef int *intptr;. The alias is then used exactly like a built-in type, so intptr x, y, z; declares three pointers. Note that this is one place the alias behaves better than the raw type - int *x, y, z; would make only x a pointer.

Please log in or register to report this question.

12.

Which of the following keywords is used to define an alternate name for an already existing data type?

Answer: C

Correct answer: typedef

typedef introduces a new name for an existing type; it creates no new type, only an alias, so the compiler treats the alias and the original as interchangeable. static and volatile are storage-class and type qualifiers, and default belongs to the switch statement.

Please log in or register to report this question.

13.

The keyword typedef cannot be used to give alias names to pointers.

Answer: B

Correct answer: False

The claim is false: aliasing a pointer type is one of the commonest uses of typedef. typedef int *intptr; makes intptr mean int *, and the same applies to pointers to functions and to structures. The one thing to watch is that intptr a, b; makes both pointers, which a plain int *a, b; would not.

Please log in or register to report this question.

Answer: B

Correct answer: It creates an alternative name for an existing type

typedef introduces a synonym; the underlying type is unchanged, so the compiler treats the alias and the original as the same type. It is used to shorten unwieldy declarations and to make the intent of a type clearer.

Please log in or register to report this question.

15.

We want to create an alias name for an identifier of the type unsigned long. The alias name is: ul. The correct way to do this using the keyword typedef is ____________

Answer: A

The syntax of the keyword typedef is: keyword ;
Hence if we want to create an alias name (ul) for an identifier of type unsigned long, the correct way to do this would be: typedef unsigned long ul;

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. Repeating a typedef name with a different type in the same scope is an error, because the compiler enforces the alias as a genuine type declaration. The other two statements are true: typedefs obey the usual scope rules, so one declared inside a function is local to it.

Please log in or register to report this question.

17.

What will be the output of the following C code?

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

 

Answer: D

Correct answer: hey

Here the declaration uses the tag rather than the alias - struct student s; - and the tag student does exist, so this compiles. The member a is a char *, assigning a string literal to it is legal, and %s prints it: the output is hey. Compare the previous question, which used struct stu and failed for exactly this reason.

Please log in or register to report this question.

18.

Which of these correctly defines STR as a name for a pointer to char ?

Answer: A

Correct answer: typedef char *STR;

A typedef is written exactly like a variable declaration with typedef placed in front, so the new name occupies the position the variable name would take. Reading char *STR as a declaration of STR makes the form easy to remember.

Please log in or register to report this question.

Answer: D

Correct answer: p is a pointer to an array of 5 integers

The parentheses are what matter. int (*p)[5] binds the asterisk to p first, so p is a pointer, and what it points to is an array of five int. Without them, int *p[5] would be an array of five pointers - the same tokens in a different grouping giving a completely different type.

Please log in or register to report this question.

20.

One of the major difference between typedef and #define is that typedef interpretation is performed by the _________________ whereas #define interpretation is performed by the _____________

Answer: C

Correct answer: compiler, pre-processor

#define is handled by the preprocessor, which performs blind textual substitution before compilation begins and understands nothing of types or scope. typedef is processed by the compiler itself, so it obeys scope rules and creates a genuine type alias. This is why #define intptr int * and typedef int *intptr; behave differently when several variables are declared on one line.

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