Which code should you use?

You have the following code.

You need to remove all of the data from the myData list.
Which code should you use?
A. for (int i = 0; i <= myData.Count; i++) myData.RemoveAt(i);
B. while (myData.Count ! = 0) myData.RemoveAt(0);
C. foreach(string currentString in myData) myData.Remove(currentString);
D. for (int i = 0; i <= myData.Count; i++) myData.RemoveAt(0);

microsoft-exams

6 thoughts on “Which code should you use?

  1. B is the correct answer!!

    for (int i = 0; i < myData.Count; i++)
    {
    myData.RemoveAt(i);
    }

    The end result will be "One element left out" in the list. Not all will be deleted.

    1. A is correct
      in you code you left out i<=MyData.Count that why you getting last element leftout if you added the = opreator you would have deleted all the string in the Array
      B – will always remove the element at position 0 only

  2. Correct Answer is while (myData.Count != 0) myData.RemoveAt(0);

    List myData = new List();
    myData.Add(“string1”);
    myData.Add(“string2”);
    myData.Add(“string3”);
    Console.WriteLine(myData.Count);
    while (myData.Count != 0) myData.RemoveAt(0);
    Console.WriteLine(myData.Count);
    /*
    3
    0
    */

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.