What is the result?

Given:

01. public static void test(String str) {
02. int check = 4;
03. if (check = str.length()) {
04. System.out.print(str.charAt(check -= 1) +”, “);
05. } else {
06. System.out.print(str.charAt(0) + “, “);
07. }
08. }

and the invocation:

test(“four”);
test(“tee”);
test(“to”);

What is the result?

Given:

01. public static void test(String str) {
02. int check = 4;
03. if (check = str.length()) {
04. System.out.print(str.charAt(check -= 1) +”, “);
05. } else {
06. System.out.print(str.charAt(0) + “, “);
07. }
08. }

and the invocation:

test(“four”);
test(“tee”);
test(“to”);

What is the result?

A.
r, t, t,

B.
r, e, o,

C.
Compilation fails.

D.
An exception is thrown at runtime.

Explanation:
Main.java:3: incompatible types
found : int
required: boolean
if (check = str.length()) {
^
1 error



Leave a Reply 0

Your email address will not be published. Required fields are marked *