C# - Structures Questions and Answers
C# Structures programming questions and answers help learners understand value types and memory management in .NET. Structures in C# are lightweight data types that store related data efficiently without the overhead of classes. Companies like Infosys, HCL, and TCS often include structure-related questions in interviews to test programming fundamentals. Practicing programming interview questions with answers based on structures allows candidates to learn syntax, constructors, field declarations, and performance differences compared to classes. Mastering structures improves understanding of memory optimization and efficient coding—vital skills for software developers and interview aspirants.
C# - Structures
Showing 10 of
14 questions
3. Which of the following statements is correct about the C#.NET code snippet given below?
class Trial
{
int i;
Decimal d;
}
struct Sample
{
private int x;
private Single y;
private Trial z;
}
Sample ss = new Sample();
- ss will be created on the heap.
- Trial object referred by z will be created on the stack.
- z will be created on the heap.
- Both ss and z will be created on the heap.
4. How many bytes will the structure variable samp occupy in memory if it is defined as shown below?
class Trial
{
int i;
Decimal d;
}
struct Sample
{
private int x;
private Single y;
private Trial z;
}
Sample samp = new Sample();
- 20 bytes
- 12 bytes
- 8 bytes
- 16 bytes
5. Which of the following will be the correct result of the statement b = a in the C#.NET code snippet given below?
struct Address
{
private int plotno;
private String city;
}
Address a = new Address();
Address b;
b = a;
- All elements of a will get copied into corresponding elements of b.
- Address stored in a will get copied into b.
- Once assignment is over a will get garbage collected.
- Once assignment is over a will go out of scope, hence will die.
6. Which of the following statements are correct?
A struct can contain properties.
A struct can contain constructors.
A struct can contain protected data members.
A struct cannot contain methods.
A struct cannot contain constants.
- 1, 2
- 3, 4
- 1, 2, 4
- 3, 5
8. When would a structure variable get destroyed?
- When no reference refers to it, it will get garbage collected.
- Depends upon whether it is created using new or without using new.
- When it goes out of scope.
- Depends upon the Project Settings made in Visual Studio.NET.
9. Which of the following statements is correct about the C#.NET code snippet given below?
struct Book
{
private String name;
private int noofpages;
private Single price;
}
Book b = new Book();
- The structure variable b will be created on the heap.
- We can add a zero-argument constructor to the above structure.
- When the program terminates, variable b will get garbage collected.
- The structure variable b will be created on the stack.
10. Which of the following statements is correct about the C#.NET code snippet given below?
struct Book
{
private String name;
private int noofpages;
private Single price;
}
Book b = new Book();
- The structure variable b will be created on the heap.
- We can add a zero-argument constructor to the above structure.
- When the program terminates, variable b will get garbage collected.
- The structure variable b will be created on the stack.