exam questions

Exam 70-483 All Questions

View all questions & answers for the 70-483 exam

Exam 70-483 topic 2 question 153 discussion

Actual exam question from Microsoft's 70-483
Question #: 153
Topic #: 2
[All 70-483 Questions]

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++)
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
djools
Highly Voted 5 years, 6 months ago
100% sure => Tested answer is B. while (myData.Count != 0) myData.RemoveAt(0);
upvoted 32 times
...
Xavios
Highly Voted 5 years, 7 months ago
I think its B
upvoted 28 times
...
M62
Most Recent 3 years, 10 months ago
Tried and tested, it is B
upvoted 1 times
...
noussa
3 years, 10 months ago
The correct answer should be B
upvoted 2 times
...
johan2020
4 years ago
I think its B List<string> myData = new List<string>(); myData.Add("string1"); myData.Add("string2"); myData.Add("string3"); while (myData.Count != 0) { myData.RemoveAt(0); } Console.WriteLine(myData.Count());
upvoted 2 times
...
Bintang0
4 years, 1 month ago
B is the only right answer. A removes some of the items in the List, not all of them ( TESTED )
upvoted 2 times
...
_IngKate
4 years, 2 months ago
A is the correct answer
upvoted 2 times
Kilsimon
4 years, 1 month ago
The problem with solution A is that it removes on position i. So you have a list: myData = {"string1", "string2", "string3"}. The first loop removes the element on position 0. myData is now: myData = {"string2", "string3"} The next time it goes through the loop is to remove position 1 in the list. myData is now: myData = {"string2"} The next step fails as i now is larger than the size of the list.
upvoted 2 times
...
...
Jannie
4 years, 5 months ago
Answer is B
upvoted 2 times
...
WTH
4 years, 7 months ago
A is wrong. It leaves "string2" in myData after the operation. The correct answer is B (verified).
upvoted 2 times
...
Sujay
4 years, 7 months ago
600 % Tested B is the correct answer
upvoted 4 times
...
Arpan3213
4 years, 7 months ago
A is right answer
upvoted 2 times
...
TonyBezerra
4 years, 8 months ago
I think A and B are the right answers. Both worked fine. I tested.
upvoted 1 times
...
Shema
4 years, 10 months ago
Definitely its not C, bcz you can't modify collection through what you iterate in foreach. See here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in "The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop."
upvoted 8 times
...
peter1994
5 years, 3 months ago
answer B. Foreach loop doesn't allow modifications during its iteration.
upvoted 19 times
...
aadi
5 years, 3 months ago
No Doubt B is Correct Answer .100% Tested.
upvoted 17 times
...
supersunny
5 years, 4 months ago
B is the tested and correct answer.
upvoted 19 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago