Java Programming Exercises

Sharpen your Java skills with comprehensive topic-wise practice problems

Beginner to Advanced 200+ Problems Topic-wise Lists

Practice Makes Perfect

Master Java programming through hands-on practice with our comprehensive collection of exercises. Each topic includes problems of varying difficulty levels to help you strengthen your understanding and problem-solving skills.

200+ Problems

Comprehensive coverage of all Java topics

Progressive Difficulty

From beginner to advanced levels

Practical Focus

Real-world problem scenarios

1. Introduction to Java, Basic Syntax and Structure

Get familiar with Java program structure and basic syntax

  1. Write a program that displays "Hello, World!" on the screen. Easy
  2. Create a program with a main method and demonstrate the structure of a Java class. Easy
  3. Write a program with single-line and multi-line comments explaining the code. Easy
  4. Create a program that demonstrates the use of packages and imports. Easy
  5. Write a program that demonstrates the difference between JDK, JRE, and JVM. Medium
  6. Create a program that shows the basic structure of a Java program with multiple methods. Medium
  7. Write a program that demonstrates the use of command-line arguments. Medium
  8. Create a program that uses both System.out and System.err for output. Medium
  9. Write a program that demonstrates the Java compilation and execution process. Hard
  10. Create a program that shows the Java platform independence feature. Hard

2. Variables and Data Types, Constants and Literals

Practice working with different data types and constants

  1. Declare variables of different data types (int, float, double, char, boolean) and display their values. Easy
  2. Create a program that uses final keyword to declare constants and demonstrate their usage. Easy
  3. Write a program that demonstrates implicit and explicit type conversion (casting) in Java. Medium
  4. Create a program that displays the size and range of various primitive data types. Easy
  5. Write a program that demonstrates the use of literals (integer, floating-point, character, string). Medium
  6. Create a program that shows the difference between primitive and reference data types. Medium
  7. Write a program that demonstrates variable scope and lifetime in different blocks. Medium
  8. Create a program that uses enumerations and shows their advantages over constants. Medium
  9. Write a program that demonstrates the use of var keyword for type inference (Java 10+). Hard
  10. Create a program that shows the difference between stack and heap memory allocation. Hard

3. Input and Output (I/O), Operators

Practice using I/O and different operators

  1. Write a program that takes user input using Scanner and displays it with appropriate formatting. Easy
  2. Create a calculator program that performs basic arithmetic operations on two numbers. Easy
  3. Write a program that demonstrates the use of relational and logical operators. Easy
  4. Create a program that demonstrates the use of compound assignment operators. Easy
  5. Write a program that uses bitwise operators to manipulate data. Medium
  6. Create a program that demonstrates operator precedence and associativity. Medium
  7. Write a program that uses BufferedReader for input and formatted output. Medium
  8. Create a program that handles input validation and exception handling. Medium
  9. Write a program that demonstrates the use of the conditional (ternary) operator. Hard
  10. Create a program that uses instanceof operator and demonstrates its use cases. Hard

4. Conditional Control Statements

Practice using if, else if, else, and switch statements

  1. Write a program that checks if a number is positive using an if statement. Easy
  2. Create a program that determines the grade based on marks using if-else if-else statements. Medium
  3. Write a program that implements a simple calculator using switch case statements. Medium
  4. Create a program that uses nested if statements to solve a complex conditional problem. Hard
  5. Write a program that demonstrates the difference between if-else and switch statements. Medium
  6. Create a program that uses conditional statements to determine leap years. Medium
  7. Write a program that implements a menu-driven interface using switch cases. Medium
  8. Create a program that uses conditional operators to find the maximum of three numbers. Medium
  9. Write a program that demonstrates switch expressions (Java 14+ feature). Hard
  10. Create a program that uses conditional statements to solve a quadratic equation. Hard

5. Loops

Practice using for, while, and do-while loops

  1. Write a program that prints numbers from 1 to 10 using a for loop. Easy
  2. Create a program that calculates the sum of numbers until the user enters 0 using a while loop. Medium
  3. Write a program that demonstrates the difference between while and do-while loops. Medium
  4. Create a program that prints patterns using nested loops (e.g., pyramid, diamond). Hard
  5. Write a program that uses loops to calculate the factorial of a number. Medium
  6. Create a program that finds prime numbers within a range using loops. Medium
  7. Write a program that demonstrates the use of break and continue statements in loops. Medium
  8. Create a program that implements the Fibonacci sequence using loops. Medium
  9. Write a program that uses enhanced for loops (for-each) to iterate over arrays. Hard
  10. Create a program that demonstrates infinite loops and how to avoid them. Hard

6. Arrays

Practice working with single and multi-dimensional arrays

  1. Write a program that declares, initializes, and displays the elements of an array. Easy
  2. Create a program that finds the sum, average, maximum, and minimum of array elements. Medium
  3. Write a program that performs matrix addition and multiplication using 2D arrays. Hard
  4. Implement sorting algorithms (bubble sort, selection sort) to sort an array of numbers. Hard
  5. Create a program that searches for an element in an array using linear and binary search. Medium
  6. Write a program that demonstrates array traversal using enhanced for loops. Medium
  7. Create a program that finds the second largest element in an array. Medium
  8. Write a program that merges two sorted arrays into a third sorted array. Hard
  9. Create a program that finds the frequency of elements in an array. Medium
  10. Write a program that implements a simple stack using arrays. Hard

7. Strings

Practice working with Java String class and methods

  1. Write a program that demonstrates declaration, initialization, and display of Java strings. Easy
  2. Create a program that performs various string operations (concatenation, comparison, length). Medium
  3. Write a program that reverses a string, converts case, and finds substrings. Medium
  4. Create a program that checks if a given string is a palindrome. Medium
  5. Write a program that counts vowels, consonants, digits, and special characters in a string. Medium
  6. Create a program that demonstrates the difference between String, StringBuilder, and StringBuffer. Medium
  7. Write a program that tokenizes a string using StringTokenizer and split() method. Hard
  8. Create a program that removes all occurrences of a character from a string. Medium
  9. Write a program that checks if two strings are anagrams of each other. Hard
  10. Create a program that implements basic string encryption and decryption. Hard

8. Methods, Recursion

Practice creating methods and using recursion

  1. Write a program with methods that perform basic mathematical operations. Easy
  2. Create a program that demonstrates method overloading with different parameter types. Medium
  3. Write a program that calculates factorial and Fibonacci numbers using recursion. Medium
  4. Create a program that demonstrates the use of variable arguments (varargs) in methods. Medium
  5. Write a program that demonstrates pass by value for primitive types and objects. Medium
  6. Create a program that demonstrates method visibility (public, private, protected). Medium
  7. Write a program that uses recursive methods to solve the Tower of Hanoi problem. Hard
  8. Create a program that demonstrates the use of static methods and variables. Hard
  9. Write a program that uses recursive methods to implement binary search. Hard
  10. Create a program that demonstrates method references (Java 8+ feature). Hard

9. Object-Oriented Programming (OOP)

Practice Object-Oriented Programming concepts

  1. Create a class to represent a Rectangle with methods to calculate area and perimeter. Medium
  2. Write a program that demonstrates inheritance with a base class and a derived class. Medium
  3. Create a program that demonstrates polymorphism with method overriding. Medium
  4. Write a program that demonstrates abstraction using abstract classes and methods. Hard
  5. Create a program that demonstrates encapsulation with private fields and public getters/setters. Medium
  6. Write a program that demonstrates the use of interfaces and multiple inheritance. Hard
  7. Create a program that demonstrates the use of the super keyword in inheritance. Medium
  8. Write a program that demonstrates the use of the final keyword with classes and methods. Medium
  9. Create a program that demonstrates composition and aggregation relationships. Hard
  10. Write a program that demonstrates the use of static and instance initializer blocks. Hard

10. Exception Handling

Practice handling exceptions in Java

  1. Write a program that demonstrates try-catch blocks for handling exceptions. Medium
  2. Create a program that uses multiple catch blocks for different exception types. Medium
  3. Write a program that demonstrates the use of finally block for cleanup operations. Medium
  4. Create a program that demonstrates the use of throw keyword to throw exceptions. Medium
  5. Write a program that creates custom exception classes. Hard
  6. Create a program that demonstrates exception propagation in method calls. Hard
  7. Write a program that demonstrates try-with-resources statement for automatic resource management. Hard
  8. Create a program that demonstrates the difference between checked and unchecked exceptions. Medium
  9. Write a program that handles multiple exceptions in a single catch block (Java 7+). Hard
  10. Create a program that demonstrates exception chaining and nested exceptions. Hard

11. Collections Framework

Practice working with Java Collections

  1. Write a program that demonstrates the use of ArrayList for storing and manipulating data. Medium
  2. Create a program that uses LinkedList to implement a simple queue or stack. Medium
  3. Write a program that uses HashSet to store unique elements and perform set operations. Medium
  4. Create a program that uses HashMap to store key-value pairs and perform operations. Medium
  5. Write a program that demonstrates the use of TreeSet for storing sorted unique elements. Hard
  6. Create a program that uses TreeMap for storing key-value pairs in sorted order. Hard
  7. Write a program that demonstrates the use of Iterator and ListIterator for traversing collections. Medium
  8. Create a program that demonstrates the use of Comparable and Comparator interfaces for sorting. Hard
  9. Write a program that demonstrates the use of Collections class utility methods. Hard
  10. Create a program that demonstrates the use of generics in collections for type safety. Hard

12. File I/O

Practice working with files in Java

  1. Write a program that reads data from a text file and displays it on the console. Medium
  2. Create a program that writes data to a text file. Medium
  3. Write a program that copies the contents of one file to another. Medium
  4. Create a program that counts the number of lines, words, and characters in a file. Hard
  5. Write a program that demonstrates the use of File class to get file information. Medium
  6. Create a program that reads and writes binary data using FileInputStream and FileOutputStream. Hard
  7. Write a program that demonstrates the use of BufferedReader and BufferedWriter for efficient I/O. Hard
  8. Create a program that serializes and deserializes objects to/from files. Hard
  9. Write a program that demonstrates the use of RandomAccessFile for random file access. Hard
  10. Create a program that demonstrates the use of NIO (New I/O) features for file operations. Hard

13. Multithreading

Practice working with threads in Java

  1. Write a program that creates threads by extending the Thread class. Medium
  2. Create a program that creates threads by implementing the Runnable interface. Medium
  3. Write a program that demonstrates thread synchronization using synchronized methods. Hard
  4. Create a program that demonstrates thread communication using wait() and notify() methods. Hard
  5. Write a program that demonstrates the use of thread priorities. Medium
  6. Create a program that demonstrates the use of ExecutorService for thread pool management. Hard
  7. Write a program that demonstrates the use of Callable and Future for returning results from threads. Hard
  8. Create a program that demonstrates the use of concurrent collections (e.g., ConcurrentHashMap). Hard
  9. Write a program that demonstrates deadlock and how to avoid it. Hard
  10. Create a program that demonstrates the use of atomic variables for thread-safe operations. Hard

14. Java 8+ Features

Practice using modern Java features

  1. Write a program that demonstrates the use of lambda expressions for functional interfaces. Medium
  2. Create a program that uses Stream API for processing collections of data. Hard
  3. Write a program that demonstrates the use of Optional class to avoid null pointer exceptions. Hard
  4. Create a program that uses method references instead of lambda expressions. Hard
  5. Write a program that demonstrates the use of default and static methods in interfaces. Hard
  6. Create a program that uses the new Date and Time API (java.time package). Hard
  7. Write a program that demonstrates the use of CompletableFuture for asynchronous programming. Hard
  8. Create a program that uses the new HTTP Client API (Java 11+). Hard
  9. Write a program that demonstrates the use of records (Java 14+ feature). Hard
  10. Create a program that demonstrates the use of sealed classes (Java 17+ feature). Hard

15. Advanced Java Concepts

Practice advanced Java programming concepts

  1. Write a program that demonstrates the use of annotations and custom annotations. Hard
  2. Create a program that uses reflection to inspect classes, methods, and fields at runtime. Hard
  3. Write a program that demonstrates the use of Java generics with wildcards. Hard
  4. Create a program that demonstrates the use of Java modules (Java 9+ feature). Hard
  5. Write a program that demonstrates the use of functional programming patterns in Java. Hard
  6. Create a program that demonstrates the use of design patterns (e.g., Singleton, Factory). Hard
  7. Write a program that demonstrates the use of Java Native Interface (JNI) for calling native code. Hard
  8. Create a program that demonstrates the use of Java Security Manager and permissions. Hard
  9. Write a program that demonstrates the use of Java Instrumentation for bytecode manipulation. Hard
  10. Create a program that demonstrates the use of Java Flight Recorder and Mission Control. Hard

Tips for Success

Practice Regularly

Consistent practice is key to mastering Java programming. Try to solve at least one problem every day.

Understand the Concepts

Don't just copy solutions. Make sure you understand the underlying concepts and principles.

Debug Your Code

Learning to debug is an essential skill. When your code doesn't work, use debugging techniques to find and fix issues.

Read Documentation

Get familiar with the official Java documentation. It's an invaluable resource for any Java developer.

Join the Community

Participate in Java forums, attend meetups, and contribute to open source projects to learn from others.

Build Projects

Apply your knowledge by building real-world projects. This will help solidify your understanding.