Module 7: Objects and Classes Flashcards

(119 cards)

1
Q

What is Object-Oriented Programming (OOP)?

A

A technology for developing reusable software using objects

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

Why use OOP?

A

To develop large-scale software and GUIs effectively through reusable components

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

What is an object?

A

An entity in the real world that can be distinctly identified, with unique identity, state, and behavior

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

What is the state of an object?

A

Properties or attributes represented by data fields with their current values

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

What is the behavior of an object?

A

Actions defined by methods that the object can perform

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

What are data fields?

A

Variables that represent the properties/state of an object

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

What is a class?

A

A template, blueprint, or contract that defines what an object’s data fields and methods will be

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

What is an instance?

A

An object created from a class

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

What is instantiation?

A

The process of creating an instance/object from a class

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

What is a constructor?

A

A special method invoked to create a new object, designed to perform initializing actions

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

What is the relationship between classes and objects?

A

A class is a template; objects are instances created from that template (like recipe vs pies)

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

What is UML?

A

Unified Modeling Language - a notation for representing classes and objects in diagrams

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

What is a class diagram?

A

A UML diagram showing class name, data fields, constructors, and methods

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

How are data fields denoted in UML?

A

dataFieldName: dataFieldType

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

How are constructors denoted in UML?

A

ClassName(parameterName: parameterType)

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

How are methods denoted in UML?

A

methodName(parameterName: parameterType): returnType

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

What is a main class?

A

A class that contains the main method and can be run

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

Can a class without a main method be run?

A

No, it’s merely a definition for objects

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

What is a client of a class?

A

A program that uses the class to create and manipulate objects

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

What is a public class?

A

A class declared with public modifier, accessible from any other class

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

How many public classes can be in one file?

A

Only one, and it must have the same name as the file

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

What files are generated when compiling a Java file with multiple classes?

A

A separate .class file for each class in the source file

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

What are the three peculiarities of constructors?

A

1) Same name as class 2) No return type 3) Invoked using new operator

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

What is a no-arg constructor?

A

A constructor without arguments, often providing default values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is a default constructor?
A public no-arg constructor with empty body, provided automatically if no constructors are defined
26
When does Java provide a default constructor?
Only when no constructors are explicitly defined in the class
27
What is the syntax for creating an object?
new ClassName(arguments);
28
What does the new operator do?
Creates an object by allocating memory and invoking a constructor
29
Can constructors be overloaded?
Yes, a class can have multiple constructors with different signatures
30
What is wrong with: public void Circle() {}
It's a method, not a constructor (constructors have no return type, not even void)
31
What is a reference variable?
A variable that contains a reference to an object (not the object itself)
32
What is a reference type?
A class type where variables reference instances rather than holding values directly
33
What is the syntax for declaring a reference variable?
ClassName objectRefVar;
34
How do you create and assign an object in one statement?
ClassName objectRefVar = new ClassName();
35
What is the difference between object reference variable and object?
Reference variable contains a reference to object; they're different but distinction often ignored
36
Are arrays treated as objects in Java?
Yes, arrays are created with new and array variables are reference variables
37
What is the dot operator?
The . operator used to access an object's data fields and methods
38
What is an instance variable?
A data field dependent on a specific instance of a class
39
What is an instance method?
A method that must be invoked on a specific instance of a class
40
What is a calling object?
The object on which an instance method is invoked
41
How do you access an object's data field?
objectRefVar.dataField
42
How do you invoke an object's method?
objectRefVar.method(arguments)
43
Can you invoke instance methods using ClassName.methodName()?
No, instance methods must be invoked on an object using objectRefVar.methodName()
44
What is an anonymous object?
An object created without being assigned to a variable, like: new Circle(5).getArea()
45
What is null?
A special Java literal value indicating a reference type doesn't reference any object
46
Is null a keyword?
No, but it is a reserved word
47
What is the default value for reference type data fields?
null
48
What is the default value for numeric data fields?
49
What is the default value for boolean data fields?
false
50
What is the default value for char data fields?
'\u0000'
51
Do local variables have default values?
No, local variables must be initialized before use
52
What is NullPointerException?
A runtime error that occurs when invoking a method on a reference variable with null value
53
What does a primitive type variable hold?
The actual value of the primitive type
54
What does a reference type variable hold?
A reference to where the object is stored in memory
55
What happens when you assign one primitive variable to another?
The value is copied from one variable to the other
56
What happens when you assign one reference variable to another?
The reference is copied, making both variables point to the same object
57
What is garbage?
An object that is no longer referenced by any variable
58
What is garbage collection?
Automatic process where JVM detects and reclaims memory from unreferenced objects
59
Where are objects stored in memory?
In the heap
60
What is the heap?
Area of memory used for dynamic memory allocation where objects are stored
61
What is the java.util.Date class used for?
Encapsulating date and time in a system-independent way
62
How do you create a Date for current time?
new java.util.Date()
63
What does Date.getTime() return?
Elapsed time in milliseconds since January 1, 1970 GMT
64
What is the java.util.Random class used for?
Generating random int, long, double, float, and boolean values
65
What is a seed in Random class?
A number used to initialize the random number generator
66
What happens with same seed in Random?
Two Random objects with same seed generate identical sequences
67
What is javafx.geometry.Point2D used for?
Representing a point in a two-dimensional plane with x and y coordinates
68
What is a static variable?
A variable shared by all objects of the class, stored in common memory location (class variable)
69
What is a static method?
A method that can be called without creating an instance of the class
70
How do you declare a static variable?
static dataType variableName;
71
How do you declare a static method?
static returnType methodName(parameters)
72
How are static members shown in UML?
Underlined
73
Should constants be declared as static?
Yes, constants should be declared as final static since they're shared
74
Can static methods access instance variables?
No, static methods cannot access instance variables or invoke instance methods
75
Can instance methods access static variables?
Yes, instance methods can access both instance and static members
76
How do you call a static method?
ClassName.methodName(arguments) or through object reference (not recommended)
77
What is the best practice for calling static methods?
Use ClassName.methodName() to improve readability
78
What is package-private or package-access?
Default visibility when no modifier is used - accessible within same package only
79
What does the private modifier do?
Makes methods and data fields accessible only from within the class
80
What does the public modifier do?
Makes classes, methods, and data fields accessible from any other class
81
Can local variables use public or private modifiers?
No, these modifiers apply only to class members
82
When should a constructor be private?
When you want to prohibit users from creating instances (like Math class)
83
What is data field encapsulation?
Declaring data fields as private to prevent direct modification
84
Why encapsulate data fields?
1) Prevent data tampering 2) Make class easier to maintain
85
What is a getter method?
A method that returns the value of a private data field (accessor)
86
What is a setter method?
A method that sets a new value for a private data field (mutator)
87
What is an accessor?
Another name for a getter method
88
What is a mutator?
Another name for a setter method
89
What is the signature for a getter method?
public returnType getPropertyName()
90
What is the signature for boolean getter?
public boolean isPropertyName()
91
What is the signature for a setter method?
public void setPropertyName(dataType propertyValue)
92
What does + mean in UML class diagram?
Public modifier
93
What does - mean in UML class diagram?
Private modifier
94
Should data fields be private?
Yes, as a design guideline to prevent tampering and ease maintenance
95
Should constructors and methods be public?
Yes, unless specified otherwise (default practice)
96
What is pass-by-value?
Java's argument passing mode where the value is passed to method
97
What happens when passing primitive type to method?
The value is copied; changes inside method don't affect original
98
What happens when passing object to method?
The reference is copied; both refer to same object (pass-by-sharing)
99
What is pass-by-sharing?
Semantic description: object in method is same as object passed
100
If you pass an object and modify its fields, does it affect original?
Yes, because both reference variables point to the same object
101
What is an array of objects?
An array where each element is a reference variable that can point to an object
102
What is the default value of array elements for objects?
null
103
How do you initialize an array of objects?
Create array, then use loop to create object for each element
104
What is an immutable object?
An object whose contents cannot be changed once created
105
What is an immutable class?
A class designed so its objects cannot be modified after creation
106
What are requirements for immutable class?
1) All data fields private 2) No mutator methods 3) No accessor returns mutable reference
107
Is String class immutable?
Yes
108
If class has all private fields and no setters, is it immutable?
Not necessarily - accessors might return references to mutable objects
109
What is the scope of instance variables?
The entire class, regardless of where declared
110
What is the scope of static variables?
The entire class, regardless of where declared
111
What is the scope of local variables?
Only within the method or block where declared
112
Can class variables appear in any order?
Yes, except when one field's initialization depends on another
113
What happens if local variable has same name as instance variable?
Local variable takes precedence; instance variable is hidden
114
What is a hidden variable?
An instance variable that has same name as local variable and is shadowed
115
What is the this keyword?
A reference to the calling object (the object on which method is invoked)
116
When is this keyword required?
To reference data field hidden by parameter, or invoke overloaded constructor
117
Can you use this to call instance methods?
Yes, but it's usually omitted for brevity: this.method() vs method()
118
What is the purpose of this in constructors?
Can invoke another constructor of same class: this(arguments)
119
Is using this keyword for instance members required?
No, but some prefer it to clearly distinguish instance variables from local variables