What is the result?

Given:

public class DoCompare4 {
public static void main(String[] args) {
String[] table = {“aa”, “bb”, “cc”};
int ii =0;
do
while (ii < table.length)
System.out.println(ii++);
while (ii < table.length);
}
}

What is the result?

Given:

public class DoCompare4 {
public static void main(String[] args) {
String[] table = {“aa”, “bb”, “cc”};
int ii =0;
do
while (ii < table.length)
System.out.println(ii++);
while (ii < table.length);
}
}

What is the result?

A.
0

B.
0
1
2

C.
0
1
2
3

D.
Compilation fails

Explanation:
table.length is 3. So the do-while loop will run 3 times with ii=0, ii=1 and ii=2. The second while statement will break the do-loop when ii = 3. Note: The Java programming language provides a do-while statement, which can be expressed as follows:
do {
statement(s)
} while (expression);



Leave a Reply 3

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


Charlotte

Charlotte

I really do hope that you are going to be elaborating more on this topic. I was hoping for a bit more information.

Jazz

Jazz

“B”

Explanation:

do{
//statements
}while(condition);

Compiler searches for :

do
while(condition); //no need of Curly Braces

James

James

The Answer is B. It is difficult to see this without proper indenting of the code.