Structures, Unions, Enums Questions
Practice Structures, Unions, Enums MCQs with answers and explanations. Page 1 of 3.
Category
C Programming
Topic
Structures, Unions, Enums
Page
1 / 3
Mode
Practice
Questions
Open any question to view the answer and explanation.
On a 16-bit DOS/Turbo C platform where pointers are 2 bytes in small/near memory models, consider the following node structure allocation. What are the sizes printed for the pointer variables p and q?
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct node
{
int data;
struct node *link;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
printf("%d, %d
", sizeof(p), sizeof(q));
return 0;
}
Open
View answer
In C, a union shares storage among its members. For the union below, values are assigned sequentially to a and b. What will be printed when v.a is output?
#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a = 10;
v.b = 20;
printf("%d
", v.a);
return 0;
}
Open
View answer
On a 16-bit Turbo C/DOS platform, consider a struct with three bit-fields declared with base type int. What is sizeof the struct value instance?
#include<stdio.h>
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
} bit;
printf("%d
", sizeof(bit));
return 0;
}
Open
View answer
In C, enum constants are not modifiable lvalues. For the enumeration below with explicit values, what will happen if we attempt to pre-increment MON inside printf?
#include<stdio.h>
int main()
{
enum days { MON = -1, TUE, WED = 6, THU, FRI, SAT };
printf("%d, %d, %d, %d, %d, %d
", ++MON, TUE, WED, THU, FRI, SAT);
return 0;
}
Open
View answer
Work with an array of structs and pointer arithmetic on arrays. What output is produced by accessing c[1].courseno and (*(c + 2)).coursename in the code below?
#include<stdio.h>
int main()
{
struct course
{
int courseno;
char coursename[25];
};
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };
printf("%d ", c[1].courseno);
printf("%s
", (*(c+2)).coursename);
return 0;
}
Open
View answer
Evaluate bitwise precedence and grouping. For i = 4 and j = 8, what is printed by the following expression sequence using & (AND), ^ (XOR), and | (OR)?
#include<stdio.h>
int main()
{
int i = 4, j = 8;
printf("%d, %d, %d
", i | j & j | i, i | j & j | i, i ^ j);
return 0;
}
Open
View answer
In C programming, determine the output of this program using a union of int and char[2].
#include <stdio.h>
int main()
{
union A {
int i;
char ch[2];
};
union A u;
u.ch[0] = 3;
u.ch[1] = 2;
printf("%d, %d, %d
", u.ch[0], u.ch[1], u.i);
return 0;
}
Open
View answer
On a 16-bit platform, what is sizeof for this enumeration variable?
#include <stdio.h>
int main()
{
enum value { VAL1 = 0, VAL2, VAL3, VAL4, VAL5 } var;
printf("%d
", (int)sizeof(var));
return 0;
}
Open
View answer
In C bit-fields, predict the printed values when using 1-bit and 4-bit signed fields.
#include <stdio.h>
int main()
{
struct value {
int bit1 : 1;
int bit3 : 4;
int bit4 : 4;
} bit = { 1, 2, 13 };
printf("%d, %d, %d
", bit.bit1, bit.bit3, bit.bit4);
return 0;
}
Open
View answer
In C, evaluate the values printed for enumerators with explicit and implicit assignments.
#include <stdio.h>
int main()
{
enum days { MON = -1, TUE, WED = 6, THU, FRI, SAT };
printf("%d, %d, %d, %d, %d, %d
", MON, TUE, WED, THU, FRI, SAT);
return 0;
}
Open
View answer
In C bit-fields, what is printed for a 1-bit signed field initialized to 1?
#include <stdio.h>
int main()
{
struct byte {
int one : 1;
};
struct byte var = { 1 };
printf("%d
", var.one);
return 0;
}
Open
View answer
Turbo C (DOS): effect of structure assignment with a char* and using strupr on the copy.
#include <stdio.h>
#include <string.h>
int main()
{
struct emp {
char n;
int age;
};
struct emp e1 = { "Dravid", 23 };
struct emp e2 = e1;
strupr(e2.n);
printf("%s
", e1.n);
return 0;
}
Open
View answer
Enumerations in C: determine the printed integral values for three students’ statuses.
#include <stdio.h>
int main()
{
enum status { pass, fail, absent };
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d
", stud1, stud2, stud3);
return 0;
}
Open
View answer
C typedef and self-referential pointers: identify whether this declaration is valid.
typedef struct data mystruct;
struct data
{
int x;
mystruct b;
};
Open
View answer
C structures: is this self-referential member declaration valid or an error?
struct emp
{
int ecode;
struct emp e;
};
Open
View answer
C unions and initializers: identify the error in the following union initializations.
#include <stdio.h>
int main()
{
union a {
int i;
char ch[2];
};
union a z1 = { 512 };
union a z2 = { 0, 2 };
return 0;
}
Open
View answer
C programming — identify the compile-time issue in this bit-field example.
#include<stdio.h>
int main()
{
struct bits
{
/* Attempting a bit-field on a floating type */
float f:2;
} bit;
printf("%d", (int)sizeof(bit));
return 0;
}
Which option best describes the problem (if any)?
Open
View answer
C programming — identify the issue (if any) in this structure input loop.
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i = 0; i <= 9; i++)
scanf("%s %f", e[i].name, &e[i].sal);
return 0;
}
Does this code have a language/compilation error?
Open
View answer
C programming — determine whether these two structs can be compared with ==.
#include<stdio.h>
int main()
{
struct emp
{
char n[20];
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
if (e1 == e2)
printf("The structures are equal");
return 0;
}
What is the correct assessment?
Open
View answer
C programming — find the error in assigning to a char array member of a struct.
#include<stdio.h>
int main()
{
struct emp
{
char name[25];
int age;
float bs;
};
struct emp e;
e.name = "Suresh"; /* assignment to array member */
e.age = 25;
printf("%s %d", e.name, e.age);
return 0;
}
What is the correct diagnosis?
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.