JAVA : What will be the output of the following Java code snippet?

Published: 05 May 2023
on channel: SDET Automation Testing Interview Pro
741
26

JAVA : What will be the output of the following Java code snippet?

SDET Automation Testing Interview Questions & Answers

We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.

JAVA : What will be the output of the following Java code snippet?

int[] arr = {10, 20, 30, 40, 50};
for(int i=0; i(arr.length; i++) {
if(arr[i] == 30) {
continue;
}
System.out.print(arr[i] + " ");
}

Options:

A) 10 20 40 50
B) 10 20 30 40 50
C) 10 20 50
D) 30 40 50

Answer: Option A) 10 20 40 50

Explanation:

In the given code snippet, an integer array arr is initialized with five elements.

A for loop is used to iterate over each element of the array.

Inside the loop, an if condition checks if the current element is equal to 30.

If the condition is true, then the continue statement is executed, which skips the remaining code inside the loop for that iteration and jumps to the next iteration.

Therefore, when the loop encounters the element with value 30, it skips printing it and moves on to the next iteration. For all other elements, the print statement inside the loop is executed, which prints the element followed by a space.

As a result, the output of this code will be: 10 20 40 50. Therefore, the correct answer is A) 10 20 40 50.


Watch video JAVA : What will be the output of the following Java code snippet? online without registration, duration hours minute second in high quality. This video was added by user SDET Automation Testing Interview Pro 05 May 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 741 once and liked it 26 people.