Multiple choice technology programming languages

Given: 11. public static void test(String str) { 12. int check = 4; 13. if (check = str.length()) { 14. System.out.print(str.charAt(check -= 1) +“, “); 15. } else { 16. System.out.print(str.charAt(0) + “, “); 17. } 18. } and the invocation: 21. test(”four”); 22. test(”tee”); 23. test(”to”); What is the result?

  1. r, t, t,

  2. r, e, o,

  3. Compilation fails.

  4. An exception is thrown at runtime.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Line 13 attempts to assign an integer (str.length() returns int) to a boolean variable (check is int, but if condition requires boolean). In Java, check = str.length() is an assignment, not a comparison, and the types are incompatible for the if condition.