0% found this document useful (0 votes)
2 views31 pages

30 Java Devnest

The document presents a collection of 30 Java programs commonly asked in interviews, complete with code and output examples. Each program addresses a specific problem, such as reversing a string, checking for palindromes, and finding duplicates in an array. It serves as a practical guide for individuals preparing for job placements in programming roles.

Uploaded by

praveenvijay1357
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

30 Java Devnest

The document presents a collection of 30 Java programs commonly asked in interviews, complete with code and output examples. Each program addresses a specific problem, such as reversing a string, checking for palindromes, and finding duplicates in an array. It serves as a practical guide for individuals preparing for job placements in programming roles.

Uploaded by

praveenvijay1357
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TOP 30

JAVA PROGRAMS
ASKED IN INTERVIEWS

Full Code + Output

Must Practice Before Your Placement

SAVE • PRACTICE • REVISE

DEVNEST
1. Reverse a String

JAVA CODE
public class Main {
public static void main(String[] args) {
String str = "Java";
String reversed = "";

for (int i = [Link]() - 1; i >= 0; i--) {


reversed += [Link](i);
}

[Link]("Reversed: " + reversed);


}
}

OUTPUT
Reversed: avaJ

DEVNEST 1/30
2. Check Palindrome String

JAVA CODE
public class Main {
public static void main(String[] args) {
String str = "madam";
String reversed = "";

for (int i = [Link]() - 1; i >= 0; i--) {


reversed += [Link](i);
}

if ([Link](reversed))
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}

OUTPUT
Palindrome

DEVNEST 2/30
3. Reverse a Number

JAVA CODE
public class Main {
public static void main(String[] args) {
int num = 1234;
int reverse = 0;

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

[Link]("Reverse: " + reverse);


}
}

OUTPUT
Reverse: 4321

DEVNEST 3/30
4. Check Palindrome Number

JAVA CODE
public class Main {
public static void main(String[] args) {
int num = 121;
int original = num;
int reverse = 0;

while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}

if (original == reverse)
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}

OUTPUT
Palindrome

DEVNEST 4/30
5. Check Prime Number

JAVA CODE
public class Main {
public static void main(String[] args) {
int num = 29;
boolean isPrime = num > 1;

for (int i = 2; i * i <= num; i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}

[Link](isPrime ? "Prime" : "Not Prime");


}
}

OUTPUT
Prime

DEVNEST 5/30
6. Print Prime Numbers in a Range

JAVA CODE
public class Main {
public static void main(String[] args) {
int start = 10, end = 30;

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


boolean prime = n > 1;

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


if (n % i == 0) {
prime = false;
break;
}
}

if (prime) [Link](n + " ");


}
}
}

OUTPUT
11 13 17 19 23 29

DEVNEST 6/30
7. Find Factorial of a Number

JAVA CODE
public class Main {
public static void main(String[] args) {
int num = 5;
long factorial = 1;

for (int i = 1; i <= num; i++) {


factorial *= i;
}

[Link]("Factorial: " + factorial);


}
}

OUTPUT
Factorial: 120

DEVNEST 7/30
8. Print Fibonacci Series

JAVA CODE
public class Main {
public static void main(String[] args) {
int n = 8;
int a = 0, b = 1;

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


[Link](a + " ");
int next = a + b;
a = b;
b = next;
}
}
}

OUTPUT
0 1 1 2 3 5 8 13

DEVNEST 8/30
9. Check Armstrong Number

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

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

[Link](sum == original
? "Armstrong Number"
: "Not Armstrong Number");
}
}

OUTPUT
Armstrong Number

DEVNEST 9/30
10. Find Sum of Digits

JAVA CODE
public class Main {
public static void main(String[] args) {
int num = 12345;
int sum = 0;

while (num != 0) {
sum += num % 10;
num /= 10;
}

[Link]("Sum: " + sum);


}
}

OUTPUT
Sum: 15

DEVNEST 10/30
11. Swap Two Numbers Without Third Variable

JAVA CODE
public class Main {
public static void main(String[] args) {
int a = 10, b = 20;

a = a + b;
b = a - b;
a = a - b;

[Link]("a = " + a);


[Link]("b = " + b);
}
}

OUTPUT
a = 20
b = 10

DEVNEST 11/30
12. Find Largest of Three Numbers

JAVA CODE
public class Main {
public static void main(String[] args) {
int a = 10, b = 25, c = 15;
int largest = a;

if (b > largest) largest = b;


if (c > largest) largest = c;

[Link]("Largest: " + largest);


}
}

OUTPUT
Largest: 25

DEVNEST 12/30
13. Count Vowels and Consonants

JAVA CODE
public class Main {
public static void main(String[] args) {
String str = "Hello Java".toLowerCase();
int vowels = 0, consonants = 0;

for (char ch : [Link]()) {


if ([Link](ch)) {
if ("aeiou".indexOf(ch) != -1)
vowels++;
else
consonants++;
}
}

[Link]("Vowels: " + vowels);


[Link]("Consonants: " + consonants);
}
}

OUTPUT
Vowels: 4
Consonants: 5

DEVNEST 13/30
14. Count Frequency of Characters

JAVA CODE
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
String str = "banana";
Map<Character, Integer> map = new LinkedHashMap<>();

for (char ch : [Link]()) {


[Link](ch, [Link](ch, 0) + 1);
}

for ([Link]<Character, Integer> e : [Link]()) {


[Link]([Link]() + " = " + [Link]());
}
}
}

OUTPUT
b=1
a=3
n=2
DEVNEST 14/30
15. Check Two Strings Are Anagrams

JAVA CODE
import [Link];

public class Main {


public static void main(String[] args) {
String a = "listen";
String b = "silent";

char[] x = [Link]().toCharArray();
char[] y = [Link]().toCharArray();

[Link](x);
[Link](y);

[Link]([Link](x, y)
? "Anagrams"
: "Not Anagrams");
}
}

OUTPUT
Anagrams

DEVNEST 15/30
16. Remove Duplicate Characters from String

JAVA CODE
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
String str = "programming";
Set<Character> seen = new LinkedHashSet<>();

for (char ch : [Link]()) {


[Link](ch);
}

StringBuilder result = new StringBuilder();


for (char ch : seen) [Link](ch);

[Link](result);
}
}

OUTPUT
progamin

DEVNEST 16/30
17. Find First Non-Repeating Character

JAVA CODE
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
String str = "swiss";
Map<Character, Integer> map = new LinkedHashMap<>();

for (char ch : [Link]()) {


[Link](ch, [Link](ch, 0) + 1);
}

for (char ch : [Link]()) {


if ([Link](ch) == 1) {
[Link]("First: " + ch);
break;
}
}
}
}

OUTPUT
First: w

DEVNEST 17/30
18. Find Duplicate Elements in Array

JAVA CODE
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 1};
Set<Integer> seen = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();

for (int n : arr) {


if (![Link](n)) [Link](n);
}

[Link]("Duplicates: " + duplicates);


}
}

OUTPUT
Duplicates: [1, 2]

DEVNEST 18/30
19. Find Largest and Second Largest Element

JAVA CODE
public class Main {
public static void main(String[] args) {
int[] arr = {12, 35, 1, 10, 34, 1};
int largest = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;

for (int n : arr) {


if (n > largest) {
second = largest;
largest = n;
} else if (n > second && n != largest) {
second = n;
}
}

[Link]("Largest: " + largest);


[Link]("Second Largest: " + second);
}
}

OUTPUT
Largest: 35
Second Largest: 34

DEVNEST 19/30
20. Find Missing Number in Array

JAVA CODE
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 5};
int n = 5;

int expected = n * (n + 1) / 2;
int actual = 0;

for (int value : arr) actual += value;

[Link]("Missing: " + (expected - actual));


}
}

OUTPUT
Missing: 4

DEVNEST 20/30
21. Remove Duplicates from Array

JAVA CODE
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 1, 4};
Set<Integer> unique = new LinkedHashSet<>();

for (int n : arr) [Link](n);

[Link]("Unique: " + unique);


}
}

OUTPUT
Unique: [1, 2, 3, 4]

DEVNEST 21/30
22. Sort an Array Without Built-in Sort

JAVA CODE
import [Link];

public class Main {


public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 3};

for (int i = 0; i < [Link] - 1; i++) {


for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

[Link]([Link](arr));
}
}

OUTPUT
[1, 2, 3, 5, 8]

DEVNEST 22/30
23. Reverse an Array

JAVA CODE
import [Link];

public class Main {


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

int left = 0, right = [Link] - 1;


while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}

[Link]([Link](arr));
}
}

OUTPUT
[5, 4, 3, 2, 1]

DEVNEST 23/30
24. Move All Zeros to End

JAVA CODE
import [Link];

public class Main {


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

for (int n : arr) {


if (n != 0) arr[index++] = n;
}

while (index < [Link]) {


arr[index++] = 0;
}

[Link]([Link](arr));
}
}

OUTPUT
[1, 3, 12, 0, 0]

DEVNEST 24/30
25. Find Common Elements in Two Arrays

JAVA CODE
import [Link];
import [Link];

public class Main {


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

Set<Integer> set = new LinkedHashSet<>();


Set<Integer> common = new LinkedHashSet<>();

for (int n : a) [Link](n);


for (int n : b) {
if ([Link](n)) [Link](n);
}

[Link]("Common: " + common);


}
}

OUTPUT
Common: [3, 4]

DEVNEST 25/30
26. Check Balanced Parentheses

JAVA CODE
import [Link];

public class Main {


public static void main(String[] args) {
String str = "{[()]}";
Stack<Character> stack = new Stack<>();
boolean balanced = true;

for (char ch : [Link]()) {


if (ch == '(' || ch == '{' || ch == '[') {
[Link](ch);
} else {
if ([Link]()) {
balanced = false;
break;
}

char open = [Link]();


if ((ch == ')' && open != '(') ||
(ch == '}' && open != '{') ||
(ch == ']' && open != '[')) {
balanced = false;
break;
}
}
}
OUTPUT
balanced = balanced && [Link]();
Balanced
[Link](balanced ? "Balanced" : "Not Balanced");
}
}
DEVNEST 26/30
27. Find Duplicate Words in a Sentence

JAVA CODE
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
String text = "Java is easy and Java is powerful";
String[] words = [Link]().split("\\s+");
Map<String, Integer> map = new LinkedHashMap<>();

for (String word : words) {


[Link](word, [Link](word, 0) + 1);
}

for ([Link]<String, Integer> e : [Link]()) {


if ([Link]() > 1)
[Link]([Link]() + " = " + [Link]());
}
}
}

OUTPUT
java = 2
is = 2

DEVNEST 27/30
28. Reverse Each Word in a Sentence

JAVA CODE
public class Main {
public static void main(String[] args) {
String text = "Java is fun";
String[] words = [Link](" ");
StringBuilder result = new StringBuilder();

for (String word : words) {


[Link](new StringBuilder(word).reverse())
.append(" ");
}

[Link]([Link]().trim());
}
}

OUTPUT
avaJ si nuf

DEVNEST 28/30
29. Find Longest Word in a String

JAVA CODE
public class Main {
public static void main(String[] args) {
String text = "Java programming is powerful";
String[] words = [Link](" ");
String longest = "";

for (String word : words) {


if ([Link]() > [Link]()) {
longest = word;
}
}

[Link]("Longest: " + longest);


}
}

OUTPUT
Longest: programming

DEVNEST 29/30
30. Count Occurrences of Each Element in Array

JAVA CODE
import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 1, 1};
Map<Integer, Integer> map = new LinkedHashMap<>();

for (int n : arr) {


[Link](n, [Link](n, 0) + 1);
}

for ([Link]<Integer, Integer> e : [Link]()) {


[Link]([Link]() + " = " + [Link]());
}
}
}

OUTPUT
1=3
2=2
3=1
DEVNEST 30/30

You might also like