Font size
WorksheetsQuiz on Structures in C
Total questions: 99
Worksheet time: 50mins
Which keyword is used to define a structure in C?
struct
class
typedef
enum
What does a structure in C allow you to do?
Group related variables of different data types under a single name
Store only integer values
Create only arrays
Allocate memory for functions
What is the role of StructureName in a structure definition in C?
It is the identifier of the structure
It is a keyword in C
It is a function name
It is a variable name
When is memory allocated for a structure in C?
When a variable of that structure is created
When the structure is defined
When the program starts
When the compiler is installed
Given the following structure definition, how would you declare two variables of this structure in C?
struct Student student1, student2;
Student student1, student2;
struct student1, student2;
int student1, student2;
Which of the following is a correct way to declare structure variables at the end of a structure definition in C?
struct Student { int roll_number; char name[20]; float percentage; } s1, s2;
struct Student { int roll_number; char name[20]; float percentage; };
struct Student s1, s2 { int roll_number; char name[20]; float percentage; };
struct Student { int roll_number; char name[20]; float percentage; } = s1, s2;
Why is it preferred to declare structure variables separately when multiple variables are required?
It allows structure variables to be declared outside the structure definition.
It makes the code run faster.
It reduces the memory usage of the program.
It prevents syntax errors in C.
Given the following structure definition, which of the following correctly declares two structure variables separately? struct Student { int roll_number; char name[20]; float percentage; };
struct Student s1, s2;
Student s1, s2;
struct s1, s2;
struct Student { s1, s2; };
What data types are used for the members of the 'Student' structure in the provided example?
int, char array, float
float, int, double
char, int, float
int, char, double
Suppose you want to declare three variables of a structure named 'Employee' with members 'id' (int), 'name' (char[30]), and 'salary' (float). Which is the correct way to declare them separately?
struct Employee emp1, emp2, emp3;
Employee emp1, emp2, emp3;
struct Employee { emp1, emp2, emp3; };
struct emp1, emp2, emp3;
Which of the following is TRUE about initializing structure members inside the structure definition in C?
A) Structure members can be initialized inside the structure definition without any error.
B) Structure members cannot be initialized inside the structure definition; it causes a compiler error.
C) Structure members can only be initialized if they are integers.
D) Structure members can be initialized only if the structure is global.
What is the correct way to initialize the members of a structure in C?
A) By assigning values directly inside the structure definition.
B) By creating a structure variable and then assigning values to its members.
C) By using the 'init' keyword inside the structure.
D) By declaring the structure as static.
Given the following structure definition, which line of code correctly initializes the roll_number member? struct student { int roll_number; char name[20]; float percentage; };
A) int roll_number = 10;
B) student.roll_number = 10;
C) s1.roll_number = 10;
D) roll_number := 10;
Why is memory not allocated to a structure’s data members when the structure is defined?
A) Because the structure is not a variable.
B) Because memory is only allocated when a structure variable is declared.
C) Because the compiler ignores structure definitions.
D) Because only global variables are allocated memory.
Suppose you try to initialize a structure member inside the structure definition. What will happen?
A) The program will run successfully.
B) The compiler will throw an error.
C) The member will be initialized to zero.
D) The member will be initialized to a random value.
Consider the following code snippet: struct student { int roll_number=10; float percentage=66.7; }; What is the result of compiling this code?
A) The code compiles and runs successfully.
B) The code compiles but does not run.
C) The code throws a compilation error.
D) The code initializes the members to zero.
How can you modify the values of a structure’s members after declaring a structure variable?
A) By accessing the members using the structure variable and assigning new values.
B) By redefining the structure.
C) By using the 'update' keyword.
D) By declaring the members as global variables.
Which operator is used to access members of a structure variable in C?
Dot operator (.)
Comma operator (,)
Semicolon operator (;)
Colon operator (:)
What is the correct way to initialize a structure variable using normal assignment in C?
s1.roll_number = 101; strcpy(s1.name, "Kumar"); s1.percentage = 74.6;
s1 = {101, "Kumar", 74.6};
s1->roll_number = 101; strcpy(s1->name, "Kumar"); s1->percentage = 74.6;
s1.roll_number == 101; strcpy(s1.name, "Kumar"); s1.percentage == 74.6;
Which of the following is an example of initializing a structure variable at the time of declaration in C?
struct student s1 = {101, "Kumar", 74.5};
s1.roll_number = 101;
strcpy(s1.name, "Kumar");
s1.percentage = 74.6;
Explain the difference between normal assignment and initialization at the time of declaration for structure variables in C. (DoK Level 2)
Normal assignment sets values after declaration, while initialization assigns values during declaration.
Both methods assign values during declaration.
Normal assignment is only used for arrays.
Initialization at the time of declaration cannot be used for structures.
Given the structure definition below, which statement correctly assigns the value "Kumar" to the name member of the structure variable s1? (DoK Level 2) struct Student { int roll_number; char name[20]; float percentage; };
strcpy(s1.name, "Kumar");
s1.name = "Kumar";
s1->name = "Kumar";
s1.name == "Kumar";
A student wants to create a structure variable and initialize all its members at once. Which method should they use? (DoK Level 3)
Initialization at the time of declaration
Normal assignment after declaration
Using only the dot operator
Using only the arrow operator
Which syntax is used to access a member of a structure by its variable in C?
structVariable -> structMember;
structVariable . structMember;
structVariable : structMember;
structVariable * structMember;
How are structure members stored in memory in C?
Random memory locations
Contiguous (adjacent) memory locations
Non-contiguous memory locations
Only in registers
In the given structure example, how many bytes does the 'name' member occupy?
4 bytes
8 bytes
16 bytes
1 byte
What is the total memory occupied by the structure 'student' in the example provided?
8 bytes
12 bytes
16 bytes
20 bytes
Why are nested structures used in C programming?
To increase the speed of execution
To allow the creation of more complex and organized data structures
To reduce memory usage
To avoid using arrays
Given a structure with an int, a char array of size 8, and a float, explain how the memory is allocated for each member and calculate the total memory used.
int: 2 bytes, char array: 8 bytes, float: 2 bytes, total: 12 bytes
int: 4 bytes, char array: 8 bytes, float: 4 bytes, total: 16 bytes
int: 4 bytes, char array: 4 bytes, float: 4 bytes, total: 12 bytes
int: 8 bytes, char array: 8 bytes, float: 8 bytes, total: 24 bytes
Which of the following is NOT a benefit of using nested structures in C?
A) Logical grouping of related data
B) Improves code readability and maintainability
C) Data re-usability
D) Increases code execution speed
What are the two primary types of nested structures in C?
A) Direct and Indirect Structure
B) Embedded and Separate Structure
C) Inline and Outline Structure
D) Static and Dynamic Structure
In the context of embedded structures in C, what does direct nesting (named nesting) refer to?
A) Defining the inner structure outside the outer structure
B) Defining the inner structure directly inside the outer structure and giving it a name
C) Using pointers to link two structures
D) Declaring structures without any members
Which statement about inner structure access in direct nesting is correct?
A) Inner structure cannot be accessed via the outer structure
B) Inner structure can only be accessed anonymously
C) Inner structure can be accessed via the outer structure
D) Inner structure must be defined outside the outer structure
Given the syntax for direct nesting in C, what is the purpose of 'inner_stru_var_name'?
A) It is the name of the outer structure
B) It is a variable of the inner structure
C) It is a function to access data members
D) It is a pointer to the outer structure
Why might a programmer choose to use embedded structures in their code?
A) To increase the number of global variables
B) To enhance code organization and readability
C) To reduce the number of functions
D) To avoid using data members
Which header file is included at the beginning of the provided C source code for structured programming?
In the given C code, what is the name of the structure that is directly nested inside the 'Student' structure?
Address
Location
City
Place
What is the purpose of the 'scanf("%d", &s.roll);' statement in the provided code?
To read the roll number from user input
To print the roll number
To assign a default value to roll number
To read the name from user input
Which of the following correctly describes the type of nesting used in the 'Student' structure in the provided code?
Direct named nesting
Anonymous nesting
Indirect nesting
Pointer-based nesting
Suppose you want to add a new field 'state' to the address structure in the provided code. Where should you add it?
Inside the struct Address, before the closing curly brace
Outside the struct Student
After the return statement in main()
Before the #include statement
If a student enters the following details: Roll Number = 101, Name = "John", City = "Hyderabad", Pincode = 500001, what will be printed for the city in the output?
Hyderabad
John
101
500001
Which of the following best describes an anonymous nested structure in C?
A structure defined inside another structure without a specific name for the inner structure.
A structure defined outside any other structure with a specific name.
A structure that only contains integer data members.
A structure that cannot be accessed from the outer structure.
In the provided example, what are the members of the anonymous inner structure within the Student structure?
roll and name
city and pincode
addr and roll
name and addr
What is the purpose of using an anonymous nested structure in C programming?
To allow direct access to inner structure members through the outer structure's instance.
To prevent access to inner structure members.
To make the code more complex and harder to read.
To avoid using any data members in the structure.
Given the following code snippet, what is the correct way to access the city member of the anonymous inner structure? struct Student { int roll; char name[50]; struct { char city[30]; int pincode; } addr; };
s.city
s.addr.city
s.name.city
s.roll.city
Suppose you want to add a new member 'state' to the anonymous inner structure in the Student structure. Which of the following is the correct way to declare it?
struct { char city[30]; int pincode; char state[20]; } addr;
struct { char state[20]; } addr;
struct Student { char state[20]; };
struct { int roll; char state[20]; } addr;
Which of the following best describes a "separate structure" approach in nested structures in C programming?
A) The inner structure is defined directly inside the outer structure.
B) The inner structure is defined independently and included as a member in the outer structure.
C) The outer structure is defined inside the inner structure.
D) Both structures are defined together in a single block.
In the syntax for separate structures, how is the inner structure included in the outer structure?
A) By declaring it as a pointer.
B) By defining it inside the outer structure.
C) By including it as a member of the outer structure.
D) By using a typedef.
What is the main difference between "separate structure" and "embedded structure" in C programming?
A) Separate structure uses pointers, embedded structure does not.
B) Separate structure defines the inner structure independently, embedded structure defines it inside the outer structure.
C) Embedded structure uses arrays, separate structure does not.
D) Embedded structure is used only for functions, separate structure is used for variables.
Given the following code snippet, what is the name of the structure that stores the city and pin information? struct Address { char city[20]; int pin; };
A) Employee
B) Address
C) CityPin
D) Location
If you want to store and display employee details using separate structures in C, which header file must be included?
A)
B)
C)
D)
Why might a programmer choose to use separate structures instead of embedded structures in C?
A) To make the code less readable.
B) To allow reuse of the inner structure in multiple outer structures.
C) To reduce the number of variables.
D) To avoid using functions.
Which of the following is a correct way to declare a structure in C that contains an array and an integer?
struct Student { int roll; char name[20]; int marks[5]; };
struct Student { int roll; int marks[5]; char name[20]; };
struct Student { char name[20]; int roll; int marks[5]; };
struct Student { int marks[5]; char name[20]; int roll; };
What is the purpose of using a structure in C programming?
To store only one type of data
To store multiple elements of different data types in a single variable
To perform mathematical operations
To create loops
In the provided code, which member of the struct Employee is itself a structure?
id
name
addr
pin
Why might you use an array within a structure in C?
To store only one value
To store multiple values of the same type together with other types
To increase the speed of the program
To avoid using functions
Given the following code snippet, what will be printed if the user enters 101 for ID, "John" for name, "NYC" for city, and 12345 for pin?
ID: 101, Name: John, City: NYC, Pin: 12345
ID: John, Name: 101, City: NYC, Pin: 12345
ID: NYC, Name: John, City: 101, Pin: 12345
ID: 12345, Name: John, City: NYC, Pin: 101
Which header file is necessary to use printf and scanf functions in C?
What is the purpose of using an array of structures in C?
To store multiple structure variables together in a single array
To store only integer values
To store only floating-point values
To store only character values
Which of the following is the correct way to declare a structure named 'Student' in C?
struct Student { int id; char name[20]; };
struct Student ( int id; char name[20]; )
Student struct { int id; char name[20]; }
struct Student: int id, char name[20];
In the provided C code, what does the following line do? scanf("%s", s1.name);
Reads a string input and stores it in the 'name' field of structure s1
Prints the name of the student
Reads an integer input and stores it in the 'name' field of structure s1
Initializes the 'name' field of structure s1 to NULL
How many marks does the program ask the user to enter for each student?
5
10
3
1
How would you use an array of structures to store information for multiple students in C?
By declaring multiple integer arrays for each student
By declaring an array of 'Student' structures and storing each student's data in a separate element of the array
By using only global variables
By using a single structure for all students
Which of the following best describes a union in C programming?
A data type that allows storing multiple values of different data types at the same time.
A user-defined data type that allows storing multiple values with different data types in the same memory location, but only one member can contain a value at any given time.
A built-in data type that stores only integer values.
A data type that allows storing only one value at a time.
What is the main advantage of using a union in C?
It allows storing multiple values of the same type.
It provides an efficient way of using the same memory location for multiple purposes.
It increases the memory usage for each variable.
It restricts the number of members to one.
Given the following syntax, what does 'union_variable' represent? union union_name { datatype member-1; datatype member-2; ... datatype member-n; } union_variable;
The name of the union type.
The name of a member inside the union.
The variable of the union type.
The datatype of the union.
In the provided C code, what is the purpose of the for loop?
To declare an array of structures.
To display the details of each student in the array.
To calculate the average marks of students.
To initialize the structure members.
Why can only one member of a union contain a value at any given time?
Because all members share the same memory location.
Because unions are only for integer values.
Because unions do not support multiple members.
Because each member has its own memory location.
How would you declare a union with three members: an integer, a float, and a character in C?
union myUnion { int a; float b; char c; } myVar;
struct myUnion { int a; float b; char c; } myVar;
union myUnion ( int a, float b, char c ) myVar;
union myUnion { int a, float b, char c; } myVar;
If you assign a value to one member of a union, what happens to the values of the other members?
They remain unchanged.
They are overwritten or become undefined.
They are set to zero.
They are copied to the assigned member.
Which keyword is used to define a union in C programming?
union
struct
class
enum
In a union, how is memory allocated for its members?
Memory is allocated for each member separately.
Memory is allocated for the member that requires the highest memory.
Memory is allocated for the smallest member.
Memory is allocated equally for all members.
What is the correct syntax for declaring a variable of union type in C?
union union_name var1, var2, ... varn;
struct union_name var1, var2, ... varn;
class union_name var1, var2, ... varn;
enum union_name var1, var2, ... varn;
Given the union declaration: union Student { int rno; char sname[100], cname[50]; }; What will be the size of the memory location allocated for a variable of this union?
50 bytes
100 bytes
150 bytes
4 bytes
Why does the variable 's' of union Student only allocate one memory location?
Because unions allocate memory for the largest member only.
Because unions allocate memory for all members separately.
Because unions do not require memory allocation.
Because unions allocate memory for the smallest member only.
In the provided C program, what is the purpose of the statement 'scanf("%d",&s.rno);'?
To print the value of rno.
To assign a value to rno from user input.
To declare the variable rno.
To allocate memory for rno.
If you want to access the member 'cname' in the union 'student', which syntax should you use?
student.cname
s.cname
student->cname
s->cname
Explain why only one member of a union can be accessed at a time.
Because all members share the same memory location.
Because each member has its own memory location.
Because unions do not support multiple members.
Because unions are similar to arrays.
Which keyword is used to define a structure in C?
struct
union
class
object
What is the main difference in memory allocation between a structure and a union in C?
Structure members have separate memory, while union members share the same memory.
Structure members share the same memory, while union members have separate memory.
Both structure and union members have separate memory.
Both structure and union members share the same memory.
In a union, how many members can hold a value at a time?
Only one member
All members
Two members
None
Which feature allows all members of a structure to be accessed at the same time?
Independent access
Simultaneous access
Single access
Sequential access
When is it useful to use a structure in C programming?
When storing information about an entity with multiple attributes
When storing a value that may be of different types at different times
When only one value needs to be stored
When memory-saving is not required
What is the correct syntax for declaring a structure variable in C?
Struct StructureName { Datatype member1; Datatype member2; Datatype memberN; }structure_variable;
union StructureName { Datatype member1; Datatype member2; Datatype memberN; }union_variable;
class StructureName { Datatype member1; Datatype member2; Datatype memberN; }class_variable;
object StructureName { Datatype member1; Datatype member2; Datatype memberN; }object_variable;
Why might a union be preferred over a structure in certain situations?
To save memory when storing different types at different times
To store multiple attributes of an entity
To allow all members to be accessed independently
To initialize all members at once
Which member of a union can be initialized at declaration?
Only the first member
All members
Only the last member
None
Given the following code snippet, what will be the output if the student name entered is "Raja" and the cname entered is "CSE"? printf("Enter student name : "); scanf("%s",&s.sname); printf("Sname=%s\n",s.sname); printf("Enter cname : "); scanf("%s",&s.cname); printf("Cname=%s\n",s.cname);
Sname=Raja Cname=CSE
Sname=Raja Cname=Raja
Sname=CSE Cname=Raja
Sname=Raja Cname=Student
Memory allocation differs between structures and unions in C. Which of the following statements best explains this difference and provides a scenario where it is significant?
Structures allocate separate memory for each member, while unions share the same memory among all members. This is significant when memory usage is a concern, such as in embedded systems where unions can save space by storing only one value at a time.
Structures share the same memory for all members, while unions allocate separate memory for each member. This is significant when storing multiple attributes.
Both structures and unions allocate separate memory for each member. This is significant when memory usage is not a concern.
Both structures and unions share the same memory for all members. This is significant when memory usage is not a concern.
Which of the following is a correct use of the typedef keyword in C language?
typedef int number;
typedef number int;
typedef int;
typedef int new;
What is the main purpose of using typedef in C language?
To create a new variable
To create a new name (alias) for an existing data type
To declare a function
To initialize a structure
In the provided example, what does 'vvit' represent after the statement 'typedef int vvit;'?
A new variable
A function
An alias for int
A structure
Which of the following is NOT an advantage of using typedef in C?
Makes code shorter and cleaner
Improves portability
Avoids repeatedly writing long type declarations
Increases the execution speed of the program
Given the following code snippet, what will be the output? typedef int vvit; int main() { vvit a = 10, b = 20; printf("a = %d, b = %d\n", a, b); return 0; }
a = 10, b = 20
a = 20, b = 10
a = 0, b = 0
Compilation error
Why is typedef considered useful with structures and pointers in C?
It allows direct memory access
It makes code more readable and easier to maintain
It increases the size of the data type
It changes the value of variables
Which of the following is the correct syntax for typedef in C?
typedef new_data_type_name existing_data_type;
typedef existing_data_type new_data_type_name;
typedef new_data_type existing_data_type_name;
typedef data_type;
Consider the following struct definition without typedef: struct Student { int id; char name[20]; }; Which of the following is the correct way to declare a variable of this struct?
Student s1;
struct Student s1;
typedef Student s1;
Student struct s1;
Which keyword is used to define a structure in C?
struct
typedef
enum
define
What is the main advantage of using typedef with struct in C?
It allows the struct to act as a new data type.
It increases the speed of the program.
It automatically initializes struct members.
It prevents memory leaks.
What does the enum keyword stand for in C language?
Enumeration
Environment
Encryption
Entity
