int[] nums = {2, 4, 6};
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
Question 6
What is printed?
int[] nums = {3, 7, 2, 8};
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
System.out.println(sum);
Question 7
What is printed?
int[] nums = {1, 2, 3, 4};
int total = 0;
for (int n : nums) {
total += n;
}
System.out.println(total);
Question 8
What is printed?
int[] nums = {5, 12, 8, 3, 15, 7};
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 6) {
count++;
}
}
System.out.println(count);
Question 9
What is printed?
int[] nums = {4, 9, 2, 7, 5};
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
System.out.println(max);
Question 10
What is printed?
int[] nums = {2, 4, 6, 8};
for (int i = 0; i < nums.length; i++) {
nums[i] = nums[i] * 2;
}
System.out.println(nums[0] + " " + nums[3]);
Section 2: Searching & Finding (Questions 11-20)
Question 11
What is printed?
int[] nums = {8, 3, 12, 5, 1, 9};
int min = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
System.out.println(min);
Question 12
What is printed? (Linear search with break)
int[] nums = {10, 25, 30, 25, 40};
int target = 25;
int index = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
index = i;
break;
}
}
System.out.println(index);
Question 13
What is printed? (No break)
int[] nums = {10, 25, 30, 25, 40};
int target = 25;
int index = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
index = i;
}
}
System.out.println(index);
Question 14
What is printed?
int[] nums = {4, 8, 15, 16, 23};
int target = 15;
boolean found = false;
for (int n : nums) {
if (n == target) {
found = true;
break;
}
}
System.out.println(found);
Question 15
What is printed?
int[] nums = {80, 90, 85, 95};
int sum = 0;
for (int n : nums) {
sum += n;
}
double avg = (double) sum / nums.length;
System.out.println(avg);
Question 16
What is printed?
int[] nums = {3, 8, 12, 7, 4, 9};
int even = 0, odd = 0;
for (int n : nums) {
if (n % 2 == 0) even++;
else odd++;
}
System.out.println(even + " " + odd);
Question 17
What is printed?
int[] nums = {1, 2, 3, 4, 5};
for (int i = nums.length - 1; i >= 0; i--) {
System.out.print(nums[i] + " ");
}
Question 18
What is printed?
int[] nums = {3, 6, 9, 12, 15, 18};
int sum = 0;
for (int n : nums) {
if (n % 2 == 0) {
sum += n;
}
}
System.out.println(sum);
int[] nums = {10, 20, 30, 40, 50};
int first = nums[0];
for (int i = 0; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
nums[nums.length - 1] = first;
System.out.println(nums[0] + " " + nums[1] + " " + nums[4]);
Question 22
What is printed after shifting right?
int[] nums = {10, 20, 30, 40, 50};
int last = nums[nums.length - 1];
for (int i = nums.length - 1; i > 0; i--) {
nums[i] = nums[i - 1];
}
nums[0] = last;
System.out.println(nums[0] + " " + nums[1] + " " + nums[4]);
Question 23
What is printed?
int[] nums = {5, 3, 5, 7, 5, 9, 5};
int target = 5;
int count = 0;
for (int n : nums) {
if (n == target) count++;
}
System.out.println(count);
Question 24
What is printed?
int[] nums = {3, 7, 12, 15, 20};
boolean sorted = true;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
sorted = false;
break;
}
}
System.out.println(sorted);
Question 25
What is the second largest value?
int[] nums = {12, 45, 7, 38, 23};
int max = nums[0], second = nums[0];
for (int n : nums) {
if (n > max) {
second = max;
max = n;
} else if (n > second && n != max) {
second = n;
}
}
System.out.println(second);
Question 26
What is printed after removing element at index 2?