Select the execution result of the following program
java.java
public class Main{
public static void main(String[] args){
String str = "abcde";
System.out.println(str.charAt(5));
}
}
A.d is displayed B.e is displayed C. Nothing is displayed D.null is displayed E. A compile error occurs F. Exception is thrown at runtime
The answer is F.
str.charAt (5) takes an integer as an argument and returns the character at that location with the argument as an index. Since the string str has only 5 characters, the index is numbered 0 to 4 and is thrown (runtime error) because there is no 5th. The answer is F because there is no grammatical problem and it is thrown without compilation errors.
If you do this with TypeScript
TypeScript.ts
var str:string = "abcde";
console.log(str.charAt(5));
Write like this.
TypeScript compiles fine because the throw is a run-time error. Also, js does not give an error in this case, so a blank is displayed.
Recommended Posts