2.13. Multiple Choice Exercises

These questions are all about Strings, but more questions on using other objects will be added in the future.

2.13.1. Easy Multiple Choice Questions

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

    2-13-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"

    2-13-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.

    2-13-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.

    2-13-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?

2.13.2. Medium Multiple Choice Questions

These problems are similar to those that you will see on the AP CS A exam.

    2-13-5: After the following code is executed, which of I, II and/or III will evaluate to true?

    1
    2
    3
    4
    5
    6
    7
    String s1 = "xyz";
    String s2 = s1;
    String s3 = s2;
    
    I.   s1.equals(s3)
    II.  s1 == s2
    III. s1 == s3
    
  • I, II, III
  • The "equals" operation on strings returns true when the strings have the same characters. The == operator returns true when they refer to the same object. In this case all three references actually refer to the same object so both == and equals will be true.
  • I only
  • This is true, since s1 and s3 contain the same characters since s1 and s3 actually refer to the same string object. But, it isn't the only thing that is true.
  • II only
  • This is true since s2 == s1. But, it isn't the only thing that is true.
  • III only
  • This is true since s3 == s2, and s2 == s1 so it follows that s1 == s3. But, it isn't the only thing that is true.
  • II and III only
  • This is true since they all refer to the same string object. But, they also contain the same characters so equals is also true.

    2-13-6: What is output from the following code?

    1
    2
    3
    4
    5
    String s = "Georgia Tech";
    String s1 = s.substring(0,7);
    String s2 = s1.substring(2);
    String s3 = s2.substring(0,3);
    System.out.println(s3);
    
  • org
  • The method substring(a,b) means start at a and stop before b. The method substring(a) means start at a and go to the end of the string. The first character in a string is at index 0.
  • eor
  • This can't be true since the e is at index 1 and s2 = s1.substring(2) will start at index 2 and take all characters till the end of the string.
  • eorg
  • This can't be true since the e is at index 1 and s2 = s1.substring(2) will start at index 2 and take all characters till the end of the string.
  • orgi
  • This would be true if substring(a,b) included the character at index b, but it doesn't.
  • You will get an index out of bounds exception
  • This would be true if the starting index was invalid or the ending index was past 2 past the last valid index.

    2-13-7: Given the following code segment, what is the value of s1 after the code executes?

    1
    2
    3
    4
    5
    6
    7
    String s1 = "Hi There";
    String s2 = s1;
    String s3 = s2;
    String s4 = s1;
    s2 = s2.toLowerCase();
    s3 = s3.toUpperCase();
    s4 = null;
    
  • null
  • This would be true if we had s1 = s4 after s4 = null was executed. Strings are immutable and so any changes to a string returns a new string.
  • hi there
  • This would only be correct if we had s1 = s2 after s2.toLowerCaase() was executed. Strings are immutable and so any change to a string returns a new string.
  • HI THERE
  • This would be correct if we had s1 = s3 after s3.toUpperCase() was executed. String are immutable and so any change to a string returns a new string.
  • Hi There
  • Strings are immutable meaning that any changes to a string creates and returns a new string, so the string referred to by s1 does not change.
  • hI tHERE
  • Strings are immutable and so any changes to a string returns a new string.

    2-13-8: There is a method called checkString that determines whether a string is the same forwards and backwards. The following data sets can be used for testing the method. What advantage does Data Set 2 have over Data Set 1?

    1
    2
    3
    4
    Data Set 1    Data Set 2
    aba               bcb
    abba              bcd
    aBa
    
  • Data Set 2 contains one string which should return true and one that should return false.
  • All of the strings in Data Set 1 should return true, so the false condition is never tested.
  • All strings in Data Set 2 have the same number of characters.
  • Variety is always good in testing, so this is not an advantage.
  • The strings in Data Set 2 are all lowercase
  • It would be better to include both upper and lower case for testing, so this is not an advantage.
  • Data Set 2 contains fewer values than Data Set 1.
  • More test conditions is usually better, so this is not an advantage.
  • There are no advantages.
  • All the values in Data Set 1 are true, so the false condition is not tested.

2.13.3. Hard Multiple Choice Questions

These problems are harder than most of those that you will usually see on the AP CS A exam.

    2-13-9: Given the following code segment, which of the following is true?

    1
    2
    3
    4
    5
    6
    7
    8
    String s1 = new String("Hi There");
    String s2 = new String("Hi There");
    String s3 = s1;
    
    I.   (s1 == s2)
    II.  (s1.equals(s2))
    III. (s1 == s3)
    IV.  (s2.equals(s3))
    
  • II and IV
  • III is also correct.
  • II, III, and IV
  • String overrides equals to check if the two string objects have the same characters. The == operator checks if two object references refer to the same object. So II is correct since s1 and s2 have the same characters. Number II is correct since s3 and s1 are referencing the same string, so they will be ==. And s2 and s3 both refer to string that have the same characters so equals will be true in IV. The only one that will not be true is I, since s1 and s2 are two different objects (even though they have the same characters).
  • I, II, III, IV
  • I is not correct since s1 and s2 are two different objects (even though they have the same characters). If s1 and s2 were both referring to literals, then I would be correct, but the new operator forces a new object to be created.
  • II only
  • III and IV are also correct.
  • IV only
  • II and III are also correct.

    2-13-10: What does the following code print?

    System.out.println("13" + 5 + 3);
    
  • 21
  • This would be correct if it was System.out.println(13 + 5 + 3), but the 13 is a string.
  • 1353
  • This is string concatenation. When you apprend a number to a string it get turned into a string and processing is from left to right.
  • It will give a run-time error
  • You can append a number to a string in Java. It turns the number into a string and then appends the second string to the first string.
  • 138
  • This would be correct if it was System.out.println("13" + (5 + 3)), but the 5 is turned into a string and appended to the 13 and then the same is done with the 3.
  • It will give a compile-time error
  • You can append a number to a string in Java. It will compile.
You have attempted of activities on this page
Next Section - 3. Boolean Expressions and If Statements