CMSC 131 (Summer 2019) Week 07 Study Questions Flashcards

1
Q

In what kind of methods does it make sense to use “this”?

A

Instance methods and constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does “this” refer to?

A

The “current object”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If you do not write any constructors, what values will instance variables of the following primitive types be assigned: int, double, boolean, char ?

A

0 for int, 0.0 for double, false for boolean, ASCII code 0 for char

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If you do not write any constructors, what values will instance variables that are references by assigned?

A

null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Under what circumstances will Java provide a default constructor for you automatically?.

A

If you do not write any constructors yourself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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:

A
public Cat(Cat x) {
   numWhiskers = x.numWhiskers;
   name = x.name;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Stack (in general, not just in Java)?

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When you push an entry into the stack does it go on the top or bottom?

A

top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When you pop an entry from the stack, does it come off the top or bottom?

A

top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does API stand for?

A

Application Programming Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If someone showed you a Java class, how can you quickly identify which members were part of the API for that class?

A

Look for “public” members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

If a member is declared as public, can it be accessed from inside the same class?

A

YES.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If a member is declared as public, can it be accessed from another class?

A

YES.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If a member is declared as private, can it be accessed from inside the same class?

A

YES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If a member is declared as private, can it be accessed from another class?

A

No

17
Q

What is a “setter”?

A

A method that modifies the value of an instance variable of the object.

18
Q

What is a “getter”?

A

A method that returns the value of an instance variable of the object.

19
Q

Explain why it is important to limit the number of public members.

A

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.

20
Q

Name and describe the two visibility specifiers that you should know at this point.

A

public: these members are visible everywhere
private: these members are visible only within the class

21
Q

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.

A

TRUE

22
Q

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.

A

FALSE

23
Q

What package is the Scanner class located in? What is the fully qualified name of the Scanner class?

A

java.util is the package. The fully qualified name is “java.util.Scanner”

24
Q

What is accomplished when you type “import java.awt.Color;” at the top of a file?

A

Now whenever you type “Color”, the compiler knows that you are talking about the Color class that resides in the package called “java.awt”.

25
Q

What is accomplished when you type “import java.awt.*;” at the top of a file?

A

You are importing EVERYTHING that is in the package java.awt.

26
Q

Which java package is automatically imported in its entirety into every Java program you write?

A

java.lang

27
Q

What method of the String class can be used to pick out one particular character in the string?

A

charAt

28
Q

What method of the String class can tell you how many characters are in the String?

A

length

29
Q

What method of the String class can be used to compare to Strings for alphabetical order?

A

compareTo

30
Q

What method of the String class can select a portion of an existing String?

A

substring

31
Q

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.

A

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;
}