unit 5
What is the difference between gette method and an accessor method? - A getter method allows you to get the value of a field while an accessor method sets the value of the field. - A getter method allows you to get the value of a field while an accessor method is not often used in Java. - A getter method gets the name of the class while an accessor method gets the value of the field. - There is no difference. They refer to the same idea.
- There is no difference. They refer to the same idea.
The purpose of specifying a postcondition is to - specify the types of objects the method accepts. - explain the method's end result, whether it is a return value or change in an object's state - state the return value's type. - set the method's expectations before the method is executed with respect to the parameters or object's state
- explain the method's end result, whether it is a return value or change in an object's state
An object's state is defined by the object's - methods and instance variables - instance variables and their values - access modifiers of the instance variables - methods and their return values
- instance variables and their values
The return type of a mutator method - is usually void - must match the type of of the instance variable being mutated - must be a primitive - must be a user defined class Answered
- is usually void
The return type of an accessor method - is void - must be a user defined class - must be a primitive - must match the type of the instance variable being accessed
- must match the type of the instance variable being accessed
It is considered good practice to - modify objects in a method whenever you want to - only modify objects when the method postcondition has specified the modification - modify objects without telling the user - never pass objects as parameters
- only modify objects when the method postcondition has specified the modification
Classes' access specifier is generally set to - private to prevent any user from accessing and modifying any instance variables. - private so that only certain users can create objects of the class. - public so that all data and methods are accessible by any user. - public so that any user can create and use objects of the class.
- public so that any user can create and use objects of the class.
Each of the methods below can be found in the Rectangle class. Which of the following methods would have access to the parameter object's private data and methods? - public void setWidth(Integer newWidth) - public void copy(Rectangle other) - public void copy(Triangle other) - public void setLabel(String newLabel)
- public void copy(Rectangle other)
Which variable will have a different value the second time it is printed? Assume the toString method of Rectangle prints the rectangle's dimensions. - width - room - Both width and room - Neither width nor room
- room
The purpose of specifying a precondition is to - specify the types of objects the method accepts. - explain the method's end result, whether it is a return value or change in an object's state - state the return value's type. - set the method's expectations before the method is executed with respect to the parameters or object's state
- set the method's expectations before the method is executed with respect to the parameters or object's state
Suppose there is a local variable declared in the method of a particular class. The local variable's scope is - the file in which it is declared - any method in which an object of the class is used - the main method - the method in which is it declared
- the method in which is it declared
Which of the following is NOT a proper use of the keyword this? - to access variables of the calling object - as an argument to other methods in the class - to access the private variables of other objects of the same class -to call methods of the class on the calling object
- to access the private variables of other objects of the same class
The purpose of an accessor method is - to modify an instance variable - to return the value of an instance variable - to return the values of all of the instance variables - to allow the user to have direct access to an instance variable Answered
- to return the value of an instance variable
The purpose of a mutator method is - to return the value of an instance variable - to safely modify an instance variable - to return the values of all of the instance variables - to allow the user to have direct access to an instance variable
- to safely modify an instance variable
Glasses
-1.5 all
What is the output running Main.bar();? public class Main { private static int n = 0; public static void bar() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.foo(); } public Main() { n = n + 1; } public void foo() { System.out.println(n); } }
3
Refer to this code snippet. public class Main { private static int n = 0; public static void bar() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.foo(); } public Main() { n = n + 1; } public void foo() { System.out.println(n); } } Suppose the following method is added: public void setN(int newValue) { n = newValue; } What would be the output of the program if bar() were changed to the following: public static void bar() { Main m1 = new Main(); Main m2 = new Main(); Main m3 = new Main(); m1.setN(5); m3.foo(); }
5
Mark the valid way to create an instance of Athlete given the following Athlete class definition: public class Athlete { String first_name; String last_name; int jersey; public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);
Which of the following is not part of the ACM Code of Ethics and Professional Conduct's General Ethical Principles?
Credit original creator when using other's work.
Which of the following would be the best example of how the Internet has impacted the economy?
Email has reduced the amount of mail that is produced and shipped.
Mark the valid way to create an instance of Foo given the following code: public class Foo { int bar; String stoo; public Foo() { this.bar = 0; this.stoo = ""; } public Foo(int bar) { this.bar = bar; stoo = "You."; } }
Foo fee; fee = new Foo();
What would the value of the static variable phrase be after the first time printPhrases was called from main? public class PrintQuestion { private static String phrase = "Hello World!"; public static void printPhrases() { String phrase = "hi"; System.out.println(phrase); phrase = "hello"; } }
Hello World!
Given the following definition for the class Athlete: public class Athlete { String first_name; String last_name; int jersey; public Athlete(String first_name, String last_name) { this.first_name = first_name; this.last_name = last_name; this.jersey = 0; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } } 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
Class members that should be public are I. Constructors II. Instance Variables III. Accessors/Mutators IV. Methods the class uses for itself, but the user does not need V. Methods the user needs to manipulate the objects of the class
I, III, V
Given this code snippet, public class Person { public String name; public Person(String name) { this.name = name; } public void changeName(String name) { this.name = name; } public void printName() { System.out.println(this.name); } } what will this code print? Person myPerson = new Person("Bob"); myPerson.changeName("Joe"); myPerson.name = "John"; myPerson.printName();
John
Given the code snippet, public class Pokemon { private String name; private int health; public Pokemon(String name, int health) { name = name; health = health; } } what is wrong with the class definition?
Must use this in constructor when the constructor parameters have the same name as instance variables. ie: this.name = name; this.health = health;
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
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.
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.
Given the following code public class Storm { private int lighteningCount; private int precipTotal; private String precipType; public Storm(String precipType) { this.precipType = precipType; precipTotal = 0; lighteningCount = 0; } public Storm(String precipType, int precipTotal) { this.precipType = precipType; this.precipTotal = precipTotal; lighteningCount = 0; } public Storm(String precipType, int lighteningCount) { this.precipType = precipType; this.lighteningCount = lighteningCount; precipTotal = 0; } }
The code will not compile because there are two constructors with the same signature.
Given the following code: public class TvShow { private String name; private int channel; public TvShow (String name, int channel) { this.name = name; this.channel = channel; } public void getName() { return name; } public void setName(String name) { this.name = name; } } Which of the following explains why this code will not compile?
The return type for the getName method should be set to String.
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? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
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? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
athlete.getJersey()
What is the output of the following program? public class Main { private String str = "bar"; public static void main(String[] args) { Main m = new Main("foo"); System.out.println(m.getString()); } public Main(String str) { str = str; } public String getString() { return str; } }
bar
Which methods of class Foo can be called without an actual instance of the class Foo? public class Foo { public static void foo() { ... } public void bar() { ... } public void baz() { ... } }
foo()
What would be printed the first time printPhrases was called from main? public class PrintQuestion { private static String phrase = "Hello World!"; public static void printPhrases() { String phrase = "hi"; System.out.println(phrase); phrase = "hello"; } }
hi
The following function computes the hypotenuse of a right triangle. public double findHypotenuse(int side1, int side2){ double hyp = Math.sqrt(side1 * side1 + side2 * side2); return hyp; } The following code found in main throws an error. What is the bug? int side1 = 3; int side2 = 4; findHypotenuse(side1, side2); System.out.println(hyp);
hyp is a local variable. It can only be accessed in findHypotenuse.
The Glasses class represents a pair of eyeglasses. One of its instance variables is a Prescription object called script. The Prescription object has instance variables that stores the prescription for the left and right eye. Which of the following constructors for the Glasses class correctly sets the script instance variable correctly? Assume any method calls are valid.
public Glasses(Prescription thePrescription) { script = new Prescription(thePrescription.getLeft(),thePrescription.getRight()); }
Which of the following correctly uses the this keyword?
public boolean isSame(Bird other) { return this.name.equals(other.name); }
The Insect class will contain a String for the insect's name, an int for the number of legs it has, and a boolean for whether or not the insect has wings. Which of the following is the most appropriate implementations for the Insect Class?
public class Insect { private String name; private int numLegs; private boolean hasWings; //constructor and methods not shown }
Given this Athlete class, which of the following setter methods for the jersey variable is correct? public class Athlete { private String first_name; private String last_name; private int jersey; public int getJersey() { return this.jersey; } public Athlete(String first_name, String last_name, int jersey) { this.first_name = first_name; this.last_name = last_name; this.jersey = jersey; } }
public void setJersey(int jersey) { this.jersey = jersey; }
Suppose you have the following methods defined public double increaseWidth(double num){ num += 10; return num; } public double increaseWidth(Rectangle rect){ double newWidth = rect.getWidth() * 2; rect.setWidth(newWidth); return newWidth; } Consider this code snippet double width = 10.5; Rectangle room = new Rectangle(10, 15); System.out.println(width); System.out.println(room); increaseWidth(width); increaseWidth(room); System.out.println(width); System.out.println(room); Which variable will have a different value the second time it is printed? Assume the toString method of Rectangle prints the rectangle's dimensions.
room
Which variables are in scope at the point labeled // POINT A? In other words, which variables exist at that point in the code? public class ScopeQuiz { private int count = 0; public void printPointSums() { int sum = 0; // POINT A for(int i = 0; i < 10; i++) { sum += i; } int result = sum + count; } }
sum and count
What is the output of the following code snippet? int x = 8; int y = 10; // x = y + 10; y *= 2; /* y = x; x *= 3; */ System.out.println("x: " + x); System.out.println("y: " + y);
x: 8 y: 20
The Glasses class represents a pair of eyeglasses. One of its instance variables is a Prescription object called script. The Prescription object has instance variables that stores the prescription for the left and right eye.Suppose the Glasses constructor was implemented as followspublic Glasses(Prescription thePrescription) { script = thePrescription; }What is the output of the following code snippet?// Create a new Prescription object Prescription reading = new Prescription(-1.25, -1.5); // Create two new Glasses objects Glasses myGlasses = new Glasses(reading); Glasses yourGlasses = new Glasses(reading); // Mutator for the prescription to change the prescription reading.updatePrescription(-1.5, -1.5); // Prints the glasses' prescription as // (Left Eye, Right Eye) e.g. (-2.0, -2.5) myGlasses.printPrescription(); yourGlasses.printPrescription();
(-1.5, -1.5)(-1.5, -1.5)
Accessors and mutators are used to - Allow private data to be accessed outside of the class and be safely modified. - Allow users to access and modify any of the class's data in any way they wish. - Prevent users from manipulating private data. - Allow public data to be accessed and safely modified.
- Allow private data to be accessed outside of the class and be safely modified.
Which of the following types can be permanently modified in a method when it is passed as a parameter to a method? - int - double - String - Any user defined class Answered
- Any user defined class
What is the difference between instance variables and static variables? - Each object has its own copy of the instance variables, but all objects share a copy of the static variables - Each object has its own copy of the static variables, but all objects share a copy of the instance variables - Instance variables can be public or private, but static variables must be public - Static variables can be public or private, but instance variables must be private
- Each object has its own copy of the instance variables, but all objects share a copy of the static variables
Where must a static variable be initialized? - In the constructor - In a static method. - Outside of the class. - In the class file, but not in a method.
- In the class file, but not in a method.
If you do not implement a constructor for your class, - you cannot create objects of that class type - you can create objects, but you cannot modify the instance variables' values - you can only use the class's static methods - Java creates one for you and gives all instance variables default values Answered
- Java creates one for you and gives all instance variables default values
Good programmers use comments to - Tell the compiler what they want the code to do - Make their code more readable by explaining particular chunks of code - Make their code more readable by explaining what every line does - Amuse their friends by adding funny jokes in their code
- Make their code more readable by explaining particular chunks of code
Which of the following is a benefit of using a mutator method? - Only methods inside the class can change an instance variable's value - Only methods outside of the class can change an instance variable's value - Mutator methods can verify the new value is a valid value for the instance variable - The user can change all of the instance variables using the same mutator method Answered
- Mutator methods can verify the new value is a valid value for the instance variable
Static methods can access - All instance variables and class methods - Any class method, but not the instance variables - Only other static instance variables and class methods - Only private instance variables and private class methods
- Only other static instance variables and class methods
Which of the following is true? - Both primitives and objects are returned by value. - Both primitives and objects are returned by reference. - Primitives are returned by value. Objects are returned by reference. - Objects are returned by value. Primitives are returned by reference.
- Primitives are returned by value. Objects are returned by reference.
Why can't this be used in static methods? - Static methods are not called using an object. Thus, this would be null. - this can only be used to reference instance variables. - Static methods must be public, and this can only be used in private methods. - this must be initialized in the constructor. Since static methods can be called before calling the constructor, this was never initialized.
- Static methods are not called using an object. Thus, this would be null.
What is the scope of private methods and private instance variables? Any class using an object of the declaring class - The declaring class - The main method - Only the private methods of the declaring class
- The declaring class
Which of the following is NOT a characteristic of a mutator? - The method changes the value of an instance variable to a user's specified value - The method's name (usually) starts with set - The method updates an instance variable's value - The method returns the value of an instance variable
- The method returns the value of an instance variable
What is the this keyword used to reference? - The this keyword references the method that was called. - The this keyword references the current class. - The this keyword references the object that called the method. - The this keyword references the instance variable that a local variable shadows.
- The this keyword references the object that called the method.
