Java Classes and Objects Study List Flashcards

1
Q

Describe inheritance

A

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviours from another class. It enables code reuse and the creation of hierarchical relationships between classes.

The subclass can access and extend the members (fields and methods) of the superclass.

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

Describe polymorphism

A

Polymorphism refers to the ability of an object to take on many forms. In Java, polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables code to work with objects in a more general way, increasing flexibility and modularity. Polymorphism is achieved through method overriding and method overloading.

Subclasses can be treated as if they are superclasses.

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

Describe encapsulation

A

Encapsulation is a principle of OOP that combines data and methods into a single unit called a class. It promotes the idea of hiding internal implementation details and providing a public interface to interact with the class. Encapsulation helps in achieving data protection, abstraction, and code maintainability by preventing direct access to data and enforcing access through methods (getters and setters).

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

Describe abstraction

A

Define the essential characteristics of an object while hiding the implementation details.

Abstraction can be achieved through abstract classes and interfaces in Java.

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

Describe the single responsibility principle

A

The Single Responsibility Principle (SRP) states that a class should have only one reason to change. It suggests that a class should have a single responsibility or purpose and should encapsulate only one aspect of functionality. By adhering to SRP, classes become more focused, maintainable, and reusable. It promotes better code organization and separation of concerns.

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

What is a class?

A

A class is a blueprint or template that defines the structure, behavior, and initial state of objects. It serves as a blueprint for creating multiple instances of objects.

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

What is an object?

A

An object is a specific instance of a class. It represents a real-world entity and has its own unique state and behaviour.

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

What is an accessor/getter?

A

An accessor or getter is a method in a class that provides access to the value of a private or protected instance variable. It allows other classes to retrieve the value of the variable without directly accessing it. Getters typically follow the naming convention getVariableName(), where “VariableName” is the name of the variable being accessed.

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

What is a mutator/setter?

A

A mutator or setter is a method in a class that allows the modification or assignment of a value to a private or protected instance variable. It provides a way to update the value of a variable while encapsulating the logic or constraints associated with the assignment. Setters typically follow the naming convention setVariableName(), where “VariableName” is the name of the variable being modified.

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

What is a constructor?

A

A constructor is a special method in a class that is used to initialize the object of that class. It is called automatically when an object is created using the “new” keyword. Constructors have the same name as the class and can have parameters to accept initial values for the object’s instance variables. Constructors are used to set up the initial state of an object.

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

What is shadowing? (not best practice).

A

Shadowing occurs when a variable in a nested scope (such as a method or block) has the same name as a variable in the enclosing scope. It leads to the overshadowing or hiding of the variable in the outer scope, making it inaccessible within the inner scope. Shadowing can cause confusion and should be avoided for better code readability.

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

What is a static method

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

What is scope of public access modifier

A

Public: Accessible from anywhere, both within and outside the class.

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

What is scope of private access modifier

A

Private: Accessible only within the same class. Not visible to other classes.

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

What is the scope of a class without an access modifier

A

Default (no explicit modifier): Accessible within the same package but not outside the package.

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

What is scope of protected access modifier

A

Protected: Accessible within the same class, subclasses, and other classes in the same package.

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

What is a static object

A

In Java, the terms “static object” and “non-static object” are not commonly used. However, we can discuss static members and non-static members within a class.

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

What is a static method

A

Static methods and variables belong to the class itself rather than individual instances of the class. They can be accessed without creating an object of the class.

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

What is a non-static method

A

Non-static methods and variables are associated with specific instances (objects) of the class and require an object to be accessed.

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

What does the abstract keyword mean?

A

The abstract keyword is used to declare abstract classes and methods in Java. An abstract class cannot be instantiated and serves as a blueprint for creating concrete subclasses.

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

How is the abstract keyword used in conjunction with classes?

A

Abstract methods are declared without a body and must be implemented in the subclasses. Abstract classes provide common behavior and attributes that can be inherited by multiple subclasses.

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

How is the collection class used?

A

The Collection class is a built-in class in Java that provides a framework for working with groups of objects. It is a high-level interface that defines common methods and behaviors for collections. Collection classes, such as ArrayList, HashSet, and HashMap, implement the Collection interface and provide specific implementations for storing and manipulating groups of objects.

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

What does it mean if a class is cohesive?

A

A cohesive class is a class that has a single, well-defined responsibility or purpose. It focuses on a specific area of functionality and encapsulates related data and methods. A cohesive class is highly focused and avoids unnecessary dependencies on other classes or responsibilities.

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

What is a local class?

A

A local class is a class defined within a block, such as a method or a constructor, within another class. It is only accessible within the block where it is defined and is often used for implementing callbacks or specific functionality within a limited scope.

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

What level of access to the properties and methods of the enclosing class does a static nested class have?

A

A static nested class has access to the public and protected properties and methods of the enclosing class.

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

Under what conditions can a local class access variables in the enclosing scope?

A

A local class can access variables in the enclosing scope if they are effectively final or declared as final.

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

Write a constructor for a Car class that takes action and reaction parameters.

A

public class Car {
private String action;
private String reaction;

public Car(String action, String reaction) {
    this.action = action;
    this.reaction = reaction;
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Write the getters and setters for your Dog class.

A

public class Dog {
private String name;
private int age;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Give an example of an interface.

A

public interface Drawable {
void draw();
}

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

Write an interface for airplane operations (turn left, climb, and so on). Create a class called AirBus787 that implements your interface.

A

public interface AirplaneOperations {
void turnLeft();
void turnRight();
void climb();
void descend();
}

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

Can you instantiate an interface?

A

Instantiating an Interface directly is not possible in Java. Interfaces are meant to be implemented by classes.

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

Compare and contrast the keywords extends and implements.

A

“Extends” is used to establish an inheritance relationship between classes. It allows a subclass to inherit the properties and behaviors of a superclass.
“Implements” is used to indicate that a class is implementing one or more interfaces. It defines the contract that the class must adhere to by implementing the methods specified in the interface.

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

When do you use the extends keyword

A

“Extends” is used to establish an inheritance relationship between classes. It allows a subclass to inherit the properties and behaviours of a superclass.

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

When do you use the implements keyword?

A

“Implements” is used to indicate that a class is implementing one or more interfaces. It defines the contract that the class must adhere to by implementing the methods specified in the interface.

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

If you have no access modifiers what is the default on your class?

A

If no access modifier is specified for a class, it has the default access level, which means it can be accessed within the same package but not from outside the package.

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

What is the base class of all classes?

A

The base class of all classes in Java is the Object class. All other classes in Java are either directly or indirectly derived from the Object class.

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

What is a List?

A

List is an interface that represents an ordered collection of elements. It allows duplicate elements and provides methods for accessing, modifying, and iterating over the elements.

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

What is an ArrayList?

A

ArrayList is a class that implements the List interface. It provides a dynamic array implementation of the List interface, allowing elements to be added, removed, and accessed by their index. ArrayList automatically adjusts its size as elements are added or removed.

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

What is a Set?

A

Set is an interface that represents a collection of unique elements with no defined ordering. It does not allow duplicate elements.

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

What is a HashSet?

A

HashSet is a class that implements the Set interface. It uses a hash table data structure to store elements, providing constant-time performance for basic operations like add, remove, and contains. HashSet does not guarantee the order of elements.

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

What is a Map?

A

Map is an interface that represents a mapping between keys and values. Each key in a Map is associated with a unique value, and keys are unique within a Map.

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

What is a HashMap?

A

HashMap is a class that implements the Map interface. It uses a hash table data structure to store key-value pairs, providing fast retrieval and insertion of elements. HashMap does not guarantee the order of keys or values.

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

What is the difference between derived and extended classes?

A

There is no specific distinction between “derived” and “extended” classes in Java. Both terms generally refer to classes that inherit from another class, either directly or indirectly.

44
Q

How does inheritance help achieve polymorphism?

A

Inheritance allows subclasses to inherit properties and behaviors from their superclass. This enables polymorphism, which means that objects of different classes can be treated as objects of a common superclass. Polymorphism allows for more flexibility in designing and using classes, as it allows objects to be used interchangeably based on their shared superclass behavior.

45
Q

What is an abstract base class?

A

An abstract base class is a class that is declared with the abstract keyword and cannot be instantiated. It is designed to be subclassed and serves as a common base for derived classes. Abstract base classes can define abstract methods (methods without implementation) that must be implemented by concrete subclasses.

46
Q

What is a superclass?

A

A superclass is a class that is higher in the inheritance hierarchy and from which other classes (subclasses) are derived. The superclass provides common attributes and behaviors that are inherited by its subclasses. Subclasses can add additional features or override inherited methods.

47
Q

What is a subclass?

A

A subclass is a class that is derived from a superclass. It inherits the properties and behaviors of the superclass and can extend or modify them as needed. Subclasses can add new attributes and methods, override inherited methods, or introduce new methods.

48
Q

How do you read from a file?

A

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader(“file.txt”))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

49
Q

How do you write to a file?

A

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(“file.txt”))) {
writer.write(“Hello, World!”);
writer.newLine();
writer.write(“This is a sample file.”);
} catch (IOException e) {
e.printStackTrace();
}
}
}

50
Q

What does it mean to be loosely coupled?

A

Loosely coupled refers to a design principle where components or modules in a system have minimal dependencies on each other. It promotes flexibility, modularity, and easier maintenance. Loosely coupled components can be modified, replaced, or tested independently without affecting other parts of the system.

51
Q

Why is it a good idea to split your applications into layers?

A

Splitting applications into layers is a common architectural approach that separates different concerns and responsibilities. Each layer has a specific purpose and interacts with adjacent layers through well-defined interfaces. This promotes modular design, code reusability, and easier maintenance.

52
Q

How do you use MVC as a design pattern in Java?

A

MVC (Model-View-Controller) is a design pattern that separates the application into three main components:

Model: Represents the data and business logic of the application.
View: Displays the user interface and interacts with the user.
Controller: Handles user input, updates the model, and manages the flow of data between the model and view.
MVC helps in achieving separation of concerns, promoting code organization, maintainability, and reusability.

53
Q

Categorize the following file into its MVC groups:
Supplier class

A

Supplier class: Model

54
Q

Categorize the following file into its MVC groups:
Game class

A

Game class: Model or Controller

55
Q

Categorize the following file into its MVC groups:
Class that maps URLs to actions

A

Class that maps URLs to actions: Controller

56
Q

Categorize the following file into its MVC groups:
List of actions you can do with the data

A

List of actions you can do with the data: Model

57
Q

Categorize the following file into its MVC groups:
Implementation of those actions you use in your app

A

Controller

58
Q

Categorize the following file into its MVC groups:
Data you use inside of your app

A

Model

59
Q

Categorize the following file into its MVC groups:
Business rules of your app

A

Model or Controller

60
Q

What is a Waterfall SDLC methodology?

A

A traditional sequential software development methodology where each phase (requirements, design, implementation, testing, deployment) is completed before moving to the next. It follows a top-down approach with minimal customer involvement and minimal flexibility for changes once a phase is completed.

61
Q

What does SDLC stand for?

A

Software Development Life Cycle

62
Q

What is the Agile SDLC methodology?

A

An iterative and incremental software development methodology that focuses on collaboration, adaptability, and customer involvement. It emphasizes frequent delivery of working software, continuous feedback, and flexibility to accommodate changes throughout the development process.

63
Q

What is a checked exception?

A

Checked exceptions are always subject to the “catch or specify” requirement.

Checked exceptions are exceptions that the compiler requires to be explicitly caught or declared in the method signature using the throws keyword. They typically represent recoverable conditions that a program can anticipate and handle.

64
Q

What is an unchecked exception?

A

Unchecked exceptions, also known as runtime exceptions, do not require explicit handling or declaration. They are usually caused by programming errors or unexpected conditions and represent unrecoverable situations. Unchecked exceptions can be caught and handled, but it is not mandatory.

65
Q

Explain how a try/catch/finally block of code works.

A

The try/catch/finally block is used to handle exceptions in Java. The try block contains the code that may throw an exception. The catch block catches and handles the exception, allowing the program to recover or take appropriate action. The finally block, if present, is executed regardless of whether an exception occurs or is caught. It is typically used to release resources or perform cleanup operations.

66
Q

When would you need to use the throws keyword?

A

The throws keyword is used in the method signature to indicate that a method may throw one or more exceptions. It specifies that the method is not handling the exceptions itself but will pass them to its caller to handle. The caller of the method must either catch the exception or declare it in its own method signature using the throws keyword.

67
Q

What are static members?

A

static members are associated with the class itself and can be accessed without creating an instance of the class

68
Q

What are non-static members?

A

non-static members belong to individual instances of the class and require an instance to be accessed.

69
Q

Code to access a static member

A

To access a static member, you can use the class name followed by the member name, like MyClass.staticVariable or MyClass.staticMethod().

70
Q

Code to access non-static member

A

To access a non-static member, you need to create an instance of the class and use that instance to access the member, like MyClass myObject = new MyClass(); myObject.nonStaticVariable or myObject.nonStaticMethod().

71
Q

What does static mean

A

1 per class

72
Q

What are collections

A

In Java, collections refer to objects that are used to store, manipulate, and organize groups of elements. They provide a way to efficiently manage and operate on groups of related objects.
Some commonly used collection interfaces in Java include List, Set, Queue, and Map.

73
Q

What are collection interfaces in Java?

A

List, Set, Queue, and Map.

74
Q

What are examples of implementations of the collection interfaces?

A

ArrayList, HashSet, LinkedList, and HashMap

75
Q

What are primitive types?

A

There are eight primitive types in Java:

byte: 8-bit integer value.
short: 16-bit integer value.
int: 32-bit integer value.
long: 64-bit integer value.
float: single-precision 32-bit floating-point value.
double: double-precision 64-bit floating-point value.
boolean: true or false
char: Represents a single Unicode character.

76
Q

What functionalities do Collections offer?

A

Adding, removing, searching, sorting, and iterating over elements. They can hold objects of any type, including primitives (through wrapper classes), and can dynamically resize to accommodate the number of elements.

77
Q

Is String a Primitive type?

A

No. String is a class that represents a sequence of characters.

78
Q

What is the Java Collections Framework?

A

provides a set of interfaces and classes that define various types of collections and their associated operations.

79
Q

What is a wrapper class?

A

it allows a primitive type to be used as an object in Java.

A class that wraps or encapsulates a primitive type and provides additional methods and functionality to work with that primitive type as an object.

80
Q

What is a functional interface?

A

A functional interface in Java is an interface that has exactly one abstract method. It serves as the foundation for lambda expressions and functional programming in Java.

81
Q

What are generics

A

Generics in Java are a way to create classes, interfaces, and methods that can work with different data types while maintaining type safety. They allow you to define classes and methods that are parameterized by one or more types.

Cannot use primitive type, need to use wrapper class instead

82
Q

What is the syntax for generics?

A

ArrayList<String> people = new ArrayList<String>();</String></String>

83
Q

What are Collections?

A

Collections are data structures that allow us to store multiple values with more flexibility than simple arrays provide.

84
Q

List some examples of collections

A

Collections usually represent a group of related data such as a class of students, a group of addresses, or a list of classes in a school schedule.

85
Q

What is an interface?

A

A contract.
A Java interface is a list of methods that must be implemented by any class that claims to fulfill that interface

86
Q

What are the 5 exception handling keywords?

A

Throw, throws, try, catch, finally

87
Q

Why handle exceptions?

A

predictable error conditions are expected and do not crash the program because additional code is written to handle these run-time errors.

88
Q

What is an exception?

A

An exceptional event which occurs in the normal execution of a program that disrupts the normal flow of the program’s instructions.

89
Q

What 2 categories of exceptions are there?

A

Checked and unchecked

90
Q

What are the 2 types of unchecked exceptions?

A

Errors and runtime exceptions

91
Q

What is method overloading?

A

when two methods share the same name but different parameter lists.

92
Q

How do you declare a java Interface?

A

With the interface keyword

93
Q

What are the benefits of inheritance?

A

Inheritance allows for more maintainable code because it cuts down on repeated code.

Inheritance allows child classes to reuse code written in parent classes.

94
Q

All interface variables are _______ by default in Java.

A

public, static, final

95
Q

A static method of an interface should be accessed with ______ and a dot operator?

A

the interface name

96
Q

What is composition?

A

Composition refers the fact that objects can be made up of other objects.

97
Q

What must you do when implementing a Java interface?

A

Provide implementations for all methods declared in the interface or mark the class as abstract.

98
Q

What does the ‘new’ keyword do?

A

When you use the new keyword followed by a class name and parentheses, Java allocates memory to create a new object of that class.

Person person = new Person();

99
Q

What is the key concept of Agile Development?

A

Build the simplest thing that could possibly work

100
Q

What are the phases of OpenUP SDLC?

A

Inception, Elaboration, Construction and Transition

101
Q

Which question is under the Elaboration phase of OpenUP SDLC?

A

Do we find that the value delivered so far and the remaining risk is acceptable?

102
Q

Which question is under the Construction phase of OpenUP SDLC?

A

Are we close to release? (This answer is unverified)

103
Q

What are quality strategies of Agile?

A

Continuous improvement, continuous reviews
Short, fixed-length feedback cycles
Dynamic code analysis
The project goals are defined and adaptable.

104
Q

What level of focus is reserved for Iteration lifecycle?

A

Team focus

105
Q

What are disadvantages of the Agile model?

A

There is not always a clear vision of a “final project.”
New requirements may conflict with the existing architecture.
Documentation is limited because it is usually “just in time.”

106
Q

SDLC Context: What is expected within a single monthly iteration?

A

Stable weekly build