C# Question & Answers – Iterators

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

1. What is an iterator?
a) a method
b) an operator
c) accessor
d) all of the mentioned
View Answer

Answer: d
Explanation: An iterator is a method, operator, or accessor that returns the members of a set of objects, one member at a time, from start to finish.

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

  1.  class MyClass
  2.  {
  3.      char[] chrs = { 'A', 'B', 'C', 'D' };
  4.      public System.Collections.IEnumerator GetEnumerator()
  5.      {
  6.          foreach (char ch in chrs)
  7.          yield return ch;
  8.      }
  9.  }
  10.  class Program
  11.  {
  12.      static void Main(string[] args)
  13.      {
  14.          MyClass mc = new MyClass();
  15.          foreach (char ch in mc)
  16.          Console.Write(ch + " ");
  17.          Console.WriteLine();
  18.          Console.ReadLine();
  19.      }
  20.  }

a) Run time error
b) Compile time error
c) Code runs successfully prints nothing
d) Code runs successfully prints A, B, C, D
View Answer

Answer: d
Explanation: None.
Output:

advertisement
advertisement
 A, B, C, D

3. Choose the correct statements for the following C# code?

Note: Join free Sanfoundry classes at Telegram or Youtube
  1.  public System.Collections.IEnumerator GetEnumerator()
  2.  {
  3.      foreach (char ch in chrs)
  4.      yield return ch;
  5.  }

a) Definition of iterator for MyClass
b) Implements the GetEnumerator() method defined by IEnumerable
c) The yield return statement returns the next object in the collection, which in this case is the next character in chrs
d) All of the mentioned
View Answer

Answer: d
Explanation: This is the definition of the iterator for MyClass. The code implicitly implements the GetEnumerator() method defined by IEnumerable. At the body of the method. It contains a foreach loop that returns the elements in chrs. It does this through the use of a yield return statement. The yield return statement returns the next object in the collection, which in this case is the next character in chrs.
advertisement

4. What does the yield return statement specify in the following C# code snippet?

advertisement
  1.  public System.Collections.IEnumerator GetEnumerator()
  2.  {
  3.      foreach (char ch in chrs)
  4.      yield return ch;
  5.  }
  6. a) returns the output
  7. b) returns the next object in the collection
  8. c) Both returns the output & returns the next object in the collection
  9. d) none of the mentioned
  10. [expand title="View Answer"] Answer:  b
  11. Explanation: The yield return statement returns the next object in the collection, which in this case is the next character in chrs in the code.
  12. [/expand]
  13.  
  14. 5. What does the following C# code snippet specify?
  15. <pre lang="csharp" line="1" cssfile="hk1_style">
  16. class MyClass
  17. {
  18.     char chrs = 'A' ;
  19.     public IEnumerator GetEnumerator()
  20.     {
  21.         for (int i = 20; i >=0; --i)
  22.         yield return (char)((chrs + i));
  23.     }
  24. }
  25. class Program
  26. {
  27.     static void Main(string[] args)
  28.     {
  29.         MyClass mc = new MyClass();
  30.         foreach (char ch in mc)
  31.         Console.Write(ch + " ");
  32.         Console.WriteLine();
  33.         Console.ReadLine();
  34.     }
  35. }

a) A B C D E F G H I J K L M N O P Q R S T U V
b) Run time error
c) U T S R Q P O N M L K J I H G F E D C B A
d) Compile successfully prints nothing
View Answer

Answer: c
Explanation: None.
Output:

 U T S R Q P O N M L K J I H G F E D C B A

6. What will be the following C# code snippet specify?

  1. class MyClass
  2. {
  3.     char chrs = 'A' ;
  4.     public IEnumerator GetEnumerator()
  5.     {
  6.         for (int i = 20; i >=0; --i)
  7.         if (i == 10) yield break;
  8.         yield return (char)((chrs + i));
  9.     }
  10. }
  11. class Program
  12. {
  13.     static void Main(string[] args)
  14.     {
  15.         MyClass mc = new MyClass();
  16.         foreach (char ch in mc)
  17.         Console.Write(ch + " ");
  18.         Console.WriteLine();
  19.         Console.ReadLine();
  20.     }
  21. }

a) Code run successfully prints nothing
b) A B C D E F G H I J K L M N O P Q R S T U V
c) U T S R Q P O N M L
d) Compile time error
View Answer

Answer: c
Explanation: The code to specify stoppage of the iterator using ‘yield break’ statement When this statement executes, the iterator signals that the end of the collection has been reached, which effectively stops the iterator.
Output:

 U T S R Q P O N M L

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

  1.  class MyClass
  2.  {
  3.      int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  4.      public IEnumerator GetEnumerator()
  5.      {
  6.          for (int i = 0; i < 20; i++)
  7.          {
  8.              if (a[i] % 2 == 0)
  9.              yield return (int)(a[i]);
  10.          }
  11.      }
  12.  }
  13.  class Program
  14.  {
  15.      static void Main(string[] args)
  16.      {
  17.          MyClass mc = new MyClass();
  18.          foreach (int i in mc)
  19.          Console.Write(i + " ");
  20.          Console.WriteLine();
  21.          Console.ReadLine();
  22.      }
  23.  }

a) prints nothing code run successfully
b) run time error
c) code runs successfully prints even number between 1 to 20
d) compile time error
View Answer

Answer: c
Explanation: None.
Output:

2, 4, 6, 8, 10, 12, 14, 16, 18, 20

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

  1.  class MyClass
  2.  {
  3.      char ch = 'A';
  4.      public IEnumerable MyItr(int end)
  5.      {
  6.          for (int i = 0 ;i < end ;i++)
  7.          yield return (char)(ch + i);
  8.      }
  9.      public IEnumerable MyItr(int begin, int end)
  10.      {
  11.          for (int i = begin ;i < end ;i++)
  12.          yield return (char)(ch + i);
  13.      }
  14.  }
  15.  class Program
  16.  {
  17.      static void Main(string[] args)
  18.      {
  19.          MyClass mc = new MyClass();
  20.          Console.WriteLine("Iterate the first 7 letters:");
  21.          foreach (char ch in mc.MyItr(7))
  22.          Console.Write(ch + " ");
  23.          Console.WriteLine("n");
  24.          Console.WriteLine("Iterate letters from F to L:");
  25.          foreach (char ch in mc.MyItr(7, 12))
  26.          Console.Write(ch + " ");
  27.          Console.WriteLine();
  28.          Console.ReadLine();
  29.      }
  30.  }

a)

   Iterate the first 7 letters:
   A B C D E F G 
   Iterate letters from F to L:
   G H I J K L

b)

   Iterate the first 7 letters:
   A B C D E F G 
   Iterate letters from F to L:
   H I J K L

c) Run time error
d) Compile time error
View Answer

Answer: b
Explanation: None.
Output:

        Iterate the first 7 letters:
         A B C D E F G 
        Iterate letters from F to L:
        H I J K L

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

  1. class MyClass
  2. {
  3.     char ch = 'A';
  4.     int e = 4;
  5.     int k = 9;
  6.     int z = 6;
  7.     public IEnumerator GetEnumerator()
  8.     {
  9.         for (int i = 0; i < 26; i++)
  10.         {
  11.             if (i == e*k /z) yield break; 
  12.             yield return (int)(ch + i);
  13.         }
  14.     }
  15. }
  16. class Program
  17. {
  18.     static void Main(string[] args)
  19.     {
  20.         MyClass mc = new MyClass();
  21.         foreach(int ch in mc)
  22.         Console.Write(ch + " ");
  23.         Console.WriteLine();
  24.         Console.ReadLine();
  25.     }
  26. }

a) Compile time error
b) Run time error
c) 65 66 67 68 69 70
d) Code run successfully prints nothing
View Answer

Answer: c
Explanation: None.
Output:

 65 66 67 68 69 70

10. What are the advantages of the named iterator?
a) They allow to pass arguments to the iterator that control what elements are obtained
b) This form of iterators can be overloaded
c) Both They allow to pass arguments to the iterator that control what elements are obtained & This form of iterators can be overloaded
d) None of the mentioned
View Answer

Answer: c
Explanation: By definition.

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

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

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.