Classes and Inheritance

Last Updated :
Discuss
Comments

Question 1

What is the purpose of the constructor method in a class?

  • A

    To define static properties of a class

  • B

    To initialize object properties

  • C

    To inherit methods from another class

  • D

    To execute a class directly

Question 2

How do you define a static method in a class?

  • A

    Using the static keyword

  • B

    By defining the method inside the constructor

  • C

    Using the classMethod keyword

  • D

    By adding the method directly to the prototype

Question 3

Which keyword is used for inheritance in JavaScript classes?

  • A

    inherit

  • B

    extends

  • C

    prototype

  • D

    super

Question 4

What does the super keyword do?

  • A

    Calls the parent class's constructor

  • B

    Creates a new object in the class

  • C

    Defines a static method

  • D

    Overrides the parent class methods

Question 5

How do you define a private field in a class?

  • A

    By using private keyword

  • B

    By using _ before the field name

  • C

    By using # before the field name

  • D

    By declaring it inside the constructor

Question 6

Which of the following is an example of a Mixin?

  • A

    A function that adds methods to a class

  • B

    A class that extends another class

  • C

    A method inside a static class

  • D

    A constructor with parameters

Question 7

What will the following code log?

JavaScript
class Parent {
    greet() {
        return "Hello";
    }
}
class Child extends Parent { }
const c = new Child();
console.log(c.greet());


  • A

    Error

  • B

    Hello

  • C

    Undefined

  • D

    Greet is not a function

Question 8

What is a key difference between a method and a static method?

  • A

    Methods are bound to the instance, while static methods are bound to the class

  • B

    Static methods are always private

  • C

    Methods can only be defined inside the constructor

  • D

    Static methods can only access private fields

Question 9

Which statement about inheritance is correct?

  • A

    A class can extend multiple classes.

  • B

    A class can extend only one class.

  • C

    Inheritance is not possible in JavaScript.

  • D

    A class must use super to inherit properties but not methods.

Question 10

Which of the following statements about private fields is true?

  • A

    They are accessible by subclasses.

  • B

    They can only be declared using the # syntax.

  • C

    They can be accessed directly from outside the class.

  • D

    They can only be used in static methods.

There are 10 questions to complete.

Take a part in the ongoing discussion