Common Data Structures

Practice Questions

Computer Science › Common Data Structures

Page 1 of 6
10 of 56
1

Consider the following code:

public static void main(String\[\] args) {

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

}

private static double graphics(double\[\]\[\] x) {

double r = 0;

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

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

r += x\[i\]\[j\] * (i + 1);

}

}

return r;

}

What is the return value for graphics in the code below:

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

2

Consider the following code:

public static void main(String\[\] args) {

int\[\] vec = {8,-2,4,5,-8};

foo(vec);

}

private static void foo(int\[\] x) {

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

int y = Math.abs(x\[i\]);

for(int j = 0; j < y; j++) {

System.out.print(x\[i\] + " ");

}

System.out.println();

}

}

Which of the following represents a possible output for the program above?

3

Which of the following blocks of code converts an array of characters into a string?

4

Which of the following blocks of code converts an array of characters into a string?

5

Consider the following code:

public static void main(String\[\] args) {

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

}

private static double graphics(double\[\]\[\] x) {

double r = 0;

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

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

r += x\[i\]\[j\] * (i + 1);

}

}

return r;

}

What is the return value for graphics in the code below:

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

6

Consider the following code:

public static void main(String\[\] args) {

int\[\] vec = {8,-2,4,5,-8};

foo(vec);

}

private static void foo(int\[\] x) {

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

int y = Math.abs(x\[i\]);

for(int j = 0; j < y; j++) {

System.out.print(x\[i\] + " ");

}

System.out.println();

}

}

Which of the following represents a possible output for the program above?

7

On a standard binary tree, what would the data structure look like if we inserted the following names into the tree, supposing that names are compared in a standard lexicographic order:

Isaac, Henrietta, Nigel, Buford, Jethro, Cletus

8

On a standard binary tree, what would the data structure look like if we inserted the following names into the tree, supposing that names are compared in a standard lexicographic order:

Isaac, Henrietta, Nigel, Buford, Jethro, Cletus

9

Which of the following is NOT a difference between the Array class and the ArrayList class in Java?

10

Consider the following code:

int\[\] vals = {5,4,2};

String s = "Hervaeus";

String s2 = "";

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

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

s2 += s.charAt(i);

}

}

System.out.println(s2);

What is the output for the code above?

Page 1 of 6
Return to subject