Hi Could you please asnwer and explain output int num new

Hi!

Could you please asnwer and explain output:

int [] num = new int[4];
int x = 0;
num[x] = 3;
x++;
num[x] = 7;
x++;
num[x] = 11;
x++;
num[x] = 13;

Question options:

1)

0 1 2 3

2)

0 3 7 11

3)

3 7 11 13

4)

1 2 3 4

1)

0 1 2 3

2)

0 3 7 11

3)

3 7 11 13

4)

1 2 3 4

Solution

Answer: Option 3 (3 7 11 13)

Explanation

int [] num = new int[4];

The above statement declares an array with the name num of type int and size 4.

As the array indexing start with 0, the array looks like below

num[0]

num[1]

num[2]

num[3]


int x = 0;

The above statement declares a variable of type int and is assigned 0.

num[x] = 3;

Now num[x]=3, means num[0]=3, which means we are assigning a value 3 to num at index 0. Now the array looks like

                                    0                                  1                                  2                                  3

num

3


x++;

Increment the value of x, now x will be 1
num[x] = 7;

Now num[x]=7, means num[1]=7, which means we are assigning a value 7 to num at index 1. Now the array looks like

                                    0                                  1                                  2                                  3

num

3

7


x++;

Increment the value of x, now x will be 2

num[x] = 11;

Now num[x]=11, means num[2]=11, which means we are assigning a value 11 to num at index 2. Now the array looks like

                                    0                                  1                                  2                                  3

num

3

7

11


x++;

Increment the value of x, now x will be 3
num[x] = 13;

Now num[x]=13, means num[3]=13, which means we are assigning a value 13 to num at index 2. Now the array looks like

                                    0                                  1                                  2                                  3

num

3

7

11

13

Hence the values in the array will be 3 7 11 13

num[0]

num[1]

num[2]

num[3]

Hi! Could you please asnwer and explain output: int [] num = new int[4]; int x = 0; num[x] = 3; x++; num[x] = 7; x++; num[x] = 11; x++; num[x] = 13; Question op
Hi! Could you please asnwer and explain output: int [] num = new int[4]; int x = 0; num[x] = 3; x++; num[x] = 7; x++; num[x] = 11; x++; num[x] = 13; Question op
Hi! Could you please asnwer and explain output: int [] num = new int[4]; int x = 0; num[x] = 3; x++; num[x] = 7; x++; num[x] = 11; x++; num[x] = 13; Question op

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site