Java Exercises
发布时间:2026-09-19 | 浏览:1
Interview Questions
Java Collection
This Java exercise collection is designed to deepen your understanding of Java and refine your coding skills. The programs provide hands-on experience in solving real-world problems, reinforce key concepts, and help you master Java fundamentals, including variables, control statements, arrays, strings, methods, and object-oriented programming.
Covers Java practice problems from beginner to advanced levels.
Helps strengthen Java programming and problem-solving skills.
Includes programs based on numbers, patterns, arrays, and strings.
Java Beginner Exercises
These exercises focus on basic Java syntax, arithmetic operations, numbers, patterns, arrays, and simple string operations.
1. Write a Hello World Program in Java
Input: Hello World! Output: Hello World!
Click Here for the Solution
2. Write a Program in Java to Add two Numbers.
Input: 2 3 Output: 5
Click Here for the Solution
3. Write a Program to Swap Two Numbers
Input: a=2 b=5 Output: a=5 b=2
Click Here for the Solution
4. Write a Java Program to convert Integer numbers and Binary numbers.
Input: 9 Output: 1001
Click Here for the Solution
5. Write a Program to Find Factorial of a Number in Java.
Input: 5 Output: 120
Click Here for the Solution
6. Write a Java Program to Add two Complex Numbers.
Input: 1+2i 4+5i Output: 5+7i
Click Here for the Solution
7. Write a Program to Calculate Simple Interest in Java
Input : P = 10000 R = 5 T = 5 Output : 2500
Click Here for the Solution
8. Write a Program to Find Sum of Fibonacci Series Number
Input: n = 4 Output: 33 Explaination: Sum of numbers at even indexes = 0 + 1 + 3 + 8 + 21 = 33.
Click Here for the Solution
Java Exercise on Pattern
9. Write a Program to Print the Pascal's Triangle in Java
Click Here for the Solution
10. Write a Program to Print Pyramid Number Pattern in Java.
Click Here for the Solution
11. Write a Java Program to Print Pattern.
Click Here for the Solution
12. Write a Java Program to Print Pattern.
Click Here for the Solution
13. Java Program to Print Patterns.
Input: number = 7
Click Here for the Solution
Java Intermediate Exercises
These exercises require more understanding of arrays, strings, searching techniques, and common algorithms.
14. Write a Java Program to Compute the Sum of Array Elements.
Input: [ 2, 4, 6, 7, 9] Output: 28
Click Here for the Solution
15. Write a Java Program to Find the Largest Element in Array
Input: [ 7, 2, 5, 1, 4] Output: 7
Click Here for the Solution
16. Write Java Program to Find the Tranpose of Matrix
Click Here for the Solution
17. Java Array Program For Array Rotation
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2
Output: 3 4 5 6 7 1 2
Explaination: d=2 so 2 elements are rotated to the end of the array. So, 1 2 is rotated back
So, Final result: 3, 4, 5, 6, 7, 1, 2
Click Here for the Solution
18. Java Array Program to Remove Duplicate Elements From an Array
Input: [ 1, 2, 2, 3, 3, 3, 4, 5 ] Output: [ 1, 2, 3, 4, 5 ]
Click Here for the Solution
19. Java Array Program to Remove All Occurrences of an Element in an Array
Input: array = [ 1, 2, 1, 3, 5, 1 ] , key = 1 Output: [2, 3, 5]
Explaination: all the occurrences of element 1 is removed from the array So, array becomes from
[ 1, 2, 1, 3, 5, 1 ] to Final result: [2, 3, 5]
Click Here for the Solution
String Exercises in Java
20. Java program to check whether a string is a Palindrome
Input: "racecar" Output: Yes Explaination: As racerar after reversing becomes racecar. As Reverse of String is same as String so it is Palindrome
Click Here for the Solution
21. Java String Program to Check Anagram
Input: str1 = "Silent" str2 ="Listen" Output: Strings are Anagram Explaination: As all the elements in str1 are exact same as to create str2 . i.e., we can create str2 using elements of str1 without removing any element or removing any extra element.
Click Here for the Solution
22. Java String Program to Reverse a String
Input: str= "Geeks" Output: "skeeG"
Click Here for the Solution
23. Java String Program to Remove leading zeros
Click Here for the Solution
Java Advanced Searching and Sorting Exercises
These exercises involve fundamental searching and sorting algorithms and require a stronger understanding of algorithmic problem-solving.
24. Write a Java Program for Linear Search.
Time Complexity: O(N) Space Complexity: O(N)
Click Here for the Solution
25. Write a Binary Search Program in Java.
Time Complexity: O(logN) Space Complexity: O(N)
Click Here for the Solution
Practice Problems in Java Sorting Algorithms
26. Java Program for Bubble Sort.
Time Complexity: O(N 2 ) Space Complexity: O(1)
Click Here for the Solution
27. Write a Program for Insertion Sort in Java.
Time Complexity: O(N 2 ) Space Complexity: O(1)
Click Here for the Solution
28. Java Program for Selection Sort.
Time Complexity: O(N 2 ) Space Complexity: O(1)
Click Here for the Solution
29. Java Program for Merge Sort.
Time Complexity: O(N logN) Space Complexity: O(N)
Click Here for the Solution
30. Java Program for QuickSort.
Time Complexity: O(N logN) Space Complexity: O(1)
Click Here for the Solution
Introduction 4 min read
Variables 2 min read
Data Types 7 min read
Operators 7 min read
Decision Making 5 min read
Methods 7 min read
Arrays 7 min read
Strings 5 min read
Introduction 6 min read
Constructors 6 min read
Encapsulation 3 min read
Inheritance 8 min read
Polymorphism 5 min read
Abstraction 5 min read
Interfaces 8 min read
Collection class 9 min read
Collection Interface 5 min read
List Interface 8 min read
Set Interface 5 min read
Queue Interface 5 min read
Map Interface in Java 6 min read
Iterator 4 min read
Exception handling 6 min read
Java Multithreading Tutorial 3 min read
Synchronization in Java 8 min read
File handling 6 min read
Method References 3 min read
Java 8 Stream Tutorial 8 min read
Memory Management 4 min read
Garbage Collection in Java 6 min read
Networking 6 min read
JDBC 5 min read
Interview Questions 15+ min read
Programs 6 min read
Practice Problems 5 min read
Quiz 1 min read
Project Ideas 15+ min read
Java Backend Course 2 min read
Placement 360 Course 2 min read