Accessing Arrays
Loops
Coding Arrays
DTO
CS Misc
100

Given the following 2d array:

double [][] ma = {{7.73, 8.89}, {9.15, 4.8, 9.70, 3.922}, {8.63, 7.76, 1.2}, {3.2, 1.77, 11.53}};

What is the length of ma[2]?

3

100

Write a loop to traverse through a 2d array named arr. Assume the array could be jagged.


for(int i = 0; i < arr.length; i++) {

    for(int j = 0; j < arr[i].length; j++) {...}

}

100

Write the code to create the following array. Give your array the name ma.

[[0, 0, 0],

[0, 0, 0]]

int[][] ma = new int[2][3];

100

Determine the output: 

int[][] a={{3,2,1},{6,5,4},{9,8,7}};

for (int y = 1; y>=0; y--) {

 for (int x = 2; x>=0; x--) {

  System.out.print(a[y][x]+",");

}

}

4, 5, 6, 7, 8, 9

100

In database management, what does the acronym "SQL" stand for?

Structured Query Language


200

Given the following 2d array:

String [][] strs= {{"hi"}, {"how", "are", "you"}, {"I", "okay"}, {"bye"}};

What is the length of strs[1][3]?

ArrayIndexOutOfBoundsException
200

Does the following loop print the array z by row first order or column first order?

for(int y=0; y<z.length; y++){

  for(int x=0; x<z[y].length; x++) {

    System.out.print(z[y][x]+",");

  } 

  System.out.println();

}                      

row first

200

Write the code to initialize the following jagged array:

[[1, 2, 3],

[4],

[5, 6]]

int[][] arr = {{1, 2, 3}, {4} {5, 6}};

200

int[][] z = {{4, 7, 2}, {1, 3, 5}, {9, 0, 2}};

for(int y=0; y<z.length; y++){

  for(int x=0; x<z[y].length; x++) {

    System.out.print(z[x][y]+",");

  }

  System.out.println();

}                      

4, 1, 9,

7, 3, 0,

2, 5, 2,

(This is what it means by column first order btw)

200

What does the acronym "HTML" stand for in web development?

HyperText Markup Language


300

What is the element at arr3[0][1][2]?

int[][][] arr3 = {{{6, 14}, {2, 7, 3}, {8}}, {{1, 3, 5}, {7, 3, 5, 2}}};

3

300

Does the following code print the elements of array a in ascending or descending order?

int[][] a={{3,2,1},{6,5,4},{9,8,7}};

for (int y = 2; y>=0; y--){

 for (int x = 0; x<3; x++){

  System.out.print(a[y][x]+",")

}

}                       


descending order

300

Write the method getCol which gets a copy of a column from a 2d arr given col. Assume arr is a rectangular array and col is in bounds. 

public static int[] getCol(int[][] arr, int col) {

...

}

int[] colcopy = new int[arr.length];

    for(int i = 0; i < colcopy.length; i++)

      colcopy[i] = arr[i][col];

    return colcopy;

300

What does the following function do? (Don't determine the output, but explain in words)

   int max = 0;

   for(String[] sArr : abc) {

      int sum = 0;

      for(String s : sArr) sum += s.length;

      if(sum > max) max = sum;

   }

return max;

Finds the row with the most characters.


300

Which data structure follows the Last In, First Out (LIFO) principle?

Stack

400

Examine the code below and fill in the numbers required to make arr the same as x:

int[][] x = new int[2][3];

for(int i = 0; i < x.length; i++) {

     for(int j = 0; j < x[i].length; j++) {

          x[i][j] = i + j;

     }

}

int[][] g = {{_, _, _}, {_, _, _}};

int[][] g = {{0, 1, 2}, {1, 2, 3}};

400
Consider the following code segment; how many times is x printed out?


for(int i = 0; i < 5; i++) {

    for(int j = 7; j >= 0 ; j--) {

         if(j > i) sysout("x");

    } 

}

25

400

Fill 2d array with ascending numbers starting from 1.

public static void numberEveryCell(int[][] ma) {

}

int temp = 1;

    for (int i = 0; i < ma.length; i++) {

      for(int j = 0; j < ma[i].length; j++) {

        ma[i][j] = temp++;

      }

    }

400

String[][] twoD = {{"AV", "J"}, {"JA", "VA", "A"}, {"JA", "J"}, {"AV", "V", "A"}};

Determine the output: 

for(String[] sArr : twoD){

    for(int i = 0; i < sArr.length; i++) {

        if(i % 2 == 0) sysout(sArr[i]);

    }

}

AV

JA 

A

JA

AV 

A

400

What is the term for a programming error that causes a program to behave unexpectedly, often resulting in crashes or unintended behavior?

Bug

500

Given the following matrix, which code segment will result in "mao"?

String[][] matrix = {{"r","m","c","h","q","m"},

{"b","j","d","e","z","w"},

{"v","w","t"},

{"f","r","k","n","a","o"}};


matrix[0][1] + matrix[3][4] + matrix[3][5]

500

Assume there exists an int[][] z that is a square shape. Write the code to print the diagonal of z. (Either diagonal works)

for(int i = 0; i < z.length; i++) {

    for(int j = 0; j < z[i].length; j++) {

         if(i = j) System.out.println(z[i][j] + " ");

    }

}

500

Explain the difference between a shallow copy and deep copy for arrays.


In a shallow copy, the reference to an array is passed. Any changes to the passed array affects the original array. 


In a deep copy, each element of the array is copied to a newly created array. This array has a different memory address than the original array. Any changes made to the new array will not affect the original array.

500

Determine the Output: 

int[] nums1 = { 14, -23, 90 };

increment(nums1);

System.out.println(Arrays.toString(nums1);

_____________________________________

public static void increment(int[] arr) {

    for(int i = 0; i < arr.length; i++) arr[i]++;

}

[15, -22, 91]

500

This programming language, developed by James Gosling at Sun Microsystems, is often used for building mobile and web applications.

Java


M
e
n
u