2.13. Multiple Choice Exercises¶
These questions are mostly about Strings, but more questions on using other objects will be added in the future.
2.13.1. Easier Multiple Choice Questions¶
- 2-13-1: A student has created a Cat class. The class contains variables to represent the following.
A String variable called color to represent the color of the cat
A String variable called breed to represent the breed of the cat
An int variable called age to represent the age of the cat
The object myCat will be declared as type Cat. Which of the following descriptions is accurate?
- An attribute of breed is String.
- The data type of breed is String.
- color, breed, and age are instances of the Cat class.
- color, breed, and age are attributes of the Cat class.
- Cat is an instance of the myCat class.
- myCat is an instance of the Cat class.
- age is an attribute of the myCat object.
- Attributes of the Cat class and myCat object are color, breed, age.
- An attribute of Cat is myCat.
- Attributes of the Cat class are color, breed, age.
- 2-13-2: A student has created a Movie class. The class contains variables to represent the following.
A String variable called title to represent the title of the movie
A String variable called director to represent the director of the movie
A double variable called rating to represent the rating of the movie
The object scaryMovie will be declared as type Movie. Which of the following descriptions is accurate?
- An attribute of the scaryMovie class is title.
- scaryMovie is an object, not a class.
- scaryMovie is an instance of the Movie class.
- scaryMovie is an object which is an instance of the Movie class.
- Title, director, and rating are instances of the scaryMovie object.
- These are attributes of the object or class.
- An attribute of the Movie instance is scaryMovie
- scaryMovie is an instance of the Movie class.
- Movie is an instance of scaryMovie.
- scaryMovie is an instance of the Movie class.
- 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 len after the following executes?
String s1 = "Hey, buddy!";
int len = s1.length();
- 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 pos after the following code executes?
String s1 = "ac ded ca";
int pos = s1.indexOf("d");
- 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-5: What is the value of s1 after the following code executes?
1String s1 = "Hey";
2String s2 = s1.substring(0,1);
3String s3 = s2.toLowerCase();
2.13.2. Medium Multiple Choice Questions¶
- a random number from 0 to 4
- This would be true if it was (int) (Math.random * 5)
- a random number from 1 to 5
- This would be true if it was ((int) (Math.random * 5)) + 1
- a random number from 5 to 9
- Math.random returns a value from 0 to not quite 1. When you multiply it by 5 you get a value from 0 to not quite 5. When you cast to int you get a value from 0 to 4. Adding 5 gives a value from 5 to 9.
- a random number from 5 to 10
- This would be true if Math.random returned a value between 0 and 1, but it won't ever return 1. The cast to int results in a number from 0 to 4. Adding 5 gives a value from 5 to 9.
2-13-6: Given the following code segment, what is the value of num
when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4.
double value = Math.random(); int num = (int) (value * 5) + 5;
- a random number from 0 to 10
- This would be true if it was (int) (value * 11)
- a random number from 0 to 9
- This would be true if it was (int) (value * 10)
- a random number from -5 to 4
- This would be true if it was (int) (value * 10) - 5
- a random number from -5 to 5
- Math.random returns a random value from 0 to not quite 1. After it is multipied by 11 and cast to integer it will be a value from 0 to 10. Subtracting 5 means it will range from -5 to 5.
2-13-7: Given the following code segment, what is the value of num
when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4.
double value = Math.random(); int num = (int) (value * 11) - 5;
- 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-8: After the following code is executed, which of I, II and/or III will evaluate to true?
1String s1 = "xyz";
2String s2 = s1;
3String s3 = s2;
4
5I. s1.equals(s3)
6II. s1 == s2
7III. s1 == 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-9: What is output from the following code?
1String s = "Georgia Tech";
2String s1 = s.substring(0,7);
3String s2 = s1.substring(2);
4String s3 = s2.substring(0,3);
5System.out.println(s3);
- 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-10: Given the following code segment, what is the value of s1 after the code executes?
1String s1 = "Hi There";
2String s2 = s1;
3String s3 = s2;
4String s4 = s1;
5s2 = s2.toLowerCase();
6s3 = s3.toUpperCase();
7s4 = null;
- 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-11: There is a method called checkString that determines whether a string is the same forwards and backwards. The following data set inputs can be used for testing the method. What advantage does Data Set 2 have over Data Set 1?
1Data Set 1 Data Set 2
2aba bcb
3abba bcd
4aBa
- Use one class, Car, which has three attributes: int numDoors, double mpg, and boolean hasAir.
- Having one class with all the attributes needed is the most efficient design in this case.
- Use four unrelated classes: Car, Doors, MilesPerGallon, and AirConditioning
- The point of storing the car information is so we can easily access the attributes related to a car.
- Use a class, Car, which has three subclasses: Doors, MilesPerGallon, and AirConditioning
- In this case, the information only refers to a couple of basic attributes so it is better to store that data as fields within a single class.
- Use a class Car, which has a subclass Doors, with a subclass AC, with a subclass MPG.
- It doesn't really make sense for AC to be a subclass of MPG, and that being a subclass of Doors.
- Use three classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.
- A car doesn't really make sense to be a subclass of AC, and so on. It would also be better to group a couple of pieces of data into a single class.
2-13-12: A car dealership needs a program to store information about the cars for sale.For each car, they want to keep track of the following information: the number of doors (2 or 4),its average number of miles per gallon, and whether the car has air conditioning. Which of the following is the best design?
2.13.3. Hard Multiple Choice Questions¶
- 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-13: Given the following code segment, which of the following is true?
1String s1 = new String("Hi There");
2String s2 = new String("Hi There");
3String s3 = s1;
4
5I. (s1 == s2)
6II. (s1.equals(s2))
7III. (s1 == s3)
8IV. (s2.equals(s3))
- 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 append 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.
2-13-14: What does the following code print?
System.out.println("13" + 5 + 3);