0% found this document useful (0 votes)
73 views4 pages

Simple Java Coding Questions For

The document contains a series of simple Java coding questions and their solutions, suitable for interview practice. It covers a variety of topics including string manipulation, mathematical calculations, array handling, and object-oriented programming concepts such as inheritance and interfaces. Each example is presented in a complete Java class format, demonstrating practical coding skills.

Uploaded by

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

Simple Java Coding Questions For

The document contains a series of simple Java coding questions and their solutions, suitable for interview practice. It covers a variety of topics including string manipulation, mathematical calculations, array handling, and object-oriented programming concepts such as inheritance and interfaces. Each example is presented in a complete Java class format, demonstrating practical coding skills.

Uploaded by

SHUBHAM PATIL SP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// ✅ SIMPLE JAVA CODING QUESTIONS FOR INTERVIEW PRACTICE

// 1. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "hello";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
System.out.println("Reversed: " + rev);
}
}

// 2. Check Palindrome (String)


public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
if (str.equals(rev)) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
}
}
}

// 3. Factorial of a Number
public class Factorial {
public static void main(String[] args) {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);
}
}

// 4. Fibonacci Series
public class Fibonacci {
public static void main(String[] args) {
int n = 5, a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}
}

// 5. Armstrong Number
public class Armstrong {
public static void main(String[] args) {
int num = 153, sum = 0, temp = num;
while (num > 0) {
int r = num % 10;
sum += r * r * r;
num /= 10;
}
if (sum == temp) {
System.out.println("Armstrong number");
} else {
System.out.println("Not an Armstrong number");
}
}
}

// 6. Prime Numbers in Range


public class PrimeRange {
public static void main(String[] args) {
for (int i = 2; i <= 10; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
if (prime) System.out.print(i + " ");
}
}
}

// 7. Swap Two Numbers without 3rd Variable


public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a: " + a + ", b: " + b);
}
}

// 8. Find Duplicate in Array


public class DuplicateElement {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4};
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.println("Duplicate: " + arr[i]);
}
}
}
}
}

// 9. Sort Array without Sort Function


public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 1, 3};
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int a : arr) System.out.print(a + " ");
}
}

// 10. Max and Min in Array


public class MaxMinArray {
public static void main(String[] args) {
int[] arr = {4, 2, 9, 1};
int max = arr[0], min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
System.out.println("Max: " + max + ", Min: " + min);
}
}

// 11. Frequency of Characters in String


public class CharFrequency {
public static void main(String[] args) {
String str = "aabcc";
int[] freq = new int[256];
for (int i = 0; i < str.length(); i++) {
freq[str.charAt(i)]++;
}
for (int i = 0; i < freq.length; i++) {
if (freq[i] > 0) {
System.out.println((char) i + ": " + freq[i]);
}
}
}
}

// 12. Check if Two Strings are Anagram


import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String a = "listen", b = "silent";
char[] ch1 = a.toCharArray();
char[] ch2 = b.toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
if (Arrays.equals(ch1, ch2)) {
System.out.println("Anagram");
} else {
System.out.println("Not Anagram");
}
}
}

// 13. Inheritance Example


class Person {
void display() {
System.out.println("I am a person");
}
}
class Student extends Person {
void show() {
System.out.println("I am a student");
}
public static void main(String[] args) {
Student s = new Student();
s.display();
s.show();
}
}

// 14. Interface Example


interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
System.out.println("Car started");
}
public static void main(String[] args) {
Car c = new Car();
c.start();
}
}

// 15. ArrayList Example


import java.util.*;
public class StudentList {
public static void main(String[] args) {
List<String> students = new ArrayList<>();
students.add("Shubham");
students.add("Ravi");
for (String name : students) {
System.out.println(name);
}
}
}

You might also like