21.) What is the output of the following code?
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a noise.`;
}
}
class Dog extends Animal {
speak() {
return `${this.name} barks.`;
}
}
const dog = new Dog("Rex");
console.log(dog.speak());
22.) What is the result of this code?
class Alpha {
constructor() {
this.alpha = "A";
}
}
class Beta extends Alpha {
constructor() {
super();
this.beta = "B";
}
}
const obj = new Beta();
console.log(obj.alpha, obj.beta);
23.) What is the output of the following code?
class Example {
static greet() { return "Hello!"; }
}
console.log(Example.greet());
24.) What is the result of the following code?
class Parent {
constructor() {
this.value = 10;
}
getValue() {
return this.value;
}
}
class Child extends Parent {
constructor() {
super();
this.value = 20;
}
}
const child = new Child();
console.log(child.getValue());
25.) What is the output of this code?
class Example {
constructor() {
this.data = 42;
}
}
Example.prototype.data = 100;
const ex = new Example();
delete ex.data;
console.log(ex.data);
26.) What is the output of the following code?
class Base {
constructor() {
this.value = 10;
}
}
class Derived extends Base {
constructor() {
super();
this.value += 5;
}
}
const obj = new Derived();
console.log(obj.value);
27.) What does this code output?
class Tool {
constructor() {
this.name = "tool";
}
static describe() {
return "This is a tool.";
}
}
const hammer = new Tool();
console.log(hammer.describe());
28.) What is the output of the following code?
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
return `Hello, ${this.name}`;
};
const person = new Person("Alice");
console.log(person.greet());
29.) What is the output of the following code?
class Counter {
constructor() {
this.count = 0;
}
increment() {
return ++this.count;
}
}
const counter1 = new Counter();
const counter2 = new Counter();
counter1.increment();
counter2.increment();
console.log(counter1.count, counter2.count);
30.) What is the output of the following code?
class Example {
static logMessage() {
console.log("Static method called!");
}
}
const ex = new Example();
ex.logMessage();