C# Questions & Answers – Introduction of Indexers

This section of our 1000+ C# multiple choice questions focuses on indexers and application of indexers in C# Programming Language.

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
View Answer

Answer: d
Explanation: By definition.

2. Choose the keyword which declares the indexer?
a) base
b) this
c) super
d) extract
View Answer

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
View Answer

Answer: c
Explanation: The indexer is implemented through the get and set accessors for the [] operator as:

advertisement
advertisement
              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
View Answer

Answer: d
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. For a class student consisting of indexer, which among the following declaration of indexers runs the C# code successfully?

   student a = new student();
   a[1,2] = 20;

a)

advertisement
   class student
   {
       int[,] p = new int[6, 6];
       public property WriteOnly int this[int i, int j]
       {
           set
           {
               a[i, j] = value;
           }
       }
   }

b)

advertisement
   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
View Answer

Answer: b
Explanation: None.

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
View Answer

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
View Answer

Answer: d
Explanation: None.

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]);
View Answer
Answer: d
Explanation: None.
 
 

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
View Answer

Answer: a
Explanation: None.

10. What will be the output of the following C# 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
View Answer

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

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.