
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.
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.
Also Read: Basic Java Program Examples
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 = 90Output
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; } } |
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
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--; } } } |
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; } } |
Ans: Consider the input and output conditions for the given Java programming problem.
Input
N = 5 arr[] = {4 5 3 1 2} X = 3Output
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; } } |
Ans: Consider the input and output conditions for the given Java programming problem.
Input
size = 6 arr[] = {1, 2, 3, 4, 5} element = 90Output
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; } } |
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 = 5Output
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 ; } } |
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); } } |
Ans: Consider the input and output conditions for the Java programming problem.
Input
m = 9, n = 6Output
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(); } } |
Input
N = 5 arr[] = {4 5 3 1 2} X = 3Output
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; } } |