Java – Multiple Choice Questions and Answers – Arrays

HOME




class array_output 
    {
        public static void main(String args[]) 
        {
            int array_variable [] = new int[10];
	        for (int i = 0; i < 10; ++i) 
            {
                array_variable[i] = i;
                System.out.print(array_variable[i] + " ");
                i++;
            }
        } 
   }


public class array_output {

    public static void main(String args[])
    {
        char array_variable [] = new char[10];
        for (int i = 0; i < 10; ++i)
        {
            array_variable[i] = 'i';
            System.out.print(array_variable[i] + "");
        }
    }
}

public class array_output {
    public static void main(String args[])
    {
        double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
        double result;
        result = 0;
        for (int i = 0; i < 6; ++i)
            result = result + num[i];
            System.out.print(result/6);

    }
}




public class Test {
    public static void main(String[] args) {
        String[] arr = {"Java", "Python", "C++"};
       System.out.println(arr[1]);
    }
}



public class Test {
    public static void main(String[] args) {
       int[] arr = new int[5];
      System.out.println(arr[1]);
    }
}

public class array_output {
    public static void main(String args[])
    {
        int[] arr = {1, 2, 3, 4};
        System.out.println(arr.length);
    }
}





public class array_output {
    public static void main(String args[])
    {
        int[] arr = {2, 4, 6, 8};
        System.out.println(arr[arr.length]);
    }
}

public class array_output {
    public static void main(String args[]) {
        int[] arr = {1, 2, 3, 4, 5};
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] * 2;
        }
        System.out.println(arr[2]);
    }
}

public class array_output {
    
    public static void main(String args[]) {
        int[] arr = {10, 20, 30, 40};
        System.out.println(arr[1] + arr[2]);
    }
}



Arrays in Java

HOME

 1256661589473369

ArrayDataType[] ArrayName;
String[] days;

Creating an Array

 You can create array by using new operator.

arrayRefVar = new dataType[arraySize];

Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combine in one statement, as shown below:

 dataType[] arrayRefVar = new dataType[arraySize];

Alternatively, you can create arrays as follows:

dataType[] arrayRefVar = {value0, value1, ..., valuek}; 

The array elements are access through the index.

Array indices are 0-based; that is, they start from 0 to arrayRefVar.length-1.

Assigning values to an Array

How to initialize an array during declaration.

ArrayType [] ArrayName = New ArrayType [Size of an Array];

String[] Days = new String[6];
Days[0] = "Monday";
Days[1]="Tuesday";
Days[2]="Wednesday";
Days[3]="Thursday";
Days[4]="Friday";
Days[5]="Saturday";
Days[6]="Sunday";

Another way

String[] Days =  {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}

public class Array_Example {

    public static void main(String[] args) {

        String[] Days= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

        //Length of Array
        int len=Days.length;

        //Access first element
        System.out.println("First element in Array: "+Days[0]);

        //Last element
        System.out.println("First element in Array: "+Days[len-1]);

        //Print values
        for(int i=0;i<len-1;i++)
        {
            System.out.println("Value stored at position "+i+" is "+Days[i]);
        }
    }
}

Passing an Array to a Method

Array can be passed easily to a method as parameters, just as if we pass integers and strings to methods.

Below is an example of passing array to a method. 

public class Array_Example {

    public static void main(String[] args) {

        String[] Days= {"Winter","Spring","Summer","Autumn"};

        //Pass array as method
        print_arrays(Days);
    }

    public static void print_arrays(String[]array) {
        int len=array.length;
        System.out.println("Length of array :" + len);

        //Print values
        for(int i=0;i<=len-1;i++)
        {
            System.out.println("Value stored at position "+i+" is "+array[i]);
        }
    }
}

 

Multi Dimensional Arrays

It is also possible to create an array of arrays known as multidimensional array. For example,

    int[][] a = new int[3][4];

    int[][] a = {  {1, 2, 3},{4, 5, 6, 9},{7}};  

 Column1Column 2Column 3Column 4
Row 11         a[0][0]2     a[0][1]3        a[0][2]  a[0][3]
Row 24                a[1][0]5     a[1][1]6      a[1][2]9     a[1][3]
Row 37                a[2][0] a[2][1]  a[2][2] a[2][3]
public class Multidimensional_Array {

    public static void main(String[] args) {

        int a[][] = {{1,2,3},
                {4,5,6,9},
                {7}};

        //print length of columns
        System.out.println("Length of Row 1 is :"+a[0].length);
        System.out.println("Length of Row 2 is :"+a[1].length);
        System.out.println("Length of Row 3 is :"+a[2].length);
        System.out.println("----------------------");

        //Print all elements of array
        for (int i=0;i<a.length;i++)
        {
            for (int j=0;j<a[i].length;j++)
            {
                System.out.println("Value of row " + i + " and column " + j + " is " + a[i][j]);
            }
        }
    }
}

What is the difference between array and string?

  • Arrays can have any data type of any length while strings are usually ASCII characters that are terminated with a null character ‘\0’.
  • An array of characters is mutable, in other words, you can replace a character in an array of characters by overwriting the memory location for that character.
  • A String is not mutable, in other words, to “replace” a character in a String, you must build a new String with the desired character in place (copying the non-changing parts from the old String).                
  • All string literals are stored in the String constant pool. They remain in the pool until they became eligible for the garbage collection, while a Character array is stored in heap memory.                                               
  • Character array is preferred over string for storing passwords in java. As string is immutable, it makes string vulnerable for storing passwords, as we cannot get rid of passwords after use until garbage collector collects it.