Pages

10 Programming questions and exercises for Java Programmers

If you have just started learning the 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 a particular part of Java, but these coding exercises
will switch you in programming mode. These are also great ways to master basic
programming construct like if-else, loops like for and
whilebreak and continue with loop, Java operators e.g., arithmetic
and logical operator, recursion, methods, or functions and standard Java API. You may also
find these Java programming questions in most Java courses taught in school,
colleges, and various Java training courses. Even I have started learning Java
by doing these exercises multiple times in different ways. They are
interesting, gives a feeling of accomplishment if you complete it. These Java programs
look 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 into them. You can also
see here more Java programming
questions, and exercises.

And, If you need to refresh your Data Structure and Algorithms skills to solve these Programming questions and exercise then check out Data Structures and Algorithms: Deep Dive Using Java course on Udemy. It's a great course to brush up essential data structures like an array, linked list, binary tree, hash table, stack, queue, and basic techniques like recursion, dynamic programming, greedy algorithms, etc.

Java Programing Questions exercises for beginners to practices

Here is my list of 10 Java programming questions or Java programs that can help any beginner to get
started in the 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 solutions 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 entirely divisible by two. For example, number 4 is even number
because when you do 4/2, the remainder is 0, which means 4 is completely divisible
by 2. On the other hand, 5 is an odd number because 5/2 will result in the 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 algorithms to find prime numbers like, instead of dividing till number,
division up to the square root of number may be enough. Start from the simplest one and
then try to solve this problem in a couple of different ways. Here is one way
to check prime numbers in Java

3. Write a Java
program to check if a number is a palindrome in Java? ( 121 is
a palindrome, 321 is not)

A number is called a palindrome if the number is equal to reverse of number
e.g., 121 is a palindrome because the reverse of 121 is 121 itself. On the other hand
321 is not a palindrome because the reverse of 321 is 123, which is not equal to 321.
See here for a solution of checking if a number is a palindrome or not
in Java.

4. How to
find if a number is the 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 like using arithmetic operators or by using a bit
shift operator.

5. Write
program to sort an integer array without using API methods?

Sorting questions are one of the integral parts of programming questions. There are many sorting algorithms out there to sort an array in Java e.g.
Bubble sort, Insertion sort, Selection sort, or quicksort. Implementing a sorting
algorithm itself a good programming exercise in Java. By the way, here is one
way to sort an integer array with a Bubble sort
algorithm in Java.

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 digits is a number for which sum of cube of its
digits is equal to number e.g., 371 is an Armstrong number because of 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 a 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 then
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 the Fibonacci series up to a given number? Write both
iterative and recursive versions.

Fibonacci series a popular number series and very popular programming question in Java, in which number is equal to the sum
of the 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 Iterations like loops and recursion. For help, see How to print the Fibonacci series in Java
using recursion. And, if you want to master the patterns on how to solve a problem using recursion etc, I suggest you check out Grokking the Coding Interview: Patterns for Coding Questions course on Educative, it's a great course to level up your coding skill also.

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 a natural
choice to solve this problem. You just need to remember the formula for calculating
Factorial, which is for n! its n*(n-1)*…1. Here is one way to calculate Factorial in Java using
recursion.

These were some programming questions and exercises for beginners learning the 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 to program and help to convert
logic to code.

Top 10 Courses to learn Data Structure and Algorithms in Java (courses)

Thanks for reading this article so far. If you like this Programming questions and exercises for Java Programmers then please share with your friends and colleagues. If you have any doubts or feedback then please drop a note.

P. S. - If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check the Data Structure in Java free course on Udemy. It's completely free, and all you need to do is create a free Udemy account to enroll in this course.

52 comments:

These are good Java programming exercises for beginners but for experienced programmer its too simple. I would rather give complex application to develop to experienced programmer in couple of hours rather than simple programming exercise.

To the previous commenter, even for experienced programmers some of these problems are still usefull to sharpen your skills with. A solid base will improve your overall ability and infact research into finding an efficint method of finding the factorial of an integer is still being conducted, if I'm not mistaken.

I was looking for some beginners Java questions for my training course. I loved your collection, they are not too difficult, easy to understand but only thing is that they lack novelty. They are quite old Java questions. Though I would like to use these, I am also putting my own e.g.

1) Write a Java program to get all hashtags and mentioned in a twitter message? (This would be a good String matching exercise in Java, as you can use regular expression, can split string etc. As hashtags starts with '#' and mentioned strats with '@' character.

2) Count How many characters a String contains, without including any white space e.g. \t \n etc.

Updated one, as we no need to repeat the biggest number of stars or number for example 999999999, and also the argument in simpleDrawing(10) has to be 1,3,5,7,9,11or so on not 10 import java.util.ArrayList;import java.util.List;

3. Write a program that reads from the user four integers representing the numerators and denominators of two fractions, calculates the results of the two fractions and displays the values of the fractions sum, subtraction, multiplication and division.

3. Write a program that reads from the user four integers representing the numerators and denominators of two fractions, calculates the results of the two fractions and displays the values of the fractions sum, subtraction, multiplication and division.

2. Write a program that reads in from the user an integer (num) between 1000 and 9999. Then it prompts the user to enter an integer (d) between 0 and 9 and a character (ch). Your program should replace the second and the last digit in num with d and it should display the character that precedes (ch) followed by the number after the change and then the character that comes after (ch). Use the division and modulus operators to extract the digits from num.

Sample run:Enter an integer between 1000 and 9999: 2134Enter a digit (between 0 and 9): 6Enter a character: bNumber was 2134. Result: a2636c.

1. Write a program that takes three double values x0, v0, and t from the user and prints the value x0 +v0t +g t2/2, where g is the constant 9.78033. This value is the displacement in meters after t seconds when an object is thrown straight up from initial position x0 at velocity v0 meters per second.

Sample run:Enter the value of x0, v0 and t: 0 2 2The displacement in meters after 2 seconds when an object is thrown straight up from initial position 0 at velocity 2meters per second is: 23.56066

Java Program to convert celsius to Farenheit and vice-versaarea of circleperimeter of circleare of rectangleperimeter of rectangleprint alphabetsprint multiplication tablelargetst of three integersfloyd's trianglepascal triangleadd matricestranspose matrixmultiply matrixperfect number or notfind common elements between two arraysbinary to decimal conversioninverse of matrix

2. Write a class named “Stock” to model a stock. The properties and methods of the class are shown in figure below. The method “changePercent” computes the percentage of the change of the current price vs the previous closing price. Write a client program to test the “Stock” class. In the client program, create a Stock object with the stock symbol SUNW, name Sun Microsystem Inc, previous closing price of 100. Set a new current price randomly and display the price change percentage.

Program#1//for beginnersQuestion: Write a program to count and print the frequency of word in a sentence(both accepted from the user)For example:Inputs//sentence-The lazy lion jumped over the the sharp fence) just kidding! word to be searched-the(irrespective of the case) The normal way of doing it wud be a little difficult nested loops and compare each letter after finding the match of the first letter. Ill show u my way of doing!!

Write a java multiple choice examination program. with 10 questions only (need a specific question). Let the user choose answer and at the end of the program output the total points to the user, output also the number of correct and wrong answer an:

A good one is given the length of a your output media (how wide your paper or screen is) print Pascal's Triangle in Triangle form. Stop when the next line of output will exceed your media width. You can do it two ways ... 1 is figure out how how to space the numbers so they all take the same amount of space (the "center" of the numbers are spaced the same from line to line and from each other; this makes a nice looking triangle, but is harder). 2 an easier version is to center the triangle on the page (left to right) but in each line of numbers the number are separated from each other by only one space You can get more lines in the triangle, but the "sides" are not straight and the relationship between elements gets skewed; however this is much easier.