Java Technical Interview Programming Questions
Java Technical Interview Programming Questions
Programming Questions
This Programming Questions were Asked in Tech Mahindra, TCS, Wipro,
Infosys, Accenture, Capgemini and Many Other Service and Product Based
Companies
ch
Input .te
a = 10
de
b = 20
co
Output
n2
a = 20
ar
b = 10
le
Explanation
To swap two numbers without using a third variable, you can use arithmetic
operations to interchange the values.
Program
2. Reverse a Number
Problem Statement
Write a program to reverse a number.
Input
ch
.te
1234
de
co
Output
n2
4321
ar
Explanation
le
To reverse a number, repeatedly extract the last digit and build the reversed
number.
Program
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
3. Reverse a String
Problem Statement
Write a program to reverse a string.
Input
ch
"ABCD"
.te
Output
de
co
"DCBA"
n2
Explanation
ar
Program
4. Fibonacci Series
Problem Statement
ch
Write a program to print the Fibonacci series up to 10 terms.
.te
Output
de
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
co
Explanation
n2
The Fibonacci series is a sequence where each number is the sum of the two
ar
preceding ones.
le
Program
Input
1221
Output
ch
.te
de
1221 is a Palindrome
co
Explanation
n2
Program
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
ch
Write a program to check if a string is a palindrome.
.te
Input
de
"DAD"
co
Output
n2
DAD is a Palindrome
ar
le
Explanation
A palindrome string reads the same backward as forward. Reverse the string
and compare it to the original.
Program
if (str.equals(reversed)) {
Input
ch
.te
2, 10
de
Output
co
Explanation
le
A prime number is a number greater than 1 with no divisors other than 1 and
itself.
Program
ch
}
.te
}
de
Problem Statement
n2
Input
le
Output
2 is a Prime Number
Explanation
Check if a number has no divisors other than 1 and itself.
Program
if (isPrime(num)) {
System.out.println(num + " is a Prime Number");
} else {
System.out.println(num + " is not a Prime Numbe
r");
}
}
ch
return false;
}
.te
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
de
return false;
co
}
}
n2
return true;
}
ar
}
le
Input
Output
Explanation
The factorial of a number is the product of all positive integers less than or
equal to that number.
Program
ch
for (int i = 1; i <= num; i++) {
factorial *= i;
.te
}
de
}
n2
}
ar
Problem Statement
Write a program to check if a number is an Armstrong number.
Input
153
Output
Program
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, 3);
ch
num /= 10;
.te
}
de
if (sum == original) {
System.out.println(original + " is an Armstrong
co
Number");
n2
} else {
System.out.println(original + " is not an Armst
ar
rong Number");
}
le
}
}
Input
1234
Explanation
To count the number of digits, repeatedly divide the number by 10 until it
becomes 0, counting the iterations.
Program
ch
int num = 1234;
int count = 0;
.te
while (num != 0) {
de
num /= 10;
co
count++;
}
n2
t);
le
}
}
Input
1234
Even Numbers: 2
Odd Numbers: 2
Explanation
To count even and odd digits, extract each digit and check if it's divisible by 2.
Program
ch
int oddCount = 0; .te
while (num != 0) {
int digit = num % 10;
de
if (digit % 2 == 0) {
co
evenCount++;
} else {
n2
oddCount++;
}
ar
num /= 10;
le
Output
Even Number
Explanation
A number is even if it is divisible by 2, otherwise it is odd.
Program
ch
public static void main(String[] args) {
.te
int num = 2;
de
if (num % 2 == 0) {
System.out.println(num + " is an Even Number");
co
} else {
n2
}
}
le
Input
1234
Output
Explanation
To find the sum of digits, extract each digit and add them together.
Program
while (num != 0) {
ch
int digit = num % 10;
sum += digit;
.te
num /= 10;
}
de
}
n2
}
ar
Problem Statement
Write a program to find the largest number among three numbers.
Input
a = 10, b = 20, c = 30
Output
Largest Number is 30
Program
ch
System.out.println("Largest Number is " + largest);
.te
}
}
de
co
Problem Statement
ar
Explanation
Use Math.random() for generating random numbers and a loop for random
strings.
Program
import java.util.Random;
ch
g.toString());
.te
}
}
de
co
Problem Statement
ar
Input
a[] = {1, 2, 3, 4}
Output
10
Explanation
To find the sum of elements, iterate through the array and add each element.
Program
ch
18. Print Even and Odd Numbers in an Array .te
Problem Statement
de
Input
n2
a[] = {1, 2, 3, 4}
ar
le
Output
Even Numbers: 2, 4
Odd Numbers: 1, 3
Explanation
To print even and odd numbers, iterate through the array and check each
number's divisibility by 2.
Program
ch
}
.te
}
de
Problem Statement
n2
Input
le
a[] = {1, 2, 3, 4}
b[] = {1, 2, 3, 4}
Output
Explanation
Two arrays are equal if they have the same length and corresponding elements
are equal.
import java.util.Arrays;
if (Arrays.equals(a, b)) {
System.out.println("a and b are equal");
} else {
System.out.println("a and b are not equal");
}
}
ch
}
.te
de
Problem Statement
n2
Input
le
a[] = {1, 2, 3, 5}
Output
Missing number is 4
Explanation
To find the missing number, calculate the expected sum and subtract the actual
sum from it.
Program
ch
}
}
.te
de
Problem Statement
n2
Input
le
a[] = {3, 4, 5, 6}
Output
Max is 6
Min is 3
Explanation
To find the maximum and minimum elements, iterate through the array and
keep track of the largest and smallest values.
) {
ch
min = a[i];
.te
}
}
de
}
ar
le
Input
a[] = {0, 1, 0, 1, 0, 1}
Output
0, 0, 0, 1, 1, 1
Program
ch
}
.te
for (int i = 0; i < count; i++) {
de
a[i] = 0;
}
co
n2
}
le
a[] = {1, 2, 3, 1, 2, 3, 4}
Output
1, 2, 3
Explanation
To find duplicates, use a nested loop to compare each element with the others.
Program
import java.util.HashSet;
ch
.te
public class learn2code {
public static void main(String[] args) {
de
if (!seen.add(a[i])) {
duplicates.add(a[i]);
le
}
}
System.out.print("Duplicates: ");
for (int num : duplicates) {
System.out.print(num + " ");
}
}
}
Input
a[] = {1, 2, 3, 4}
Output
Explanation
To find the second largest element, maintain two variables for the largest and
ch
second largest values. .te
Program
de
Input
Output
1, 3, 12, 0, 0
ch
.te
de
Explanation
co
To move all zeros to the end, iterate through the array and place non-zero
elements at the front, then fill the rest with zeros.
n2
Program
ar
le
Input
ch
.te
a[] = {1, 2, 3, 4}
de
b[] = {3, 4, 5, 6}
co
Output
n2
3, 4
ar
le
Explanation
The intersection of two arrays is the set of elements that are present in both
arrays.
Program
import java.util.HashSet;
System.out.print("Intersection: ");
for (int i = 0; i < b.length; i++) {
if (set.contains(b[i])) {
System.out.print(b[i] + " ");
}
}
}
}
Input
n2
a[] = {1, 2, 3, 4}
ar
b[] = {3, 4, 5, 6}
le
Output
1, 2, 3, 4, 5, 6
Explanation
The union of two arrays is the set of all distinct elements present in both arrays.
Program
import java.util.HashSet;
System.out.print("Union: ");
for (int num : set) {
ch
System.out.print(num + " ");
.te
}
}
de
}
co
By practicing these problems, you’ll not only prepare for interviews but also
n2