adplus-dvertising
frame-decoration

Question

Which of the following loop correctly prints the elements of the array?

char[ ] arr = new char[ ] {'k', 'i','C', 'i','t'} ;

a.

do
{
Console.WriteLine((char) i);
}
while (int i = 0; i < arr; i++);

b.

foreach (int i in arr)
{
Console.WriteLine((char) i);
}

c.

for (int i = 0; i < arr; i++)
{
Console.WriteLine((char) i);
}

d.

while (int i = 0; i < arr; i++)
{
Console.WriteLine((char) i);
}

Posted under C# programming

Answer: (b).foreach (int i in arr)
{
Console.WriteLine((char) i);
}

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Which of the following loop correctly prints the elements of the array?