Chapter 4: Class Flashcards

1
Q

What’s the diff between local variable and instance variable? What about global variable?

A

Local variable lives inside of {} (methond body or code block)

instance variable: inside of a class

There is no global variable in Java but there is for C/C++.

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

Explain: pass by value

A

In Java, variables are passed by value, meaning that the value of the variable is COPIED and passed into a parameter.

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

How to do testing in Java? What is bottom-up testing? What if a method called has not been implemented?

A

Tool: JUnit, allow to run method or code without writing main()

each method should be unit tested.

Bottom-up testing: always test methods being called first.

Use a stub if a method is not ready yet.

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

Explain: Encapsulation, abstraction, and API

A

Abstraction and encapsulation have areas in common: they both require separating “implementation details” from “well-defined interface”

API stands for application programming interface: it is the documentation of how to use a class

Abstraction reduces complexity and encapsulation also improve privacy and security

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

Access modifiers: name them, and explain their function

A

public: can be used anywhere
protected: can be used by child class and other unrelated classes in the same package

default (package): can be used by classes in the same package

private: can only be used within the class definition

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

Explain: Overloading

A

Overloading a method name: same name but different parameter list, essentially diff methods so can have different return type and access modifier

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

add(double, int) and add(int, double)

What’s this called? is it legal? Any issue related to it?

A

Method overloading

completely legal

Auto-type conversion can cause issue: add(1,2)

Always think as if you are a compiler, see if you know which method to invoke

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

Explain the use of StringTokenizer

A
Example:
wordFactory = new StringTokenizer(String, delimiter);

wordFactory.hasMoreTokens();

wordFactory.nextToken();

wordFactory.countTokens();

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

What are static variables and methods? How to use them?

A

Only one copy per class. All members can access static variables but it is better be used via ClassName.staticVariable;

Special:
import static java.util.Math.*;

we don’t need to write Math.abs(); anymore

just: abs();

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

Draw the memory analysis for a class method execution being invoked from the main()

A

Stack(stack frames) Heap Method area

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

compare none objects like: null and the result of getClass(object)

A

use “==” because they are not objects

do not use equals()

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

Explain: deep copy and shallow copy

A

Deep copy: a copy that is completely independent from the original object

you can do direct assignment with immutable objects

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

How to make a package?

A

at the very beginning:

package com.letcs.calculator;

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

what is javadoc? how to use javadoc?

A

Auto-extract all API for a class or an entire package

use /**
*/
to comment

run in the cli: javadoc -d directory package_name

@tags:
@param; @return; @throws; @deprecated (dropped, no longer in use)
@see other class; @ author; @version;

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

How to use a variable number of parameters?

A

method(type … name)

vararg specification

… : is called an ellipsis

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

How to use the enum type? what are some of the most common methods?

A

enum gender {MALE, FEMALE};
to access: gender.MALE;

int ordinal(enum.VALUE); returns the index of the value

int compartTo(enum.VALUE); returns a negative integer if the calling object comes before the passed in VALUE

17
Q

What is a ragged array?

A

2D array with diff length