Classes and Objects Flashcards

1
Q

What is an Abstract Data Type (ADT)?

A

An ADT is a high-level data type that defines a set of values and a set of operations that can be performed on those values. It is an abstraction that hides the implementation details of a data structure, allowing users to work with the data in a more intuitive way.

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

What is encapsulation in the context of ADTs?

A

Encapsulation is the process of hiding the implementation details of an ADT from the user. This allows the user to work with the ADT at a high level, without having to worry about how it is implemented

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

How does an ADT consist of one or more classes?

A

An ADT can be implemented as a class or a group of classes that work together to provide the desired functionality. The classes define the data that is stored and the operations that can be performed on that data.

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

What are the parts of an ADT?

A

An ADT has two main parts: the data that is stored and the operations that can be performed on that data. The data is defined as a set of values, while the operations are defined as a set of functions or methods that can be called on those values.

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

What is the purpose of an ADT?

A

The purpose of an ADT is to provide a high-level interface for working with complex data structures. By encapsulating the details of the data structure, an ADT acts as a wall between the software component and the outside world, allowing users to work with the data in a more intuitive way.

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

How can an ADT be modelled in Java?

A

An ADT can be modelled in Java as an interface. The interface defines the methods that can be called on the data, but does not provide any implementation details.

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

What is the relationship between an ADT and a data structure?

A

An ADT is a high-level abstraction that defines the behavior of a data structure, while a data structure is a concrete implementation of that behavior. In other words, an ADT defines what operations can be performed on the data, while a data structure defines how those operations are implemented.

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

What is Object-Oriented Design (OOD)?

A

OOD is a programming paradigm that uses objects to model real-world entities and their relationships. It focuses on encapsulating data and behavior within objects and promoting code reusability through inheritance and polymorphism.

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

What is a class in OOD?

A

In OOD, a class is a blueprint for creating objects. It describes the attributes (data) that the object will contain and the methods (behavior) that the object will exhibit. A class can be thought of as a template for creating objects.

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

What are the parts of a class in OOD?

A

The two main parts of a class in OOD are attributes and methods. Attributes are the data that is stored by the class, while methods are the operations that can be performed on that data.

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

What is a data structure in OOD?

A

In OOD, a data structure is a way of organizing and storing data in a computer program. We can use one or more classes to construct a data structure.

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

What is an object in OOD?

A

An object is an instance of a class. It contains its own set of values for the attributes defined by the class, and can execute the methods defined by the class.

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

What is the difference between a class and an object in OOD?

A

A class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines the attributes and methods that an object will have, while an object contains the specific values for those attributes and can execute the methods

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

What are some examples of data types that can be stored in a class in OOD?

A

Some examples of data types that can be stored in a class include integers (int), characters (char), strings (String), booleans (boolean), and floating point numbers (float or double).

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

What is the benefit of using OOD in software development?

A

: OOD promotes code reusability, which can save time and effort in software development. It also allows for better organization and management of complex programs, as well as easier maintenance and modification of existing code.

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

What are attributes in object-oriented programming?

A

Attributes are the data stored in an object. They describe the characteristics of an object and provide a way to store and retrieve data. Attributes are defined in a class and can be accessed by objects of that class.

17
Q

What is the syntax for defining an attribute in Java?

A

In Java, attributes are defined using the following syntax:
access_modifier data_type attribute_name;

For example:
private String title;

18
Q

What is the purpose of access modifiers in Java attributes?

A

Access modifiers in Java attributes control the visibility and accessibility of attributes. The four access modifiers in Java are private, protected, public, and default (also known as package-private).

19
Q

What are behaviors in object-oriented programming?

A

Behaviors are the operations that can be performed on objects. They define what an object can do or how it can be manipulated. Behaviors are also defined in a class and can be called by objects of that class.

20
Q

What is the syntax for defining a behavior in Java?

A

In Java, behaviors are defined using the following syntax:
access_modifier return_type method_name(parameter_list) { method_body }

For example:
public void play() { System.out.println(“Playing…”); }

21
Q

What is the purpose of access modifiers in Java behaviors?

A

Access modifiers in Java behaviors control the visibility and accessibility of behaviors. The four access modifiers in Java are private, protected, public, and default (also known as package-private).

22
Q

What is the difference between attributes and behaviors in object-oriented programming?

A

Attributes describe the data stored in an object, while behaviors describe the operations that can be performed on objects. Attributes are accessed using getter and setter methods, while behaviors are called directly by objects.

23
Q

Can a behavior modify an attribute in an object?

A

Yes, a behavior can modify an attribute in an object. In fact, anything that modifies an attribute is considered a behavior. For example, a setter method is a behavior that modifies an attribute

24
Q

What is a constructor in object-oriented programming?

A

A constructor is a special method that is used to create and initialize objects. It is called automatically when an object is created and is used to set the initial values of the object’s attributes.

25
Q

What is the syntax for defining a constructor in Java?

A

In Java, a constructor is defined using the following syntax:
access_modifier class_name (parameter_list) { constructor_body }

For example:
public DVD (String title) { this.title = title; }

26
Q

What is the purpose of the “this” keyword in Java constructors?

A

The “this” keyword is used in Java constructors to refer to the current object being created. It is used to distinguish between the parameter passed to the constructor and the attribute of the object.

27
Q

Can a class have multiple constructors?

A

Yes, a class can have multiple constructors with different parameter lists. This is called constructor overloading and allows objects to be created with different initializations.

28
Q

What is the default constructor in Java?

A

The default constructor is a constructor that takes no parameters and is automatically generated by Java if no constructor is defined in a class. It simply initializes all the attributes to their default values

29
Q

How do you call one constructor from another in Java?

A

In Java, one constructor can call another constructor using the “this” keyword followed by the parameter list of the constructor being called. This is called constructor chaining.

For example:
public DVD (String title) { this(title, 0); }
public DVD (String title, int runningTime) { this.title = title; this.runningTime = runningTime; }

30
Q

What is the difference between a constructor and a method in Java?

A

A constructor is a special method used to create and initialize objects, while a method is a regular function that operates on an object. Constructors have the same name as the class and do not have a return type, while methods can have any name and can return a value.

31
Q

What does it mean for an attribute or method to be public in Java?

A

A public attribute or method in Java can be accessed by any other class, regardless of its package or location in the code.

32
Q

What does it mean for an attribute or method to be private in Java?

A

A private attribute or method in Java can only be accessed within the same class in which it is defined. It is not visible to any other classes, including subclasses.

33
Q

What is the benefit of using private attributes and methods in Java?

A

Encapsulation is the primary benefit of using private attributes and methods in Java. By hiding implementation details and restricting access to certain parts of the code, private attributes and methods can help make the code more secure, maintainable, and easier to modify.

34
Q

What is the difference between a static attribute and a non-static attribute in Java?

A

A static attribute in Java is associated with the class itself, rather than with instances of the class. There is only one copy of the static attribute, which is shared by all instances of the class. A non-static attribute is associated with each individual instance of the class and has its own copy for each instance.

35
Q

What is the benefit of using static attributes and methods in Java?

A

Static attributes and methods in Java can be used to share data and functionality across all instances of a class, without the need to create additional instances or pass data between them. This can help reduce memory usage and simplify the code.

36
Q

When should you use public attributes and methods in Java?

A

Public attributes and methods in Java should be used when you want to expose certain parts of your code to external classes or APIs. For example, if you are designing a library or framework that will be used by other developers, you may want to make some of your classes’ methods and attributes public so that they can be easily accessed and used.

37
Q

When should you use static attributes and methods in Java?

A

Static attributes and methods in Java should be used when you want to share data or functionality across multiple instances of a class, or when you want to provide utility methods that do not require an instance of the class to be created. However, it’s important to use static attributes and methods judiciously, as overuse can lead to code that is difficult to understand and maintain.

38
Q

Why is a reference needed to change the value of an object in a non-static method?

A

In a non-static method, the method is associated with a particular instance of the class, rather than with the class itself. To modify the state of the object associated with the method, a reference to that object is needed. This reference is typically accessed using the this keyword in Java.