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.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
Explanation
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
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).
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).
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
Explanation
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
Explanation
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
Explanation
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
Explanation
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
Explanation
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
Explanation
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.