Simple Java Programs With Examples

Simple java programs are good for assessing the coding skills of a programmer. You will find coding related questions in almost any interview.

The idea of these programming questions is to look into the thought process of the candidate. Most of the times the initial solution is not efficient, which calls for further questions and see if interviewee can improve it further or not.

Simple Java Programs

Here are I am providing 10 simple java programs. They are good for coding practice and can be used in interviews. Please try to write the solution yourself before looking at the answer, that will give you more confidence and you can check if your solution is good or not. Note that the solutions can be improved further, so if you got a better approach then please let us know through comments.

Reverse a String

  • Null check to avoid NullPointerException.
  • Using StringBuilder instead of StringBuffer for better performance.
  • Creating a local variable for input string length, rather than having it in if condition. Less number of function calls, better the performance.
  • Could have been improved by taking user input rather than static testing data

Fibonacci Series

  • Program can be improved by taking user input for number of integers to print.
  • Notice the use of System.out.print function to print numbers in single line, good job.
  • Fibonacci numbers starts from 0 or 1, above program can be extended to take user input for starting point.
  • Nicely written simple program, good to see no use of recursion or complex coding.

Prime Number Check

  • Good to see use of Scanner class to take user input.
  • Notice the few if checks to check the numbers where rules are different i.e. 0,1 and 2. 0 and 1 are not prime numbers and 2 is the only even prime number.
  • The division check is done only till i , smart thinking and good for performance.
  • The method will fail incase of negative integers – Either let user know to enter positive integer in the main method Or make the integer positive and then process it.

Check for Palindrome String

  • Creating a new string in reverse order using StringBuilder and then checking if its value is same as the original string.
  • Implementation can be improved by comparing the characters from both end. If String length is 5 then it’s palindrome if chars at 0th==4th and 1st==3rd. No need to create a separate string. Try to write code for this and post in comments section.

Sort an array programmatically

  • There are many sorting algorithms, bubble sort is easier to implement.
  • Sorting is complex and you should rely on Java API methods for sorting a collection or array for better performance that inventing the wheel again.
  • Also mention the use of Comparable and Comparator in sorting will add bonus points for you.

Read 3 input Strings, concat and print

  • Program is flexible to concat any number of strings, that shows thinking to code reuse and keeping it flexible.
  • Proper messages to guide the user when someone runs the program.
  • Use of StringBuilder rather than String + operator for concatenation.
  • Closing resources as soon as we are done with it, hence avoiding memory leak. Shows good programming habits.

Remove odd numbers from integer array

If you are reading this code, you should see that it adheres to all the points mentioned above. Try to write clean and simple code, follow best practices, naming conventions for methods and variables and you will be good.

Delete all matching elements from a list

  • Notice the removeAll method is created for list of objects, so it will work with any type of list. This is how we write reusable code.
  • Using iterator to remove the element from the list.
  • Trimming the input to remove any accidental white spaces from input strings.

Average of numbers with rounding half-up and scale 2

java basic programs, java interview programs

Swapping two numbers

We can easily swap numbers using a temporary variable. But if you can do it without that, then it’s awesome.

That’s all for simple java programs for interviews. Go through them and try to learn the best ways to write code and impress the interviewer.

Reference: Really Big Index for Java

By admin

Leave a Reply

%d bloggers like this: