About Question enthuware.ocajp.i.v7.2.1404 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1404 :

Post by ElizabethCM »

Hello,

I have a question regarding the first available optional answer for this topic, i.e int[][] a = new int[2][];
After the review, checking the answers, it says the above option is not correct because:
"This will instantiate only the first dimension of the array. The elements in the second dimension will be null. In other words, a will be instantiated to two elements but a[0] and a[1] will be null and so a[0][0] (and access to all other such ints) will throw a NullPointerException."

First question: Why would it throw a NullPointerException and why a[0] and a[1] would be null since they're declared as "int"? Wouldn't they be 0 instead?

However, I have tried it in Eclipse as I was not convinced by the above:
The below code runs smoothly:

import java.util.Arrays;

public class StringExercise {

public static void main(String args[]) {

int[][] a = new int[2][];
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
a[1][2] = 5;
a[1][3] = 6;
}
}

Please correct me if this answer is not ok. According to me it should be right.
Please note that I ran this with the Java 8 installed. If this is the issue, please let me know if the above would be wrong in Java 7.
Any other explanations are welcomed by a beginner like me :D

Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1404 :

Post by admin »

Did you try running the code that you've posted? It cannot run smoothly.
If the array is of ints, then all its elements will be initialized to 0. But you have to create the array first. a is a two dimensional array, so you have to create the second dimension also.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1404 :

Post by flex567 »

Can we imagine

Code: Select all

int[][] a = new int[2][];
a[0] = new int[2];
a[1] = new int[4];

a[0][0] = 1;         
a[0][1] = 2;                   
a[1][0] = 3;         
a[1][1] = 4;         
a[1][2] = 5;         
a[1][3] = 6;
to be ike this

Image

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1404 :

Post by admin »

yes, that is a very good way to represent a multi dimensional array.
If you like our products and services, please help us by posting your review here.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1404 :

Post by baichen7788 »

It says a[0] and a[1] are null in option 1.
But why a[0]=new int[2] is valid in option 4?
Does it mean null = new int[2] is valid?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1404 :

Post by admin »

baichen7788 wrote:
Sun Jun 13, 2021 5:31 am
Does it mean null = new int[2] is valid?
NOO!!! a[0] is a "reference". It initially points to null. Then it is made to point to an actual array by the statement a[0]=new int[2]. That doesn't mean null = = new int[2] at all!

References and objects are something very basic in Java. If you are not clear about this, I strongly suggest you to go through a book before attempting mock exams.
At least checkout "Kickstarter for Beginners" chapter of Deshmukh's book: https://amzn.to/2PucBeT
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests