If you have just started learning basics of Java programming language or familiar with programming in either C or C++ then these Java programming questions and exercises for you. It doesn't focus on particular part of Java but these coding exercises will switch you in programming mode.These are also great way to master basic programming construct like if-else , loops like for and while,break and continue with loop, Java operators e.g. arithmetic and logical operator, recursion, methods or functions and common Java API. You may also find these Java programming questions in most of Java courses taught in school, colleges and various Java training courses. Even I have started learning Java by doing these exercises multiple times with different ways. They are interesting, gives feeling of accomplishment if you complete it. These Java programs looks simple but they are still tricky for novice Java programmers. Try to solve these coding exercises by yourself but if you stuck you can check relevant links or of-course use google to get more insight on them. You can also see here more Java programming questions and exercises.
Java Programing Questions exercises for beginners to practices
Here is my list of 10 Java programming questions or Java programs which can help any beginner to get started in programming world. These are classics, popular and very effective. You can use either notepad or any Java IDE like Eclipse or Netbeans for coding. See links for solution and hints.
1. Write a program in Java to check if a number is even or odd in Java? (input 2 output true, input 3 : output false)
A number is called even if it is completely divisible by two and odd if it’s not completely divisible by two. For example number 4 is even number because when you do 4/2 , remainder is 0 which means 4 is completely divisible by 2. On the other hand 5 is odd number because 5/2 will result in remainder as 1. See here to find how to check even and odd number in Java.
2. Write a program in Java to find out if a number is prime in Java? (input 7: output true , input 9 : output false)
A number is called prime if it is divisible by either itself or 1. There are many algorithm to find prime numbers e.g. instead of dividing till number, division upto square root of number may be enough. Start from simplest one and than try to solve this problem with couple of different ways. Here is one way to check prime numbers in Java
3. Write Java program to check if a number is palindrome in Java? ( 121 is palindrome, 321 is not)
A number is called a palindrome if number is equal to reverse of number e.g. 121 is palindrome because reverse of 121 is 121 itself. On the other hand 321 is not palindrome because reverse of 321 is 123 which is not equal to 321. See here for solution of checking if a number is palindrome or not in Java.
4. How to find if a number is power of 2 in Java? (1,2, 4 power of 2, 3 is not)
This is another interesting Java programming exercise. This program can be solved using different ways e.g. using arithmetic operator or by using bit shift operator.
5. Write program to sort an integer array without using API methods?
6. Write Java program to check if a number is Armstrong number or not? (input 153 output true, 123 output false)
An Armstrong number of 3 digit is a number for which sum of cube of its digits are equal to number e.g. 371 is an Armstrong number because 3*3*3 + 7*7*7 + 1*1*1 = 371). See here for sample Java program to check if a number is Armstrong number or not.
7. Write a program in Java to reverse any String without using StringBuffer?
This is another classical Java programming question. You can reverse String in various way in Java but two programming technique is used to do e.g. Iteration and Recursion. Try solving this problem using Iteration first by using Java’s arithmetic operator and than look to implement a recursive solution. Here is one way to reverse String in Java without using StringBuffer.
8. Write a program in Java to print Fibonacci series up to given number? Write both iterative and recursive version.
Fibonacci series a popular number series and very popular programming question in Java, in which number is equal to sum of previous two numbers, starting from third. Fibonacci series is also a good recursion exercise and often asked in Interviews as well. Try doing this exercise by using both Iteration e.g. loops and recursion. For help see How to print Fibonacci series in Java using recursion.
9. Write a Java program to calculate factorial of an integer number ? Both iterative and recursive solution.
Calculating Factorial is also a classic recursion exercise in programming. Since factorial is a recursive function, recursion becomes natural choice to solve this problem. You just need to remember formula for calculating factorial which is for n! its n*(n-1)*…1. here is one way to calculate Factorial in Java using recursion.
10. Print following structure in Java?
This program is a good exercise for mastering loops e.g. for loop and while loop in Java. This also teaches you How to use break and continue statement with loops in Java. By the way you can print any character and use System.out.print() and System.out.println())
These were some programming questions and exercises for beginners learning Java programming language. This list is simple and you can solve these coding exercises in any programming language. I am sure Java beginners will find these exercises interesting and useful. You can also post any coding exercise which you think can help junior programmers to learn programming and help converting logic to code.
Other Java programming tutorials for beginners
How to set PATH for Java in Windows and Linux
How classpath works in Java
Difference between PATH and Classpath in Java
Difference between JDK, JRE and JVM
How to run Java program from command line in Windows
Java Programming Exercises, Practice, Solution
Java is the foundation for virtually every type of networked application and is the global standard for developing and delivering embedded and mobile applications, games, Web-based content, and enterprise software. With more than 9 million developers worldwide, Java enables you to efficiently develop, deploy and use exciting applications and services.
The best way we learn anything is by practice and exercise questions. Here you have the opportunity to practice the Java programming language concepts by solving the exercises starting from basic to more complex exercises. A sample solution is provided for each exercise. It is recommended to do these exercises by yourself first before checking the solution.
Hope, these exercises help you to improve your Java programming coding skills. Currently, following sections are available, we are working hard to add more exercises .... Happy Coding!
Note: If you are not habituated with Java programming you can learn from the following :
More to Come !
Popularity of Programming Language
Worldwide, Mar 2018 compared to a year ago:
Source : http://pypl.github.io/PYPL.html
TIOBE Index for March 2018
Source : https://www.tiobe.com/tiobe-index/
List of Exercises with Solutions :
- HTML CSS Exercises, Practice, Solution
- jQuery Exercises, Practice, Solution
- jQuery-UI Exercises, Practice, Solution
- CoffeeScript Exercises, Practice, Solution
- Twitter Bootstrap Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution
- C# Sharp Programming Exercises, Practice, Solution
- PHP Exercises, Practice, Solution
- Python Exercises, Practice, Solution
- Java Exercises, Practice, Solution
- SQL Exercises, Practice, Solution
- MySQL Exercises, Practice, Solution
- PostgreSQL Exercises, Practice, Solution
- SQLite Exercises, Practice, Solution
- MongoDB Exercises, Practice, Solution
- Euler Project
[ Want to contribute to Java exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]