# Easy Multiple Choice QuestionsΒΆ

These problems are mostly easier than what you will see on the AP CS A exam.

4-7-1: Given the following code segment, what is in the string referenced by s1?

String s1 = "xy";
String s2 = s1;
s1 = s1 + s2 + "z";

• (A) xyz
• s1 will equal "xy" plus another "xy" then z at the end.
• (B) xyxyz
• s1 contains the original value, plus itself, plus "z"
• (C) xy xy z
• No spaces are added during concatenation.
• (D) xy z
• No spaces are added during concatenation, and an additional "xy" should be included at the beginning.
• (E) z
• s1 was set to "xy" initially, so the final answer will be "xyxyz"

4-7-2: What is the value of len after the following executes?

String s1 = "Hey, buddy!";
int len = s1.length();

• (A) 8
• Be sure to count spaces and punctuation in the length (the number of characters in the string).
• (B) 10
• Did you forget to count a space or punctuation?
• (C) 11
• The length method returns the number of characters in the string, including spaces and punctuation.

4-7-3: What is the value of pos after the following code executes?

String s1 = "ac ded ca";
int pos = s1.indexOf("d");

• (A) 3
• The method indexOf returns the first position of the passed str in the current string starting from the left (from 0).
• (B) 4
• The first character is at index 0 in a string, not 1.
• (C) 5
• Does the indexOf method find the first occurrence of the character, or the last?
• (D) -1
• Does the string contain a d? The pos method will return the first index that the character is at in the string.

4-7-4: What is the value of s1 after the following code executes?

String s1 = "Hey";
String s2 = s1.substring(0,1);
String s3 = s2.toLowerCase();

• (A) Hey
• Strings are immutable, meaning they don't change. Any method that that changes a string returns a new string. So s1 never changes unless you set it to a different string.
• (B) he
• The substring method returns a new string starting at the first index and ending before the second index.
• (C) H
• This would be true if we asked what the value of s2 was after the code executes. What is the value of s1?
• (D) h
• This would be true if we asked what the value of s3 was after the code executes. What is the value of s1?
Next Section - Medium Multiple Choice Questions