0% found this document useful (0 votes)
9 views

Java Technical Interview Programming Questions

Uploaded by

shaktisaali30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Technical Interview Programming Questions

Uploaded by

shaktisaali30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Java Technical Interview

Programming Questions
This Programming Questions were Asked in Tech Mahindra, TCS, Wipro,
Infosys, Accenture, Capgemini and Many Other Service and Product Based
Companies

1. Swap Two Numbers


Problem Statement
Write a program to swap two numbers without using a third variable.

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

public class learn2code {


public static void main(String[] args) {
int a = 10;
int b = 20;

// Swapping without a third variable

Java Technical Interview Programming Questions 1


a = a + b; // a = 30
b = a - b; // b = 10
a = a - b; // a = 20

System.out.println("a = " + a);


System.out.println("b = " + b);
}
}

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

public class learn2code {


public static void main(String[] args) {
int num = 1234;
int reversed = 0;

while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;

Java Technical Interview Programming Questions 2


num /= 10;
}

System.out.println("Reversed Number: " + reversed);


}
}

3. Reverse a String
Problem Statement
Write a program to reverse a string.

Input

ch
"ABCD"
.te
Output
de
co

"DCBA"
n2

Explanation
ar

To reverse a string, convert it to a character array and swap characters from


le

the ends towards the center.

Program

public class learn2code {


public static void main(String[] args) {
String str = "ABCD";
char[] chars = str.toCharArray();
int left = 0, right = chars.length - 1;

while (left < right) {


char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;

Java Technical Interview Programming Questions 3


left++;
right--;
}

String reversedStr = new String(chars);


System.out.println("Reversed String: " + reversedSt
r);
}
}

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

public class learn2code {


public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;

System.out.print(a + ", " + b);

for (int i = 2; i < n; i++) {


int next = a + b;
System.out.print(", " + next);
a = b;

Java Technical Interview Programming Questions 4


b = next;
}
}
}

5. Check if a Number is a Palindrome


Problem Statement
Write a program to check if a number is a palindrome.

Input

1221

Output
ch
.te
de

1221 is a Palindrome
co

Explanation
n2

A palindrome is a number that reads the same backward as forward. Reverse


the number and compare it to the original.
ar
le

Program

public class learn2code {


public static void main(String[] args) {
int num = 1221;
int original = num;
int reversed = 0;

while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}

Java Technical Interview Programming Questions 5


if (original == reversed) {
System.out.println(original + " is a Palindrom
e");
} else {
System.out.println(original + " is not a Palind
rome");
}
}
}

6. Check if a String is a Palindrome


Problem Statement

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

public class learn2code {


public static void main(String[] args) {
String str = "DAD";
String reversed = new StringBuilder(str).reverse().
toString();

if (str.equals(reversed)) {

Java Technical Interview Programming Questions 6


System.out.println(str + " is a Palindrome");
} else {
System.out.println(str + " is not a Palindrom
e");
}
}
}

7. Find Prime Numbers in a Given Range


Problem Statement
Write a program to find prime numbers between 2 and 10.

Input

ch
.te
2, 10
de

Output
co

Prime numbers between 2 and 10 are: 2, 3, 5, 7


n2
ar

Explanation
le

A prime number is a number greater than 1 with no divisors other than 1 and
itself.

Program

public class learn2code {


public static void main(String[] args) {
int start = 2, end = 10;

System.out.print("Prime numbers between " + start +


" and " + end + " are: ");

for (int i = start; i <= end; i++) {


if (isPrime(i)) {

Java Technical Interview Programming Questions 7


System.out.print(i + " ");
}
}
}

static boolean isPrime(int n) {


if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;

ch
}
.te
}
de

8. Check if a Number is Prime


co

Problem Statement
n2

Write a program to check if a given number is a prime number.


ar

Input
le

Output

2 is a Prime Number

Explanation
Check if a number has no divisors other than 1 and itself.

Program

Java Technical Interview Programming Questions 8


public class learn2code {
public static void main(String[] args) {
int num = 2;

if (isPrime(num)) {
System.out.println(num + " is a Prime Number");
} else {
System.out.println(num + " is not a Prime Numbe
r");
}
}

static boolean isPrime(int n) {


if (n <= 1) {

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

9. Find Factorial of a Number


Problem Statement
Write a program to find the factorial of a number.

Input

Output

Java Technical Interview Programming Questions 9


120

Explanation
The factorial of a number is the product of all positive integers less than or
equal to that number.

Program

public class learn2code {


public static void main(String[] args) {
int num = 5;
int factorial = 1;

ch
for (int i = 1; i <= num; i++) {
factorial *= i;
.te
}
de

System.out.println("Factorial of " + num + " is " +


factorial);
co

}
n2

}
ar

10. Check if a Number is an Armstrong Number


le

Problem Statement
Write a program to check if a number is an Armstrong number.

Input

153

Output

153 is an Armstrong Number

Java Technical Interview Programming Questions 10


Explanation
An Armstrong number is a number that is equal to the sum of its own digits
raised to the power of the number of digits.

Program

public class learn2code {


public static void main(String[] args) {
int num = 153;
int original = num;
int sum = 0;

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

}
}

11. Count Number of Digits in a Number


Problem Statement
Write a program to count the number of digits in a number.

Input

1234

Java Technical Interview Programming Questions 11


Output

Number of digits are 4

Explanation
To count the number of digits, repeatedly divide the number by 10 until it
becomes 0, counting the iterations.

Program

public class learn2code {


public static void main(String[] args) {

ch
int num = 1234;
int count = 0;
.te
while (num != 0) {
de

num /= 10;
co

count++;
}
n2

System.out.println("Number of digits are " + coun


ar

t);
le

}
}

12. Count Even and Odd Digits in a Number


Problem Statement
Write a program to count the number of even and odd digits in a number.

Input

1234

Java Technical Interview Programming Questions 12


Output

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

public class learn2code {


public static void main(String[] args) {
int num = 1234;
int evenCount = 0;

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

System.out.println("Even Numbers: " + evenCount);


System.out.println("Odd Numbers: " + oddCount);
}
}

13. Check if a Number is Even or Odd


Problem Statement
Write a program to check if a number is even or odd.

Java Technical Interview Programming Questions 13


Input

Output

Even Number

Explanation
A number is even if it is divisible by 2, otherwise it is odd.

Program

public class learn2code {

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

System.out.println(num + " is an Odd Number");


}
ar

}
}
le

14. Find Sum of Digits in a Number


Problem Statement
Write a program to find the sum of digits in a number.

Input

1234

Output

Java Technical Interview Programming Questions 14


10

Explanation
To find the sum of digits, extract each digit and add them together.

Program

public class learn2code {


public static void main(String[] args) {
int num = 1234;
int sum = 0;

while (num != 0) {

ch
int digit = num % 10;
sum += digit;
.te
num /= 10;
}
de

System.out.println("Sum of digits: " + sum);


co

}
n2

}
ar

15. Find the Largest Number


le

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

Java Technical Interview Programming Questions 15


Explanation
To find the largest number, compare each number with the others and
determine the maximum.

Program

public class learn2code {


public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 30;

int largest = (a > b) ? (a > c ? a : c) : (b > c ?


b : c);

ch
System.out.println("Largest Number is " + largest);
.te
}
}
de
co

16. Generate Random Number and String


n2

Problem Statement
ar

Write a program to generate a random number and a random string.


le

Explanation
Use Math.random() for generating random numbers and a loop for random
strings.

Program

import java.util.Random;

public class learn2code {


public static void main(String[] args) {
// Generate random number
int randomNumber = (int) (Math.random() * 100);
System.out.println("Random Number: " + randomNumbe

Java Technical Interview Programming Questions 16


r);

// Generate random string


String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd
efghijklmnopqrstuvwxyz0123456789";
StringBuilder randomString = new StringBuilder();
Random rand = new Random();
int length = 10;

for (int i = 0; i < length; i++) {


randomString.append(characters.charAt(rand.next
Int(characters.length())));
}

System.out.println("Random String: " + randomStrin

ch
g.toString());
.te
}
}
de
co

17. Find Sum of Elements in an Array


n2

Problem Statement
ar

Write a program to find the sum of elements in an array.


le

Input

a[] = {1, 2, 3, 4}

Output

10

Explanation
To find the sum of elements, iterate through the array and add each element.

Program

Java Technical Interview Programming Questions 17


public class learn2code {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int sum = 0;

for (int i = 0; i < a.length; i++) {


sum += a[i];
}

System.out.println("Sum of elements: " + sum);


}
}

ch
18. Print Even and Odd Numbers in an Array .te
Problem Statement
de

Write a program to print even and odd numbers in an array.


co

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

public class learn2code {


public static void main(String[] args) {

Java Technical Interview Programming Questions 18


int[] a = {1, 2, 3, 4};

System.out.print("Even Numbers: ");


for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
System.out.print(a[i] + " ");
}
}

System.out.print("\\nOdd Numbers: ");


for (int i = 0; i < a.length; i++) {
if (a[i] % 2 != 0) {
System.out.print(a[i] + " ");
}
}

ch
}
.te
}
de

19. Check if Two Arrays are Equal


co

Problem Statement
n2

Write a program to check if two arrays are equal.


ar

Input
le

a[] = {1, 2, 3, 4}
b[] = {1, 2, 3, 4}

Output

a and b are equal

Explanation
Two arrays are equal if they have the same length and corresponding elements
are equal.

Java Technical Interview Programming Questions 19


Program

import java.util.Arrays;

public class learn2code {


public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int[] b = {1, 2, 3, 4};

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

20. Find Missing Number in an Array


co

Problem Statement
n2

Write a program to find the missing number in an array.


ar

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

Java Technical Interview Programming Questions 20


public class learn2code {
public static void main(String[] args) {
int[] a = {1, 2, 3, 5};
int n = a.length + 1;
int totalSum = n * (n + 1) / 2;
int actualSum = 0;

for (int i = 0; i < a.length; i++) {


actualSum += a[i];
}

int missingNumber = totalSum - actualSum;


System.out.println("Missing number is " + missingNu
mber);

ch
}
}
.te
de

21. Find the Max and Min Element in an Array


co

Problem Statement
n2

Write a program to find the maximum and minimum elements in an array.


ar

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.

Java Technical Interview Programming Questions 21


Program

public class learn2code {


public static void main(String[] args) {
int[] a = {3, 4, 5, 6};
int max = a[0];
int min = a[0];

for (int i = 1; i < a.length; i++) {


if (a[i] > max) {
max = a[i];
}
if (a[i] < min

) {

ch
min = a[i];
.te
}
}
de

System.out.println("Max is " + max);


co

System.out.println("Min is " + min);


}
n2

}
ar
le

22. Separate 0s and 1s in an Array


Problem Statement
Write a program to separate 0s and 1s in an array.

Input

a[] = {0, 1, 0, 1, 0, 1}

Output

0, 0, 0, 1, 1, 1

Java Technical Interview Programming Questions 22


@199

Becoming a Java Developer

JAVA CERTIFICATION COURSE


2024 EDITION -
BEGINNER TO MASTER
Visit: https://learn2code.tech/
Explanation
To separate 0s and 1s, count the number of 0s and 1s and then place them
accordingly.

Program

public class learn2code {


public static void main(String[] args) {
int[] a = {0, 1, 0, 1, 0, 1};
int count = 0;

for (int i = 0; i < a.length; i++) {


if (a[i] == 0) {
count++;
}

ch
}
.te
for (int i = 0; i < count; i++) {
de

a[i] = 0;
}
co
n2

for (int i = count; i < a.length; i++) {


a[i] = 1;
ar

}
le

System.out.print("Array after separating 0s and 1s:


");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}

23. Find Duplicates in an Array


Problem Statement
Write a program to find duplicates in an array.

Java Technical Interview Programming Questions 23


Input

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

int[] a = {1, 2, 3, 1, 2, 3, 4};


HashSet<Integer> seen = new HashSet<>();
co

HashSet<Integer> duplicates = new HashSet<>();


n2

for (int i = 0; i < a.length; i++) {


ar

if (!seen.add(a[i])) {
duplicates.add(a[i]);
le

}
}

System.out.print("Duplicates: ");
for (int num : duplicates) {
System.out.print(num + " ");
}
}
}

24. Find the Second Largest Element in an Array

Java Technical Interview Programming Questions 24


Problem Statement
Write a program to find the second largest element in an array.

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

public class learn2code {


public static void main(String[] args) {
co

int[] a = {1, 2, 3, 4};


n2

int largest = Integer.MIN_VALUE;


int secondLargest = Integer.MIN_VALUE;
ar
le

for (int i = 0; i < a.length; i++) {


if (a[i] > largest) {
secondLargest = largest;
largest = a[i];
} else if (a[i] > secondLargest && a[i] != larg
est) {
secondLargest = a[i];
}
}

System.out.println("Second largest element is " + s


econdLargest);

Java Technical Interview Programming Questions 25


}
}

25. Move all Zeros to End of Array


Problem Statement
Write a program to move all zeros to the end of an array.

Input

a[] = {0, 1, 0, 3, 12}

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

public class learn2code {


public static void main(String[] args) {
int[] a = {0, 1, 0, 3, 12};
int count = 0;

for (int i = 0; i < a.length; i++) {


if (a[i] != 0) {
a[count++] = a[i];
}
}

while (count < a.length) {


a[count++] = 0;
}

Java Technical Interview Programming Questions 26


System.out.print("Array after moving zeros to end:
");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}

26. Find Intersection of Two Arrays


Problem Statement
Write a program to find the intersection of two arrays.

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;

public class learn2code {


public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int[] b = {3, 4, 5, 6};
HashSet<Integer> set = new HashSet<>();

Java Technical Interview Programming Questions 27


for (int i = 0; i < a.length; i++) {
set.add(a[i]);
}

System.out.print("Intersection: ");
for (int i = 0; i < b.length; i++) {
if (set.contains(b[i])) {
System.out.print(b[i] + " ");
}
}
}
}

27. Find Union of Two Arrays


ch
.te
Problem Statement
de

Write a program to find the union of two arrays.


co

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;

public class learn2code {

Java Technical Interview Programming Questions 28


public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int[] b = {3, 4, 5, 6};
HashSet<Integer> set = new HashSet<>();

for (int i = 0; i < a.length; i++) {


set.add(a[i]);
}

for (int i = 0; i < b.length; i++) {


set.add(b[i]);
}

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

build a solid foundation in programming logic.


ar
le

Java Technical Interview Programming Questions 29

You might also like