AP® Computer Science A Practice Questions
Work through AP Computer Science A practice questions unit by unit, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Multiple choice
Unit 1: Using Objects and Methods
Question 142
Multiple choice
Unit 1: Using Objects and Methods
Question 143
Multiple choice
Unit 1: Using Objects and Methods
Question 144
Multiple choice
Unit 1: Using Objects and Methods
Question 145
Multiple choice
Unit 1: Using Objects and Methods
Question 146
Multiple choice
Unit 1: Using Objects and Methods
Question 147
Multiple choice
Unit 1: Using Objects and Methods
Question 148
Multiple choice
Unit 1: Using Objects and Methods
Question 149
Multiple choice
Unit 1: Using Objects and Methods
Question 150
Free response
Unit 1: Using Objects and Methods
Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".
helloName("Bob") → "Hello Bob!"
helloName("Alice") → "Hello Alice!"
helloName("X") → "Hello X!"
public String helloName(String name) {
}
The answer and the full solution open in a window.
Main Solution
The method should concatenate the string "Hello ", the parameter name, and the exclamation mark "!", then return the result.
public String helloName(String name) {
return "Hello " + name + "!";
}
Explanation:
- The method receives a
Stringparameter calledname. - Using the
+string concatenation operator, we build the greeting:"Hello "+name+"!". - For example, if
nameis"Bob", the expression evaluates to"Hello Bob!". - The resulting string is returned with the
returnstatement.
Teacher Explanation
This question tests basic string concatenation in Java. The solution simply returns
"Hello " + name + "!", combining a string literal, the parameter, and another string literal.Education Coach Advice
On AP CSA FRQ questions involving string manipulation, remember that Java's
+ operator concatenates strings. Always double-check that your spacing and punctuation (like the space after "Hello" and the "!" at the end) exactly match the required output format.Traps
A common mistake is forgetting the space between "Hello" and the name, writing
"Hello" + name + "!" instead of "Hello " + name + "!", which would produce "HelloBob!" instead of "Hello Bob!". Another trap is omitting the exclamation mark at the end.
Question 141 of 3,140
Page 15 of 314
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

