# 4.8. 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?

 ```1 2 3``` ```String s1 = "xy"; String s2 = s1; s1 = s1 + s2 + "z"; ```
• xyz
• s1 will equal "xy" plus another "xy" then z at the end.
• xyxyz
• s1 contains the original value, plus itself, plus "z"
• xy xy z
• No spaces are added during concatenation.
• xy z
• No spaces are added during concatenation, and an additional "xy" should be included at the beginning.
• 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();
```
• 8
• Be sure to count spaces and punctuation in the length (the number of characters in the string).
• 10
• Did you forget to count a space or punctuation?
• 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");
```
• 3
• The method indexOf returns the first position of the passed str in the current string starting from the left (from 0).
• 4
• The first character is at index 0 in a string, not 1.
• 5
• Does the indexOf method find the first occurrence of the character, or the last?
• -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?

 ```1 2 3``` ```String s1 = "Hey"; String s2 = s1.substring(0,1); String s3 = s2.toLowerCase(); ```
• 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.
• he
• The substring method returns a new string starting at the first index and ending before the second index.
• H
• This would be true if we asked what the value of s2 was after the code executes. What is the value of s1?
• h
• This would be true if we asked what the value of s3 was after the code executes. What is the value of s1?