Standard Data Structures
Help Questions
AP Computer Science A › Standard Data Structures
Which of the following blocks of code converts an array of characters into a string?
private static void string() {
char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};
String s = "";
for(int i = 0; i < vals.length; i++) {
s += vals\[i\];
}
}
private static void string() {
char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};
String s;
for(int i = 0; i < vals.length; i++) {
s += vals\[i\];
}
}
private static void string() {
char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};
String s = "";
for(int i = 0; i < vals.length; i++) {
s = vals\[i\];
}
}
private static void string() {
char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};
String s = "";
for(int i = 0; i < vals.length; i++) {
vals\[i\] = s;
}
}
private static void string() {
String s = "At 6 am!";
char\[\] vals = new char\[s.length()\];
for(int i = 0; i < s.length(); i++) {
vals\[i\] = s.charAt(i);
}
}
Explanation
The easiest way to consider this is by commenting on the correct answer. You must begin by defining the character array:
char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};
Next, you must initialize the string value s to be an empty string. This is critical. Otherwise, you can't build your string!
String s = "";
Next, you have the loop. This goes through the characters and concatenates the values to the variable s. The operation to concatenate the characters is the "+=". This will give you the string value of the array of characters.
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Which is true?
a = 4
b = 2
c = 5
d = 6
a) a = c - d
b) a = b - d
c) a = d - b
d) a = c * d
c)
a)
b)
d)
Explanation
c) is the correct choice because a = 6 - 2 since d = 6 and b = 2. Substitute the numbers for the variables in each answer choice. You will see that all of the answer choices are not equal to 4 except for c).
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Explanation
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".