5.2.6 - Typedef.
101. What is the purpose of the typedef keyword in C?
- (A) To create a new variable
- (B) To provide existing data types with meaningful names
- (C) To declare functions
- (D) To allocate memory
Correct Answer: (B) To provide existing data types with meaningful names
102. How do you define a new alias for an existing data type using typedef?
- (A) typedef alias_name existing_name;
- (B) typedef existing_name alias_name;
- (C) typedef existing_name as alias_name;
- (D) alias_name typedef existing_name;
Correct Answer: (B) typedef existing_name alias_name;
103. Which of the following correctly defines an alias for unsigned int as unit?
- (A) typedef unsigned int unit;
- (B) typedef unit unsigned int;
- (C) unsigned int typedef unit;
- (D) unsigned int unit;
Correct Answer: (A) typedef unsigned int unit;
104. What is the output of the following code?
typedef struct {
int x;
int y;
} point;
point p = {10, 20};
printf("%d %d", p.x, p.y);
- (A) 10 20
- (B) 0 0
- (C) Compilation error
- (D) 20 10
Correct Answer: (A) 10 20
105. What does typedef struct students { ... } stu; achieve?
- (A) It creates a structure called students.
- (B) It creates an alias stu for the structure students.
- (C) It defines a function stu.
- (D) It declares a variable stu.
Correct Answer: (B) It creates an alias stu for the structure students.
106. In the statement typedef int* ptr;, what does ptr represent?
- (A) A function
- (B) A data type
- (C) A pointer to an integer
- (D) An array of integers
Correct Answer: (C) A pointer to an integer
107. How can typedef improve code readability when using structures?
- (A) By shortening the code
- (B) By avoiding the use of struct repeatedly
- (C) By changing the data type
- (D) By defining functions
Correct Answer: (B) By avoiding the use of struct repeatedly
108. What is the output of the following code snippet?
typedef int Arr[4];
Arr temp = {1, 2, 3, 4};
printf("%d", temp[2]);
Correct Answer: (C) 3
109. Which statement is true about typedef compared to #define?
- (A) typedef can define constants, while #define cannot.
- (B) typedef can create new types, while #define cannot.
- (C) typedef requires a semicolon at the end, while #define does not.
- (D) All of the above
Correct Answer: (D) All of the above
110. Which of the following is NOT a valid use of typedef?
- (A) typedef int x;
- (B) typedef int y[5];
- (C) typedef struct { int a; } z;
- (D) typedef #define MAX 100;
Correct Answer: (D) typedef #define MAX 100;
111. What is the main difference between typedef and #define?
- (A) typedef is for defining constants, while #define is for data types.
- (B) typedef defines data types, while #define defines constants.
- (C) There is no difference; they are interchangeable.
- (D) typedef requires arguments, while #define does not.
Correct Answer: (B) typedef defines data types, while #define defines constants.
112. How would you declare a pointer to a structure using typedef?
- (A) typedef struct { ... } *ptr;
- (B) typedef struct { ... } ptr;
- (C) typedef struct { ... } &ptr;
- (D) typedef struct *ptr;
Correct Answer: (A) typedef struct { ... } *ptr;
113. What is the output of the following code?
typedef int* ptr;
ptr p1, p2;
int x = 10;
p1 = &x;
printf("%d", *p1);
- (A) 10
- (B) 0
- (C) Compilation error
- (D) Undefined behavior
Correct Answer: (A) 10
114. When using typedef with arrays, what does it allow you to do?
- (A) Create multiple array types
- (B) Declare multiple variables of the same type in one line
- (C) Allocate memory for arrays
- (D) None of the above
Correct Answer: (B) Declare multiple variables of the same type in one line
115. How would you declare an array of 10 integers using typedef?
- (A) typedef int arr[10];
- (B) typedef int[10] arr;
- (C) typedef arr int[10];
- (D) int arr[10];
Correct Answer: (A) typedef int arr[10];
116. Which statement about typedef is FALSE?
- (A) It can simplify complex declarations.
- (B) It can create type aliases.
- (C) It can be used to define constants.
- (D) It can improve code readability.
Correct Answer: (C) It can be used to define constants.
117. Can you use typedef to create an alias for a function pointer?
- (A) Yes
- (B) No
- (C) Only in C++
- (D) Only for specific data types
Correct Answer: (A) Yes
118. What will happen if you do not include a semicolon at the end of a typedef declaration?
- (A) It will compile successfully
- (B) It will cause a syntax error
- (C) It will be ignored
- (D) It will create a default type
Correct Answer: (B) It will cause a syntax error
119. Which of the following is a valid definition of a typedef for a function that returns an integer and takes two integers as arguments?
- (A) typedef int func(int, int);
- (B) typedef int (*func)(int, int);
- (C) typedef int func(int);
- (D) typedef int func[];
Correct Answer: (B) typedef int (*func)(int, int);
120. How do you declare a variable st of type stu, where stu is defined using typedef for a structure?
- (A) stu st;
- (B) typedef stu st;
- (C) struct st;
- (D) struct stu;
Correct Answer: (A) stu st;