Java Programming Interview Questions for freshers
-
What is Java Array?
It refers to a variable-size, ordered collection whose elements have the identical type. -
What are the types of Java arrays?
Single-dimensional arrays (for example, of type int[] arr = new int[5]) and multi-dimensional arrays (e.g.,int[][] arr = new int[3][3]. -
How do you initialize an array in Java?
Use either int[] arr = {1, 2, 3, 4, 5}; (remember the semicolon) or int[] arr = new int[5];. -
What is the default value of an array in Java?
Numeric types default to 0, boolean to false, and reference types such as an object's field value through a NULL pointer exception error. -
Is the size of an array in Java changeable after initialization?
No! An array has no dynamic conversion and will always be the same from the day it is initialized until the end. -
What's the difference between Java array and ArrayList?
-
How do you copy an array in Java?
You can useSystem.arraycopy()
,Arrays.copyOf()
, or theclone()
method. -
How do you sort an array in Java?
Arrays.sort(array);
-
How do you search for an element in an array?
Use a loop orArrays.binarySearch(array, key);
for sorted arrays. -
How do you convert an array to an exact corresponding index in a List?
Arrays.asList(array);
A method that returns a fixed-size list backed by the array.
Java Interview Programming Questions
-
Write a Java program to reverse a string.
public class ReverseString { public static void main(String[] args) { String str = "Hello"; String reversed = new StringBuilder(str).reverse().toString(); System.out.println("Reversed String: " + reversed); } }
-
Write a Java program to test a number for prime status.
public class PrimeCheck { public static boolean isPrime(int num) { if (num <= 1) return false; for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) return false; } return true; } public static void main(String[] args) { int number = 29; System.out.println(number + " is prime: " + isPrime(number)); } }
-
Write a Java program to find the largest number in an array.
public class LargestElement { public static void main(String[] args) { int[] arr = {10, 20, 5, 8, 30}; int max = arr[0]; for (int num : arr) { if (num > max) { max = num; } } System.out.println("Largest Element: " + max); } }
-
Write a Java program to delete duplicates from an array.
import java.util.HashSet; import java.util.Arrays; public class RemoveDuplicates { public static void main(String[] args) { int[] arr = {1, 2, 3, 1, 2, 4, 5}; HashSet<Integer> set = new HashSet<>(); for (int num : arr) { set.add(num); } System.out.println("Array without duplicates: " + set); } }
-
Write a Java program to find the missing number in an array of 1 to N.
public class MissingNumber { public int findMissing(int[] arr, int n) { int sum = n * (n + 1) / 2; for (int num : arr) { sum -= num; } return sum; } public static void main(String[] args) { int[] arr = {1, 2, 4, 5, 6}; int n = 6; System.out.println("Missing number: " + new MissingNumber().findMissing(arr, n)); } }
No comments