Standard Data Structures - Computer Science

Card 0 of 20

Question

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

number = 100 + foo[4];

What is the value of number?

Answer

The answer is 105 because arrays are zero indexed. This means that the first position has the subscript 0, the second subscript 1, and so on. 5 is in the fifth space, located at subscript 4. We would access it by saying foo\[4\]. As another example, if we wanted to access one, we would say foo\[0\].

Compare your answer with the correct one above

Question

For the following question, consider the following code:

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

{

double\[\] vec = {4,8,10,18};

System.out.println(fun(vec));

}

privatestaticdouble fun(double\[\] x)

{

double p = 0;

for(int i = x.length-1; i > -1; i--)

{

p += x\[i\];

}

return p / x.length;

}

Which of the following is a possible output for this program?

Answer

First of all, you can eliminate the following two options very quickly, as the fun method returns a double value:

\[D@4b71bbc9\]

\[4,9,11,19\]

These are trying to trick you into thinking that it returns an array. (The first value is really what would be the output, since there is no toString defined for the double array by default.)

Now, in fun, the loop simply runs through the values in the array, summing those up. Notice, it runs "backwards" through the array, from the end to the beginning. It then divides by the length of the array, giving you the average of the values in the array.

Compare your answer with the correct one above

Question

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?

Answer

In this code's loop, notice that it takes the absolute value of each element. This is done on the line:

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

This value is then used for the second loop, which goes for y times, each time outputting the value of the given member of the original array—but now with its particular sign value. Thus, even numbers like will be output multiple times (i.e. 8). This is done line by line for each member of the parameter array.

Compare your answer with the correct one above

Question

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

Answer

The graphics method takes the 2D array matrix and then runs through each element. This is the point of the double for loop in the method itself. Notice that for each element it does several things. First of all, it is clearly accumulating a value into the variable r. Now, for each element, you are adding:

x\[i\]\[j\] (the current value in the 2D array iteration)

TIMES

(i + 1), or, the current row number. Thus, for the data given you are doing the following:

1* 1 + 1 * 6 + 1 * 7 + 2 * 1 + 2 * 4 + 2 * 5 = 34

Compare your answer with the correct one above

Question

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?

Answer

Let's look at the main loop in this program:

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

}

}

The first loop clearly runs through the vals array for the number of items in that array. After this, it creates in the newVals 2D array a second dimension that is as long as the value in the input array vals. Thus, for 41, it creates a 3rd row in newVals that is 41 columns wide.

Then, we go to the second loop. This one iterates from 0 to the value stored in the current location in vals. It places in the given 2D array location the multiple of the value. Think of j + 1. This will go from 1 to 41 for the case of 41 (and likewise for all the values). Thus, you create a 2D array with all the multiples of the initial vals array.

Compare your answer with the correct one above

Question

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?

Answer

This code is using what are called parallel arrays. The boolean array has one value for each character in the String s. Notice the logic in the loop. There is a condition:

if(!b\[i\]) {

c = Character.toUpperCase(c);

} else {

c = Character.toLowerCase(c);

}

This means that if the boolean is false, then you will make the given letter upper case. (This is because of the ! in the condition. Be careful!) Otherwise, you make it lower case.

Carefully walking through the code, you will get:

i rEAd LoGiC fOr FuN!

Compare your answer with the correct one above

Question

TWO DIMENSIONAL ARRAYS

Given the following initialized array:

int fourth;

int\[\]\[\] myArray = { {1, 2, 3},

{4, 5, 6},

{7, 8, 9} };

Using myArray, how can I store in variable "fourth", the number 4?

Answer

When a two dimensional array is created and initialized, the way to access the items inside the matrix is by calling the array with the row and column (i.e. myArray\[ROW\]\[COLUMN\]). Keeping in mind that arrays start at 0, the number four would be in row 1, column 0. Therefore to save that number into the variable "fourth" we'll do the following:

fourth = myArray\[1\]\[0\];

*Note: myArray\[1\]\[0\] is not the same as myArray\[0\]\[1\].

myArray\[1\]\[0\] =4 because it is the item located at row=1 and column = 0.

myArray\[0\]\[1\] =12 because it is the item located at row=0 and column = 1.

Compare your answer with the correct one above

Question

Given the following in C++:

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

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

Answer

Let's look at this line of code:

int * data = new int\[12\]

An int pointer is created and an array of size 12 is assigned to it.

When data\[5\] is called, the "data" is dereferenced and the value of the 6th position is returned.

The only one of the choices that does this is

*(data + 5)

In line above, the pointer is incremented ot the 6th position and is then dereferenced to get the value.

If the code was

*data

The first item at the first position will be returned.

*(data+1)

This will return the item and the second position and so on.

Compare your answer with the correct one above

Question

True or False.

The best data structure to represent a set of keys and values is an array.

Answer

Arrays can be two-dimensional. However, when trying to keep track of keys and values it can become complicated when using an array. HashMaps are the best way to represent data containing keys and values.

Compare your answer with the correct one above

Question

Define an unwrapped integer array in Swift (iOS).

Answer

In Swift, the variable must be declared first with var then given a name. So now we have var arr then to unwrap, we must add a type var arr: \[Int\] and then initialize. Therefore, we have var arr: \[Int\] = \[\]. var arr = \[\] is technically correct, but the prompt asks you to unwrap the variable.

Compare your answer with the correct one above

Question

Define an unwrapped string array in Swift (iOS).

Answer

In Swift, the variable must be declared first with var then given a name. So now we have var arr then to unwrap, we must add a type var arr: \[String\] and then initialize. Therefore, we have var arr: \[String\] = \[\]. var arr = \[\] is technically correct, but the prompt asks you to unwrap the variable.

Compare your answer with the correct one above

Question

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?

Answer

The code segment will work only when the array has at least one integer. If the array has no negative integers, then the while loop will continue running, incrementing i past the length of the array, in which case an Out of Bounds Exception will occur.

Compare your answer with the correct one above

Question

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

Answer

You create a matrix also known as a 2 dimensional array the same way you'd instantiate a normal array except the first array space remains blank and you'd insert the number for the amount of rows. Due to the fact that you want 5 columns and 4 rows, you'd only input the 4 into the second array.

Compare your answer with the correct one above

Question

Consider the code below:

int[] vals = {841,-14,1,41,49,149,14,148,14};

boolean[] bools = new boolean[vals.length];

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

bools[i] = vals[i] > 20 && vals[i] % 2 == 0;

}

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

if(bools[i]) {

System.out.println(vals[i]);

}

}

What is the output of the code above?

Answer

This code uses a pair of parallel arrays, one being a boolean containing the result of the logic

vals[i] > 20 && vals[i] % 2 == 0

as applied to each element in the vals array.

The logic will evaluate to true when a given value is greater than 20 and also is even. (Remember that the % is the modulus operator, giving you the remainder of the division by 2 in this case. When this is 0, the division is an even division without remainder. When the divisor is 2, this means the number is divisible by 2—it is even.)

Now, in the second loop, it merely prints the values for which this was true. (This isn't the most efficient algorithm in the world. It is merely trying to test your ability to use parallel arrays and boolean values!) There is only one number in the entire list that fits: 148. Notice, however, that it does not output the boolean values. Those answers are traps that are trying to see if you are not paying attention.

Compare your answer with the correct one above

Question

int j=6;

int k=0;

int l=2;

int c = (j|k) & l;

What is the value of c?

Answer

The parenthesis indicate which operations need to be completed first. J or'd with k gives an answer of 6. Remember that these boolean operations are done by using the binary representtion of the numbers. 6 in binary is 110 and 0 in binary is 000. 6 anded with 2 is 2.

Compare your answer with the correct one above

Question

In the following equation, considering the boolean variables x, y, and z, which of the following combinations of values will evaluate to true?

(x\,\&\&\,!y)\,||\,((!x\,||\,z)\,\&\&\,!(y\,||\,z))

Answer

When evaluated with x == true, y == false, z == false_,_ the equation comes out to be

All other combinations of values produce false.

Compare your answer with the correct one above

Question

#include

using namespace std;
bool bigger(int x, int y)
{
if (x>y)
return true;
else
return false;
}
int main()
{

bool test;
test!=bigger(10,7);
cout<<test;
}

What will be the value of test after the previous code is run?

Answer

The function "bigger" defined before main returns a value of type bool and takes in two integer inputs. It compares the first value to the second. If it it greater, it returns true. Otherwise, it returns false. In main, a new variable test is called, and it is of type bool as well. This automatically eliminates 3 out of the 5 possibilities.

"test" is defined as the opposite of the outcome of the bigger function with the two inputs are 10 and 7. 10 is bigger than 7, so the function returns true. Since test is the opposite of that, it is false.

Compare your answer with the correct one above

Question

Convert the decimal number 67 to binary.

Answer

In a regular number system, also known as decimal, the position furtherest to the right is , then one over to the left would be , then . These are the ones, tens and hundreds place. Binary is a base 2 number system. Therefore, the digit to the furthest right is , then to the left , then , and so on.

Explanation

1000011 The bolded number has a value of 1

1000011 The bolded number has a value of 2

1000011 The bolded number has a value of 64

The positions that are marked true (as 1) in the binary number 1000011 corresponds to the numbers 64, 2, and 1 which added up equals 67

Step By Step

  • To convert the number 67 to binary, first find the digit place number that has the largest number possible that is less than or equal to 67. In this case it would be the position holding .
  • The number for 64 is 1000000
  • To get 67, we need to add 3
  • Again, find the digit place number that has the largest number possible less than or equal to 3. In this case, it would be
  • The number for 66 (64 + 2) is 1000010
  • To get from 66 to 67, repeat the steps from before.
  • The number for 67 (66 + 1) is 1000011

Compare your answer with the correct one above

Question

In Swift (iOS), define an unwrapped boolean.

Answer

In Swift, every variable must be declared with var before the name of the variable. This automatically knocks out two of the answers. After we eliminate those two, we see var variable and var variable: Bool. var variable would be a correct answer if the prompt had not asked for an unwrapped boolean. var variable: Bool performs the unwrapping by declaring the variable to be a boolean specifically.

Compare your answer with the correct one above

Question

How do we set a method to return a Boolean in Swift(iOS)?

Answer

In Swift, all methods must first say what they are. They are functions, so they are prefixed with func. Next, methods must have a name. In this case, we named it method. All methods need to specify parameters, even if there are no parameters. So, method() i.e. no parameters. Finally, we wanted to return a boolean. So we set the return type using -> Bool.

Compare your answer with the correct one above

Tap the card to reveal the answer