0% found this document useful (0 votes)
26 views15 pages

NOTHIING

nothing

Uploaded by

fightfiller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
26 views15 pages

NOTHIING

nothing

Uploaded by

fightfiller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 15

Pratical prep

Friday, May 9, 2025 1:20 PM

✅ 1. Check if two strings are anagrams of each other


java
Copy code
import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
char[] a1 = str1.toCharArray();
char[] a2 = str2.toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
System.out.println(;Arrays.equals(a1, a2) ? "Anagrams" : "Not Anagrams");
}
}

✅ 3. Find the second largest element in an array


N
java
Copy code
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {10, 20, 4, 45, 99};
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
System.out.println("Second largest: " + second);
}
}

✅ 5. Rotate an array to the right by k steps


java
Copy code
import java.util.Arrays;
public class RotateArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
int n = arr.length;
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[(i + k) % n] = arr[i];
}
System.out.println("Rotated Array: " + Arrays.toString(result));
}
}

✅ 7. Check if two arrays are equal (same elements, any order)


JAVA Page 1
✅ 7. Check if two arrays are equal (same elements, any order)
java
Copy code
import java.util.Arrays;
public class ArraysEqual {
public static void main(String[] args) {
int[] a = {1, 2, 3};
int[] b = {3, 1, 2};
Arrays.sort(a);
Arrays.sort(b);
System.out.println(Arrays.equals(a, b) ? "Equal" : "Not Equal");
}
}

✅ 9. Remove duplicate elements from an array


java
Copy code
import java.util.HashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4}; Use HashSet
HashSet<Integer> set = new HashSet<>();
for (int num : arr) {
set.add(num);
}
System.out.println("After removing duplicates: " + set);
}
}

✅ 11. Sort an array using bubble sort


java
Copy code
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 1, 4, 2, 8};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp;
}
}
}
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}

✅ 13. Check if a string is a palindrome


java
Copy code
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(rev) ? "Palindrome" : "Not Palindrome");
}
}

JAVA Page 2
}

✅ 15. Count vowels, consonants, digits, and special characters


java
Copy code
public class CountCharacters {
public static void main(String[] args) {
String str = "Hello@123";
int vowels = 0, consonants = 0, digits = 0, special = 0;
for (char ch : str.toCharArray()) {
if (Character.isLetter(ch)) {
if ("aeiouAEIOU".indexOf(ch) != -1) vowels++;
else consonants++;
} else if (Character.isDigit(ch)) digits++;
else special++;
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
System.out.println("Digits: " + digits);
System.out.println("Special characters: " + special);
}
}

✅ 17. Find the first non-repeating character in a string


java
Copy code
import java.util.LinkedHashMap;
public class FirstNonRepeating {
public static void main(String[] args) {
String str = "aabbcdeff";
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (char ch : str.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
for (char ch : map.keySet()) {
if (map.get(ch) == 1) {
System.out.println("First non-repeating: " + ch);
break;
}
}
}
}

✅ 19. Reverse each word in a sentence


java
Copy code
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Hello World";
String[] words = sentence.split(" ");
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(new StringBuilder(word).reverse()).append(" ");
}
System.out.println("Reversed sentence: " + result.toString().trim());
}
}

✅ 21. Check if two strings are anagrams (Duplicate of Q1)


JAVA Page 3
✅ 21. Check if two strings are anagrams (Duplicate of Q1)
Same code as Q1.

✅ 23. Print Floyd’s triangle using nested loops


java
Copy code
public class FloydsTriangle {
public static void main(String[] args) {
int n = 4, num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num++ + " ");
}
System.out.println();
}
}
}

✅ 25. Generate and print the first n Fibonacci numbers


java
Copy code
public class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int next = a + b;
a = b;
b = next;
}
}
}

✅ 27. Check whether a number is Armstrong or not


java
Copy code
public class ArmstrongCheck {
public static void main(String[] args) {
int num = 153, sum = 0, temp = num;
while (temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, 3);
temp /= 10;
}
System.out.println(sum == num ? "Armstrong number" : "Not Armstrong");
}
}

✅ 29. Display a pattern like a diamond using *


java
Copy code
public class DiamondPattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
System.out.print(" ".repeat(n - i));

JAVA Page 4
System.out.print(" ".repeat(n - i));
System.out.println("*".repeat(2 * i - 1));
}
for (int i = n - 1; i >= 1; i--) {
System.out.print(" ".repeat(n - i));
System.out.println("*".repeat(2 * i - 1));
}
}
}

✅ 31. Print multiplication table for a given number


java
Copy code
public class MultiplicationTable {
public static void main(String[] args) {
int num = 5;
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}

✅ 33. Remove duplicates from an ArrayList of integers


java
Copy code
import java.util.*;
public class RemoveDuplicatesArrayList {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 2, 3, 4, 4));
HashSet<Integer> set = new HashSet<>(list);
System.out.println("Unique elements: " + new ArrayList<>(set));
}
}

✅ 35. Sort a HashMap by its values


java
Copy code
import java.util.*;
public class SortHashMapByValue {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("Apple", 30);
map.put("Banana", 10);
map.put("Mango", 20);
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
list.sort(Map.Entry.comparingByValue());
for (Map.Entry<String, Integer> entry : list) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}

✅ 37. Count frequency of characters in a string using HashMap


java
Copy code
import java.util.*;
public class CharFrequency {

JAVA Page 5
public class CharFrequency {
public static void main(String[] args) {
String str = "hello world";
HashMap<Character, Integer> freq = new HashMap<>();
for (char ch : str.toCharArray()) {
if (ch != ' ')
freq.put(ch, freq.getOrDefault(ch, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}

✅ 39. Reverse a LinkedList without using built-in reverse


java
Copy code
import java.util.*;
public class ReverseLinkedList {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5));
ListIterator<Integer> itr = list.listIterator(list.size());
while (itr.hasPrevious()) {
System.out.print(itr.previous() + " ");
}
}
}

Tougher repeatative questions


✅ 1. Check if two strings are anagrams of each other
java
Copy code
import java.util.Arrays;
public class AnagramCheck {
public static boolean isAnagram(String str1, String str2) {
char[] a = str1.replaceAll("\\s", "").toLowerCase().toCharArray();
char[] b = str2.replaceAll("\\s", "").toLowerCase().toCharArray();
Arrays.sort(a);
Arrays.sort(b);
return Arrays.equals(a, b);
}
public static void main(String[] args) {
System.out.println(isAnagram("listen", "silent")); // true
}
}

✅ 2. Create a BankAccount class (Encapsulation)


java
Copy code
public class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0)
balance += amount;
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance)
balance -= amount;

JAVA Page 6
balance -= amount;
}
public double checkBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(1000);
acc.withdraw(500);
System.out.println("Balance: " + acc.checkBalance()); // 500
}
}

✅ 3. Vehicle → Car, Bike (Inheritance & Method Overriding)


java
Copy code
class Vehicle {
void start() {
System.out.println("Vehicle started");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car started");
}
}
class Bike extends Vehicle {
@Override
void start() {
System.out.println("Bike started");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v1 = new Car();
Vehicle v2 = new Bike();
v1.start(); // Car started
v2.start(); // Bike started
}
}

✅ 4. Shape abstract class with Rectangle & Circle (Abstraction &


Polymorphism)
java
Copy code

abstract class Shape {


abstract double area();
}
class Rectangle extends Shape {
double length = 5, width = 3;
double area() {
return length * width;
}
}
class Circle extends Shape {
double radius = 4;
double area() {
return Math.PI * radius * radius;

JAVA Page 7
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Shape s1 = new Rectangle();
Shape s2 = new Circle();
System.out.println(s1.area()); // 15.0
System.out.println(s2.area()); // ~50.27
}
}

✅ 5. Employee class with overloaded constructors


java
Copy code
class Employee {
String name;
int id;
Employee() {
name = "Default";
id = 0;
}
Employee(String n) {
name = n;
id = 1;
}
Employee(String n, int i) {
name = n;
id = i;
}
void display() {
System.out.println("Name: " + name + ", ID: " + id);
}
public static void main(String[] args) {
Employee e1 = new Employee();
Employee e2 = new Employee("John");
Employee e3 = new Employee("Alice", 101);
e1.display();
e2.display();
e3.display();
}
}

✅ 6. Calculator class with method overloading (Compile-time


Polymorphism)
java
Copy code
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // 5

JAVA Page 8
System.out.println(calc.add(2, 3)); // 5
System.out.println(calc.add(2.5, 3.5)); // 6.0
System.out.println(calc.add(1, 2, 3)); // 6
}
}

✅ 7. Flyable, Swimmable interfaces → Duck (Multiple


Inheritance via Interface)
java
Copy code
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Duck implements Flyable, Swimmable {
public void fly() {
System.out.println("Duck is flying");
}
public void swim() {
System.out.println("Duck is swimming");
}
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
}
}

✅ 8. Runtime Polymorphism (Overridden methods via parent


reference)
java
Copy code
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound(); // Dog barks
a = new Cat();
a.sound(); // Cat meows
}
}

JAVA Page 9
✅ 9. Sortable interface with BubbleSort & SelectionSort
java
Copy code
interface Sortable {
void sort(int[] arr);
}
class BubbleSort implements Sortable {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
for (int j = 0; j < arr.length - 1 - i; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
class SelectionSort implements Sortable {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[min]) min = j;
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1};
Sortable sorter = new BubbleSort(); // or new SelectionSort();
sorter.sort(arr);
for (int i : arr) System.out.print(i + " "); // 1 2 5 9
}
}

2. BankAccount class with deposit, withdraw, and checkBalance


methods (Encapsulation)
java
Copy code
class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient balance");
}
}
public double checkBalance() {
return balance;
}

JAVA Page 10
}
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
account.withdraw(500);
System.out.println("Current Balance: " + account.checkBalance());
}
}

4. Vehicle → Car, Bike (Inheritance & Method Overriding)


java
Copy code
class Vehicle {
void start() {
System.out.println("Vehicle started");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car started");
}
}
class Bike extends Vehicle {
@Override
void start() {
System.out.println("Bike started");
}
public static void main(String[] args) {
Vehicle v1 = new Car();
Vehicle v2 = new Bike();
v1.start();
v2.start();
}
}

6. Shape abstract class with Rectangle and Circle subclasses


java
Copy code
abstract class Shape {
abstract double area();
}
class Rectangle extends Shape {
double length, width;
Rectangle(double l, double w) {
length = l;
width = w;
}
double area() {
return length * width;
}
}
class Circle extends Shape {
double radius;
Circle(double r) {
radius = r;
}
double area() {
return Math.PI * radius * radius;
}

JAVA Page 11
}
public static void main(String[] args) {
Shape s1 = new Rectangle(4, 5);
Shape s2 = new Circle(3);
System.out.println("Rectangle Area: " + s1.area());
System.out.println("Circle Area: " + s2.area());
}
}

8. Employee class with overloaded constructors


java
Copy code
class Employee {
String name;
int age;
Employee(String n) {
name = n;
age = 0;
}
Employee(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Employee e1 = new Employee("Alice");
Employee e2 = new Employee("Bob", 30);
e1.display();
e2.display();
}
}

10. Calculator class demonstrating method overloading


java
Copy code
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum (int): " + calc.add(5, 3));
System.out.println("Sum (double): " + calc.add(5.5, 3.2));
}
}

12. Duck class implementing Flyable and Swimmable interfaces


java
Copy code
interface Flyable {
void fly();
}
interface Swimmable {
void swim();

JAVA Page 12
void swim();
}
class Duck implements Flyable, Swimmable {
public void fly() {
System.out.println("Duck is flying");
}
public void swim() {
System.out.println("Duck is swimming");
}
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
}
}

14. RemoteControl interface implemented in TV and AC


java
Copy code
interface RemoteControl {
void turnOn();
void turnOff();
}
class TV implements RemoteControl {
public void turnOn() {
System.out.println("TV is ON");
}
public void turnOff() {
System.out.println("TV is OFF");
}
}
class AC implements RemoteControl {
public void turnOn() {
System.out.println("AC is ON");
}
public void turnOff() {
System.out.println("AC is OFF");
}
public static void main(String[] args) {
RemoteControl tv = new TV();
RemoteControl ac = new AC();
tv.turnOn();
tv.turnOff();
ac.turnOn();
ac.turnOff();
}
}

16. Payment abstract class with CreditCardPayment and


UPIPayment subclasses
java
Copy code
abstract class Payment {
abstract void pay(double amount);
}
class CreditCardPayment extends Payment {
void pay(double amount) {
System.out.println("Paid ₹" + amount + " using Credit Card");
}
}

JAVA Page 13
}
class UPIPayment extends Payment {
void pay(double amount) {
System.out.println("Paid ₹" + amount + " using UPI");
}
public static void main(String[] args) {
Payment p1 = new CreditCardPayment();
Payment p2 = new UPIPayment();
p1.pay(500);
p2.pay(300);
}
}

18. Runtime polymorphism example


java
Copy code
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Calls Dog's version (runtime polymorphism)
}
}

20. Interface Sortable with BubbleSort and SelectionSort


java
Copy code
interface Sortable {
void sort(int[] arr);
}
class BubbleSort implements Sortable {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
for (int j = 0; j < arr.length - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
class SelectionSort implements Sortable {
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[min])
min = j;
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}

JAVA Page 14
}
}
class SortTest {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 1, 2};
Sortable sorter = new BubbleSort(); // or new SelectionSort();
sorter.sort(arr);
for (int i : arr)
System.out.print(i + " ");
}
}

JAVA Page 15

You might also like