Structures Questions
Practice Structures MCQs with answers and explanations. Page 1 of 1.
Category
C# Programming
Topic
Structures
Page
1 / 1
Mode
Practice
Questions
Open any question to view the answer and explanation.
C#.NET structs — Which of the following statements are correct?
1) A struct can contain properties.
2) A struct can contain constructors.
3) A struct can contain protected data members.
4) A struct cannot contain methods.
5) A struct cannot contain constants.
Open
View answer
C#.NET — Which statements about classes vs. structs are true?
1) A class is a reference type, whereas a struct is a value type.
2) Objects are created using new, whereas struct variables can be created using new or without new.
3) A struct variable will always be created slower than an object.
4) A struct variable will die when it goes out of scope.
5) An object will die when it goes out of scope.
Open
View answer
C#.NET — Is it allowed to declare an empty struct (no fields or members)?
Open
View answer
C#.NET — Given the struct below, which is the correct way to assign values to its fields?
struct Emp
{
public string name;
public int age;
public Single sal;
}
Emp e = new Emp();
Open
View answer
C#.NET — Consider the structure and usage below. Which statements are correct?
struct Book
{
private string name;
protected int totalpages;
public Single price;
public void Showdata()
{
Console.WriteLine(name + " " + totalpages + " " + price);
}
Book()
{
name = "";
totalpages = 0;
price = 0.0f;
}
}
Book b = new Book();
Evaluate the following claims:
1) We cannot declare totalpages as protected.
2) We cannot declare name as private.
3) We cannot define a zero-argument constructor inside a struct.
4) We cannot declare price as public.
5) We can define a Showdata() method inside a struct.
Open
View answer
C#.NET — How many bytes does the following struct instance occupy? (Assume a 32-bit process / 4-byte object references.)
class Trial
{
int i;
Decimal d;
}
struct Sample
{
private int x; // 4 bytes
private Single y; // 4 bytes
private Trial z; // reference (pointer) → 4 bytes on 32-bit
}
Sample samp = new Sample();
Open
View answer
C#.NET — When does a struct variable get destroyed (become eligible for reuse)?
Open
View answer
C#.NET — What will be the output of the following program (note value-type copy semantics for struct)?
namespace CuriousTabConsoleApplication
{
struct Sample { public int i; }
class MyProgram
{
static void Main()
{
Sample x = new Sample();
x.i = 10;
fun(x);
Console.Write(x.i + " ");
}
static void fun(Sample y)
{
y.i = 20;
Console.Write(y.i + " ");
}
}
}
Open
View answer
C#.NET — What will be the output of this program (struct assignment copies the value)?
namespace CuriousTabConsoleApplication
{
struct Sample { public int i; }
class MyProgram
{
static void Main(string[] args)
{
Sample x = new Sample();
Sample y;
x.i = 9;
y = x; // copies the struct value
y.i = 5; // modifies the copy
Console.WriteLine(x.i + " " + y.i);
}
}
}
Open
View answer
C#.NET — Identify the correct statement about structs, value types, and the type system.
Open
View answer
C#.NET — True or False: structures (struct) are always value types in .NET.
Open
View answer
C#.NET — Choose the correct way to declare and initialize a variable of the struct Emp.
struct Emp
{
private string name;
private int age;
private float sal;
}
Which one of the following single-line or two-line snippets correctly declares a variable and initializes it?
Open
View answer
C#.NET — Trace ref parameter behavior with a struct to determine output.
namespace CuriousTabConsoleApplication
{
struct Sample
{
public int i;
}
class MyProgram
{
static void Main(string[] args)
{
Sample x = new Sample();
x.i = 10;
fun(ref x);
Console.Write(x.i + " ");
}
public static void fun(ref Sample y)
{
y.i = 20;
Console.Write(y.i + " ");
}
}
}
What will be printed?
Open
View answer
C#.NET — True or False: “The space required for structure variables is allocated on the stack.”
Open
View answer
C#.NET — Allocation of struct variables and reference fields.
Given:
class Trial
{
int i;
decimal d;
}
struct Sample
{
private int x;
private float y;
private Trial z; // reference-type field
}
Sample ss = new Sample();
Which statement is correct?
Open
View answer
C#.NET — Behavior of a struct variable: location, constructors, GC, and inheritance.
Given:
struct Book
{
private string name;
private int noofpages;
private float price;
}
Book b = new Book();
Which statement is correct?
Open
View answer
C#.NET — Select the correct statements about structs (declaration, inheritance, access modifiers, empty structs, field initialization). Which are valid facts?
Open
View answer
C#.NET — What happens when assigning one struct to another?
Given:
struct Address
{
private int plotno;
private string city;
}
Address a = new Address();
Address b;
b = a;
Choose the correct effect of “b = a;”.
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.