Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 80
Question ID: UKOCP14416
Consider below code of Test.java file:
- package com.udayankhattry.ocp;
- public class Test {
- public static void main(String[] args) {
- int [] arr = {1, 2, 3, 4, 5};
- int x = 0;
- for(/*INSERT*/) {
- x += arr[n];
- }
- System.out.println(x);
- }
- }
Which 3 options, if used to replace /*INSERT*/, on execution will print 9 on to the console?
-
A
int n = 0; n < arr.length; n++ -
B
int n = 0; n < arr.length; n += 2 -
C
int n = 3; n < arr.length; n++ -
D
int n = 1; n < arr.length - 1; n++ -
E
int n = 1; n < arr.length; n += 2
Reveal correct answers
Correct answers: B, C, D
Explanation
UKOCP14416:
Logic in for loop is adding array elements. You need to find out which array elements when added will result in 9. Possible options are: {1+3+5, 2+3+4, 4+5}.
Based on these 3 combinations you can select 3 correct options.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
