Enhanced For Loop Array Iteration
Which of the following lines correctly replaces the missing line so that each element of testGrades is printed on a new line using an enhanced-for loop?
public class PrintGrades {
public static void main(String[] args) {
int[] testGrades = {90, 80, 100, 85, 92};
// Missing line: Use an enhanced-for loop to print each test grade on a new line.
}
}
A
for (int grade = 0; grade < testGrades.length; grade++) { System.out.println(grade); }
B
for (int grade : testGrades) { System.out.println(grade); }
C
for (int i = 0; i < testGrades.length; i++) { System.out.println(testGrades[i]); }
D
for (int grade : testGrades) System.out.print(grade);
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE