Computer Science › Arrays
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?
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);
Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?
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?
Given the following in C++:
int * data = new int\[12\];
Pick an expression that is equivalent to : data\[5\];
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?
Define an unwrapped integer array in Swift (iOS).
int foo[] = {1, 2, 3, 4, 5};
number = 100 + foo[4];
What is the value of number
?
Define an unwrapped string array in Swift (iOS).
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?