General Knowledge Quiz on Java Arrays

1) What happens when you compile and run this program?
        int[] Intger = new int[0];
 
        System.out.println(i[0]);
a) compile time error
b) Illegal use of keyword Intger
c) Illegal definition of main() method
d) You will get ArrayIndexOutOfBoundsException at run time.

2) What will be the output of this program?
        int[] a = new int[3];
 
        a[1] = 50;
 
        a[1] = 60;
 
        Object o = a;
 
        int[] bb = (int[])o;
 
        b[b1] = 100;
 
        System.out.println(a[1]);
 
        ((int[])o)[1] = 500;
 
        System.out.println(a[1]);
a) 50, 60
b) 60, 100
c) 50, 100
d) 100, 500

3) What will be the outcome of the following program?
public class ArraysInJava
{
    static final int[] ab;
 
    static
    {
        ab= new int[] {1, 2, 3};
    }
 
    public static void main(String[] args)
    {
        ab = new int[5];
    }
}
a) cannot assign a value to final variable ab = new int[5];
b) missing variable initializer
c) ( expected instead of []
d) array not initialized

4) What will be the outcome of this program?
        int[] a = {7, 9, 13, 21, 35, 48};
 
        System.out.println( a[ -1 ] );
a) array not initialized
b) illegal character
c) ArrayIndexOutOfBoundsException at run time.
d) None of the above

5) What happens when you compile and run the following program?
         int[][] a = {{1,2,}, {3,4}};
 
         int[] b = (int[]) a[1];
 
         Object o1 = a;
 
         int[][] a2 = (int[][]) o1;
 
         int[] b2 = (int[]) o1;
 
         System.out.println(b[1]);
a) generic array creation
b) tthrows ClassCastException at run time.
c) incompatible type
d) not accessible

6) Which declaration is incorrect?
int a[]  
      [];  
 
int b[3];    
 
int []c[];  
 
int d[][];    
a) a[] []; ;
b) int []c[];
c) int b[3];
d) int d[][];

7) What is the result of compiling and running the following code?
    int[] a = new int[0];
    System.out.print(a.length);
a) Compilation error because a.length is not a property of arrays; and a.length() should be used. s
b) Compilation error because arrays can not be size of 0.
c) 0
d) none of the above s

8) What is the result of compiling and running the following program?
final static int x[] = new int[5];

public static void main(String[] args)
{
    final int x = new Tester().x[5];
    if (x <= 10) 
       System.out.println("howtodoinjava") ; 
} 

a) not initialized
b) Compilation error
c) both a and b
d) ArrayIndexOutOfBoundsException occurred

9) What is true after running the following code?
 String entries[] = { "entry1", "entry2" };
    int count = 0;
    while (entries[count++] != null) 
    {
        System.out.println(count);
    }
    System.out.println(count);
a) ArrayIndexOutOfBoundsException: 2
b) 1 will be printed as part of output.
c) 2 will be printed as part of output.
d) all of the above

10) What is the result of compiling and running the following code?
 
 static void LenTest(int[] a) {
    int[] b = new int[2];
    a = b;
    System.out.print(b.length);
    System.out.print(a.length);
}
 
public static void main(String[] args) {
    int[] a = new int[5];
    LenTest(a);
    System.out.print(a.length);
}
a) 222
b) 225
c) 255
d) 2
e) None of the above

11) In Java, can you change size of array once created?
a) yes
b) sometimes
c) no
d) all of the above

12) In Java, can you store String in an int array?
       int[] a = new int[5];
       a[0] = "bbb" ; 
a) yes
b) no - it's a runtime error
c) no, it's a compile time error
d) None of the above

13) In Java, what happens when you compile the following code?
int a[] = {10, 15, 25, 45, 55} ;
a) it's an illegal statement
b) it's a compile time error
c) both a and b
d) it compiles OK

14) In Java, Which of the following variables are arrays?
int a[] , b ;
int [] c , f ;
 
a) a
b) it's a compile time error
c) a and c
d) a, c and f

15) InJjava, what happens when you compile/run the following code?
  
 Object[] stringArray = new String[5];  

 stringArray[2] = 100;    
 
a) runtime array store error
b) Compiletime error
c) Compiles and runs ok.
d) None of the above

16) In java, what happens when you compile/run the following code?
  
 Object[] stringArray = new String[ -5];  
 
a) Compiles ok.
b) NegativeArraySizeException at run time
c) Both a and b
d) .None of the above

17) In Java, what happens when you compile/run the following code?
  
 int [] a = new int [ 5];  

 a = new int [ 15];  
 
a) compile time error.
b) run time error.
c) The compiler will complain that a array has been defined twice.
d) None of the above.

18) In Java, a primitive array can have elements of different data types.
a) True
b) False
c) Only when the array is aprimitive and Object.
d) None of the above

19) In Java, items in an array are called elements.
a) False
b) True
c) They are called Indexes
d) None of the above

20) In Java, items in an array are accessed by a
a) pointer
b) indicator
c) index
d) Indication

21) What happens when you compile and run this program?
 
public class JavaArrays
{
    public static void main(String[] args)
    {
        int[] ar = new int[0];
 
        System.out.println(ar[0]);
    }
}
 
a) compiles and runs ok
b) does not compile
c) produces ArrayIndexOutOfBoundsException at run time
d) none of the above

22) An array contains group of variables (called _____________).
a) elements
b) components
c) both a) and b)
d) none of the above

23) An array contains variables, all of the same _____________.
a) data type
b) various data types
c) two data types
d) none of the above

24) The first element in every array has index of___________ (fill in the blank).
a) one
b) minus one
c) zero
d) none of the above

25) An index can be a negative integer.
a) sometimes
b) never.
c) both positive or negative
d) none of the above

26) Can a program use an expression as an index?
a) yes
b) no
c) only if the expression evaluates to an integer
d) both a and c
e) none of the above

27) How do you get the length of an array (fill in the blank)?
 

    int[] arr = {23, 15, 45, 76};
    int arrayLength = arr.___________;
    System.out.format("The Java array arr length is %d", arrayLength);
 
a) size
b) range
c) quantity
d) span
e) length

28) How do you specify an array (fill in the blank)?
 

    int[] arr ;
    arr = _________ int[ 7 ] ;
 
a) int
b) serial
c) volatile
d) super
e) new

29) Does the following code compile and run?
 

    int[] arr = {23, 15, 45, 76};
    arr = new arr[ 7 ] ;
 
a) compiles ok
b) compiles and runs ok
c) does not compile
d) none of the above

30) Declare arr as an int and use an array initializer to include the following elements: 5,17,23,35.
a) int[] arr = (5, 17, 23, 35);
b) int[] arr = {5, 17, 23, 35};
c) int[] arr = [5, 17, 23, 35];
d) none of the above

Array Quiz script provided by
Total applications works



HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#