volume_mute

Is the following code correct?

publish date2020/10/17 04:52:00 GMT+11

volume_mute
public class MyClass {
  public static void main(String[] args) {
    int[] myNumbers = {"1", "2", "3", "4"};
    for (int i = 0; i < myNumbers.length; ++i) {
        System.out.println(myNumbers[i]);
    }
  }
}
Nothing wrong with the code
Loop will through an exception
Array initialization is not correct
Main method cannot be inside a class

Correct Answer

Array initialization is not correct

Explanation

Array is defined to include int values, but it is initialized with string values.  An int value does not need to be wrapped with double quotes.  The right code will be 

public class MyClass {
  public static void main(String[] args) {
    int[] myNumbers = {1, 2, 3, 4};     for (int i = 0; i < myNumbers.length; ++i) {
      System.out.println(myNumbers[i]);
    }
  }
}

Quizzes you can take where this question appears