Module 05: Writing Classes Flashcards
what will this code print?
Person myPerson = new Person(“Bob”);
myPerson.changeName(“Joe”);
myPerson.name = “John”;
myPerson.printName();
John
Given this code snippet,
public class Athlete
{
public Athlete(String name)
{
this.name = name;
}
}
what is missing from the class definition?
- Need to declare the name instance variable
- Missing void in constructor definition
- Class constructors must be private
- Java does not use the keyword this, instead it uses self
- No constructor is defined for the class
- Need to declare the name instance variable
what is wrong with the class definition?
Hint : We expect the instance variables name and health to be initialized in the constructor.
- Missing void in constructor definition
- Class constructors must be private
- The constructor parameters cannot have the same names as the instance variables.
- Must use this in constructor when the constructor parameters have the same name as instance variables. ie:
this. name = name;
this. health = health;
- Must use this in constructor when the constructor parameters have the same name as instance variables. ie:
this.name = name;
this.health = health;
What is the output running Main.bar();?
3
The instance variable n Is declared static, so it is shared by all instances of class Main. Since n is incremented by each constructor, the value of n after three instantiations is 3. It does not matter on which instance foo() is called.
Refer to this code snippet.
5
What is the output of the following program?
- The program does not compile.
- The program does not produce any output.
- foo
- bar
- bar
In Main(String str), the parameter name string shadows the instance variable name string. The statement string = string assigns the parameter string to itself, and so the assignment has no effect outside of Main(String str). Therefore, getString() will return the static variable bar.
Given an instance of the Athlete class called athlete, what is the proper way to set the value of the jersey number after it has been instantiated?
- athlete.jersey = 23;
- athlete.getJersey() = 23;
- athlete.getJersey(23);
- athlete.setJersey(23);
- You cannot set the jersey, since jersey is private and there is no setter method.
- You cannot set the jersey, since jersey is private and there is no setter method
Given an instance of the Athlete class called athlete, what is the proper way to get the value of the jersey number?
- athlete.jersey
- athlete.jersey()
- athlete.getJersey
- athlete.getJersey()
- None of the above
- athlete.getJersey()
Mark the valid way to create an instance of Athlete given the following Athlete class definition:
- athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
- Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
- Athlete athlete = new Athlete(“Dirk” “Nowitzki” 41);
- Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, “41”);
- Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, 41);
Mark the valid way to create an instance of Foo given the following code:
- Foo fee = Foo(32);
- Foo fee;
new fee = Foo(); - Foo fee = new Foo(10, “You.”)
- Foo fee;
fee = new Foo();
4. Foo fee; fee = new Foo();
Given the following definition for the class Athlete:
Which of the following are valid instantiations of the class Athlete? I – Athlete joe = new Athlete("Joe", "Montana"); II – Athlete joe = new Athlete("Joe, "Montana", "16"); III – Athlete joe = new Athlete("Joe", "Montana", 16);
I and III
I only
II only
I II and III
II and III
I and III
Which methods of class Foo can be called without an actual instance of the class Foo?
- All methods require an instance of Foo in order to be called.
- No methods require an instance of Foo in order to be called.
- bar()
- baz()
- foo()
- foo()
foo() is declared static, so it can be invoked on the class, without an instance.
Which of the following explains why this code will not compile?
- The constructor is missing a return type and it should be set to void.
- The constructor is missing a return type and it should be set to boolean.
- The instance variables name and channel should be public.
- The return type for the getName method should be set to String.
- The return type for the setName method should be set to String.
- The return type for the getName method should be set to String
Which statement best describes this code?
- The code will not compile because there are two constructors with the same signature.
- The code will not compile because the constructors do not have a return type.
- The code will compile, but will not run because there are multiple constructors.
- The code will compile, but will not run because the variables are not initialized.
- The code will compile and work as intended.
- The code will not compile because there are two constructors with the same signature.
Which of the following would be the best example of how the Internet has impacted the economy?
- Social networking has allowed friends to keep in touch even when they move apart.
- People rely on email and texting to keep in touch instead of phone calls.
- Email has reduced the amount of mail that is produced and shipped.
- Superstores such as Wal-Mart and Target have caused a decline in the smaller family owned stores.
- Email has reduced the amount of mail that is produced and shipped
Which of the following is not part of the ACM Code of Ethics and Professional Conduct’s General Ethical Principles?
- Respect privacy.
- Credit original creator when using other’s work.
- Be honest and trustworthy.
- Be fair and take action not to discriminate.
- Credit original creator when using other’s work
Consider the following code segment:
public static String mystery(String word, int i, int j)
{
String mystery = word.substring(i, i+ 1);
mystery += word.substring(i, j);
return mystery;
}
Which of the following is the most appropriate precondition for the variable i in mystery so that substring does not throw an exception?
- Precondition: i >= 0, i < word.length, i <= j.
- Precondition: i >= 0, i <= word.length, i <= j.
- Precondition: i > 0, i < word.length, i < j.
- Precondition: i >= 0, i < word.length, i > j.
- Precondition: i > 0, i <= word.length.
- Precondition: i >= 0, i < word.length, i <= j
Consider the following code segment:
public static String mystery(String word, int i, int j)
{
String mystery = word.substring(i, i+ 1);
mystery += word.substring(i, j);
return mystery;
}
Which of the following is the most appropriate precondition for the variable j in mystery so that substring does not throw an exception?
- Precondition: j > i and j <= word.length.
- Precondition: j >= i and j <= word.length.
- Precondition: j >= i and j < word.length.
- Precondition: j > 0 and j <= word.length.
- Precondition: j < i and j <= word.length.
- Precondition: j >= i and j <= word.length
What is the difference between a public and private method/variable?
Public: allow access to data and methods from classes outside the declaring class
What is Encapsulation?
The process of hiding the implementation details of a class from the user
What are accessor methods?
Methods used to access instance variables and object data. Also referred to as getter methods
What are mutator methods?
Methods used to change or manipulate instance variables or object data. Also referred to as setter methods.