Structure Questions with Answers

8 practice questions on Structure (C#) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Medium

Which of the following are true about classes and struct?
I. A class is a reference type, whereas a struct is a value type.
II. Objects are created using new, whereas structure variables can be created either using new or without using new.
III. A structure variable will always be created slower than an object.
IV. A structure variable will die when it goes out of scope.
V. An object will die when it goes out of scope.

  1. 1, 2, 4
  2. 3, 5
  3. 2, 4
  4. 3, 4, 5

Answer: A. 1, 2, 4

Explanation

A type as a class (a reference type) or as a struct (a value type).Good understanding of the differences in the behavior of reference types and value types is crucial in making this choice. A class is a reference type.
When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data. A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data.
When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

Step-by-step solution

  1. I is true: a class is a reference type and a struct is a value type.
  2. II is true: structs can be created with or without new.
  3. III is false: structs are created faster, not slower.
  4. IV is true, and V is false: an object is cleaned up later by the garbage collector.
  5. So option A (1, 2, 4) is correct.

Why the other options are wrong

  • B. III and V are false.
  • C. It leaves out I, which is true.
  • D. III and V are false.

Tip: Structs are value types and go away at end of scope; objects wait for garbage collection.

Question 2Medium

The space required for local structure variables is allocated on the stack.

  1. True
  2. False

Answer: A. True

Explanation

A structure is a value type, so a local structure variable gets its space on the stack.

Step-by-step solution

  1. A struct is a value type in C#.
  2. Value types hold their data directly in the variable.
  3. A local struct variable is stored on the stack.
  4. The statement is True: option A.

Why the other options are wrong

  • B. Structs are value types and local struct variables are stored on the stack, so 'False' is wrong.

Tip: Local value types (int, struct) live on the stack; reference type objects live on the heap.

Question 3Medium

Which of the following statements is correct about the C#.NET code snippet given below?

       struct Book
{
    private String name;
    private int no of pages;
    private Single price;
}
Book b = new Book();
  1. The structure variable b will be created on the heap.
  2. We can add a destructor to the above structure
  3. When the program terminates, variable b will get garbage collected
  4. The structure variable b will be created on the stack.

Answer: D. The structure variable b will be created on the stack.

Explanation

Because of structure value type that stores values in stack only.

Step-by-step solution

  1. Book is declared with struct, so it is a value type.
  2. new Book() on a struct does not allocate on the heap; the value lives where the variable lives, here on the stack.
  3. Stack values are freed at scope end, not garbage collected.
  4. So b is created on the stack: option D.

Why the other options are wrong

  • A. Structs are value types, not heap objects.
  • B. A struct cannot declare its own zero-argument constructor in this C# version.
  • C. Stack values are not garbage collected.

Tip: struct = value type = stack; class = reference type = heap.

Question 4Medium

Which of the following will be the correct output for the C#.NET program given below?

`
namespace compConsoleApplication
{
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 + " ");
}
}
}

  1. 10 20
  2. 10 10
  3. 20 10
  4. 20 20

Answer: C. 20 10

Explanation

As Structures are value type. They hold different memory location for each instance (struct in this case is value type ). Hence x.i would point to one mem location whereas for y.i to another.
Hence fun(x) would call the fun method. But again y.i is assigned 20. Hence a Console would give 20 first and then control would be transferred to main() again and the next statement for x.i (print ) would give 10.
Hence the output: 20 10.

Step-by-step solution

  1. Sample is a struct, so it is a value type and passing it to a method passes a copy.
  2. In Main, x.i is set to 10, and fun(x) receives a copy of x as y.
  3. Inside fun, y.i is set to 20 and printed as 20 followed by a space.
  4. Back in Main, x is unchanged, so x.i prints 10. The output is 20 10, option C.

Why the other options are wrong

  • A. The first value printed is 20, not 10, because fun prints y.i first.
  • B. The first value printed is 20 from inside fun, not 10.
  • D. x.i stays 10 in Main because the struct was copied, so the second value is 10, not 20.

Tip: Structs are value types: a struct passed to a method is copied, so changes inside stay inside.

Question 5Medium

When would a structure variable get destroyed?

  1. When no reference refers to it, it will get garbage collected.
  2. Depends upon whether it is created using new or without using new.
  3. When it goes out of scope
  4. Depends upon the Project Settings made in Visual Studio.NET

Answer: C. When it goes out of scope

Explanation

structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy.
This is important to remember when working with collections of value types

Step-by-step solution

  1. A structure is a value type, so it is not tracked through references the way objects are.
  2. A structure variable's life ends when it goes out of scope.
  3. So the correct answer, when it goes out of scope, is correct.

Why the other options are wrong

  • A. Garbage collection tracks references, which a value type does not rely on.
  • B. Whether new is used does not change when a struct variable ends.
  • D. Project settings do not control the lifetime of a struct variable.

Tip: Value types end when their scope ends; reference objects end when no reference is left (garbage collection).

Question 6Medium

Creating empty structures is allowed in C#.NET.

  1. True
  2. False

Answer: A. True

Explanation

C# allows an empty struct such as struct S { }. It still has a default (parameterless) value and can be used normally.

Step-by-step solution

  1. C# allows a struct with no fields or members, such as struct S { }.
  2. Such a struct compiles and can be used, and it has an implicit parameterless default value.
  3. So the statement is True, option A.

Why the other options are wrong

  • B. Empty structs are allowed in C#, so False is wrong.

Tip: Structs are value types; an empty struct is valid and every struct has a default value.

Question 7Medium

Which of the following statements are correct?
I. A struct can contain properties.
II. A struct can contain constructors.
III. A struct can contain protected data members.
IV. A struct cannot contain methods.
V. A struct cannot contain constants.

  1. 1, 2
  2. 3, 4
  3. 1, 2, 4
  4. 3, 5

Answer: A. 1, 2

Explanation

Bcoz Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

Step-by-step solution

  1. Statement 1: a struct can have properties, so it is true.
  2. Statement 2: a struct can have constructors, so it is true.
  3. Statement 3: a struct cannot have protected members, so it is false.
  4. Statements 4 and 5 are false: a struct can have methods and constants.
  5. So the correct answer, statements 1 and 2, is correct.

Why the other options are wrong

  • B. Statements 3 and 4 are false: a struct cannot have protected members, and it can have methods.
  • C. Statement 4 is false because a struct can have methods, so the list with 4 is wrong.
  • D. Statements 3 and 5 are false: a struct can have constants, and 3 is not allowed.

Tip: Structs can hold most members that classes hold, but not protected ones and not inheritance from a class.

Question 8Medium

Which of the following will be the correct output for the program given below?

namespace IndiabixConsoleApplication
{
    struct Sample
    {
        public int i;
    }
    class MyProgram
    {
        static void Main(string[] args)
        {
            Sample x = new Sample();
            Sample y;
            x.i = 9;
            y = x;
            y.i = 5;
            Console.WriteLine(x.i + " " + y.i);
        }
    }
}
  1. 9 9
  2. 9 5
  3. 5 5
  4. 5 9

Answer: B. 9 5

Explanation

Though the object is copied as it is but in the next line for the second object the value is changed so it will be a change in value for the object y's variable i.

Step-by-step solution

  1. Sample is a struct, which is a value type, so y = x copies the whole value.
  2. x.i is set to 9 and then copied into y.
  3. y.i is changed to 5, but y is a separate copy, so x.i stays 9.
  4. The output is 9 5, so the answer is option B.

Why the other options are wrong

  • A. Y is a copy, so changing y.i to 5 does not change x.
  • C. x.i was never changed to 5; it stays 9.
  • D. x.i is 9 and y.i is 5, so the order is 9 then 5.

Tip: Assigning a struct copies its values; changing the copy does not change the original.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics