Java Complete Interview Codes
Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Automation";
String reversed = new StringBuilder(str).reverse().toString();
[Link]("Reversed: " + reversed);
}
}
Palindrome Check
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
if ([Link](rev))
[Link](str + " is a Palindrome");
else
[Link](str + " is not a Palindrome");
}
}
Factorial
public class Factorial {
public static void main(String[] args) {
int num = 5, fact = 1;
for (int i = 1; i <= num; i++)
fact *= i;
[Link]("Factorial = " + fact);
}
}
Fibonacci Series
public class FibonacciSeries {
public static void main(String[] args) {
int n1 = 0, n2 = 1, n3, count = 10;
[Link](n1 + " " + n2);
for (int i = 2; i < count; i++) {
n3 = n1 + n2;
[Link](" " + n3);
n1 = n2;
n2 = n3;
}
}
}
Prime Check
public class PrimeCheck {
public static void main(String[] args) {
int n = 7;
boolean prime = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
[Link](n + (prime ? " is Prime" : " is Not Prime"));
}
}
Find Maximum in Array
public class MaxInArray {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 7};
int max = arr[0];
for (int i : arr)
if (i > max) max = i;
[Link]("Max = " + max);
}
}
Find Duplicates in Array
import [Link].*;
public class FindDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 3};
Set<Integer> set = new HashSet<>();
for (int n : arr)
if ()
[Link]("Duplicate: " + n);
}
}
Sort Array Without sort()
import [Link];
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 1};
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]("Sorted Array: " + [Link](arr));
}
}
Reverse Words in Sentence
public class ReverseWords {
public static void main(String[] args) {
String input = "I love testing";
String[] words = [Link](" ");
for (String w : words)
[Link](new StringBuilder(w).reverse() + " ");
}
}
Character Frequency
import [Link].*;
public class CharFrequency {
public static void main(String[] args) {
String s = "testing";
Map<Character, Integer> map = new HashMap<>();
for (char c : [Link]())
[Link](c, [Link](c, 0) + 1);
[Link](map);
}
}
Anagram Check
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
String s1 = "listen", s2 = "silent";
char[] a = [Link]();
char[] b = [Link]();
[Link](a);
[Link](b);
if ([Link](a, b))
[Link]("Anagram");
else
[Link]("Not Anagram");
}
}
Remove Duplicate Characters
public class RemoveDuplicateChars {
public static void main(String[] args) {
String str = "programming";
String result = "";
for (char c : [Link]())
if ([Link](c) == -1)
result += c;
[Link]("After removing duplicates: " + result);
}
}
Inheritance Example
class Parent {
void display() {
[Link]("Parent class method");
}
}
public class Child extends Parent {
void show() {
[Link]("Child class method");
}
public static void main(String[] args) {
Child obj = new Child();
[Link]();
[Link]();
}
}
Polymorphism Example
class Animal {
void sound() {
[Link]("Animal sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal a = new Dog();
[Link]();
}
}
Encapsulation Example
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}
public class EncapsulationExample {
public static void main(String[] args) {
Person p = new Person();
[Link]("Hemlata");
[Link]("Name: " + [Link]());
}
}
Abstract Class Example
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
}
public class AbstractExample {
public static void main(String[] args) {
Shape s = new Circle();
[Link]();
}
}
Interface Example
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
[Link]("Car started");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Vehicle v = new Car();
[Link]();
}
}
File Handling Example
import [Link].*;
public class FileExample {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("[Link]");
[Link]("Welcome to Java File Handling");
[Link]();
[Link]("File written successfully");
} catch (Exception e) {
[Link](e);
}
}
}
Selenium Style Example (Pseudo)
public class SeleniumPseudoExample {
public static void main(String[] args) {
[Link]("Launching browser...");
[Link]("Navigating to login page...");
[Link]("Entering username and password...");
[Link]("Clicking Login button...");
[Link]("Validating successful login message...");
[Link]("Closing browser...");
}
}