Strings

Help Questions

AP Computer Science A › Strings

Questions 1 - 10
1

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

}

}

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.

2

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.

3

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.

4

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.

5

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.

6

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

Explanation

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

7

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

Explanation

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

8

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

Explanation

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

9

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

Explanation

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

10

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

Explanation

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

Page 1 of 4
Return to subject