C# Questions & Answers on Structures for Freshers

1.Which of the following is a correct statement about the C#.NET code given below?
  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
Answer: c
2. Which of the following is a correct statement about the C#.NET code given below?
  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
Answer: d
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
Answer: d
Explanation: Basic definition of structures in C#.NET.
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
Answer: c
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
Answer: a
6. The correct way to define a variable of type struct abc among the following is?
  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;
Answer: a
7. Which of the following is the correct way to settle down values into the structure variable ‘e’ defined in the following 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
Answer: a
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()
Answer: c
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
Answer: d
Output – 12 bytes
10. Which of the following is the correct output for the C#.NET program code given below?
  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
Answer: d
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
Answer: c
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
Answer: a
13. Which of the following will be the correct output for the program given below?
  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
Answer: b
Output: 10 15

Related

Multiple Choice Questions 9094149471683365164

Post a Comment

emo-but-icon

item