Card 34 / 76: Which code example makes use of arrays without producing a compiler or runtime error?
A)
public class Actor { String[] characterName = new String[3]; { characterName[0] = "Captain Video"; characterName[1] = "Quizmaster"; characterName[2] = "J.C. Money"; characterName[3] = "Jersey Joe"; } }
B)
public class Actor { String[] characterName = new String[1..4]; { characterName[0] = "Captain Video"; characterName[1] = "Quizmaster"; characterName[2] = "J.C. Money"; characterName[3] = "Jersey Joe"; } }
C)
public class Actor { String characterName = new String[4]; { characterName[0] = "Captain Video"; characterName[1] = "Quizmaster"; characterName[2] = "J.C. Money"; characterName[3] = "Jersey Joe"; } }
D)
public class Actor { String[] characterName = new String[4]; { characterName[0] = "Captain Video"; characterName[1] = "Quizmaster"; characterName[2] = "J.C. Money"; characterName[3] = "Jersey Joe"; } }
Answer:
D) public class Actor { String[] characterName = new String[4]; { characterName[0] = "Captain Video"; characterName[1] = "Quizmaster"; characterName[2] = "J.C. Money"; characterName[3] = "Jersey Joe"; } }
Previous Card | ← Previous Card Button |
Next Card | → Next Card Button |
Flip Card | Space-Bar |
A, B, and C are incorrect.
A compiles with an ArrayIndexOutOfBoundsException exception thrown at runtime.
This is because the array index characterName[3] is out of bounds.
B fails to compile because only an integer is expected in the box brackets.
C fails to compile because the box brackets that are needed to create the array are missing.
|