ЁЯУв Too many exams? DonтАЩt know which one suits you best? Book Your Free Expert ЁЯСЙ call Now!


    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). Correct Answer Incorrect Answer
    B A NullPointerException will occur. Correct Answer Incorrect Answer
    C A compile-time error will occur. Correct Answer Incorrect Answer
    D The array length will be incorrect, but the string length will be correct. Correct Answer Incorrect Answer
    E The string length will be incorrect, but the array length will be correct. Correct Answer Incorrect Answer

    Solution

    Correct Answer: C (In Java, arrays use .length (a field), while String objects use .length() (a method). The code swaps these, leading to compile-time errors.)

    Practice Next
    More IT Operating System Questions
    ask-question