2.15. Multiple Choice Exercises¶
These questions are mostly about Strings, but more questions on using other objects will be added in the future.
2.15.1. Easier Multiple Choice Questions¶
These problems are mostly easier than what you will see on the AP CSA exam.
- 2-15-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-15-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.
- new Person john = Person("John", 16);
- The new keyword should be placed before the call to the Person constructor.
- Person john("John", 16);
- The new keyword is needed to create an object in Java
- Person john = ("John", 16);
- The new keyword is needed to create an object in Java
- Person john = new Person("John", 16);
- Correct!
- Person john = new Person(16, "John");
- The Person constructor expects a String and an int parameter in that order.
2-15-3: Which of the following code segments will correctly create an instance of a Person object?
public class Person
{
private String name;
private int age;
public Person(String a, int b)
{
name = a;
age = b;
}
}
- 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-15-4: 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-15-5: 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-15-6: What is the value of s1 after the following code executes?
String s1 = "Hey";
String s2 = s1.substring(0,1);
String s3 = s2.toLowerCase();
2.15.2. Medium Multiple Choice Questions¶
These problems are similar to those that you will see on the AP CSA exam.
- Movie one = new Movie("Harry Potter", "Bob");
- There is no Movie constructor with 2 parameters.
- Movie two = new Movie("Sponge Bob");
- This creates a Movie object with the title "Sponge Bob".
- Movie three = new Movie(title, rating, director);
- It is not clear whether the variables title, rating, and director are the correct types or in the correct order.
- Movie four = new Movie("My Cool Movie", "Steven Spielburg", "4.4");
- The third argument "4.4" is a String because it is in quotes, but the constructor expects a double.
- Movie five = new Movie(t);
- It is not clear whether t is the correct type for this constructor.
2-15-7: Consider the following class. Which of the following code segments would successfully create a new Movie object?
public class Movie { private String title; private String director; private double rating; private boolean inTheaters; public Movie(String t, String d, double r) { title = t; director = d; rating = r; inTheaters = false; } public Movie(String t) { title = t; director = "unknown"; rating = 0.0; inTheaters = false; } }
- 100.00
- Remember that we have added and withdrawn money
- 110.00
- The constructor sets the total to 100, the withdraw method subtracts 30, and then the deposit method adds 40.
- 90.00
- We added more money than we took out
- 10.00
- We set the value of total to be 100 first
2-15-8: Given the BankAccount class definition below, what is the output of the code in the main method?
public class BankAccount
{
private int accountID;
private double total;
public BankAccount(int id, double initialDeposit)
{
accountID = id;
total = initialDeposit;
}
public void deposit(double money)
{
total = total + money;
}
public void withdraw(double money)
{
total = total - money;
}
public void printCurrentTotal()
{
System.out.print(total);
}
public static void main(String[] args)
{
BankAccount newAccount = new BankAccount(12345, 100.00);
newAccount.withdraw(30.00);
newAccount.deposit(40.00);
newAccount.printCurrentTotal();
}
}
- 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-15-9: 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-15-10: 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-15-11: 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
- 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-15-12: 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);
- 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-15-13: 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;
- 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-15-14: 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?
Data Set 1 Data Set 2
aba bcb
abba bcd
aBa
- 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-15-15: 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?
- Hello Bob
- The constructor is called first and prints out one "Hello ".
- Hello Hello Bob
- The constructor is called first and prints out one "Hello " followed by the printSomething() method which prints out "Hello Bob ".
- Hello Bob Hello Bob
- The constructor is called first and prints out one "Hello ".
- Hello Bob Hello
- The constructor is called first and prints out one "Hello " followed by printSomething().
2-15-16: Assume that SomeClass and MainClass are properly defined in separate files. What is the output of the code in main()?
class SomeClass
{
public SomeClass()
{
System.out.print("Hello ");
}
void printSomething(String name)
{
System.out.print("Hello " + name + " ");
}
}
public class MainClass
{
public static void main(String[] args)
{
SomeClass someClass = new SomeClass();
someClass.printSomething("Bob");
}
}
2.15.3. Hard Multiple Choice Questions¶
These problems are harder than most of those that you will usually see on the AP CSA exam.
- Woo Hoo Hoo Woo
- 'Woo Hoo' is what gets passed to someOtherFunc()
- Hoo Woo Hoo
- 'Woo ' gets printed first.
- Woo Hoo Woo Hoo
- We first print 'Woo ' then 'Hoo ' then the appended "Woo Hoo"
- Woo Woo Hoo Hoo
- 'Woo ' gets printed first, then the 'Hoo ' from someOtherFunc().
2-15-17: What is the output of the following code?
public class Test
{
String someFunc(String str)
{
return someOtherFunc(str + " Hoo");
}
String someOtherFunc(String str)
{
return "Hoo " + str;
}
public static void main(String[] args)
{
Test x = new Test();
System.out.print("Woo " + x.someFunc("Woo"));
}
}
- 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-15-18: Given the following code segment, which of the following is true?
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))
- 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-15-19: What does the following code print?
System.out.println("13" + 5 + 3);
- unknown value
- x needs to be initialized with a call to the SomeClass constructor.
- 0
- x and someVar have not been initialized.
- compile error
- This will give an error that x has not been initialized. It needs to be initialized with a call to the SomeClass constructor.
- runtime error
- This code will not run.
2-15-20: Assume that SomeClass and MainClass are properly defined in separate files. What is the output of main()?
class SomeClass
{
int someVar;
}
public class MainClass
{
public static void main(String[] args)
{
SomeClass x;
System.out.println(x.someVar);
}
}
- unknown value
- ints get initialized to 0 by default if not explicitly initialized.
- 0
- ints get initialized to 0 by default if not explicitly initialized.
- compile error
- This code will compile.
- runtime error
- someVar has a value assigned by default.
2-15-21: Assume that SomeClass and MainClass are properly defined in separate files. What is the output of main()?
class SomeClass
{
int someVar;
}
public class MainClass
{
public static void main(String[] args)
{
SomeClass x = new SomeClass();
System.out.println(x.someVar);
}
}
The Mark Complete button and green check mark are intentionally not included for this page because there may be many quiz-bank exercises on this page.