CMSC 131 (Summer 2019) Week 07 Study Questions Flashcards
In what kind of methods does it make sense to use “this”?
Instance methods and constructors.
What does “this” refer to?
The “current object”.
If you do not write any constructors, what values will instance variables of the following primitive types be assigned: int, double, boolean, char ?
0 for int, 0.0 for double, false for boolean, ASCII code 0 for char
If you do not write any constructors, what values will instance variables that are references by assigned?
null
Under what circumstances will Java provide a default constructor for you automatically?.
If you do not write any constructors yourself.
What is a copy constructor? Give an example.
The copy constructor accepts an argument that is the same type as the object being constructed. It initializes the fields of the current object to match that of the parameter.
Example:
public Cat(Cat x) { numWhiskers = x.numWhiskers; name = x.name; }
What is a Stack (in general, not just in Java)?
A stack is a simple linear data type in which elements are both inserted (“pushed”) and removed (“popped”) from the same end. (We usually picture the stack vertically, and say that we “push” and “pop” items from the top.)
When you push an entry into the stack does it go on the top or bottom?
top
When you pop an entry from the stack, does it come off the top or bottom?
top
True/False: In java, when you pass a reference variable as an argument to a method, it is possible for the method to modify the object to which the variable refers.
TRUE
What does API stand for?
Application Programming Interface
If someone showed you a Java class, how can you quickly identify which members were part of the API for that class?
Look for “public” members.
If a member is declared as public, can it be accessed from inside the same class?
YES.
If a member is declared as public, can it be accessed from another class?
YES.
If a member is declared as private, can it be accessed from inside the same class?
YES
If a member is declared as private, can it be accessed from another class?
No
What is a “setter”?
A method that modifies the value of an instance variable of the object.
What is a “getter”?
A method that returns the value of an instance variable of the object.
Explain why it is important to limit the number of public members.
Encapsulation of the fields (instance variables) of a class is very important. We frequently need to make changes to a class. If these changes do not modify the class API, then the modified class will work perfectly well within an existing project. However, if you modify the class in such a way that the API is changed, then you will have to re-program other components of the project so that they will work with the new class. By limiting the API we are free to make more extensive changes to the class without having to re-program other parts of the project.
Name and describe the two visibility specifiers that you should know at this point.
public: these members are visible everywhere
private: these members are visible only within the class
True/False: If you change a class in such a way that the API changes, then other classes which depend on this one will have to be re-coded.
TRUE
True/False: If you change a class without modifying the API, then other classes which depend on this one will have to be re-coded.
FALSE
What package is the Scanner class located in? What is the fully qualified name of the Scanner class?
java.util is the package. The fully qualified name is “java.util.Scanner”
What is accomplished when you type “import java.awt.Color;” at the top of a file?
Now whenever you type “Color”, the compiler knows that you are talking about the Color class that resides in the package called “java.awt”.
What is accomplished when you type “import java.awt.*;” at the top of a file?
You are importing EVERYTHING that is in the package java.awt.
Which java package is automatically imported in its entirety into every Java program you write?
java.lang
What method of the String class can be used to pick out one particular character in the string?
charAt
What method of the String class can tell you how many characters are in the String?
length
What method of the String class can be used to compare to Strings for alphabetical order?
compareTo
What method of the String class can select a portion of an existing String?
substring
Write a method called “count”. The method should be public and static. It takes one parameter, (a reference to a String). The method will return an int. The return value should be equal to the number of X’s that appear in the String. For example, if the parameter is: “XaXXXbXXc” then the return value would be 6.
public static int count(String s) {
int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'X') { count++; } } return count; }