Search Placementyogi.com
 

Arrays in Java Objective Questions and Answers

  :: Topics ::

1

Which of the following statements are valid array declarations?(Multiple Choice)

A.

int number();

B.

int number[];

C.

double[] marks;

D.

counter int[];

See Answer & Explanation Lets Discuss
Correct answer is : B,C
Explanation

Option A is incorrect because array cannot be declared using Parentheses but square brackets. 

Option D is incorrect because counter is not a valid data type.

 
2

What will be printed using following code block?

int[] a = {0,1,2,3,4,5,6,7};
System.out.println(a.length);
A.

6

B.

7

C.

8

D.

9

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

The length of an array is equal to the number of elements in the array.

In the above array, there are 8 elements.

 
3

How to initialize an array named palette of Color object of size 6? (Multiple Choice)

A.

Color palette = new Color[5];

B.

Color palette[6];

C.

Color[5] palette;

D.

Color[] palette = new Color[6];

E.

Color palette[] = new Color[6];

See Answer & Explanation Lets Discuss
Correct answer is : D,E
Explanation

Options A,B and C have incorrect syntax.

Hence, option D and E are correct.

 
4

What would be the signature of a method myfunc() that :

1. Has a double array as a formal parameter 

2. Returns an array of integers.

A.

public void myfunc(double[] a)

B.

public [] myfunc(double a)

C.

public int myfunc(double a)

D.

public int[] myfunc()

E.

public int[] myfunc(double[] a)

See Answer & Explanation Lets Discuss
Correct answer is : E
Explanation

Only E has return type as integer array and parameter as double so it is the correct option.

 
5

What will happen when following Java code block is compiled and run?

public class A { 
 
    public static void main(String argv[]){ 
 
        int ary[]=new int[]{1,2,3};
        System.out.println(ary[1]); 
    } 
}
A.

1

B.

Compilation Error: Incorrect Syntax

C.

2

D.

Compilation Error: size of array must be defined

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

Here a new array is created, the elements 1,2,3 are inserted in it and the reference is stored in the "ary" variable.

ary[1] refers to the second element in the array as array index starts from 0 .

 
 

Banner to LogicGuns.com

Hide Page Information

Arrays Multiple Choice Questions and Answers in Java

Arrays are very important for job interviews and written round.

Objective Questions on Arrays in Java are asked in campus placement

Where Can I find Arrays Multiple Choice Objective Questions in Java?

Placementyogi.com has a lot of Questions from SCJP, OCJP , and campus written round that are explained in detail with answers. Practicing Arrays Objective Questions will help in placement exams and interview as well as this will build your concepts.

Good Questions on Arrays in Java Objective Questions as well as Multiple Choice Questions.

Tricky and tough objective Questions are available here with answers and explanation