int j; for(int i=0;i<14;i++) { if(i<10) { j = 2 + i; } System.out.println("j: " + j + " i: " + i); } What is WRONG with the above code?

  1. Integer "j" is not initialized.

  2. Nothing.

  3. You cannot declare integer i inside the for-loop declaration.

  4. The syntax of the "if" statement is incorrect.


Correct Option: B
Explanation:

To solve this question, the user should have knowledge of basic Java syntax and control flow statements.

The code is a basic for-loop that initializes an integer variable i to 0 and then increments it by 1 with each iteration until it reaches 13. Inside the loop, there is an if statement that checks if i is less than 10. If it is, it initializes an integer variable j to 2 + i. Then, it prints out the values of j and i.

Now, let's go through each option and explain why it is right or wrong:

A. Integer "j" is not initialized: This option is incorrect. The integer "j" is initialized inside the for-loop. Although it is not initialized before the loop, it is initialized before it is used.

B. Nothing: This option is incorrect. There is something wrong with the code.

C. You cannot declare integer i inside the for-loop declaration: This option is incorrect. The declaration of integer i inside the for-loop is valid Java syntax.

D. The syntax of the "if" statement is incorrect: This option is incorrect. The syntax of the if statement in the code is correct.

Therefore, the correct answer is:

The Answer is: B

Find more quizzes: