All Computer Science Resources
Example Questions
Example Question #1 : Arrays
True or False.
The best data structure to represent a set of keys and values is an array.
False
True
False
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.
Example Question #101 : Standard Data Structures
Define an unwrapped integer array in Swift (iOS).
var arr: [Int] = []
var arr: Int = []
var arr = []
var arr = [Int]
var arr: [Int] = []
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.
Example Question #102 : Standard Data Structures
Define an unwrapped string array in Swift (iOS).
var arr = [String]
var arr: String = []
var arr: [String] = []
var arr = []
var arr: [String] = []
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.
Example Question #1 : Arrays
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?
Yes, it works as intended.
It works when there are no negative integers.
It will work, but unexpectedly fail on occasion.
It never works.
When array contains at least one negative integer
When array contains at least one negative integer
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.
Example Question #103 : Standard Data Structures
Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?
int [] [] matrx = new int [] [5];
int [] [] matrx = new int [4] [5];
int [] [] matrx = new int [] [4];
int [] [] matrx = new int [5] [4];
int [] [] matrx = new int [5] [];
int [] [] matrx = new int [] [4];
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.
Example Question #1 : Constant Declarations
Which of the following code samples adequately uses constants?
final String s = "Hello, "
int intVal;
// Variable intVal read in during this code.... Excerpted...
if(intVal < 0) {
s += "World!";
} else if(intVal == 0) {
s += "People!";
} else {
s += "Folks!";
}
const int rows = 4;
for(int i = 0; i < rows; i++) {
System.out.println(i);
}
final int rows;
int intVal;
// Variable intVal read in during this code.... Excerpted...
rows = intVal;
for(int i = 0; i < rows; i++) {
System.out.println(i);
}
for(final int i = 0, rows = 5; i < rows; i++) {
System.out.println(i);
}
final int rows = 4;
for(int i = 0,l = rows; i < l; i++) {
rows += 2;
}
final int rows;
int intVal;
// Variable intVal read in during this code.... Excerpted...
rows = intVal;
for(int i = 0; i < rows; i++) {
System.out.println(i);
}
Remember that a constant variable cannot be changed once it has been assigned. Normally, you assign the variable immediately on the same line as the declaration. However, if you were to create the constant and wait to assign a value, that would be find syntactically as well. Thus, the correct answer does not have a problem, though it might appear so at first if you did not know this. Now, remember that the way to declare a constant is to use the keyword "final". (The keyword "const" works in some other languages like C++.) All of the incorrect answers (other than the one using "const") alter the constant after it has been defined.
Example Question #2 : Programming Constructs
In the following block of code, which of the following lines contains an error?
final int i = 20, j, k;
int l,m=50,n = 2;
j = n + m;
l = m * i + j;
for(int a = 0; a < m; a++) {
l += l;
}
m = 20 + j + n * i + m;
j = m + i;
k = 50 + 20 * j;
for(int a = 0; a < m; a++)
l = m * i + j;
l += l;
k = 50 + 20 * j;
j = m + i;
j = m + i;
The line
j = m + i;
has an error because it contains a reassignment to a constant (final) variable. You are permitted to assign a constant on a line that is not the line of declaration. However, once you do this, you cannot reassign a value. The variable j was assigned a value on the line:
j = n + m;
Thus, the line above (j = m + i;) represents a re-assignment, hence causing an error.
Example Question #1 : Variable Declarations
// ^ is Exclusive-Or (XOR) operator
x = x ^ y
y = x ^ y
x = x ^ y
What is the effect of the above snippet of code?
x
is equal to y
The values are the same.
None of the other answers is true.
The bits of x
and y
are negated.
The values are switched.
The values are switched.
Consider two variables: (binary 1010) and (binary 0101). The exclusive or operator returns 1 in every bit that is different in the two numbers and returns 0 in every bit that is the same.
In order to switch the numbers, we must do this:
^
^
^
Continuing with the example, ^ would return 1111, because no bits are the same between the two numbers. Store this in the variable . Then 1111 ^ 0101 would return 1010. That result will be stored in . Finally, find the value of (1111) ^ (1010) which is 0101, and store the result in variable . The values are now switched.
Example Question #1 : Variable Declarations
Which of the following declares a String array of size i? (Presume Java syntax.)
None of the others
String s[] = new String[i];
String s = new String[i];
String[] s[i];
String[] s = new String[i];
String[] s = new String[i];
For arrays, it is best to think about the array itself as being a type. So, just as an int named x is declared:
int x;
And a String named s is declared:
String s;
You begin the declaration of a String array like this:
String[]
This is like saying, "What is coming next will be an array of String objects."
Thus, your best declaration is:
String[] s = new String[i];
This declares the new size as well, appropriately using the new operator.
Example Question #1 : Variable Declarations
Which of the following code snippets declares an array of integers using an array literal?
int[] a = new int[6];
a[0] = 42;
a[1] = 26;
a[2] = 134;
a[3] = -13;
a[4] = 45;
a[5] = 234;
int[] a = {42,26,134,-13,45,234};
int a = {42,26,134,-13,45,234};
int[] a = {0:42,1:26,2:134,3:-13,4:45,5:234};
int[] a;
a[0] = 42;
a[1] = 26;
a[2] = 134;
a[3] = -13;
a[4] = 45;
a[5] = 234;
int[] a = {42,26,134,-13,45,234};
There are two things to pay attention to for this question. First, you can make a simple array literal in Java by enclosing the list of array elements in a pair of curly braces. Thus, the array literal
{42,26,134,-13,45,234}
is just fine! However, you do not give the indices. These are inferred. (The first element is at 0, the second at 1, etc.)
Now, you just have to assign this to an array object. Thus the following is wrong:
int a = {42,26,134,-13,45,234};
You need to have the [] to indicate that a is an array:
int[] a = {42,26,134,-13,45,234};