Arrays

Practice Questions

Computer Science › Arrays

Page 1 of 2
10 of 13
1

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?

2

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);

3

Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?

4

Suppose your friend has the following lines of code that intend to find the first index of the first positive integer in array\[0\] ... array\[N-1\], where array is an array of N integers

int i = 0;

while (array\[i\] >=0)

{

i++;

}

location = i;

Will your friend's code work as intended?

5

Given the following in C++:

int * data = new int\[12\];

Pick an expression that is equivalent to : data\[5\];

6

Consider the following code:

String s = "I read logic for fun!";

boolean\[\] b = {true,false,true,false,false,true,true,false,true,false,true,false,false,true,false,true,true,false,true,false,false};

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

char c = s.charAt(i);

if(!b\[i\]) {

c = Character.toUpperCase(c);

} else {

c = Character.toLowerCase(c);

}

System.out.print(c);

}

What is the output for this function code?

7

Define an unwrapped integer array in Swift (iOS).

8

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

number = 100 + foo[4];

What is the value of number?

9

Define an unwrapped string array in Swift (iOS).

10

Consider the following code:

int\[\] vals = {6,1,41,5,1};

int\[\]\[\] newVals = new int\[vals.length\]\[\];

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

newVals\[i\] = new int\[vals\[i\]\];

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

newVals\[i\]\[j\] = vals\[i\] * (j+1);

}

}

What does the code above do?

Page 1 of 2
Return to subject