Search Placementyogi.com
 

Arrays in Java Difficult Objective Questions and Answers

  :: Topics ::

1

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

 Boolean[] b1 = new Boolean[10];
   boolean[] b2 = new boolean[10];
   System.out.println("The value of b1[1] = " +b1[1]);
   System.out.println("The value of b2[1] = " +b2[1]);
A.

Prints "The value of b1[1] = false"

         "The value of b2[1] = false"

B.

Prints "The value of b1[1] = null"

         "The value of b2[1] = null"

C.

Prints "The value of b1[1] = null"

         "The value of b2[1] = false"

D.

Prints "The value of b1[1] = false"

         "The value of b2[1] = null"

E.

Compilation Error

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

By default objects will be initialized to null and primitives to their corresponding default values.

The same rule applies to array of objects and primitives.

 
2

What is the output of the following program?

class Array {
  public static void main(String[] args) {
    int length = 100;
    int[] d = new int[length];
 
    for (int index = 0; index < length; index++)
    System.out.println(d[index]);
  }
}
A.

The code will not compile because the int[] array is not declared correctly.

B.

The code will compile but will throw an IndexArrayOutOfBoundsException when it runs and nothing will appear in the standard output.

C.

The code will display the numbers 0 through 99 in the standard output, and then throw an IndexOutOfBoundsException.

D.

The code will compile but the println() method will throw a NoSuchMethodException.

E.

This code will work fine and display 100 zeroes in the standard output.

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

The code is absolutely fine. When an array is declared with a size, it is initialized to default values of its declared data type. Here since data type is int so all 100 elements are initialized to 0(zero).

 
 

Banner to LogicGuns.com

Hide Page Information

Difficult Arrays Multiple Choice Questions and Answers in Java

Tricky 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