Deck Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What does it mean if it says “Extend” in the code?

A

It means it’s inheritance.

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

What is the general nature of an object? [2]

A

An object is an abstract identity;
And it’s components are data;

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

What are two disadvantages of OOP? [4]

A

OOP are larger than other programs;
Thus slower;

Unsuitable for minor/small projects;
As OOP increases complexity for little gain;

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

Modularity (concept)

A

When you break down a system into modular components, you can work on specific parts separately. This approach is known as modular development or modular programming.

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

Outline one advantage of modularity [2]

A

Faster development;
Since different programming teams can work together on different modules;

Easier to debug;
Because the smaller modules will have fewer mistakes than one big program;

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

Relationships between classes (definition concept)

A

In Object-Oriented Programming (OOP), there are several types of relationships between classes. Let’s break them down in easy terms:

Association:

Easy Explanation: Think of association as a connection between two classes. It’s like saying one class knows about another.
Example: A Person class might be associated with an Address class, meaning a person has an address.

Aggregation:

Easy Explanation: Aggregation is a special type of association where one class is part of another class.
Example: A University class may have many Student classes. The students are part of the university, but they can exist independently.

Composition:

Easy Explanation: Composition is a strong form of aggregation where the child class is so dependent on the parent class that it cannot exist independently.
Example: An Engine class is composed within a Car class. If the car is destroyed, the engine is gone too.
Inheritance:

Easy Explanation: Inheritance is like saying one class is a specialized version of another class. It allows a class to inherit attributes and behaviours from another class.
Example: A Dog class could inherit from an Animal class. The dog is a type of animal and shares some common characteristics.

Dependency:

Easy Explanation: Dependency occurs when one class relies on another class, but changes in the dependent class don’t necessarily affect the class that depends on it.
Example: A Car class might depend on a Fuel Tank class. If you change how the fuel tank works, it doesn’t necessarily change the car’s basic functionality.

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

Relationships in OOP (in code)

A

Association:
public class Person {
String name;

public Person(String name) {
    this.name = name;
} }

public class Address {
String street;
String city;

public Address(String street, String city) {
    this.street = street;
    this.city = city;
} }

// Association
public class AssociationExample {
public static void main(String[] args) {
Person person = new Person(“John”);
Address address = new Address(“123 Main St”, “Cityville”);

    person.address = address;  // Person knows about Address
} } Aggregation:

import java.util.ArrayList;
import java.util.List;

public class University {
String name;
List<Student> students = new ArrayList<>(); // Aggregation</Student>

public University(String name) {
    this.name = name;
} }

public class Student {
int studentId;
String name;

public Student(int studentId, String name) {
    this.studentId = studentId;
    this.name = name;
} }

// Aggregation
public class AggregationExample {
public static void main(String[] args) {
University university = new University(“ABC University”);
Student student1 = new Student(1, “Alice”);
Student student2 = new Student(2, “Bob”);

    university.students.add(student1);
    university.students.add(student2);
} } Composition:

public class Engine {
public void start() {
System.out.println(“Engine started”);
}
}

public class Car {
Engine engine = new Engine(); // Composition

public void start() {
    engine.start();
} }

// Composition
public class CompositionExample {
public static void main(String[] args) {
Car car = new Car();
car.start(); // Starting the car also starts its engine
}
}
Inheritance:

public class Animal {
String species;

public Animal(String species) {
    this.species = species;
}

public void makeSound() {
    // Abstract method
} }

public class Dog extends Animal {
public Dog(String species) {
super(species);
}

@Override
public void makeSound() {
    System.out.println("Woof!");
} }

// Inheritance
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog(“Canine”);
System.out.println(dog.species); // Accessing inherited attribute
dog.makeSound(); // Accessing overridden method
}
}
Dependency:

public class FuelTank {
public void refill() {
System.out.println(“Fuel tank refilled”);
}
}

public class Car {
FuelTank fuelTank; // Dependency

public Car(FuelTank fuelTank) {
    this.fuelTank = fuelTank;
}

public void drive() {
    System.out.println("Car is driving");
} }

// Dependency
public class DependencyExample {
public static void main(String[] args) {
FuelTank fuelTank = new FuelTank();
Car car = new Car(fuelTank);
car.drive(); // Car depends on FuelTank for driving
fuelTank.refill(); // Changes to FuelTank don’t affect Car
}
}

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

Outline one advantage of binary search [1]

A

Binary search is much faster than sequential search;
Because it halves the search range for every comparison;

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

Outline one disadvantage of binary search [1]

A

Not applicable to unsorted data;

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

Outline the use of data hiding as a use of a security feature in OOP [2]

A

Encapsulation allows making instance variables of a class private;

So that other classes can’t access accidentally;

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

Outline why having more than one method with the same name does not cause a conflict? [2]

A

Because they both have different parameters;
Allowing the compiler to choose the right one;

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

What is the name for this type of method: why having more than one method with the same name does not cause a conflict? [1]

A

Polymorphism;

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

Define the term method signature [2]

A

The method name and the type of all of its parameters;

Is the unique identifiers (concept)

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

Outline an advantage of using a library [2]

A

Convenience;
Because implementations of common tasks are available;

Reliability;
Because these implementations are fully developed, functional and robust;

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

Advantages and disadvantages of dynamic linked lists and static arrays.

A

check one note

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

Code that instantiates array “line” with 20 cart objects [2]

A

private Cart[] line = new Cart([20];

Concept:
Imagine a Shelf:

Think of your array like a shelf where you want to store some items. The type of items you want to store is like the type of things you want to put on the shelf.
Specify the Shelf:

Say out loud what type of items your shelf will hold. For example, “I want a shelf for Cart items.” In Java, this sounds like Cart[].
Name the Shelf:

Give your shelf a name. For example, “I’ll call it ‘line’.” In Java, this becomes Cart[] line.
Get a New Shelf:

Use the word “new” to ask for a brand new shelf. In Java, it’s new.
Tell the Size of the Shelf:

Specify how many items your shelf can hold. For example, “I want it to hold 20 Cart items.” In Java, this is done with square brackets, like new Cart[20].
Put the Shelf in its Place:

Finally, tell Java where to put your shelf. Connect it to the name you chose earlier. For example, “Put the ‘line’ shelf where I can access it.” In Java, this is Cart[] line = new Cart[20];.

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

Distinguish between a class and an instantiation [2]

A

A class is the template of an object; OR the blue print of an object;

Is the creation of the actual object;

18
Q

What is “this.” used in java for?

A

this is a keyword in Java used to refer to the current object. In this context, this.line[i] means the line array of the current object

19
Q

Define the term inheritance [2]

A

A new class is derived from an existing class;
The new class inherits all data of the other class;

20
Q

How to use flags? (concept)

A

Use flags as true or false, and make sure to type them as booleans;

Also make sure to put them at the end of each loop as not true or if(flag) #checks if true

To skip an element

21
Q

Define the term object reference [2]

A

A pointer to a memory location;
Where the object is stored;

22
Q

How do you find the first object in linked list?

A

Use the head; eg. head.getNex()

As the head always points towards the first object.

23
Q

How do you traverse through linked list?

A

// Loop until reaching the nth node (no need to check for null)
while (i < n) {
prev = curr; // Save the current node as the previous node
curr = curr.getNext(); // Move to the next node
i++;
}

24
Q

Explain the importance of using coding style and naming conventions when programming [4]

A

Indentation;
Annotations;

Helps understanding;
For easier maintenance;

25
Q

Outline one effect of using the modifier static when declaring a variable [2]

A

Static means it is the same instances for all classes;
Static means that less memory is taken up;

26
Q

Why is the keyword “this.” used in a class?

A

To distinguish between 2 variables with the same name;
Allows the local variable to be set to the class variable;

27
Q

How to create an instance of a class?

A

[Class name] [Variable name] = new class name;

eg.
Car myCar = new Car(“Toyota”, “Camry”, 2022);

28
Q

How to use a method?

A

To use a method write the variable name and then a “.” and then the method, and in brackets write the class . whatever the action is.

29
Q

Outline two differences bwtween inheritance and aggregation

A

Inheritance is when the behaviour of a class is transferred to another class;

whereas aggregation is when a class is contained inside another and used;

Aggregation is useful for modelling a real life situation of containment aka. “has a” (eg
contents of a box);

whereas Inheritance is useful for modelling real life situation that one entity is a particular
(more specific) type of another entity, aka. “is a”;

While both can achieve the same result, Aggregation (composition) can lead to more robust
and safer code;

whereas inheritance is a more flexible solution but prone to errors due to the behaviour of
subclasses being changed;

Inheritance allows for code reuse / lowers maintenance cost;

whereas aggregation does neither of the above;

Can have more than 1 aggregation;

but only 1 superclass (in Java);

Inheritance allows for code reuse as in the creation of sub-classes;

whereas with aggregation each class must be written out in full;

30
Q

Advantages of OOP [4]

A

Reuse objects across sub-groups;
Which will speed up development;

Quicker to test;
As each module is small/independent;

Work can be split up across the sub-groups;
Which will speed up development;

31
Q

Outline the consequence of having the accessor and mutator methods declared as protected [2]

A

Access is restricted to the same package;

32
Q

How can a binary tree be utilized for a name to be found more quickly [5]

A

A (binary) tree will be used:

Set up in order of routeCode;

When a search is made the tree is searched via the root;

Moving to the left/right accordingly / eliminating ; the tree with each comparison;
Until there is a match between the route codes;

The corresponding route name is then retumed;

33
Q

How can inheritance be used [4]

A

The responses should focus on these aspects, with keywords underlined:

e Several sub-classes can inherit characteristics from a superciass;

e Whilst adding specific characteristics particular to themselves;
Examples can then expand or be described to include the features above.
For example:
There could be a “GUI” superclass that contains the main elements for the Uls
with the GUI for each screen inheriting from its parent;
For example:
An “Employee” superclass that allows the different types of employees to inherit
standard characteristics;

34
Q

How to declare a linked list

A

The line of code private LinkedList<Sales> salesHistory; declares an instance variable named salesHistory within a class. Let's break down what this line is doing:</Sales>

Access Modifier:

private: This is an access modifier that restricts access to the salesHistory variable to only within the class where it is declared. It cannot be directly accessed from outside the class.
Data Type:

LinkedList<Sales>: This specifies the data type of the variable. It's declaring salesHistory to be an object of the LinkedList class, specifically a linked list that holds objects of type Sales.
Variable Name:</Sales>

salesHistory: This is the name of the variable. It’s the reference to the actual linked list object that will be created based on this declaration.

35
Q

Different modifiers

A

Public - allows access to variables from outside the class;

Final - prevents variables from being modified;

Static - refers to variables that act on the class as a whole;

36
Q

OOP features

A
37
Q

Identify two essential features of a recursive algorithm [2]

A

The method calls itself;
With a changing paremeter;
There is a base case;

38
Q

Define the term encapsulation

A

Encapsulation refers to the practice of hiding the structure and representation of data within a class and making it accessible outside that class via accessor function

39
Q

Identify the features of an ADT (abstract data type)

A

No implementation details are known;
The actions are standard

40
Q

How to get the last element in a linked list (coding concept)

A

public E getLast() throws Exception
{
// Throws an Exception if the List is empty
if (head == null) {
throw new Exception(
“No elements found in Linked List”);
}

    Node<E> temp = head;
 
    // The while loop takes us to the tail of the Linked
    // List
    while (temp.next != null) {
        temp = temp.next;
    }
 
    // Returns the last element
    return temp.data;
} }
41
Q
A