Open In App

Java Exercises – Basic to Advanced Java Practice Programs with Solutions

Last Updated : 03 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Looking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills.

As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers often find it difficult to find a platform for Java Practice Online. In this article, we have provided Java Practice Programs. That covers various Java Core Topics that can help users with Java Practice.

So, with ado further take a look at our free Java Exercises to practice and develop your Java programming skills. Our Java programming exercises Practice Questions from all the major topics like loops, object-oriented programming, exception handling, and many more.

Java Exercise

Java Practice Programs

This Java exercise is designed to deepen your understanding and refine your Java coding skills, these programs offer hands-on experience in solving real-world problems, reinforcing key concepts, and mastering Java programming fundamentals. Whether you’re a beginner who looking to build a solid foundation or a professional developer aiming to sharpen your expertise, our Java practice programs provide an invaluable opportunity to sharpen your craft and excel in Java programming language.

1. Write Hello World Program in Java

The solution to the Problem is mentioned below:

Java
// Java Program to Print
// Hello World
import java.io.*;

// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {

        // Printing Hello World
        System.out.println("Hello World!");
    }
}

Output
Hello World!


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 Print the Pascal’s Triangle in Java

Input : N = 5
Output:
          1
        1   1
      1   2   1
    1   3   3   1
  1   4   6   4   1
1   5   10   10   5   1

Click Here for the Solution

9. 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

Pattern Exercises in Java

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.

          1 
        2 1 2 
      3 2 1 2 3 
    4 3 2 1 2 3 4 
  5 4 3 2 1 2 3 4 5 
6 5 4 3 2 1 2 3 4 5 6 

Click Here for the Solution

13. Java Program to Print Patterns.

     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

Click Here for the Solution

Array Exercises in Java

Array Exercises in Java

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

Input: 
[ [ 1, 2, 3 ]
  [ 4, 5, 6 ]
  [ 7, 8, 9 ] ]
Output: 
[ [ 1, 4, 7]
  [ 2, 5, 8]
  [ 3, 6, 9] ]

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

Strings 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

Input : 00000123569
Output : 123569

Click Here for the Solution

Java Practice Problems for Searching Algorithms

Searching Exercises in Java

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

Sorting_in_java

26. Java Program for Bubble Sort.

Time Complexity: O(N2)
Space Complexity: O(1)

Click Here for the Solution

27. Write a Program for Insertion Sort in Java.

Time Complexity: O(N2)
Space Complexity: O(1)

Click Here for the Solution

28. Java Program for Selection Sort.

Time Complexity: O(N2)
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

Conclusion

After completing these Java exercises you are a step closer to becoming an advanced Java programmer. We hope these exercises have helped you understand Java better and you can solve beginner to advanced-level questions on Java programming.

Solving these Java programming exercise questions will not only help you master theory concepts but also grasp their practical applications, which is very useful in job interviews.

More Java Practice Exercises

Click Here – To Practice Java Online please check our Practice Portal.

Java Exercise – FAQ

1. How to do Java projects for beginners?

To do Java projects you need to know the fundamentals of Java programming. Then you need to select the desired Java project you want to work on. Plan and execute the code to finish the project. Some beginner-level Java projects include:

  • Reversing a String
  • Number Guessing Game
  • Creating a Calculator
  • Simple Banking Application
  • Basic Android Application

2. Is Java easy for beginners?

As a programming language, Java is considered moderately easy to learn. It is unique from other languages due to its lengthy syntax. As a beginner, you can learn beginner to advanced Java in 6 to 18 months.

3. Why Java is used?

Java provides many advantages and uses, some of which are:

  • Platform-independent
  • Robust and secure
  • Object-oriented
  • Popular & in-demand
  • Vast ecosystem


Next Article

Similar Reads

Java OpenCV Programs - Basic to Advanced
Java is a popular programming language that can be used to create various types of applications, such as desktop, web, enterprise, and mobile. Java is also an object-oriented language, which means that it organizes data and behaviour into reusable units called classes and objects. Java is known for its portability, performance, security, and robust
2 min read
Java Threading Programs - Basic to Advanced
Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing parallel execution of multiple tasks. Java threading pr
3 min read
Java Directories Programs: Basic to Advanced
Directories are an important part of the file system in Java. They allow you to organize your files into logical groups, and they can also be used to control access to files. In this article, we will discuss some of the Java programs that you can use to work with directories. We will cover how to create directories, delete directories, check if a d
2 min read
Java Collection Programs - Basic to Advanced
As it cleared from its name itself "Collection" it is a pre-defined collective bunch of classes and Interfaces present in the "Collection Framework" in Java. Their Classes, Interfaces and Methods are frequently used in competitive programming. This article provides a variety of programs on Java Collections, that are frequently asked in the Technica
3 min read
Java JDBC Programs - Basic to Advanced
Just like in many other programming languages, there is a specialized library available for executing CRUD operations when working with databases. Similarly, In Java, We have JDBC, an API(Application programming interface) used in Java programming to interact with databases. The classes and interfaces of JDBC allow the application to send user requ
3 min read
Java Regex Programs - Basic to Advanced
In Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Regular Expressions in Java are provided under java.util.regex package. This consists of 3 classes and 1 interface. In Java Interviews, Regex questions are generally asked by Int
5 min read
Java Networking Programs - Basic to Advanced
Java allows developers to create applications that can communicate over networks, connecting devices and systems together. Whether you're learning about basic connections or diving into more advanced topics like client-server applications, Java provides the tools and libraries you need. This Java Networking programs will guide you through essential
3 min read
Java Apache POI Programs - Basic to Advanced
This Java Apache POI program guide provides a variety of programs on Java POI, that are frequently asked in the technical round in various Software Engineering/core JAVA Developer Interviews. Additionally, All practice programs come with a detailed description, Java code, and output. Apache POI is an open-source java library to create and manipulat
3 min read
Difference between Core Java and Advanced Java
Core Java: Core Java is the part of Java programming language that is used for creating or developing a general-purpose application. Advanced Java: Advanced Java is also a part of Java programming language that generally deals with online application like the website and mobile application. Below is the difference between the two: Core JavaAdvanced
1 min read
Output of Java Programs | Set 55 (Java Collections Framework)
Pre-requisites: Java Collection Framework. 1. What is the Output of following Java Program? import java.util.*; class Demo { public static void main(String[] args) { ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(11); arr.add(2); arr.add(3); arr.add(5); arr.add(7); arr.remove(new Integer(7)); arr.remove(2); for (int i = 0; i
6 min read
three90RightbarBannerImg