curriculum/challenges/english/blocks/quiz-javascript-classes/67358ac128957c865dcf3ddf.md
To pass the quiz, you must correctly answer at least 18 of the 20 questions below.
What is the purpose of a class in JavaScript?
To define only properties, not methods.
To define static variables only.
To organize only primitive data.
To create reusable blueprints for objects.
How do you define a class in JavaScript?
Using the className keyword.
Using the object keyword.
Using the constructor keyword only.
Using the class keyword.
Which method is used in a class to initialize properties?
static
init
define
constructor
What does this represent inside a class constructor?
The global class object.
The function itself.
An undefined reference.
An instance of the class.
Which of the following describes how this behaves in an arrow function inside a class method?
It inherits the value of this from the nearest function in a different scope.
It refers to a newly created instance of the class.
It inherits the value of this from the global object in strict mode.
It inherits the value of this from the enclosing scope where it is defined.
What is the primary purpose of the extends keyword?
To define static methods in a parent class constructor.
To create a method in a class.
To initialize default properties in the parent class.
To create a subclass that inherits from a parent class.
Which of the following is an example of how to create a Car class that inherits from a Vehicle class?
class Car uses Vehicle {}
class Car = Vehicle {}
class Car inherits Vehicle {}
class Car extends Vehicle {}
What is the output of the following code?
class Animal {
speak() {
return "Animal speaks";
}
}
class Dog extends Animal {
speak() {
return "Dog barks";
}
}
const myDog = new Dog();
console.log(myDog.speak());
Animal speaks
An error is raised.
undefined
Dog barks
What is the function of the super keyword in a subclass?
To define a new instance of the parent class.
To delete methods and properties from the parent class.
To access only the static methods of the parent class.
To call the constructor or methods of the parent class.
Which of the following describes a static method?
A method that is inaccessible inside the class itself.
A method that is used only in arrow functions.
A method that is unique to each instance of a class.
A method that belongs to the class itself, not to any instance.
What will the following code output?
class Calculator {
static add(a, b) {
return a + b;
}
}
console.log(Calculator.add(3, 4));
An error is raised.
undefined
NaN
7
How would you access a static property in JavaScript class?
By using this.propertyName within a method.
By using instance.propertyName after creating an instance.
By using super.propertyName within a subclass.
By using ClassName.propertyName.
In which scenario would you use a static method over an instance method?
When you need to access and modify the instance's properties.
When the method needs to be called on an object instance.
When the method doesn't rely on instance-specific data and operates on class-level data.
When the method operates on data that is shared by all instances of the class.
What will the following code output?
class MyClass {
sayHello() {
return "Hello!";
}
}
console.log(MyClass.sayHello());
Hello!
null
undefined
It throws an error.
What keyword is used to inherit properties and methods from another class?
import
inherit
static
extends
What is printed by the following code?
class Person {
static species = "Human";
}
console.log(Person.species);
undefined
null
An error is raised.
Human
Which of the following best describes inheritance?
It allows a method to return a new object.
It allows an instance to define its own properties.
It allows a static property to be converted into a non-static property.
It allows a class to acquire properties and methods from another class.
What happens if a constructor is not defined in a subclass?
An error will be thrown.
The subclass will inherit the constructor from any class in the code.
The subclass cannot create new instances.
The superclass's constructor will be used by default.
What does this represent in the following code?
class Animal {
constructor(name) {
this.name = name;
}
}
The global object.
The Animal class itself.
An undefined variable.
The instance of Animal.
What is the primary use of static properties?
To define default properties for each class instance.
To override methods in a subclass.
To define instance-specific data.
To store class-wide data shared across instances.