0% found this document useful (0 votes)
8 views3 pages

20 Core Java Problems With Solutions

The document presents 20 core Java problems along with their solutions, covering various concepts such as class creation, method overloading, method overriding, interfaces, string manipulation, collections, exceptions, threading, synchronization, and design patterns. Each problem is accompanied by a code snippet that illustrates the solution. This serves as a practical guide for Java programming concepts and techniques.

Uploaded by

test25793558
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)
8 views3 pages

20 Core Java Problems With Solutions

The document presents 20 core Java problems along with their solutions, covering various concepts such as class creation, method overloading, method overriding, interfaces, string manipulation, collections, exceptions, threading, synchronization, and design patterns. Each problem is accompanied by a code snippet that illustrates the solution. This serves as a practical guide for Java programming concepts and techniques.

Uploaded by

test25793558
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

20 Core Java Problems with Solutions

Problem: 1. Create a BankAccount class with deposit and withdraw methods.

class BankAccount {
private double balance;
public BankAccount(double balance){ [Link] = balance; }
public void deposit(double amt){ balance += amt; }
public void withdraw(double amt){
if(amt <= balance) balance -= amt;
}
public double getBalance(){ return balance; }
}

Problem: 2. Demonstrate method overloading.

class MathUtil {
int add(int a, int b){ return a+b; }
double add(double a, double b){ return a+b; }
}

Problem: 3. Demonstrate method overriding.

class Animal {
void sound(){ [Link]("Animal sound"); }
}
class Dog extends Animal {
void sound(){ [Link]("Bark"); }
}

Problem: 4. Create an interface Payment and implement it.

interface Payment {
void pay(double amount);
}
class UPIPayment implements Payment {
public void pay(double amount){
[Link]("Paid via UPI");
}
}

Problem: 5. Find duplicate characters in a string.

String str = "programming";


Map<Character,Integer> map = new HashMap<>();
for(char c: [Link]()){
[Link](c, [Link](c,0)+1);
}

Problem: 6. Reverse a string without inbuilt method.

String str = "hello";


String rev="";
for(int i=[Link]()-1;i>=0;i--){
rev += [Link](i);
}

Problem: 7. Check palindrome.

String s="madam";
String rev = new StringBuilder(s).reverse().toString();
[Link]([Link](rev));

Problem: 8. Sort an ArrayList.

List<Integer> list = [Link](5,2,8);


[Link](list);

Problem: 9. Custom Exception example.

class InsufficientBalanceException extends Exception {


public InsufficientBalanceException(String msg){
super(msg);
}
}

Problem: 10. Thread using Runnable.

class MyThread implements Runnable{


public void run(){
[Link]("Thread running");
}
}

Problem: 11. Synchronization example.

synchronized void bookTicket(){


if(tickets>0) tickets--;
}

Problem: 12. Use HashMap.

Map<Integer,String> map = new HashMap<>();


[Link](1,"Java");

Problem: 13. Find second highest number.

List<Integer> list = [Link](1,5,3,9);


int second = [Link]()
.sorted([Link]())
.skip(1).findFirst().get();

Problem: 14. Remove duplicates using Set.

List<Integer> list = [Link](1,2,2,3);


Set<Integer> set = new HashSet<>(list);
Problem: 15. Lambda expression example.

List<Integer> list = [Link](1,2,3);


[Link](n -> [Link](n));

Problem: 16. Functional Interface example.

@FunctionalInterface
interface MyFunc{
void display();
}

Problem: 17. File reading example.

BufferedReader br = new BufferedReader(


new FileReader("[Link]"));

Problem: 18. Comparable example.

class Student implements Comparable<Student>{


int marks;
public int compareTo(Student s){
return [Link] - [Link];
}
}

Problem: 19. Stream filter even numbers.

List<Integer> list = [Link](1,2,3,4);


[Link]().filter(n -> n%2==0)
.forEach([Link]::println);

Problem: 20. Singleton pattern.

class Singleton{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null) instance=new Singleton();
return instance;
}
}

You might also like