Basic Java programs for practice: Candidates can learn Java programming language by solving questions based on arrays, linked list, stacks, trees, hash, graphs, etc. Practising helps strengthen your knowledge of the Java language. In this article, some basic Java problems are solved for practice.
Is Java programming worth it in 2024?
Java is a widely used programming language used for application development. Most of the big tech companies require highly skilled Java programmers in their organisations. If you want to learn Java programming, then you must practise a lot of Java questions, which will help you become job-ready as a Java developer and software developer.
Read this article to solve some popular basic Java programs for practice based on arrays.
Highlights:
- Java Programming Practice: The content emphasizes the importance of practicing Java programming by solving problems related to arrays, linked lists, stacks, trees, hash, and graphs. It suggests that practicing these problems can strengthen one’s knowledge of the Java language.
- Job Readiness: Learning Java programming and practicing it extensively can make candidates job-ready as Java developers or software developers. Many big tech companies require highly skilled Java programmers, making Java a valuable skill in the job market.
- Online Resources: Candidates are encouraged to utilize online resources and materials to learn and practice coding. The article mentions using an online compiler like PW Lab to practice coding in various languages, including Java.
- Basic Java Problems: The content provides solutions to some basic Java programming problems, covering topics such as inserting elements into an array, finding maximum and minimum elements, reversing an array, checking if an array is sorted, counting elements greater than a given value, inserting elements at the end of an array, finding the maximum frequency of an element, finding the second largest element, swapping numbers in an array, and counting elements in an array.
- Learning Resources: The content promotes learning Java programming with Decode Java with DSA Course, which covers important fundamentals of Java along with data structures and algorithms. Completing this course is suggested for achieving industry-level recognition.
Basic Java Program: How to Practice Coding?
Nowadays, candidates can learn and practice coding through various online resources and materials. However, practising important Java programming questions is essential. You can use our online PW Lab compiler to practice coding online. PW Lab supports all languagesÂ
Also Read: Basic Java Program Examples
Top 10 Basic Java Programs for Practice
Most of the interview questions start with basic array problems.
Example 1: Consider a 0-based index array. Write a program to insert an element at a given index.
Ans: Check for an example of the input and expected output of the program’s given output.
Input
N (Size of array) = 6
arr[] = {1, 2, 3, 4, 5}
index = 5, element = 90
Output
1 2 3 4 5 90
Best Java Codes for Practice |
class Solution
{     // You need to insert the given element at the given index.     // After inserting the elements at index, elements     // from index onward should be shifted one position ahead     // You may assume that the array already has sizeOfArray – 1     // elements.  Public void insertAtIndex(int arr[], int sizeOfArray, int index, int element)     {         for (int i = sizeOfArray – 1; i > index; i–)         {             arr[i] = arr[i – 1];         }         arr[index] = element;     } } |
Example 2: Find the maximum and minimum element in an array of size N.
Ans: Check an example for input and output condition of the given programming problem.
Input
N = 4
arr[] = {5 4 2 1}
Output
5 1Â
Best Java Codes for Practice |
class Get
{ Â Â Â Â public static int maximumElement(int arr[],int n) Â Â Â Â { Â Â Â Â Â Â Â Â int maxElement = Integer.MIN_VALUE; Â Â Â Â Â Â Â Â for(int i=0;i<n;i++) Â Â Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â Â Â maxElement = Math.max(maxElement,arr[i]); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â return maxElement; Â Â Â Â } Â Â Â Â public static int minimumElement(int arr[],int n) Â Â Â Â { Â Â Â Â Â Â Â Â int minElement = Integer.MAX_VALUE; Â Â Â Â Â Â Â Â for(int i=0;i<n;i++) Â Â Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â Â Â minElement = Math.min(minElement,arr[i]); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â return minElement; Â Â Â Â } } |
Also Read: Basic Java Programs For Beginners
Example 3: Reverse a given array of size N.
Ans: Consider the input and output conditions of the given Java problem. Check the code implementation below.
Input
n = 5
arr[] = {1 1 2 2 3}
Output
3 2 2 1 1
Best Java Codes for Practice |
class Get {
    public static void reverseArray(int arr[], int n) {         // Your code here         int temp;         int low=0;         int high=n-1;         while(low<high){             temp=arr[low];             arr[low]=arr[high];             arr[high]=temp;             low++;             high–;         }     } } |
Example 4: Write a basic Java program to find whether an array is sorted or not.
Ans: Check the input and output condition of the following Java programming problem.
Input
N = 5
a[] = {1 1 2 2 3}
Output
1
Best Java Codes for Practice |
class Solution
{     //Complete the function     public static int isSorted(int arr[], int n)     {        if(n==1)             return 1;         int flag = 1;        if(arr[0]<=arr[1])        {            for(int i=1;i<n;i++)            {                if(arr[i] == arr[i-1])                     Continue;                else if(arr[i]<arr[i-1])                {                     flag = 0;                     break;                }            }        }        if(arr[0]>=arr[1])        {            flag = 1;            for(int i=1;i<n;i++)            {                if(arr[i] == arr[i-1])                     continue;                else if(arr[i]>arr[i-1])                 {                     flag=0;                     break;                 }            }        }         return flag;     } } |
Example 5: Write a Java program code to count the number of elements strictly greater than an element X in a non-zero array.Â
Ans: Consider the input and output conditions for the given Java programming problem.
Input
N = 5
arr[] = {4 5 3 1 2}
X = 3
Output
2
Best Java Codes for Practice |
class Solution
{     // arr[]: input array     // n: size of the array     // x: element for which you need to return the count     public static int greaterThanX(int arr[], int n, int x)     {         // Your code here         int count = 0;         for(int i = 0; i<n; i++)         {             if(arr[i]>x)             count++;         }         return count;     } } |
Example 6: Write a Java program to insert an element of an array at the end.
Ans: Consider the input and output conditions for the given Java programming problem.
Input
size = 6
arr[] = {1, 2, 3, 4, 5}
element = 90
Output
1 2 3 4 5 90
Best Java Codes for Practice |
class Insert
{     // You only need to insert the given element at     // the end, i.e., at index sizeOfArray – 1. You may     assume that the array already has sizeOfArray – 1     // elements.     public void insertAtEnd(int arr[], int sizeOfArray, int element)     {         arr[sizeOfArray-1]=element;     } } |
Example 7: Write a Java Program to find the maximum frequency of an element in an array.
Ans: Consider the input and output conditions for the given Java programming problem.
Input
N = 11
arr[] = {1,1,2,2,3,3,4,4,4,4,5}
x = 4, y = 5
Output
4
Best Java Codes for Practice |
class Solution {
    // Function to find an element with more appearances between two elements in an array.     // array.     public int majorityWins(int arr[], int n, int x, int y) {         // code here         int cntx = 0;         int cnty = 0;         for (int ele : arr) {             if (ele == x) {                 cntx++;             }             else if (ele == y) {                 cnty++;             } else {                 continue;             }         }         if (cntx == cnty) {             return Math.min(x, y);         }         return cntx > cnty ? x : y ;     } } |
Example 8: Write a Java program code to calculate the second largest component present in the array.
Ans: Consider the input and output conditions for the given Java programming problem.
Input
arr[] = {12, 35, 2, 10, 34, 1}
Output
The second largest element is 34.
Best Java Codes for Practice |
// Java program to find the second largest component in an array
import java.util.*; class SecondLargest{ // Function to print the second largest elements static void print2largest(int arr[], int arr_size) {   int i, first, second;   // There should be   // atleast two elements   if (arr_size < 2)   {     System.out.printf(” Invalid Input “);     return;   }   // Sort the array   Arrays.sort(arr);   // Start from second last element as the largest element is at last   for (i = arr_size – 2; i >= 0; i–)   {     // If the element is not equal to largest element     if (arr[i] != arr[arr_size – 1])     {       System.out.printf(“The second largest ” + “element is %d\n”, arr[i]);       return;     }   }   System.out.printf(“There is no second ” + “largest element\n”); } // Driver code public static void main(String[] args) {   int arr[] = {12, 35, 1, 10, 34, 1};   int n = arr.length;   print2largest(arr, n);   } } |
Example 9: Write a Java program code to swap two numbers in an array.
Ans: Consider the input and output conditions for the Java programming problem.
Input
m = 9, n = 6
Output
m = 6, n = 9
Best Java Codes for Practice |
import java.util.Scanner;
public class SwapNumbers {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         // Input the numbers         System.out.print(“Enter the first number: “);         int num1 = scanner.nextInt();         System.out.print(“Enter the second number: “);         int num2 = scanner.nextInt();         // Display the numbers before swapping         System.out.println(“Before swapping:”);         System.out.println(“First number: ” + num1);         System.out.println(“Second number: ” + num2);         // Swap the numbers         int temp = num1;         num1 = num2;         num2 = temp;         // Display the numbers after swapping         System.out.println(“\nAfter swapping:”);         System.out.println(“First number: ” + num1);         System.out.println(“Second number: ” + num2);         scanner.close();     } } |
Example 10: Write a Java program to count the number of elements in an array.
Ans: Consider the input and output conditions for the Java programming problem.
Input
N = 5
arr[] = {4 5 3 1 2}
X = 3
Output
2
Best Java Codes for Practice |
class Solution
{     public static int smallerThanX(int arr[], int n, int x)     {         // Your code here         int count = 0;         for (int i =0; i<n; i++)         {             if(arr[i]<x)             count++;         }         return count;     } } |
Learn Java with PW Skills
Learn Java programming with our Decode Java with DSA Course, completely packed with the latest resources and materials for learning Java with industry level experts covering the entire course. Learn important fundamentals of Java with important data structures and algorithms, Get industry-level recognized after completing the course.
Basic Java Program FAQs
How can I practice Java?
The best way to learn a programming language is through practice. Nowadays, there are many online compilers that can help you practice anywhere, anytime. First, learn the basic syntax. Then start with beginner-level programming questions and slowly move forward.
Is learning Java difficult?
Learning Java is not at all difficult if you are consistent and learn with the best resources and planning. Java is an object oriented programming language that is independent and can be used on multiple OS devices.
Which language, Java or C++, is better?
If you are a beginner looking for a better programming language to start your programming journey, then use some of the frequently used programming languages and then decide by yourself. Programming languages are just a medium through which to communicate with the computer.
Can I self-learn Java?
Yes, candidates can easily learn Java without any other people's help or support. However, many efficient and productive resources available online can make your preparation more effective.