AP Computer Science A › Strings
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++) {
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() {
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);
}
}
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);
}
}
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.
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);
}
}
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.
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);
}
}
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.
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);
}
}
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.
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);
}
}
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\[\] vals = {5,4,2};
String s = "Hervaeus";
String s2 = "";
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j < vals\[i % vals.length\]; j++) {
s2 += s.charAt(i);
}
}
System.out.println(s2);
What is the output for the code above?
HHHHHeeeerrvvvvvaaaaeeuuuuussss
HHHHHeeeeerrrrrrvvvvvaaaaaeeeeeuuuuusssss
The code will note execute.
HHHHHeeeerrvaeus
Hervaeeeeeuuuuss
The main thing to look at for this question is the main loop for the code. This loop first goes through each of the characters in the String s:
int i = 0; i < s.length(); i++
Next, notice the condition on the inner loop:
vals\[i % vals.length\]
The modulus on i will yield values that are between 0 and 2 (given the length of vals). This means that you will loop in the inner loop the following sequence of times:
5,4,2,5,4,2,5,4
This will replicate the given letter at the index (i) in the initial string, using 5,4,2,5, etc as the replication count. Thus, you will replicate "H" 5 times, "e" 4, etc. This gives you an output:
HHHHHeeeerrvvvvvaaaaeeuuuuussss
Consider the following code:
int\[\] vals = {5,4,2};
String s = "Hervaeus";
String s2 = "";
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j < vals\[i % vals.length\]; j++) {
s2 += s.charAt(i);
}
}
System.out.println(s2);
What is the output for the code above?
HHHHHeeeerrvvvvvaaaaeeuuuuussss
HHHHHeeeerrvvvvvaaaaeeuuuuussss
HHHHHeeeeerrrrrrvvvvvaaaaaeeeeeuuuuusssss
HHHHHeeeeerrrrrrvvvvvaaaaaeeeeeuuuuusssss
The code will note execute.
The code will note execute.
HHHHHeeeerrvaeus
HHHHHeeeerrvaeus
Hervaeeeeeuuuuss
Hervaeeeeeuuuuss
The main thing to look at for this question is the main loop for the code. This loop first goes through each of the characters in the String s:
int i = 0; i < s.length(); i++
Next, notice the condition on the inner loop:
vals\[i % vals.length\]
The modulus on i will yield values that are between 0 and 2 (given the length of vals). This means that you will loop in the inner loop the following sequence of times:
5,4,2,5,4,2,5,4
This will replicate the given letter at the index (i) in the initial string, using 5,4,2,5, etc as the replication count. Thus, you will replicate "H" 5 times, "e" 4, etc. This gives you an output:
HHHHHeeeerrvvvvvaaaaeeuuuuussss
Consider the following code:
int\[\] vals = {5,4,2};
String s = "Hervaeus";
String s2 = "";
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j < vals\[i % vals.length\]; j++) {
s2 += s.charAt(i);
}
}
System.out.println(s2);
What is the output for the code above?
HHHHHeeeerrvvvvvaaaaeeuuuuussss
HHHHHeeeeerrrrrrvvvvvaaaaaeeeeeuuuuusssss
The code will note execute.
HHHHHeeeerrvaeus
Hervaeeeeeuuuuss
The main thing to look at for this question is the main loop for the code. This loop first goes through each of the characters in the String s:
int i = 0; i < s.length(); i++
Next, notice the condition on the inner loop:
vals\[i % vals.length\]
The modulus on i will yield values that are between 0 and 2 (given the length of vals). This means that you will loop in the inner loop the following sequence of times:
5,4,2,5,4,2,5,4
This will replicate the given letter at the index (i) in the initial string, using 5,4,2,5, etc as the replication count. Thus, you will replicate "H" 5 times, "e" 4, etc. This gives you an output:
HHHHHeeeerrvvvvvaaaaeeuuuuussss
Consider the following code:
int\[\] vals = {5,4,2};
String s = "Hervaeus";
String s2 = "";
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j < vals\[i % vals.length\]; j++) {
s2 += s.charAt(i);
}
}
System.out.println(s2);
What is the output for the code above?
HHHHHeeeerrvvvvvaaaaeeuuuuussss
HHHHHeeeeerrrrrrvvvvvaaaaaeeeeeuuuuusssss
The code will note execute.
HHHHHeeeerrvaeus
Hervaeeeeeuuuuss
The main thing to look at for this question is the main loop for the code. This loop first goes through each of the characters in the String s:
int i = 0; i < s.length(); i++
Next, notice the condition on the inner loop:
vals\[i % vals.length\]
The modulus on i will yield values that are between 0 and 2 (given the length of vals). This means that you will loop in the inner loop the following sequence of times:
5,4,2,5,4,2,5,4
This will replicate the given letter at the index (i) in the initial string, using 5,4,2,5, etc as the replication count. Thus, you will replicate "H" 5 times, "e" 4, etc. This gives you an output:
HHHHHeeeerrvvvvvaaaaeeuuuuussss
Consider the following code:
int\[\] vals = {5,4,2};
String s = "Hervaeus";
String s2 = "";
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j < vals\[i % vals.length\]; j++) {
s2 += s.charAt(i);
}
}
System.out.println(s2);
What is the output for the code above?
HHHHHeeeerrvvvvvaaaaeeuuuuussss
HHHHHeeeeerrrrrrvvvvvaaaaaeeeeeuuuuusssss
The code will note execute.
HHHHHeeeerrvaeus
Hervaeeeeeuuuuss
The main thing to look at for this question is the main loop for the code. This loop first goes through each of the characters in the String s:
int i = 0; i < s.length(); i++
Next, notice the condition on the inner loop:
vals\[i % vals.length\]
The modulus on i will yield values that are between 0 and 2 (given the length of vals). This means that you will loop in the inner loop the following sequence of times:
5,4,2,5,4,2,5,4
This will replicate the given letter at the index (i) in the initial string, using 5,4,2,5, etc as the replication count. Thus, you will replicate "H" 5 times, "e" 4, etc. This gives you an output:
HHHHHeeeerrvvvvvaaaaeeuuuuussss