AP® Computer Science A Using Objects and Methods Practice Questions
Work through AP Computer Science A practice questions on Using Objects and Methods, 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.
Subtopic: Using Objects Clear filter
Question 92
Question 93
Question 94
Question 95
Question 96
Question 97
Question 98
Question 99
Question 100
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) {
}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
"Hello " + name + "!", combining a string literal, the parameter, and another string literal.Education Coach Advice
+ 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
"Hello" + name + "!" instead of "Hello " + name + "!", which would produce "HelloBob!" instead of "Hello Bob!". Another trap is omitting the exclamation mark at the end.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.

