L4 + L5 - Defining Classes Flashcards

1
Q

What is the central concept in Java that makes object-oriented programming (OOP) possible?

A

Classes: Classes define types and form the basis for creating objects, methods, and overall structure of Java programs.

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

How does Java programming typically organize software components?

A

Java programming consists of defining a number of classes. Each programmer-defined type in Java is implemented as a class, and even built in classes (like String and Scanner) are used to build software

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

What is the role of a class in Java?

A

A class serves as a blueprint for creating objects. It defines the properties (fields) and behaviors (methods) that the objects instantiated from the class will have.

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

Example of Predefined classes:

A

String and Scanner

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

What does it mean when we say “a class is a type” in Java?

A

It means that classes in Java are not only blueprints for objects but also serve as data types. When you create an object from a class, you’re creating an instance of that type.

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

How are programmer-defined types created in Java?

A

They are created by defining new classes. Every new type that a programmer defines in expressed as a class, which then can be used to instantiate objects.

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

What is the significance of classes in the structure of a Java program?

A

Classes are fundamental because they encapsulate data and behavior, making it possible to organize, reuse, and modularize code effectively. All software components can be thought of as collections of classes.

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

What is encapsulation?

A

The bundling of data (variables) and methods that operate on that data into a single unit, typically a class, while restricting direct access to some of the object’s details.

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

Describe the connection between objects and classes.

A

Objects are instances of classes. While a class is the abstract definition or blueprint, an object is a concrete instance that occupies memory and has state and behavior defined by its class.

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

Why is learning to define your own classes important for Java programming?

A

It allows you to create custom types that model real-world entities and problems. Defining your own classes is essential for building structured, modular, and maintainable programs.

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

What is one of the key differences between using a predefined class and user-defined class in Java?

A

With predefined classes, you use the functionality provided by Java (like String or Scanner), whereas with user-defined classes, you design and implement your own structures tailored to your specific application needs.

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

What is a class in Java?

A

A programmer-defined type that serves as a blueprint for creating objects, defining their attributes and behaviors

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

What are the two key components of a class definition?

A

Instance variables (fields) and methods.

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

How do you create an object in Java?

A

Using the ‘new’ operator. Example:

Classname obj - new Classname();

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

What is an instance variable?

A

A variable defined in a class that holds data for an object

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

How do you access an instance variable?

A

objectName.instanceVar;

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

What are the two types of methods in Java?

A

Void methods - Perform an action but do not return a value.

Return methods - Compute and return a value.

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

How do you invoke a method on an object?

A

Example:

objectName.methodName();

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

What is the purpose of the main method in Java?

A

It serves as the entry point of a Java program and has the signature:

public static void main(String[] args)

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

What is a constructor?

A

A special method used to initialize objects when they are created.

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

What are they key characteristics of a constructor?

A
  • Has the same name as the class
  • Does not have a return type
  • Often overloaded to provide multiple initialization options
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What happens if you do not define a constructor?

A

Java provides a default no-argument constructor.

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

What is the ‘this’ keyword used for?

A

Refers to the calling object and is used to differentiate instance variables from parameters.

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

What are accessor and mutator methods?

A

Accessor Methods - ( get methods ) retrieve instance variable values.

Mutator Methods ( set methods) modify instance variable values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What are 'public' and 'private' access modifiers?
'public' - Accessible from anywhere 'private' - Only accessible within the class
26
What is method overloading?
Defining multiple methods with the same name but different parameter lists.
27
What is the return statement used for?
It ends a method and specifies the value to return.
28
What is the difference between local and instance variables?
Local variables - are declared inside methods and exist only within the method. Instance variables - are defined in the class and exist for the lifetime of an object.
29
What is a formal parameter vs. an actual parameter?
Formal parameters - are declared in the method definition. Actual parameters - values passed to the method during invocation.
30
What is an abstract data type (ADT)?
A data type that hides its implementation details and only exposes a well-defined interface.
31
What is a static method?
A method that belongs to a class and can be used without an object.
32
How do you declare a static method?
Use the 'static' keyword in the method header. Example: public static int myMethod(int x) { return x * 2; }
33
How do you call a static method?
Use the class name instead of an object: int result = MyClass.myMethod(5);
34
Can a static method invoke an instance method?
No. A static method cannot access instance variables or methods because it has not 'this'
35
What is a static variable?
A variable shared by all objects of a class. Only one copy exists. Example: private static in count = 0;
36
How do you declare a static constant?
Use 'public static final' Example: public static final int MAX_VALUE = 100;
37
What is special about the Math class?
It contains static mathematical methods and constants. Like Math.PI
38
How do you generate a random number between 0.0 and 1.0?
Use: double randomNum = Math.random();
39
What are wrapper classes?
Wrapper classes provide a way to use primitive data types as objects. (e.g., Integer, Double, char)
40
What is automatic boxing and unboxing?
Boxing: Converting a primitive into an object automatically. Example: Integer num = 10; //auto-boxing Unboxing: Converting an object into a primitive automatically. Example: int x = num;
41
What are two types of memory in a computer?
1. Main Memory (RAM) - Stores variables during execution. 2. Secondary memory (Disk) - Stores files permanently.
42
How is a class-type variable stored?
It stores a reference (memory address), not the actual object.
43
What does 'null' represent in Java?
A reference that does not point to any object.
44
What does 'new' do in Java?
It creates an object and returns its memory reference.
45
What is an anonymous object?
An object that is created but not assigned to a variable. Example: new myClass().someMethod();
46
What is a copy constructor?
A constructor that creates a new object as a copy of another object.
47
What is the difference between deep and shallow copies?
Shallow copy - Copies references (not independent objects) Deep copy - Creates new independent objects.
48
How can privacy leaks occur in classes?
Returning mutable instance variables directly instead of creating copies.
49
What is an immutable class?
A class whose objects cannot be changed after creation (e.g., String)
50
Why override the equals method?
To compare object contents instead of memory addresses.
51
What does 'toString()' do?
Converts an object into a readable string representation.
52
What is a package?
A collection of related classes grouped in a directory.
53
How do you import a package in Java?
import java.util.Scanner;
54
What happens if no package is declared?
The class belongs to the default package.
55
How do you document a class using javadoc?
Use special comments: /** * @param name The name of the user * @return A greeting message */
56
How do you generate documentation for a class?
Run: javadoc myClass.java
57
What is a class invariant?
A condition that is always true for a valid object.
58
What does CLASSPATH do?
Specifies where Java looks for user-defined packages.
59
What is the difference between '==' and equals()?
- '==' checks memory addresses - equals() checks content.
60
What happens when you assign one object reference to another using '='?
Both variables point to the same object, meaning changes to one affect the other. Example: person p1 = new Person("Alice"); person p2 = p1; //p2 now refers to the same object as p1
61
Why should you not use '==' to compare objects?
'==' checks memory addresses, not content. Instead, use the equals() method. Example: String s1 = new String("Hello"); String s2 = new String("Hello"); System.out.println(s1 == s2); // false (different objects) System.out.println(s1.equals(s2)); // true (same content)
62
How do assignment operators behave with class-type variables?
They copy the reference, not the actual object. Both references point to the same memory location. Example: Person p1 = new Person("Bob"); Person p2 = p1; // Both p1 and p2 now refer to the same person.
63
What is the key difference between passing primitive and class-type parameters?
Primitive Types - A copy of the value is passed (modifications do not affect the original) Class Types - A copy of the reference is passed (modification affect the original object). Example: void modifyPrimitive(int x) { x = 10; } // No effect on original variable void modifyObject(Person p) { p.setName("John"); } // Changes original object.
64
What makes a class mutable or immutable?
Immutable class: No methods can modify instance variables after creation (e.g., String) Mutable class: provides methods that change internal state.