C# Questions & Answers on Introduction of Indexers for Freshers

1. Choose the correct statement among the followings?

a) Indexers are location indicators
b) Indexers are used to access class objects
c) Indexer is a form of property and works in the same way as a property
d) All of the mentioned
Answer: d

Explanation: By definition.
2. Choose the keyword which declares the indexer?

a) base
b) this
c) super
d) extract
Answer: b

Explanation: The indexer is declared using the name this.
3. Choose the operator/operators which is/are not used to access the [] operator in indexers?

a) get
b) set
c) access
d) all of the mentioned
Answer: c

Explanation: The indexer is implemented through the get and set accessors for the [] operator as:
  public double this[int idx]
              {
                 get
                 {
                   if()
                  {
                  }
                 else
                 {
                    return([idx]);
                 }
 
              }
                 set
                 {
                      array[idx];
 
                 }
           }
4. Choose the correct statement among the following?

a) A property can be a static member whereas an indexer is always an instance member
b) A get accessor of a property corresponds to a method with no parameters whereas get accessor of an indexer corresponds to a method with the same formal parameters lists as the indexer
c) It is an error for indexer to declare a local variable with the same name as indexer parameters
d) All of the mentioned
Answer: d
5. For a class student consisting of indexer,which among the following declaration of indexers runs the code successfully ?

student a = new student();
a[1,2] = 20;
a) class student
   {
       int[,] p = new int[6, 6];
       public property WriteOnly int this[int i, int j]
       {
           set
           {
               a[i, j] = value;
           }
       }
   }
b) class student
   {
       int[,] a = new int[6, 6];
       public int this[int i, int j]
       {
           set
           {
               a[i, j] = value;
           }
       }
   } 
c) class student
   {
       int[,] a = new int[6, 6];
       public int property WriteOnly
       {
           set
           {
               a[i, j] = value;
           }
       }  
   }
d) None of the mentioned

Answer: b

6. Which among the following are the advantages of using indexers?

a) To use collection of items at a large scale we make use of indexers as they utilize objects of class that represent the collection as an array
b) Indexers are also convenient as they can also make use of different types of indexers like int, string etc
c) An indexer allows an object to be indexed such as an array
d) All of the mentioned
Answer: d

Explanation: Indexers provides a view at large scale to visualize a collection of items as an array.It is really easy to use the object of the class that represents a collection as if it is an array.Hence, indexed properties allow us to represent such a view. Indexers can also use different types of indexes like int , string etc. Use int as an index where sequential access to a collection is desired.When symbolic access is needed,use string as an index.
7. Choose the correct statement about properties describing the indexers?

a) No need to use the name of the property while using an indexed property
b) An indexer property should accept at least one argument
c) Indexers can be overloaded
d) All of the mentioned
Answer: d
8. Choose the correct alternative that utilizes the indexed property such that a group named class has indexed property which stores or retrieves value to/from an array of 5 numbers?

a) group[3] = 34;
b) group g = group();
c) Console.WriteLine(group[3]);
d) group g = new group();
Console.WriteLine(g[3]);
Answer: d
9. Choose the correct option among the following indexers which correctly allows to index in same way as an array?

a) A class
b) An interface
c) A function
d) A property
Answer: a
10. Choose the output for the given set of code?
  1.  class list
  2.  {
  3.      ArrayList array = new ArrayList();
  4.      public object this[int index]
  5.      {
  6.          get
  7.          {
  8.              if (index < 0 || index >= array.Count)
  9.              {
  10.                  return null;
  11.              }
  12.              else
  13.              {
  14.                  return (array[index]);
  15.              }
  16.          }
  17.          set
  18.          {
  19.              array[index] = value;
  20.          }
  21.      }
  22.      public int Count 
  23.     { 
  24.         get;
  25.         set; 
  26.     }
  27. }
  28. class Program
  29. {
  30.     static void Main(string[] args)
  31.     {
  32.         list list1 = new list();
  33.         list1[0] = "123";
  34.         list1[1] = " abc ";
  35.         list1[2] = "xyz";
  36.         for (int i = 0; i<=list1.Count; i++)
  37.         Console.WriteLine(list1[i]);
  38.         Console.ReadLine();
  39.     }
  40. }
a) Compile time error
b) Run time error
c) 123, abc, xyz
d) 0
Answer: b

Explanation: Index out of range which arises only when index is non negative or less than the collection of size.

Related

Multiple Choice Questions 8132513289748788816

Post a Comment

emo-but-icon

item