Java OCA OCP Practice Question 451

Question

Which of the following are legal? (Choose all that apply.)

  • A. for (int i=0, j=1; i<10; i++, j++)
  • B. for (int i=0, j=1;; i++, j++)
  • C. for (int i=0, float j=1; ; i++, j++)
  • D. for (String s = ""; s.length()<10; s += '!')


A, B, D.

Note

A and B are using multiple initialization and increment parts, which are legal.

In B, the test part is empty, so the loop will run forever unless it hits a break statement, throws an exception, or does something equally.

C is illegal because only one type may be declared in the initialization.

D is correct because it uses strings rather than the more commonly seen ints.




PreviousNext

Related