4.9. Medium Multiple Choice QuestionsΒΆ

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

    4-8-1: After the following code is executed, which of I, II and/or III will evaluate to true?

    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.

    4-8-2: What is output from the following code?

    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.

    4-8-3: Given the following code segment, what is the value of s1 after the code executes?

    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.

    4-8-4: 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?

    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.
Next Section - 4.10. Hard Multiple Choice Questions