Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Arrays 1D Practice

One-dimensional arrays: traversal, searching, sorting helpers, merging and pairs.

1
Sum of Array Elements
Easy
Read N and then N integers. Print their sum.
First line: N. Second line: N integers.
A single integer: the sum.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
15
Sample Input 2
3
10 -2 4
Sample Output 2
12
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
4
-1 -1 -1 -1
Sample Output 4
-4
Sample Input 5
6
1 1 1 1 1 1
Sample Output 5
6
1
Loop and accumulate.
2
Largest Element
Easy
Read N and then N integers. Print the largest element.
First line: N. Second line: N integers.
A single integer: the maximum.
Sample Input 1
5
3 7 2 9 4
Sample Output 1
9
Sample Input 2
4
-5 -2 -8 -1
Sample Output 2
-1
Sample Input 3
1
100
Sample Output 3
100
Sample Input 4
6
1 1 1 1 1 1
Sample Output 4
1
Sample Input 5
5
10 20 30 40 50
Sample Output 5
50
1
Track a running maximum.
3
Smallest Element
Easy
Read N and then N integers. Print the smallest element.
First line: N. Second line: N integers.
A single integer: the minimum.
Sample Input 1
5
3 7 2 9 4
Sample Output 1
2
Sample Input 2
4
-5 -2 -8 -1
Sample Output 2
-8
Sample Input 3
1
100
Sample Output 3
100
Sample Input 4
6
1 1 1 1 1 1
Sample Output 4
1
Sample Input 5
5
50 40 30 20 10
Sample Output 5
10
1
Track a running minimum.
4
Average of Array
Easy
Read N and then N integers. Print the integer average (floor of the exact average).
First line: N. Second line: N integers.
A single integer: floor(sum / N).
Sample Input 1
5
1 2 3 4 5
Sample Output 1
3
Sample Input 2
4
10 20 30 40
Sample Output 2
25
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
3
1 1 2
Sample Output 4
1
Sample Input 5
5
10 20 30 40 50
Sample Output 5
30
1
sum / n using integer division.
5
Count Positive Numbers
Easy
Read N and then N integers. Print how many are positive (> 0).
First line: N. Second line: N integers.
A single integer: the count of positives.
Sample Input 1
5
3 -1 0 7 2
Sample Output 1
3
Sample Input 2
3
-1 -2 -3
Sample Output 2
0
Sample Input 3
1
0
Sample Output 3
0
Sample Input 4
4
1 2 3 4
Sample Output 4
4
Sample Input 5
6
-1 0 1 2 -3 4
Sample Output 5
3
1
Count arr[i] > 0.
1
Reverse Array
Medium
Read N and then N integers. Print them in reverse order on one line, space-separated.
First line: N. Second line: N integers.
The reversed array on one line.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
5 4 3 2 1
Sample Input 2
3
10 20 30
Sample Output 2
30 20 10
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
4
-1 0 1 2
Sample Output 4
2 1 0 -1
Sample Input 5
6
1 1 2 2 3 3
Sample Output 5
3 3 2 2 1 1
1
Loop from N-1 down to 0.
2
Count Even Numbers
Medium
Read N and then N integers. Print how many are even.
First line: N. Second line: N integers.
A single integer: the count of evens.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
2
Sample Input 2
4
2 4 6 8
Sample Output 2
4
Sample Input 3
1
3
Sample Output 3
0
Sample Input 4
5
1 1 1 1 2
Sample Output 4
1
Sample Input 5
6
2 4 6 8 10 12
Sample Output 5
6
1
Count arr[i] % 2 == 0.
3
Search Element
Medium
Read N, then N integers, then a target X. Print "Found" if X is present, otherwise "Not Found".
First line: N. Second line: N integers. Third line: X.
"Found" or "Not Found".
Sample Input 1
5
3 7 2 9 4
7
Sample Output 1
Found
Sample Input 2
4
1 2 3 4
9
Sample Output 2
Not Found
Sample Input 3
1
5
5
Sample Output 3
Found
Sample Input 4
3
-1 -2 -3
-2
Sample Output 4
Found
Sample Input 5
5
1 2 3 4 5
0
Sample Output 5
Not Found
1
Linear search for X.
4
Second Largest
Medium
Read N and then N integers. Print the second largest element. N >= 2 and all elements are distinct.
First line: N. Second line: N integers.
A single integer: the second largest.
Sample Input 1
5
3 7 2 9 4
Sample Output 1
7
Sample Input 2
4
10 20 30 40
Sample Output 2
30
Sample Input 3
2
5 6
Sample Output 3
5
Sample Input 4
5
100 50 75 25 60
Sample Output 4
75
Sample Input 5
3
-1 -2 -3
Sample Output 5
-2
2
Track the largest and second largest.
5
Frequency of Target
Medium
Read N, then N integers, then a target X. Print how many times X appears.
First line: N. Second line: N integers. Third line: X.
A single integer: the count of X.
Sample Input 1
5
3 7 3 9 3
3
Sample Output 1
3
Sample Input 2
4
1 2 3 4
5
Sample Output 2
0
Sample Input 3
1
7
7
Sample Output 3
1
Sample Input 4
6
1 1 1 1 1 1
1
Sample Output 4
6
Sample Input 5
4
2 4 6 8
4
Sample Output 5
1
1
Count matches.
6
Replace Negatives with Zero
Medium
Read N and then N integers. Print the array with every negative element replaced by 0.
First line: N. Second line: N integers.
The modified array on one line.
Sample Input 1
5
3 -1 0 -7 2
Sample Output 1
3 0 0 0 2
Sample Input 2
3
-1 -2 -3
Sample Output 2
0 0 0
Sample Input 3
1
5
Sample Output 3
5
Sample Input 4
4
0 0 0 0
Sample Output 4
0 0 0 0
Sample Input 5
6
1 -2 3 -4 5 -6
Sample Output 5
1 0 3 0 5 0
1
if (arr[i] < 0) arr[i] = 0;
7
First and Last Elements
Medium
Read N and then N integers. Print the first and last elements on one line, separated by a space.
First line: N. Second line: N integers.
Two integers: first and last, space-separated.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
1 5
Sample Input 2
3
10 20 30
Sample Output 2
10 30
Sample Input 3
1
7
Sample Output 3
7 7
Sample Input 4
4
-1 0 1 2
Sample Output 4
-1 2
Sample Input 5
6
1 1 2 2 3 3
Sample Output 5
1 3
1
Print arr[0] and arr[N-1].
1
Is Array Sorted
Hard
Read N and then N integers. Print "Sorted" if the array is sorted in non-decreasing order, otherwise "Not Sorted".
First line: N. Second line: N integers.
"Sorted" or "Not Sorted".
Sample Input 1
5
1 2 3 4 5
Sample Output 1
Sorted
Sample Input 2
4
1 3 2 4
Sample Output 2
Not Sorted
Sample Input 3
1
7
Sample Output 3
Sorted
Sample Input 4
5
5 5 5 5 5
Sample Output 4
Sorted
Sample Input 5
3
3 2 1
Sample Output 5
Not Sorted
1
Check every adjacent pair.
2
Remove Duplicates
Hard
Read N and then N integers that are already sorted in non-decreasing order. Print the array with duplicates removed, preserving order.
First line: N. Second line: N sorted integers.
The deduplicated array on one line.
Sample Input 1
6
1 1 2 2 3 3
Sample Output 1
1 2 3
Sample Input 2
5
1 2 3 4 5
Sample Output 2
1 2 3 4 5
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
4
1 1 1 1
Sample Output 4
1
Sample Input 5
8
1 1 2 3 3 3 4 4
Sample Output 5
1 2 3 4
1
Keep an element if it differs from the previous kept one.
3
Pair Sum Check
Hard
Read N, then N integers, then a target sum S. Print "Yes" if any two distinct elements add up to S, otherwise "No".
First line: N. Second line: N integers. Third line: S.
"Yes" or "No".
Sample Input 1
5
1 2 3 4 5
9
Sample Output 1
Yes
Sample Input 2
4
1 2 3 4
10
Sample Output 2
No
Sample Input 3
2
5 6
11
Sample Output 3
Yes
Sample Input 4
5
1 2 3 4 5
1
Sample Output 4
No
Sample Input 5
6
-1 0 1 2 3 4
1
Sample Output 5
Yes
2
Nested loops checking all pairs.
4
Max Difference
Hard
Read N and then N integers. Print the maximum absolute difference between any two elements.
First line: N. Second line: N integers.
A single integer: max |arr[i] - arr[j]|.
Sample Input 1
5
3 7 2 9 4
Sample Output 1
7
Sample Input 2
4
10 20 30 40
Sample Output 2
30
Sample Input 3
1
5
Sample Output 3
0
Sample Input 4
3
-1 -2 -3
Sample Output 4
2
Sample Input 5
6
1 2 3 4 5 6
Sample Output 5
5
1
max - min gives the largest absolute difference.
5
Rotate Right by One
Hard
Read N and then N integers. Print the array rotated right by one position (last element moves to the front).
First line: N. Second line: N integers.
The rotated array on one line.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
5 1 2 3 4
Sample Input 2
3
10 20 30
Sample Output 2
30 10 20
Sample Input 3
1
7
Sample Output 3
7
Sample Input 4
4
1 2 3 4
Sample Output 4
4 1 2 3
Sample Input 5
6
-1 0 1 2 3 4
Sample Output 5
4 -1 0 1 2 3
1
Save the last element, shift the rest right, put it at index 0.
6
Array Equality
Hard
Read N, then N integers for array A, then N integers for array B. Print "Equal" if the two arrays are identical element-by-element, otherwise "Not Equal".
First line: N. Second line: A. Third line: B.
"Equal" or "Not Equal".
Sample Input 1
3
1 2 3
1 2 3
Sample Output 1
Equal
Sample Input 2
3
1 2 3
1 2 4
Sample Output 2
Not Equal
Sample Input 3
1
5
5
Sample Output 3
Equal
Sample Input 4
4
1 2 3 4
4 3 2 1
Sample Output 4
Not Equal
Sample Input 5
3
1 1 1
1 1 1
Sample Output 5
Equal
1
Compare each index.
7
Sum of Odd Index Elements
Hard
Read N and then N integers. Print the sum of elements at odd indices (1, 3, 5, ... 0-based).
First line: N. Second line: N integers.
A single integer: the sum.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
6
Sample Input 2
4
10 20 30 40
Sample Output 2
60
Sample Input 3
1
7
Sample Output 3
0
Sample Input 4
3
1 2 3
Sample Output 4
2
Sample Input 5
6
1 1 1 1 1 1
Sample Output 5
3
1
for (int i = 1; i < n; i += 2) sum += arr[i];
8
Merge Two Sorted Arrays
Hard
Read N, then N sorted integers for A, then M, then M sorted integers for B. Print the merged sorted array on one line.
First line: N. Second line: A. Third line: M. Fourth line: B.
The merged sorted array on one line.
Sample Input 1
3
1 3 5
3
2 4 6
Sample Output 1
1 2 3 4 5 6
Sample Input 2
2
1 2
2
3 4
Sample Output 2
1 2 3 4
Sample Input 3
1
5
1
2
Sample Output 3
2 5
Sample Input 4
4
1 1 2 3
3
1 4 5
Sample Output 4
1 1 1 2 3 4 5
Sample Input 5
2
10 20
3
1 5 15
Sample Output 5
1 5 10 15 20
1
Two-pointer merge.
Competitive MCQs — Java Arrays 1D
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 35
Q1
What is the index of the first element of a Java array?
Correct!
Wrong — correct answer is .
Java arrays are 0-indexed.
Q2
What is the output of this code?
java
1
2
int[] a = {1, 2, 3};
System.out.println(a[1]);
Correct!
Wrong — correct answer is .
a[1] is the second element: 2.
Q3
What is the output of this code?
int[] a = {1, 2, 3};
System.out.println(a[1]);
Correct!
Wrong — correct answer is .
a[1] is the second element: 2.
Q4
Which property gives the number of elements in an array?
Correct!
Wrong — correct answer is .
Arrays use the length field (no parentheses).
Q5
What is the output of this code?
java
1
2
int[] a = new int[3];
System.out.println(a[0]);
Correct!
Wrong — correct answer is .
Numeric array elements default to 0.
Q6
What is the output of this code?
int[] a = new int[3];
System.out.println(a[0]);
Correct!
Wrong — correct answer is .
Numeric array elements default to 0.
Q7
What happens when you access a[-1] or a[5] on a 3-element array?
Correct!
Wrong — correct answer is .
Invalid indices throw ArrayIndexOutOfBoundsException at runtime.
Q8
What is the output of this code?
java
1
2
3
int[] a = {5, 1, 4};
Arrays.sort(a);
System.out.println(a[0]);
Correct!
Wrong — correct answer is .
After sorting, the first element is the smallest, 1.
Q9
What is the output of this code?
int[] a = {5, 1, 4};
Arrays.sort(a);
System.out.println(a[0]);
Correct!
Wrong — correct answer is .
After sorting, the first element is the smallest, 1.
Q10
Which loop prints every element of int[] nums?
Correct!
Wrong — correct answer is .
For-each visits each element; the alternatives mis-index.
Q11
What is the output of this code?
java
1
2
3
4
int[] a = {1, 2, 3, 4};
int sum = 0;
for (int v : a) sum += v;
System.out.println(sum);
Correct!
Wrong — correct answer is .
Sums 1+2+3+4 = 10.
Q12
What is the output of this code?
int[] a = {1, 2, 3, 4};
int sum = 0;
for (int v : a) sum += v;
System.out.println(sum);
Correct!
Wrong — correct answer is .
Sums 1+2+3+4 = 10.
Q13
Which correctly copies the first 3 elements of a 5-element array a?
Correct!
Wrong — correct answer is .
Arrays.copyOf(a, 3) returns a 3-element copy.
Q14
What is the output of this code?
java
1
2
int[] a = {2, 4, 6};
System.out.println(a.length);
Correct!
Wrong — correct answer is .
a.length is the element count: 3.
Q15
What is the output of this code?
int[] a = {2, 4, 6};
System.out.println(a.length);
Correct!
Wrong — correct answer is .
a.length is the element count: 3.
Q16
What is the output of this code?
int[] a = {10, 20, 30};
System.out.println(a[a.length - 1]);
Correct!
Wrong — correct answer is .
a.length - 1 is the last index, holding 30.
Q17
Which statement declares and initializes an array?
Correct!
Wrong — correct answer is .
The braces initializer works at declaration with int[] syntax.
Q18
What is the output of this code?
int[] a = new int[5];
System.out.println(a[4]);
Correct!
Wrong — correct answer is .
Numeric array cells default to 0, so a[4] is 0.
Q19
Which exception does an invalid array index cause?
Correct!
Wrong — correct answer is .
Negative or oversized indexes throw ArrayIndexOutOfBoundsException.
Q20
What is the output of this code?
int[] a = {1, 2, 3};
int sum = 0;
for (int v : a) sum += v;
System.out.println(sum);
Correct!
Wrong — correct answer is .
The enhanced loop sums 1+2+3 = 6.
Q21
Which code finds the largest value in int[] a?
Correct!
Wrong — correct answer is .
Java has no built-in max for arrays; loop and track the largest.
Q22
What is the output of this code?
int[] a = {5, 3, 8};
System.out.println(a[0] + a[2]);
Correct!
Wrong — correct answer is .
a[0] is 5 and a[2] is 8; their sum is 13.
Q23
How do you copy an array reference without copying elements?
Correct!
Wrong — correct answer is .
Assignment shares the same array; clone/copyOf create real copies.
Q24
What is the output of this code?
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);
Correct!
Wrong — correct answer is .
b and a reference the same array, so the change is visible through a.
Q25
Which method fills an array with a value?
Correct!
Wrong — correct answer is .
java.util.Arrays.fill(array, value) sets every element.
Q26
What is the output of this code?
int[] a = {1, 2, 3, 4};
for (int i = a.length - 1; i >= 0; i--) {
System.out.print(a[i] + " ");
}
Correct!
Wrong — correct answer is .
Iterating backward prints the array in reverse.
Q27
What is the output of this code?
int[] a = {4, 1, 3};
java.util.Arrays.sort(a);
System.out.println(a[1]);
Correct!
Wrong — correct answer is .
After sorting to {1, 3, 4}, a[1] is 3.
Q28
Which expression swaps a[0] and a[1]?
Correct!
Wrong — correct answer is .
A temp variable is needed to avoid overwriting one value.
Q29
What is the output of this code?
int[] a = {7, 7, 7};
int c = 0;
for (int v : a) if (v == 7) c++;
System.out.println(c);
Correct!
Wrong — correct answer is .
All three elements equal 7, so the count is 3.
Q30
What is an array's default reference value when uninitialized?
Correct!
Wrong — correct answer is .
A declared but not created array variable is null.
Q31
Which creates an array of 3 Strings?
Correct!
Wrong — correct answer is .
new String[3] allocates a String array of length 3.
Q32
What is the output of this code?
int[] a = {2, 4};
System.out.println(a.length + a[0]);
Correct!
Wrong — correct answer is .
a.length is 2 plus a[0] = 2 gives 4.
Q33
Which of these correctly prints each element of a?
Correct!
Wrong — correct answer is .
Indexing 0..length-1 safely visits every element.
Q34
What is the output of this code?
int[] a = new int[3];
a[1] = 5;
System.out.println(a[1] + a[0]);
Correct!
Wrong — correct answer is .
a[1] is 5 and a[0] defaults to 0, totaling 5.
Q35
What is the output of this code?
int[] a = {1, 2};
System.out.println(a[2]);
Correct!
Wrong — correct answer is .
Index 2 is out of bounds for a length-2 array.