C# Questions & Answers – Structures

This section of our 1000+ C# MCQs focuses on structures of C# Programming Language.

1. What will be the correct statement in the following C# code?

  1. struct book
  2. {
  3.     private String name;
  4.     private int pages;
  5.     private Single price;
  6. }
  7. book b = new book();

a) New structure can be inherited from struct book
b) When the program terminates, variable b will get garbage collected
c) The structure variable ‘b’ will be created on the stack
d) When the program terminates, variable b will get garbage collected
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

2. What will be the correct statement in the following C# code?

  1. class trial
  2. {
  3.     int i;
  4.     float d;
  5. }
  6. struct sample
  7. {
  8.     private int x;
  9.     private Single y;
  10.     private trial z;
  11. }
  12. sample s = new sample();

a) trial object referred by z is created on the stack
b) z is created on the heap
c) Both s and z will be created on the heap
d) s will be created on the stack
View Answer

Answer: d
Explanation: None.

3. Choose the correct statement among the following which supports the fact that C# does not allow the creation of empty structures?
a) C#.NET supports creation of abstract user-defined data types using structures
b) By having empty structures, it would mean that the new data types have no data associated with, which does not make any sense in C#.NET
c) Basic reason to create structures is the inability to represent real life objects using standard data types offered by the language
d) All of the mentioned
View Answer

Answer: d
Explanation: Basic definition of structures in C#.NET.
advertisement

4. Choose the correct statement about structures as to why they are defined as value types but not reference types?
a) Since space required for structure variables is allocated on stack which is a form of memory that is automatically available when a variable to be used is in scope
b) Structures generally are used to represent user defined data types that consists of small amount of data in them. Hence using stack for declaration of such variables is not a problem
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

5. Choose the wrong statement about structures in C#.NET?
a) Structures can be declared within a procedure
b) Structures can implement an interface but they cannot inherit from another structure
c) Structure members cannot be declared as protected
d) A structure cannot be empty
View Answer

Answer: a
Explanation: None.
advertisement

6. The correct way to define a variable of type struct abc in the following C# code?

  1.  struct abc
  2.  { 
  3.      public string name;
  4.      protected internal int age;
  5.      private Single sal;
  6.  }

a) abc e = new abc();
b) abc();
c)

abc e;
e = new abc;

d) abc e = new abc;
View Answer

Answer: a
Explanation: None.

7. Which of the following is the correct way to settle down values into the structure variable ‘e’ defined in the following C# code snippet?

  1.  struct emp
  2.  { 
  3.      public String name;
  4.      public int age;
  5.      public Single sal;
  6.  }
  7.  emp e = new emp();

a)

  1.      e.name = "Ankit";
  2.      e.age = 24;
  3.      e.sal = 200;

b)

  1.    With emp e
  2.    {
  3.      .name = "Ankit";
  4.      .age = 24;
  5.      .sal = 200;
  6.    }

c)

  1.    name = "Ankit";
  2.    age = 24;
  3.    sal = 200;

d) All of the mentioned
View Answer

Answer: a
Explanation: None.

8. When does a structure variable get destroyed?
a) When no reference refers to it, it will get garbage collected
b) Depends on whether it is created using new or without new operator
c) As variable goes out of the scope
d) Depends on either we free its memory using free() or delete()
View Answer

Answer: c
Explanation: None.

9. Calculate the number of bytes a structure variable s occupies in the memory if it is defined as follows.

  1.   class abc
  2.   {
  3.       int i;
  4.       Decimal d;
  5.   }
  6.   struct sample
  7.   {
  8.      private int x;
  9.      private Single y;
  10.      private trial z;
  11.   }
  12.   sample s = new sample();

a) 24 bytes
b) 8 bytes
c) 16 bytes
d) 12 bytes
View Answer

Answer: d
Explanation: None.
Output –

12 bytes

10. What will be the output of the following C# code?

  1. {
  2.     struct abc
  3.     {
  4.         public int i;
  5.     }  
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             sample a = new sample();
  11.             a.i = 10;
  12.             fun(ref a);
  13.             Console.WriteLine(a.i);
  14.         }
  15.         public static voidn fun(ref sample x)
  16.         {
  17.             x.i = 20; 
  18.             Console.WriteLine(x.i);
  19.         }
  20.     }
  21. }

a)

   10
   10

b)

   20
   10

c)

   10
   20

d)

   20
   20
View Answer
Answer: d
Explanation: None.
Output –

         20
         20

11. Select the wrong statements among the following?
a) A structure can contain properties
b) A structure can contain constructors
c) A structure can contain protected data members
d) A structure can contain constants
View Answer

Answer: c
Explanation: None.

12. Which of the following is the correct result for the given statement in the C#.NET statement given below?
p = q

  1. struct employee
  2. {
  3.     private int employee id;
  4.     private string city;
  5. }
  6. employee q = new employee();
  7. employee p;
  8. p = q;

a) Elements of ‘q’ will be copied into corresponding elements of p
b) Address stored in q will get copied into p
c) Address of first element of q will get copied into p
d) Once assignment is over. q will go out of scope and hence get exited forever
View Answer

Answer: a
Explanation: None.

13. What will be the output of the following C# code?

  1.  {
  2.      struct abc
  3.      {
  4.          int i;
  5.      }
  6.      class Program
  7.      {
  8.          static void Main(string[] args)
  9.          {
  10.              abc x = new abc();
  11.              abc z;
  12.              x.i = 10;
  13.              z = x;
  14.              z.i = 15;
  15.              console.Writeline(x.i + "  " + y.i)
  16.          }
  17.      }
  18.  }

a) 10 10
b) 10 15
c) 15 10
d) 15 15
View Answer

Answer: b
Explanation: None.
Output:

 10 15

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.