Important Introduction to Programming Short Questions
Chapter: 20
Question 1:
How is a structure defined in C/C++?
Answer:
A structure is defined using the struct keyword, followed by the structure name and its members enclosed in curly braces. For example: struct student { char name[60]; float GPA; };
Chapter: 20
Question 2:
How do you declare variables of a structure type?
Answer:
Variables of a structure type are declared like ordinary variables. For example: student std1, std2; where student is the structure type.
Chapter: 20
Question 3:
How can you define and declare a structure in one line?
Answer:
You can define and declare a structure in one line using the following syntax: struct student { char name[60]; float GPA; } std1, std2;
Chapter: 20
Question 4:
Can a structure contain other structures?
Answer:
Yes, a structure can contain other structures as its members. For example, a student structure can include an address structure as one of its members.
Chapter: 20
Question 5:
What is the difference between (*sptr).name and sptr->name?
Answer:
(*sptr).name uses the dereference operator * and dot operator . to access a member of the structure pointed to by sptr, while sptr->name is a shorthand that directly accesses the member using the arrow operator ->.
Chapter: 20
Question 6:
How do you initialize a structure variable at the time of declaration?
Answer:
You can initialize a structure variable by providing values in curly braces. For example: student s1 = {"Ali", "CS201", 19, 2002};
Chapter: 20
Question 7:
What is the purpose of the sizeof operator in relation to structures?
Answer:
The sizeof operator is used to determine the amount of memory occupied by a structure. For example, sizeof(s1) returns the size in bytes of the structure variable s1.
Chapter: 20
Question 8:
How are structures passed to functions in C/C++?
Answer:
Structures can be passed to functions either by value, where a copy of the structure is made, or by reference, where the address of the structure is passed.
Chapter: 20
Question 9:
What is the role of the dot operator (.) in structures?
Answer:
The dot operator (.) is used to access the members of a structure when using a structure variable. For example, s1.name accesses the name member of the structure variable s1.
Chapter: 20
Question 10:
How do you access a structure member using a pointer to the structure?
Answer:
You use the arrow operator (->) to access members through a pointer to a structure. For example, if sptr is a pointer to a student, sptr->name accesses the name member.