week03 Abstraction&relationaships&testing Flashcards

1
Q

What is OOP?

A

OOP is a style of programming that models the real world. It uses objects and classes to create reusable code.

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

True or false?
“OOP only uses Encapsulation and Inheritance as its main techniques”

A

False. OOP uses Modularity, Abstraction, Encapsulation, Inheritance, and Polymorphism.

polymorphism: ある1つのメソッドを呼び出しに対して、オブジェクト毎に異なる機能や動作を示すこと。多態性。

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

What does modularity mean in OOP?

A

Modularity refers to dividing a program into separate modules that can be developed and tested independently.

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

Why is encapsulation is important in OOP?

A

Encapsulation protects object integrity by hiding its internal state and requiring all interaction to be performed through its methods.

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

What is inheritance in OOP?

A

Inheritance allows a new class to adopt the properties and methods an existing class.

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

True or false?

“Polymorphism in OOP means that a single function can work in many different ways”

A

True. Polymorphism allows methods to do different things based on the object they are acting upon

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

What are the benefits of using OOP?

A

OOP makes code more flexible, easier to maintain, and resusable, leading to efficient and effective programming practices.

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

What does coupling in mean in Java?

A

Coupling is how closely different parts of a program work together. We want less couling to make changes easier.

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

What is cohesion in Java?

A

Cohesion is when parts of a program that belong together are kept together. High cohesion makes a program more strong and organized.

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

True or false?

“We want maximum cohesion in our Java programs”

A

True.

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

Why maximum independence important in java?

A

Independence means each part can work on its own. This makes the progra more flexible and easier to fix.

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

What is composition in OOP?

A

Composition is when a class includes objects from other classes as part of its structure. It’s ‘has-a’ relationship.

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

True or false?
“A unidirectional association means both classes know about each other”

A

False. In a unidirectional association, only one class contains a reference to the other.

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

True or false?
“In a bidirectional association, both objects contain references to one another.”

A

True.

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

What is the Law of Demeter?

A

It is a rule for writing software. It says objects should only talk to close friends, not strangers. This means a class should only know limited things about other classes. It should talk only to its direct friends.

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

What does making a parameter final do?

A

It does not let you change the parameter’s value inside the method.

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

How to write symbolic constants in Java?

A

Use final keyword. Write class-level constants in ALL_CAPS and local constatnts in camelCase.

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

True or false?
“Assign a value to a symbolic constant when it’s declared.”

A

True. A symbolic constant should get its value immediately when declared.

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

When to assign value to a final instance variable?

A

Assign during object construction if it’s meant to only set once.

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

True or false?
“Class-level constants should be static and final for memory efficiency”

A

True. Static means it’s class-wide, final means it’s unchangeable. It’s memory efficient.

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

Where does the equals method come from?

A

The equals method is inherited from the Object class, which is the parent class for all Java classes. Every class you create automatically has this method. (, and it’s a good practice to override it for proper equality comparison based on the class’s fields.)

22
Q

What are the three methods from Object that are commonly overridden?

A
  1. toString(): gives a string representation of the object.
  2. equals(Object obj): checks if this object is equal to another object.
    3.hashCode(): provides the hash code of the object, which should be consistent with equals.
23
Q

True or false?
“Without overriding, equals checks if two object are the exact same object.”

A

True. The default implementation of equals in the Object class uses == to determine if two references point to the same object in memory, not if they are logically equal.

24
Q

True or false?
“A student object can access another Student’s private fields.”

A

True. Within the equals method, you can access the private fields of another object of the same class directly. This is because the scope of private access control is within the same class, not just the same object.

25
Q

What does hashCode() do in Java?

A

hashCode() is a method that gives a number for an object. This number comes from a hash function. This function takes data of any size and gives back a fixed size number. The idea is to make a unique number for different object states. If two objects are the same, they should have the same hash number.

26
Q

What is hashing?

A

Hashing is when you use a function to find the right index for an item in a list or table. It is much faster than seraching every place if you know the index. To use hashing in Java, hashCode() must work well with equals().

27
Q

Why are bad hash fucntions a problem?

A

A bad hash function gives the same number for different objects. This is called a collision. Collisions make things slow because they need extra work to find the right object.

28
Q

True or false?
“You can use this in a static method”

A

False. Static methods belong to a class, so it doesn’t know what instance is invoked, which means it doesn’t understand what this is.

29
Q

True or false?
“Hashing always gives a unique result for each object”

A

False. Hasing tries to give unique results but sometimes gives the same number for different objects. This is why we have to deal with collisions.

30
Q

What is an enum in Java?

A

An enum is a special type in Java used to define collections of constants. It is typesafe, meaning you can use it to declare variables that can only take one of the predefined constants. Enums are a clean way to group together related constants and namage them.

31
Q

What are key properties of enums in Java?

A

Enums have named constants that are public, static, and final. Java does not allow you to create new enum instances because constructors are private. Enums are always static, so we access enum constants with the enum type name.

32
Q

When should you use enums?

A

Use enums when you know all possible values of a variable at compile time, like days of the week. For example, if you have an enum Day with values MONDAY, TUESDAY, etc., it’s impossible to add a new day ike FUNDAY. This makes the code safer and more predictable because it limits the values to a known, fixed set, preventing errors like assigning an invalid value to a variable of the enum type.

33
Q

What are static variables in Java?

A

Static variables belong to the class, not any instatnce. All instance share the same static variable. They are set up when the class is first used and stay the same for every instance. Use the static keyword to create them.

34
Q

True or false?
“Static methods can use non-static variables”

A

False. Static methods cannot use non-static variables. They don’t have this, so they can’t access instance variables.

35
Q

True or false?
“Static variables can be constants.”

A

True. Static variables can be constants if they are final. They can be pulic or private. A public static final variable is a constant that can be used anywhere, like Math.PI.

36
Q

When do you use static methods?

A

Use static methods when the method doesn’t need to access instance variables. They are good for actions that don’t change the state of an object, like Math.max();

37
Q

What are static factory methods?

A

Static factory methods are methods that create and return instances of a class. They can have descriptive names, reuse instances, return subtypes, and vary return types based on input, offering more flexibility than constructors.

38
Q

What are some downsides of static factory methods?

A

They can be hard to tell apart from other static methods. Also, classes without public constructors can’t be subclassed, which can limit how you use them.

39
Q

True or false?
“You can always use == for comparing double values if they look the same.”

A

False. Even if two double values look the same, their underlying binary representation might not match exactly due to floating-point precision.Use compare method in Double class.

40
Q

What does the compare method in the Double class do?

A

The compare method in the Double class evaluates two double values. It returns a negative value if the first double is smaller than the second, zero if both doubles are equal. and a positive value if the first double is larger than the second.

41
Q

What does type-safe mean for enumerations in Java?

A

It means the set of constants in an enum is fixed and know at compile time. New values cannot be added at runtime, ensuring only predefined values are used.

42
Q

Why are enums considered type-safe in Java?

A

Enums are type-safe because they limit the values to a predefined set know at compile time, preventing the use of invalid or unexpected values.

43
Q

What is a static initialization block in Java?

A

A static initialization block is a code block that executes once when the class is loaded into the JVM, used for initializing static variables or performing class-specific setup operations.

44
Q

Why is a static initialization block used in Java?

A

It’s used for complex initializations of static variables or to execute setup actions that need to happen before the class is first used.

45
Q

What is unidirectional association?

A

A one-way relationship where one class know about or interacts with another class, but not vice versa.

46
Q

What is bidirectional association?

A

Both classes are aware of each other and can interact mutually. This means that each class has a reference to the other, allowing both to access each other’s properties or methods.

47
Q

What does dependency mean in Java?

A

In Java, a dependency occurs when a class uses another class’s methods, variables, or data types, implying reliance on that class for functionality.

48
Q

What is delegation in Java?

A

Delegation is a design pattern where an object hands over its responsibilities to another object to perform specific tasks or functionalities.

49
Q

Why is delegation used in Java?

A

Delegation is used for decoupling objects, promoting code reusability and flexibility, and achieving separation of concerns.

50
Q

How does delegation differ from inheritance in Java?

A

Unlike inheritance where a subclass inherits behavior from a parent class, in delegation, an object relies on another object to perform certain tasks, without inheriting from it.