Programing Concepts Flashcards

1
Q

Polymorphism

A

Polymorphism in programming refers to the ability of a single function, method, or operator to work with different types of data or objects. In JavaScript, polymorphism can be achieved through method overloading, function overloading, or through the use of interfaces and inheritance.
Overloading method:
function add(x, y) {
if (typeof x === ‘number’ && typeof y === ‘number’) {
return x + y;
} else if (typeof x === ‘string’ && typeof y === ‘string’) {
return x.concat(y);
}
}

console.log(add(5, 10)); // Outputs: 15
console.log(add(‘Hello’, ‘ World’)); // Outputs: Hello World

inheritance method:
class Shape {
area() {
return ‘Shape area’;
}
}

class Circle extends Shape {
constructor(radius) {
super();
this.radius = radius;
}

area() {
    return Math.PI * Math.pow(this.radius, 2);
} }

class Rectangle extends Shape {
constructor(width, height) {
super();
this.width = width;
this.height = height;
}

area() {
    return this.width * this.height;
} }

const circle = new Circle(5);
const rectangle = new Rectangle(4, 6);

console.log(circle.area()); // Outputs: 78.53981633974483
console.log(rectangle.area()); // Outputs: 24

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

Heaps

A

There are mean heaps and max heaps. Max heap is the one that the highest value is the root and each parent is bigger than its children. In Min Heap, parent’s value is lower than the children’s value. Insertion in heap has two methods and starts from top to button and then left to right. first method is bubble-up in which we add the new element to the most bottom right space and the it bubbles up, meaning it is swapped with parent if it is higher than it in Max Heap or lower than parent if it is a Min Heap. Second method is Sink-down in which we remove the root or the highest priority element and replace it with its children until it is in the right place.

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