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

Typedef Questions and Answers

Answer: A

Correct answer: type PFI, for pointer to function (of two char * arguments) returning int

Read the declarator from the name outward, respecting the parentheses. PFI is inside (*PFI), so it is a pointer; the (char *, char *) that follows makes it a pointer to a function taking two char *; and the leading int gives the return type. Without the parentheses, int *PFI(char *, char *) would instead be a function returning int *.

Please log in or register to report this question.

Answer: C

Correct answer: typedef is handled by the compiler and respects scope, while #define is a textual substitution by the preprocessor

Because typedef is part of the language proper it obeys the usual scoping rules and handles complex declarations correctly. A macro merely pastes text, which is why a #define for a pointer type applies the star to only the first name in a multiple declaration.

Please log in or register to report this question.

23.

The type of linked list in which the node does not contain any pointer or reference to the previous node is _____________

Answer: B

Correct answer: Singly linked list

A singly linked list gives each node one pointer, to the next node only. There is no way back, so traversal is forward from the head and a node cannot be deleted without a reference to its predecessor.

A doubly linked list adds a pointer to the previous node, which costs memory but allows backward traversal and deletion given only the node itself. The circular variants differ in whether the last node points back to the first, which is independent of how many pointers each node carries.

Please log in or register to report this question.

Answer: C

Correct answer: Both a & b

The two statements are the same fact expressed twice. A typedef introduces no new type, only an additional name for one that already exists, and the two names are completely interchangeable thereafter. Nothing at compile time distinguishes a variable declared through the alias from one declared through the original type.

Please log in or register to report this question.

25.

Point out the error in the following code?

typedef struct
{
    int data;
    NODEPTR link;
}*NODEPTR;

 

Answer: B

Correct answer: Error: typedef cannot be used until it is defined

Inside the braces, the member NODEPTR link; uses a name that the typedef has not yet introduced - the alias only comes into existence at the semicolon ending the whole declaration. The remedy is the pattern of the previous question: declare typedef struct node *NODEPTR; first, using a tag, and then define the structure.

Please log in or register to report this question.

26.

Which option should be selected to work the following C expression?

string p = "HELLO";

 

Answer: B

Correct answer: typedef char *string;

A string in C is a pointer to its first character, so the alias must be a pointer type: typedef char *string; lets string p = "HELLO"; assign the literal’s address to p. The array form is not merely wrong syntax - even typedef char string[]; would give an incomplete type that cannot be initialised this way.

Please log in or register to report this question.

27.

Are the properties of i, j and x, y in the following program same?

typedef unsigned long int uli;
uli i, j;
unsigned long int x, y;

 

Answer: A

Correct answer: Yes

uli is an alias for unsigned long int, not a new type, so i and j have exactly the same type, size, alignment and range as x and y. They are interchangeable in every context, and no compiler diagnostic distinguishes them. The alias exists purely for readability.

Please log in or register to report this question.

28.

Local variables are stored in an area called ___________

Answer: D

Correct answer: Stack

Local variables have automatic storage duration: created on entry to their block and destroyed on exit. A stack matches that pattern exactly, since the most recently created is always the first to be destroyed.

Allocation therefore costs only an adjustment of the stack pointer, which is why automatic variables are far cheaper than heap blocks. The same mechanism is why returning the address of a local is a fault - the storage is reclaimed as the function returns.

Please log in or register to report this question.

29.

What is the size of myArray in the code shown below? (Assume that 1 character occupies 1 byte)

typedef char x[10];
x myArray[5];

 

Answer: D

Correct answer: 50 bytes

typedef char x[10]; makes x mean “array of 10 char”. So x myArray[5]; declares an array of five such arrays, which is 5 × 10 = 50 characters, or 50 bytes. This nesting is why an array typedef is worth reading carefully: the new dimension is prepended, not appended.

Please log in or register to report this question.

30.

Is there any difference in the #define and typedef in the following code?

typedef char * string_t;
#define string_d char *;
string_t s1, s2;
string_d s3, s4;

 

Answer: A

Correct answer: Yes

There is a real difference, and it appears at the second declaration of each pair. string_t s1, s2; makes both pointers, because the alias is a single type. string_d s3, s4; expands textually to char * s3, s4;, which makes s3 a pointer and s4 a plain char. The macro also leaves a stray semicolon from its own definition.

Please log in or register to report this question.

Loading…
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