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

Typedef Questions and Answers

41.

What will be the output of the following C code?

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

 

Answer: A

Correct answer: Compile time error

Because the declaration begins with typedef, the k after the closing brace is a type name, and a type name cannot be given an initialiser - = {1, 2} is meaningless there. The later p k1 = k; compounds the error by using the tag p as a type. Remove the typedef and k becomes an initialised variable.

Please log in or register to report this question.

42.

Which of the following is an example for non linear data type?

Answer: A

A data structure is said to be linear if its elements form a sequence or a linear list. For example array, linked list, queue, stack etc. Elements in a non linear data structure do not form a sequence. For example Trees, graphs etc.

Please log in or register to report this question.

43.

Is the following declaration acceptable?

typedef long no, *ptrtono;
no n;
ptrtono p;

 

Answer: A

Correct answer: Yes

One typedef may declare several aliases, each with its own derived type built on the same base. Here no becomes long and ptrtono becomes long *, so both subsequent declarations are valid. It is the same grammar as an ordinary declaration such as long n, *p;, with typedef changing only what the declared names mean.

Please log in or register to report this question.

44.

What will be the output of the following C code?

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

 

Answer: A

Correct answer: Compile time error

The body of the program is correct - stu is a proper alias, s.a = "hi" is a valid pointer assignment - but there is a stray s after the closing brace of main. At file scope that is a declaration with no type, which the compiler rejects. It is worth reading to the very end of a listing before answering.

Please log in or register to report this question.

45.

Which of the following is an example of static memory allocation?

Answer: D

Correct answer: Array

An array declared with a fixed size has its storage settled at compile time, in the stack frame or the data segment according to where it is declared. Nothing is requested at run time and nothing is freed.

The other three are dynamic structures built from nodes obtained with malloc as they are needed. A stack and a queue can be implemented over a fixed array, but as abstract structures they grow and shrink, which is what places them on the dynamic side of the distinction.

Please log in or register to report this question.

Answer: C

Correct answer: First In First Out (FIFO)

A queue serves elements in the order they arrived: the first item added is the first removed. Insertion happens at one end, the rear, and removal at the other, the front.

That is the opposite of a stack, which is last in first out and does both operations at the same end. The distinction is not merely academic - it decides which structure suits a problem, with queues natural for scheduling and buffering and stacks natural for recursion and expression evaluation.

Please log in or register to report this question.

Answer: D

Correct answer: “INT x” would define x of type int. “INTPTR *y” would define pointer y of type int **. “ONEDARR z” would define z as array of 10 int. “TWODARR t” would define t as array of 10 by 10 int.

One typedef may carry several declarators, exactly as an ordinary declaration may, and each gets its own derived type from the same base int. So all four aliases are created. This is the same rule that makes int a, *b, c[10]; legal - typedef changes what the declared names mean, not how the declaration is parsed.

Please log in or register to report this question.

48.

What will be the output of the following C code?

#include <stdio.h>
    int *(*(x()))[2];
    typedef int **(*ptrfoo)())[2];
    int main()
    {
        ptrfoo ptr1;
        ptr1 = x;
        ptr1();
        return 0;
    }
    int *(*(x()))[2]
    {
        int (*ary)[2] = malloc(sizeof * ary);
        return &ary;
    }

 

Answer: B

Correct answer: Compile time error

The typedef line is syntactically invalid: typedef int **(*ptrfoo)())[2]; has unbalanced parentheses, the () and the [2] both trying to attach after the declarator has already been closed. Compiling it produces an error at that line before execution is ever reached, and a second error follows on the incompatible assignment ptr1 = x;.

Please log in or register to report this question.

49.

What will be the output of the following C code?

#include <stdio.h>
    int (*(x()))[2];
    typedef int (*(*ptr)())[2] ptrfoo;
    int main()
    {
        ptrfoo ptr1;
        ptr1 = x;
        ptr1();
        return 0;
    }
    int (*(x()))[2]
    {
        int (*ary)[2] = malloc(sizeof*ary);
        return &ary;
    }

 

Answer: A

Correct answer: Compile time error

The line typedef int (*(*ptr)())[2] ptrfoo; is malformed. A typedef declares the new name inside the declarator, in the position a variable name would occupy - here that position is already taken by ptr, so the trailing ptrfoo is a second declarator with nothing joining it. The compiler rejects the declaration before any of the rest matters.

Please log in or register to report this question.

Answer: A

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

The parentheses around *PFI bind the asterisk to the name before the function parameter list is applied, so the name is a pointer first and a pointer to a function second. Such aliases are the standard way of making callback tables readable: PFI compare; is a good deal clearer than writing the full declarator each time.

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