Sharpen Your Skills: Programming Languages Practice Q&A

Programming practice questions

Java Array

 
1

Question- How to Initialization Array in Java?



Answer-
public class BasicArray
{
    public static void main(String[] args)
{
        int[] numbers = {1, 2, 3, 4, 5};
        for (int number : numbers)
{
            System.out.println(number);
       }
    }
}

2

Question- How to Array Declaration and Initialization in Java?



Answer-
public class ArrayDeclaration
{
    public static void main(String[] args)
{
        String[] fruits = new String[3];
        fruits[0] = "Apple";
        fruits[1] = "Banana";
        fruits[2] = "Cherry";
        for (String fruit : fruits)
{
            System.out.println(fruit);
        }
    }
}

3

Question- How to Multidimensional Array in Java?



Answer-
public class MultiDimensionalArray
{
    public static void main(String[] args)
{
        int[][] matrix =
{
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        for (int i = 0; i < matrix.length; i++)
   {
            for (int j = 0; j < matrix[i].length; j++)
   {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

4

Question- How to create Array of Objects in Java?



Answer-
 class Person {
    String name;
    int age;

    Person(String name, int age)
 {
        this.name = name;
        this.age = age;
    }
 }

public class ArrayOfObjects {
    public static void main(String[] args)
 {
        Person[] people = {
            new Person("John", 25),
            new Person("Jane", 30),
            new Person("Jack", 22)
        };
        for (Person person : people)
 {
            System.out.println(person.name + " is " + person.age + " years old.");
        }
    }
 }

5

Question- How to Array Copying in Java?



Answer-
import java.util.Arrays;

public class ArrayCopying {
    public static void main(String[] args) {
        int[] original = {1, 2, 3, 4, 5};
        int[] copy = Arrays.copyOf(original, original.length);
        for (int number : copy) {
            System.out.println(number);
        }
    }
}

6

Question- How to sort array in Java?



Answer-
import java.util.Arrays;

public class ArraySorting {
    public static void main(String[] args) {
        int[] numbers = {5, 3, 8, 1, 2};
        Arrays.sort(numbers);
        for (int number : numbers) {
            System.out.println(number);
        }
    }
}

7

Question- How to search array element in Java?



Answer-
import java.util.Arrays;

public class ArraySearching {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int index = Arrays.binarySearch(numbers, 3);
        if (index >= 0) {
            System.out.println("Element found at index: " + index);
        } else {
            System.out.println("Element not found");
        }
    }
}

8

Question- How to Initialization Array through loop in Java?



Answer-
import java.util.Arrays;

public class ArraySearching {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int index = Arrays.binarySearch(numbers, 3);
        if (index >= 0) {
            System.out.println("Element found at index: " + index);
        } else {
            System.out.println("Element not found");
        }
    }
}