week02 Foundational OOP Flashcards

1
Q

What is the role of the main method in Java?

A

It’s the starting point of the program. It’s where objects are created and the program begins execution.

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

What are the requirements for the main method in Java?

A

It must be public and static, with a single String array argument(String[] args).

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

What are two key characteristics of the main method?

A

The main method must exist for the program to run and should initiate the program’s operations by creating objects and calling methods.

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

What is a constructor in Java?

A

A constructor is a special method to create and initialize an object. It has the same name as the class and sets up initial values for object attributes.

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

What are the key characteristics of a constructor?

A

A constructor does not have a return type, not even void. It initializes all instance variables with values.

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

How is a constructor different from a regular method?

A

Unlike methods, a constructor is called automatically when a new object is created and does not need a return type.

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

How does a constructor use parameters in Java

A

It uses parameters passed to it to assign values to the object’s attributes using this keyword.

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

What is the equivalent of a constructor in Python?

A

The \_\_init\_\_ method in Python is like a constructor in Java. It initializes object’s properties.

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

The instance variables are declared in the constructor.
True or false?

A

False.
The instance variables are declared in the class.

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

What are instance variable in Java?

A

Instance variables are data elements of a class. Each object has its own copy.

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

Where do you declare instance variables?

A

Declare them at the top of a class. They should be outside any method or constructor.

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

How do you assign initial values to instance variables?

A

Set initial values in the constructor, which is called when creating an object.

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

What is the scope of instance variables?

A

The entire class. All methods and constructors in the class can use them.

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

What else can instance variables be called?

A

Fields, attributes, or data members. They store information about an object’s state.

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

Do instance variables have a data type?

A

Yes, each instance variable must be declared with a data type.

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

What is the purpose of instance variables?

A

They represent the state of an object of instance and hold values specific to each object.

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

What happens to instance variables that are not initialized?

A

They get a default value from Java: 0 for numbers, false for boolean, null for objects.

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

How are local variables different from instance variables regarding initialization?

A

Local variables do not get a default value and must be initialized before use.

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

What does Java do if you don’t write a constructor for a class?

A

Java creates a default constructor that sets all instance variables to default values.

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

What happens to the default constructor when you write your own constructor(s)?

A

If you write any constructors, Java does not create a default one.

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

Java class can only have one constructor.
True or false?

A

False. You can have multiple constructors and they can call each other with this.

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

What are the default values for data types in Java?

A

・byte, short, int, long: 0(or 0L for long)
・float, double: 0.0(or 0.0f for float)
・char: ‘\u0000’(null character)
・boolean: false
・object: null

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

What is a Java object?

A

It’s an instance of a class that has its own space in memory.

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

What determines the state of a Java object?

A

Its state is determined by the values stored in its instance variables.

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

How do you access a Java object?

A

Through a reference, which is a variable that holds the memory address of the object.

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

What part of an object is visible to other classes?

A

Only the parts marked as public. Private parts are hidden inside the object.

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

What is the difference between an object’s public interface and private implementation?

A

Public interface is what other classes can see and use. Private implementation is internal working that other classes cannot access.

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

What does encapsulation mean in Java?

A

Encapsulation is bundling data with methods that operate on the data.

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

How are constructors related to encapsulation?

A

Constructors set initial values for an object’s state within the safe boundaries of encapsulation.

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

What is information hiding in Java?

A

It’s when an object hides its state and can only be accessed through its public methods, not directly.

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

What are visibility modifiers in Java?

A

They are keywords that define an object’s accessibility. Common modifiers are public, private, abd protected.

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

How do public and private visibility modifiers work?

A

Public members can be accessed from anywhere. Private members can only be accessed within their own class.

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

What is the rule of thumb for encapsulationg your classes?

A

Make variables private and methods public. This protects the integrity of the data.

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

How can encapsulated objects be visualized?

A

Think of them as a black box. You can’t see inside, but you can use its features.

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

What’s the difference between a class’s public interface and private implementation?

A

The public interface is what you can use from outside the class, like buttons on a machine. Private implementation is the hidden inner workings inside the class, like the machine’s engine.

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

What are accessors in Java?

A

Accessors, also know as getters, are methods that let you read private data. They return a value and have no parameters.

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

How do you write an accessor method?

A

Start with get followed by the variable name, make it public, and return the variable value. Example:
public int getPrice() { return price;}.

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

What are mutators in Java?

A

Mutators, also know as setters, are methods that let you change private data. They don’t return a value but take a parameter.

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

How do you wirte a mutator method?

A

Start with set followed by the variable name, make it public, and assign the parameter value to the variable. Example:
public void setFirstName(String newName) {this.firstName = newName;}.

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

Why do we use accessors and mutators?

A

They enforce encapsulation. Accessors allow read-only access to variables, and mutators enable controlled changes to variables.

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

Why should the use of mutators be restricted?

A

Limiting mutability ensures class stability and maintains valid state. Mutators should be used carefully to prevent invalid object states.

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

What should mutator contain?

A

Mutators must include logic to check if the new value is valid. They should do nothing if the value is not acceptable.

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

What are side effect in methods?

A

Side effects are changes that a method makes outside its main job.
i.g. Changing variables that are not directly part of the method or engaging in activities like reading from or writing to a file.

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

Accessors should have side effects.

True or False?

A

False. Accessors must not have any side effects. They should only return values.

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

What shoud mutator methods change?

A

Mutotar methods should only change the value of the instance variable(the implicit argument) they are meant to modify, not any other state or object.

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

What is the purpose of the toString method in Java?

A

To provide a text representation of the object’s state, mainly for debugging and logging purposes.

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

True or False?
“The toString method should contain the values of some instance variables.”

A

False. The toString method should contain the values of all instance variables.

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

What happens when you pass an object to System.out.println()?

A

Java automatically calls the object’s toString() method to get and print the string representation.

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

True or false?

These two lines equivalent:
System.out.println(myGuppy); and System.out.println(myGuppy.toString());

A

True. Both lines of code will result in the toString method being called and the output being printed.

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

What is Java class?

A

A Java class is a data structure that encapsulates data and methods to work on that data. It represents a specific entity or concept.

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

True or false?
“A Java class should expose all its data to the outside.”

A

False. A Java class should encapsulates its data, providing controlled access through methods.

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

What is the purpose of a constructor in a Java class?

A

A constructor initializes a new object to ensure it has a valid state before being used.

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

True or false?
“Java will provide a default constructor for a class if none is provided. “

A

True. If no constructors are explicitly defined, Java will provide a default constructor.

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

What does this refer to in Java?

A

this refers to the current object whose method or constructor is being executed.

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

Why do we use this inside methods?

A

To clarify that a variable or method belongs to the current object, especially when local variable might overshadow instance variables.

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

True or false?
this can be used to call another constructor within the same class.”

A

True. this is used to invoke antoher constructor in the same class.

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

When is this helpful for variable names?

A

Use this when an instancde variable and a method parameter have the same name to distinguish them.

58
Q

True or false?
this should be used to improve code readability. “

A

True. We should use this wherever it makes the code clearer and easier to understand.

59
Q

What makes up a class’s public interface?

A

A class’s public interface consists of its public members, which is what other parts of a program can see and interact with.

60
Q

True or false?
“A class’s API is the same as its private members.”

A

False. The API refers to the public interface of a class, not its private members.

61
Q

What do we mean by exposing the interface of a class?

A

Exposing the interface means making the public methods available for other classes to use.

62
Q

How do instances of classes communicate in a system?

A

Instances communicate with the system and other instances through their APIs, the set of public methods they offer.

63
Q

True or false?
“Other parts of the system are aware of a class’s internal workings”

A

False. The system and other objects do not know about a class’s internal data and how it performs its tasks.

64
Q

What should a well-designed class do with its internal data and implementation?

A

It should hide its internal data and the specifics of its implementation, revealing only what is necessary through its public interface.

65
Q

What is information hiding in OOP?

A

Information hiding is keeping a class’s implementation details private, exposing only what is necessary through its public interface.

66
Q

What does information hiding accomplish in a system’s architecture?

A

It decouples components, allowing them to be developed and tested independently.

67
Q

True or false?
“Information hiding prevents the development of components in parallel.”

A

False. information hiding enables parallel development because components interact through well-defined interfaces, not direct access to each other’s data.

68
Q

How does information hiding affect maintenance?

A

Maintenance is easier because classes are less tightly interconnected, reducing the complexity of changes.

69
Q

True or false?
“In Java, you should declare local variables at the beginning of a method.”

A

False. Minimize the scope of local variables by declaring them as close as possible to the point where they are first used.

70
Q

True or false?
“All instance variables should be made public to ensure accessibility.”

A

False. All instance variables should be private to restrict direct access and maintain encapsulation.

71
Q

True or false?
“Making instance variables final and assigning them in the constructor can enhance immutability.”

A

True. Final instance variables cannot be changed once assigned, which promotes immutability.

72
Q

True or false?
“You should always provide mutators for all instance variables.”

A

False. Only provide a mutator for an instance variable when it’s necessary to change the variable’s after the object’s construction.

73
Q

True or false?
“Mutator methods should modify any arguments passed to them.”

A

False. Mutator methods should only change the implicit argument, which is the instance variable they are designed to modify.

74
Q

True or false?
“You should provide accessors for immutable fields so they can be modified.”

A

False. You don’t need to provide accessors for immutable fields as they shouldn’t be modified after initialization.

75
Q

True or false?
“Accessors shoud have side effects to be more useful.”

A

False. Accessors should not have any side effects, as their sole purpose is to return a value.

76
Q

What is the key concept of Responsibility -Driven Design in Java?

A

Each class should have one responsibility and should manage its own data and behavior.

77
Q

True or False:
“In Responsibility-Driven Design, classes can have multiple responsibilities for flexibility.”

A

False. Each class should have a single, clearly defined responsibility.

78
Q

How do you decide which classes to implement in Responsibility-Driven Design?

A

By analyzing the language of the problem domain and identifying nouns as potential classes and verbs as potential methods.

79
Q

What is one benefit of information hiding in Java?

A

It decouples system components, allowing independent development and easier maintenance.

80
Q

True or false?
“Minimal coupling is a desirable characteristic in Java class design.”

A

True. Minimal coupling allows classes to remain independent, making the system more maintainable.

81
Q

What does it mean to localize change in system design?

A

Changes in one part of the system should affect as few other parts as possible.

82
Q

True or false:
“Side effects include changing program state that’s not related to method’s purpose.”

A

True. Side effects can be changes to any state not directly related to the method’s main objective.

83
Q

True or false?
“It’s good practice to design methods that modify their explicit arguments.”

A

False. Generally, you should avoid designing methods that modify their explicit arguments to reduce unexpected behavior.

84
Q

What is the Single Responsibility Principle(SRP)?

A

Each class should have one, and only one reason to change, meaning it should have one job or responsibility.

85
Q

True or false?
“Java methods can exist outside of classes.”

A

False. Every method in Java must be inside a class; there are no global functions in Java.

86
Q

What are the parts of a Java method declaration?

A

A Java method has a visibility modifier, a return type, a method name, and a parameter list.

87
Q

What is the correct term for the names given to the inputs of a method?

A

The names given in a method declaration are called formal parameters.

88
Q

What do Java methods accept and generate?

A

Java methods cam accept input through parameters and generate output through a return value.

89
Q

True or false?
“A method in Java must return a value.”

A

False. A method in Java can have a return type of void, meaning it does not return any value.

90
Q

True or false?
“You must always use this when calling a method from within the same object.”

A

False. The use of this within an object to call its own methods is optional.

91
Q

True or false:
“Helper(auxiliary)” methods in a Java class should always be public.

A

False. Helper methods are typically private because they are internal to the class’s functionality.

92
Q

True or false?
“A well-designed Java class often has a few public members and many private members.”

A

True.

93
Q

True or false?
“Users of your Java code need to understand how the methods are implemented to use your services”

A

False. Users of your code do not need to know about the interna implementation of the services provided.

94
Q

What are variable-length parameters in Java methods?

A

They allow you to pass a varying number of arguments to a method, treating them as an array within the method.

95
Q

True or false?
“It’s better to create overload methods than to use a variable-length parameter list for methods that need to accept a varying number of arguments.”

A

False. Overloading for each possible number of arguments is impractical compared to using a variable-length parameter list.

96
Q

How are variable-length parameters processed inside a method?

A

They are automatically put into an array, which can then be processed using a for-each loop.

97
Q

True or false?
“Variable-length parameter lists can accept any type of parameter, including primitives and objects.”

A

True.

98
Q

Where must the variable-length parameter be placed in the method signature?

A

The variable-length parameter must be the last in the list of formal arguments.

99
Q

True or false?
“A Java method can accept two sets of variable-length parameters”

A

False. A method cannot accept two sets of variable-length parameters.

100
Q

True or false?
“Constructors can be defined to accept a variable number of parameters.”

A

True. Constructors can also be set up to accept a variable number of parameters.

101
Q

What does method overloading rely on in Java?

A

Method overloading relies on the method signature, which includes the method name and parameter list but excludes the visibility and return type.

102
Q

True or false?
“You can overload a method in Java based on different return types alone. “

A

False. Method overloading cannot be based solely on different return types; the parameter list must differ. (異なる戻り値の型だけでメソッドをオーバーロードすることはできず、引数のリストが異なる必要があるということ)

103
Q

Why would you overload constructors in Java?

A

To allow the instantiation of a class different ways, each with its own set of parameters.

104
Q

True or false?
“Each overload constructor must ensure all instance variables are left uninitialized.(オーバーロードされた各コンストラクタはインスタンス変数を未初期化のままにしてはいけない)”

A

False. Each overload constructor must ensure all instance variables are set to reasonable values.

105
Q

Why is duplicated code in constructors considered an anti-pattern?

A

It leads to unnecessary repetition and increase the risk of errors and maintenace difficulty.

106
Q

How can you reduce duplication in overloaded constructors?

A

By passing arguments from supplementary constructors to a main constructor using this.

107
Q

True or false?
“Constructors can be private.”

A

True. Constructors can be private to prevent direct instantiation outside the class.

108
Q

Why might you make a constructor private in Java?

A

To control the instantiation of a class, such as limiting the number of instances.

109
Q

When a constructor is private, how can instances of the class be created?

A

Using a static factory method within the class that manages instance creation.

110
Q

What design pattern uses a private constructor and a static method to control instances?

A

The Singleton design pattern.

111
Q

True or false?
“The Singleton pattern allows for many instances of a class to be created.”

A

False. The Singleton pattern ensures only one instance of a class is created.

112
Q

Where is the Singleton pattern commonly used?

A

In scenarios like logging, driver objects, caching, and thread pools, where a single point of access is beneficial.

113
Q

True or false?
“The Singleton pattern is often used in Java as an alternative to global variables.”

A

True. Singletons can be used in place of global variables to ensure controlled access and instantiation.

114
Q

True or false?
“The numberFormat class in Java has public constructors that can be used to instantiate it.”

A

False. The NumberFormat class does not have public constructors.

115
Q

How can you instantiate a NumberFormat object?

A

By using its static factory methods, such as getCurrencyInstance() or getPercentInstance().

116
Q

True or false?
“The static methods of the NumberFormat class, like getCurrencyInstance(), create a new instance every time they are called.”

A

False. These methods check if an instance already exists and return it; if not, they create one and the return it.

117
Q

True or false?
“You can access getCurrencyInstance() or getPercentInstance() without instantiating the NumberFormat. class.”

A

True. because these methods are static, they can be accessed without instantiating the class first.

118
Q

What is the purpose of Javadoc comments in Java?

A

Javadoc comments are used to generate the official documentation of the code. They become the public API for any public class or method.

119
Q

True or false?
“Every public class in Java requires a Javadoc comment with a description, @author tag, and @version tag.”

A

True. Every public class should have these Javadoc elements.

120
Q

What should be included in a Javadoc comment for a public method?

A

A description of what the method does(with the first word as a verb in present tense), @param tags for each parameter, and a @return tag if there is a return value.

121
Q

True or false?
“Private methods and variables should also have Javadoc comments.”

A

False. Typically, private members don’t get Javadoc comments as they are not part of the pubic API.

122
Q

How many kinds of comments are ther in Java?

A

Three.
1. single-line comments(//)
2. multi-line comments(/* /)
3. Javadoc comments(/* */)

123
Q

True or false?
“Multi-line comments are only used for commenting out code.”

A

False. Multi-line comments can be used for explanations or commenting out code, but they are not limited to just code removal.

124
Q

How is the Java API organized since Java 9?

A

The Java API is organized into modules, with each module containing nested packages and related classes.

125
Q

True or false?
“The java.base contains optional APIs that need to be imported before use.”

A

False. The java.base module contains foundational APIs of the Java SE that are fundamental and do not need to be imported.

126
Q

What are some of the contents of the java.base module?

A

It includes java.lang for fundamental classes, java.math for Big numbers, java.text for handling text and dates, and java.util for collections and utilities like the Scanner class.

127
Q

True or false?
“All classes in the java.lang package must be explicitly imported.”

A

False. Classes in java.lang do not require an import statement to be used.

128
Q

True or false?
“To use java.util.Random, you need to declare and initialize an instance and then invoke one of its methods.”

A

True. That’s the process to generate random numbers using this class.

129
Q

True or false?
“You can generate pseudorandom numbers in Java without using java.util.Random.”

A

True. Other options like Math.random() or ThreadLocalRandom can also generate pseudorandom numbers.

130
Q

How many methods do you need to generate a random number with java.util.Random?

A

Just one. After initializing the Random object, you can call the method that fits the type of number you need(e.g. nextInt(), nextDouble()).

131
Q

True or false?
BigInteger can change after it is make.”

A

False. BigInteger is immutable.

132
Q

True or false?
“Floating point numbers always exact in Java.”

A

False. Floating point numbers are not always exact.

133
Q

True or false?
BigDecimal is used for big decimal numbers.”

A

True.

134
Q

True or false?
“You can use standard operators(+, -, *, /) with BigInteger.”

A

False. You must use methods like add and divide.

135
Q

Why do we format output in Java?

A

We format output to make it easy to read and consistent.

136
Q

What is java.util.Formatter used for?

A

It is for printf-style formatting.

137
Q

What is the problem with using the + operator for string concatenation in Java?

A

It is slow, especially when concatenating many strings, because it operates in quadratic time due to string immutability.

**Quadratic Time: The number of steps it takes to accomplish a task is square of n.

138
Q

True or false?
“Every time you concatenate strings with +, a new string is created.”

A

True.

139
Q

What should you use instead of + for efficient string concatenation?

A

Use StringBuilder to concatenate strings efficiently.

140
Q

True or false?
StringBuilder is recommended because it does not require copying the string contents every time you append.”

A

True.

141
Q

Besides concatenation, name one thing you can do with StringBuilder that you cannot do with the + operator.

A

StringBuilder can reverse the string with its reverse() method, among other functions.

142
Q

Why is StringBuilder more efficient than using the + operator for string manupulation?

A

Because StringBuilder modifies the existing string buffer instead of creating a new string with each operation, saving memory and time.