Sample questions
Question 1Medium
What is the output of this C code?
#include <stdio.h>
typedef struct p
{
int x, y;
}k;
int main()
{
struct p p = {1, 2};
k k1 = p;
printf("%d\n", k1.x);
}
- Compile time error
- 1
- 0
- Depends on the standard
Answer: B. 1
Explanation
User define Data type Alias works same as user define data type, and unlike array it is valid to assignone instance of structure to other structure.
Step-by-step solution
- The code defines a struct p with two integers x and y.
- It then declares an instance of p initialized with values {1, 2}.
- A new type k is defined as an alias for struct p.
- Instance k1 of type k is assigned the value of p.
- k1.x is accessed and printed.
Why the other options are wrong
- A. The code compiles and runs without errors.
- C. k1.x is assigned the value 1 from p.
- D. The standard defines that type aliases work as expected.
Tip: Remember that type aliases in C are just alternative names for existing types.
Question 2Medium
- Does not create a new type
- It merely adds a new name for some existing type.
- Both a & b
- None of the mentioned
Answer: C. Both a & b
Explanation
a keyword called typedef, which you can use to give a type a new name and You can use typedef to give aname to user defined data type as well.
For example you can use typedef with structure to define a new data type and then use that data type to define structure variables directly
Step-by-step solution
- A typedef gives a new name to an existing type, such as typedef int myint.
- Because it only gives a name, it does not create a new type, since myint and int are the same type.
- So statement a is true, and statement b is also true.
- So the answer is Both a & b.
Why the other options are wrong
- A. Statement a is true, but statement b is also true, so this is incomplete.
- B. Statement b is true, but statement a is also true, so this is incomplete.
- D. Both statements are true, so none of the mentioned is wrong.
Tip: A typedef is only an alias, so the type stays the same.
Question 3Medium
The correct syntax to use typedef for struct is.
a) typedef struct temp
{
int a;
}TEMP;
b) typedef struct
{
int a;
}TEMP;
c) struct temp
{
int a;
};
typedef struct temp TEMP;
d) All of the mentioned
- a
- b
- c
- d
Answer: D. d
Explanation
All three forms are valid C. Option a names the struct and typedefs it, option b uses an unnamed struct, and option c declares the struct first and then typedefs it. So all of the mentioned options are correct.
Step-by-step solution
- Option a is valid: it names the struct temp and gives TEMP as a typedef name.
- Option b is valid: an anonymous struct can be given a typedef name.
- Option c is valid: the struct is declared first, then typedef gives it the name TEMP.
- All three are correct, so the answer is option D: All of the mentioned.
Why the other options are wrong
- A. Option a is valid, but b and c are also valid.
- B. Option b is valid, but a and c are also valid.
- C. Option c is valid, but a and b are also valid.
Tip: Typedef can name a struct with or without a tag.
Question 4Medium
What is the output of this C code?
#include <stdio.h>
typedef struct student
{
char *a;
}stu;
void main()
{
struct student s;
s.a = "hey";
printf("%s", s.a);
}
- Compile time error
- Varies
- he
- hey
Answer: D. hey
Explanation
char *a
is a pointer variable it will easily store the string So hey will be output of this program.
Step-by-step solution
- The code defines a struct student with a char pointer
a. - In main, an instance of student is created and
a is assigned the string "hey". - printf("%s", s.a); prints the value pointed to by
a. - Since
a points to the string "hey", it will be printed.
Why the other options are wrong
- A. The code compiles and runs without errors.
- B. The output is always the same for this input.
- C. The string "hey" is fully printed, not just part of it.
Tip: Remember that strings in C are stored as arrays of characters ending with '\0'.
Question 5Medium
For the following expression to work,which option should be selected.
string p = "HELLO";
typedef char [] string;
typedef char *string;
- Both (a) and (b)
- Such expression cannot be generated in C
Answer: B.
Explanation
In option Bst size of array is unknown,so correct answer is option b.
Step-by-step solution
- The declaration
string p = a string literal needs string to be a type name for a pointer to char. - ```c
Typedef char *string;
```
makes
string a name for char *, which can hold the address of a string literal. - ```c
Typedef char [] string;
```
is not valid, because an array type needs a size.
- So the answer is
```c
typedef char *string;
```
Why the other options are wrong
- A. char [] has no size, so this typedef is not valid in C.
- C. Option A is wrong, so both (a) and (b) is wrong.
- D. A typedef can give the name string to a pointer type, so the declaration can be written.
Tip: To make a string type in C, typedef a pointer to char.
Question 6Medium
What is the output of this C code?
#include <stdio.h>
typedef struct student
{
char *a;
}stu;
void main()
{
struct stu s;
s.a = "hi";
printf("%s", s.a);
}
- Compile time error
- Varies
- hi
- h
Answer: A. Compile time error
Explanation
struct stu s;
This is invalid statement because above code create alias of structure stu.
It is invalid to write struct keyword with alias.
Step-by-step solution
- Typedef gives the name stu to the anonymous struct type that has the member a.
- The declaration struct stu s; uses a struct tag called stu, but the tag in this program is student.
- So the compiler reports an unknown struct tag before the program runs.
- So the answer is Compile time error.
Why the other options are wrong
- B. The output does not vary, because the compile fails before the program runs.
- C. Hi would print only if the declaration were stu s;, but the program does not compile as written.
- D. H would be a single character, and the code never reaches printf.
Tip: A typedef name is used alone, while the struct keyword needs the tag name, such as struct student.
Question 7Medium
What is the output of this 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;
}
- Compile time error
- Nothing
- Undefined behaviour
- Depends on the standard
Answer: A. Compile time error
Explanation
The typedef line typedef int **(*ptrfoo)())[2]; has an unbalanced parenthesis and is not valid C, so the code fails to compile.
Step-by-step solution
- Check the typedef declaration syntax.
- int **(*ptrfoo)())[2] has an extra closing parenthesis.
- The code cannot compile: Compile time error.
Why the other options are wrong
- B. Nothing would print only if it compiled and ran.
- C. A syntax error is diagnosed at compile time, not undefined behaviour.
- D. The syntax error is independent of the standard version.
Tip: Count the parentheses in complicated declarations before reasoning about runtime.
Question 8Medium
What is the output of this 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;
}
- Compile time error
- Undefined behaviour
- Depends on the standard
- 10
Answer: D. 10
Explanation
The code defines an alias integer for the int data type. The variable j is declared as integer, which is equivalent to int. Since ptr is a pointer to j, it points to an integer value, and dereferencing ptr gives the value of j, which is 10.
Step-by-step solution
- I is an int with value 10.
- J is an integer (alias for int) with value of i, so j = 10.
- Ptr is a pointer to int, initialized to address of j.
- *ptr dereferences ptr to get the value at its address, which is 10.
Why the other options are wrong
- A. The code compiles and runs without errors.
- B. There's no undefined behavior in this code.
- C. The output is consistent across different standards.
Tip: Aliases for data types work just like the real types.
Question 9Medium
Which of the given option is the correct method for initialization?
typedef char *string;
*string *p = "Hello";
string p = "Hello";
*string p = "A";
- None of the mentioned
Answer: B.
Explanation
typedef char *string;
This expression declare alias of string pointer in option a .and b again declare pointer of string this invalid declaration because of we cant create pointer of alias.
Step-by-step solution
- Typedef char *string makes string a new name for the type char *.
- So string p declares p as a char pointer.
- The initialization string p = "Hello" is valid, because string is a type name.
- Lines starting with *string are invalid, because string is a type, not a pointer variable.
- The correct initialization is string p = "Hello";.
Why the other options are wrong
- A. *string is malformed because string is a type name, not a pointer variable.
- C. *string p is malformed for the same reason, so the initialization is invalid.
- D. Option B is valid, so None of the mentioned is wrong.
Tip: After a typedef, use the new name as a type, with no extra * in front of it.
Question 10Medium
What is the output of this 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;
}
- Compile time error
- Nothing
- Undefined behaviour
- Depends on the standard
Answer: A. Compile time error
Explanation
In function 'x':
warning:
incompatible implicit declaration of built-in function 'malloc'
[enabled by default]
int (*ary)[2] = malloc(sizeof*ary);
return from incompatible pointer type
[enabled by default]
return &ary;
warning: function returns address of local variable [-Wreturn-local-addr]
Step-by-step solution
- The typedef line names the declarator ptr, and then gives another name ptrfoo after the declarator.
- A typedef must put the new type name inside the declarator, so the second name is a syntax error.
- A syntax error stops compilation, so nothing runs.
- So the answer is Compile time error.
Why the other options are wrong
- B. Nothing would run, since the code does not compile.
- C. Undefined behavior needs the code to compile first.
- D. The error is a fixed rule of the language, so the standard does not change it.
Tip: In a typedef, the new name goes exactly where a variable name would go.
Practise all 11 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →