How many times is 2 printed as a part of the output?

Given:

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

How many times is 2 printed as a part of the output?

Given:

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

How many times is 2 printed as a part of the output?

A.
Zero

B.
Once

C.
Twice

D.
3 times

E.
Compilation fails.

Explanation:
Compilation fails because of missing curly brackets.



Leave a Reply 5

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


Abed Abu Alhalawa

Abed Abu Alhalawa

D

Sheky Wang

Sheky Wang

D,watashitoomou

James

James

The Answer is D. This question demonstrates the use of Java’s enhanced for loop, which was introduced in J2SE 5.0. The code above prints the following:

aa,0
aa,1
aa,2
bb,0
bb,1
bb,2
cc,0
cc,1
cc,2

BT

BT

What about the two missing curly brackets ?
Would this not make the compilation fail as in the answer D ?

BT

BT

The two curly brackets that should be at the bottom of the script.