Question

A Java method tries to get the length of an array and a string. public class LengthChecker {     public void checkLengths() {         String[] names = {"Alice", "Bob"};         String greeting = "Hello";         System.out.println("Array length: " + names.length()); // Potential bug here         System.out.println("String length: " + greeting.length); // Potential bug here     } } What will happen when checkLengths() is executed?

A Both lines will print the correct lengths (2 and 5).
B A NullPointerException will occur.
C A compile-time error will occur.
D The array length will be incorrect, but the string length will be correct.
E The string length will be incorrect, but the array length will be correct.
Practice Next

Hey! Ask a query