Learn Object-Oriented Programming in JavaScript Educative Quiz Answers

Get Learn Object-Oriented Programming in JavaScript Educative Quiz Answers

Learn to write cleaner, more modular, and more scalable code in JavaScript by gaining mastery of Object Oriented Programming (OOP).

You’ll start with the basics of object-oriented programming and build up to more advanced concepts such as prototypal inheritance, prototype chaining, method overriding and mixins. Along the way, you’ll learn how each concept applies to JavaScript’s sometimes unique way of handling things. You’ll also see how various JavaScript features make it particularly convenient for OOP, with particular emphasis on changes introduced in ES6.

This course is filled with hands-on coding challenges, playgrounds, snippets, and illustrations to keep things interactive.

Enroll on Educative

Quiz 1: Introduction

Q1. Object-oriented programming is a tool used to break a program into segments of objects that can communicate with each other.

  • True
  • False

Q2. OOP in JavaScript is exactly the same as OOP in C++, Java and C#.

  • True
  • False

Q3. OOP in JavaScript follows prototype-based programming.

  • True
  • False

Q4. Prototype-based programming involves making a class that acts as a blueprint with all the properties encapsulated within it.

  • True
  • False

Q5. Class-based programming is more flexible and transparent than Prototyped-based programming.

  • True
  • False

Q6. Which of the following is not true for the ES6 version?

  • class keyword is used in order to define a class
  • extends keyword is used for inheritance
  • constructor functions are declared using thefunction keyword.
  • static keyword is used to declare static members

Quiz 2: Objects in JavaScript

Q1. An object’s set of properties include data values and functions.

  • True
  • False

Q2. What does the create() method do?

  • Uses an object constructor to make an object
  • Uses figure brackets to make the object
  • Creates an object based on an existing object
  • Uses square brackets to make an object

Q3.

const human = {name : 'Adam', age : 28}

In the above code, the value of name and age cannot be updated. Is this statement true or false?

  • True
  • False

Q4. What is the error in the following code:

var human = {
            
      name : 'Penny',
      age : 32
}
console.log(human[name])
  • Object declaration is wrong
  • Round brackets instead of square brackets should be used when accessing the name property
  • name should be written as a string while accessing i.e., it should be human['name']
  • Dot notation should be used instead of using square brackets

Q5. A function can be called using the square brackets in the following way:

var human = {
            
      name : 'Penny',
      age : 32,
      display() {
      console.log("Name is penny and age is 32")
      }
}
human['display']()
  • True
  • False

Q6. Properties, including both data values and functions, can be deleted or removed from an object using the delete operator.

  • True
  • False

Q7. What should be the return statement in the display function?

var human = {
            
      name : 'Penny',
      display() {
      //function returns the name of the human
      }
}
  • Option 1
return name
  • Option 2
return human.name
  • Option 3
return this.name
  • Option 4
return

Q8. get and set keywords allow the function to be accessed as a property.

  • True
  • False

Quiz 3: Functions as Objects

Q1. All objects created from a constructor function contain all the properties in it?

  • True
  • False

Q2. Point out the error in the code below:

function Shape(name, sides) {
  name: name
  sides: sides
}
var square = new Shape('Square', 4)
  • Constructor function declaration is incorrect
  • The syntax to assign properties is wrong
  • The new keyword should not be used to create a new object instance
  • All of the above

Q3. Code in the constructor function executes every time a new object is instantiated.

  • True
  • False

Q4. this refers to the new object created and is used to set its property values.

  • True
  • False

Q5. What will be the result of the following code?

function Shape(name, sides){
  this.name = name
  this.sides = sides
}

Shape.length = 4
var shape1 = new Shape('Square', 4)
console.log("Length is:", shape1.length)
  • Option 1
Length is: 0
  • Option 2
Length is: 4
  • Option 3
Length is: undefined
  • Option 4
None of the above

Q6. Properties/methods added to the prototype object of the constructor function cannot be inherited by the objects created from it.

  • True
  • False

Q7. Declaring properties using the var keyword makes them accessible outside the constructor function.

  • True
  • False

OOP in JS – Exam 1

Q1. Which of the following features were introduced in the ES6 version of JavaScript?

  • function and class
  • class and extends
  • object and extends
  • function and object

Q2. Given the following code, which of the following statements is correct?

const house = {
    bedrooms : 3, 
    washrooms : 2, 
    basement : 'no',
}
  • “house” is a property consisting of the objects “bedrooms”, “washrooms”, and “basement”
  • “house” is an object consisting of the methods “bedrooms”, “washrooms”, and “basement”
  • “house” is an object consisting of the properties “bedrooms”, “washrooms”, and “basement”

Q3. The following code shows an incorrect way to create an object in JavaScript. What is wrong about the syntax?

var game = {
            players = 2
            level = "hard"
           }
  • The “game” object should be defined as “Game”
  • The properties, players and level, should be separated by a comma
  • There should be a : sign instead of the = sign to define the properties
  • Both options B & C

Q4. What will be the output of the following code?

var dress = {
  size: "large",
  color: "pink"
}

var gown = Object.create(dress);
gown.color = "red";
console.log(gown.size);
console.log(gown.color);
  • large pink
  • large red
  • large undefined
  • Code will not run

Q5. Given the code below, which methods will change the coat property’s value to 'blue'?

var clothes = {
      coat : "red"
}

clothes.coat = 'blue';    //method 1
clothes.coat.red = 'blue'; //method 2
clothes["coat"] = 'blue';  //method 3
clothes["coat"]["red"] = 'blue'; //method 4
  • methods 1 and method 2
  • methods 2 and method 4
  • methods 1,2, and 3
  • methods 1 and 3
  • methods 1,2,3, and 4

Q6. Given the code below, which option correctly shows the output displayed on the console?

var shape = {
     sides: 4
}
console.log(shape.color);
  • null
  • undefined
  • 0
  • Error

Q7. What will be the output of the following code?

var rectangle = {
       length : 4,
        width : 3,
        area : function() {
          var recArea = length * width;
          return recArea;
    }
}
console.log(rectangle.area());
  • 12
  • undefined
  • null
  • ReferenceError

Q8. Study the code given below, which option correctly shows the output?

var op = {
    arr : [2,4,8,9],
    get last(){
        return this.arr[this.arr.length - 1];
    },
    set lastVal(val){
        this.arr[this.arr.length - 1] = val;
    }
}
op.lastVal = 15;
console.log(op.last);
  • 9
  • 15
  • undefined
  • null

Q9. How is a constructor function created in JavaScript?

  • A function named “constructor” is a constructor function
  • A function defined using the keyword “constructor” is a constructor function
  • A function preceded by the “new” keyword upon invocation acts as a constructor function
  • Constructor functions can’t be created in JavaScript

Q10. What will be the output of the following code?

function Ticket(from, to){
    this.from = from;
    this.to = to;
    display(){
    console.log(`Your flight is from ${this.from} to ${this.to}`);
  }
}
var flight1 = Ticket("Toronto", "California");
flight1.display();
  • Your flight is from Toronto to California
  • Your flight is from undefined to undefined
  • Your flight is from null to null
  • Cannot read property ‘display’ of undefined

Q11. What will be the output of the following code:

function Name(first,middle,last){
       this.first = first;
       this.middle = middle;
       this.last = last;
}
var person = new Name("Kimmy", "Susi", "Schmidt");
Name.prefix = "Ms";
console.log("The prefix is", person.prefix);
  • The prefix is Ms
  • The prefix is undefined
  • The prefix is null
  • TypeError : Cannot read property ‘prefix’ of undefined

Q12. When a function is used a constructor function, the new objects created from it share its _____ .

  • object
  • constructor
  • prototype object
  • methods

Q13. What will be the output of the following code?

function Food(cuisine, price){
  this.cuisine = cuisine;
  this.price = price;
}

var dish1 = new Food("Indian",10);
Food.prototype.available = 'yes'; 
var dish2 = new Food("Italian",15)
console.log(dish1.available);
console.log(dish2.available);
  • yes yes
  • yes no
  • no yes
  • no no

Q14. The ES5 version of JavaScript allowed the use of both static and super keywords.

  • True
  • False

Q15. In JavaScript, functions cannot have their own properties and methods.

  • True
  • False

Q16. The code on the left side shows the correct way to access a property using square brackets.

var icecream = {
  flavor: "chocolate",
  toppings : {
               sprinkles: "no",
               oreos : "yes"
             }
}
console.log(icecream[toppings][oreos]);
  • True
  • False

Q17. Match the terms in the left column with their respective definitions on the right side:

  • internal property
  • prototype property
  • constructor functions
  • delete operator
  • functions used to construct
  • used to delete or remove properties from an object
  • hidden property inside every object created from a constructor function
  • a property or method that can only be accessed by methods present inside the constructor function

Answer:

  • internal property –
  • prototype property –
  • constructor functions –
  • delete operator –

Q18. In this coding exercise, you are asked to create a Parallelogram constructor function with the base and height properties.

It should also have a method printArea that calculates and prints the parallelogram area.

For example, if a parallelogram has a base of 4 and a height of 5 then, calling the function printArea should print the parallelogram’s area, which would be 20.

Sample input
var parallelogram = new Parallelogram(4,5);
parallelogram.printArea();
Sample output
20
Solution:

Q19. In this coding challenge, you are given a constructor function Employee with a method info defined on its prototype. This method displays the employee’s name and the company in which they work.

If you look at the code on the side, you’ll see that the company property is not defined. That has been done on purpose. It is for you to decide how you want to define it.

In this coding challenge, you must write a specific code that displays the corresponding result mentioned in the coding widget on the right.

Solution:

Quiz 4: Classes

Q1. The constructor is used to initialize the properties of the class.

  • True
  • False

Q2. The class declaration creates a function as an end result.

  • True
  • False

Q3. What is the error in the following code?

class Shape{
  constructor(name,sides,length){
    this.name = name,
    this.sides = sides,
    this.length = length
  }
  displayName() {
  return this.name
  },
  displaySides(){
  return this.sides
  }
}
var shape1 = new Shape('Square',4,10)
  • The class declaration should be as class shape = { } instead
  • this should be replaced with var in the body of the constructor
  • A comma cannot be used to separate the members of a class
  • displayName function cannot access name directly

Q4. Methods defined inside the class are assigned to the prototype of the class.

  • True
  • False

Q5. Methods inside the class that are assigned to the prototype of the class are known as static methods.

  • True
  • False

Q6. Static methods cannot be inherited by the objects of the class.

  • True
  • False

Q7. What is the error in the following code?

class Shape{
  constructor(name,sides,length){
    this.name = name,
    this.sides = sides,
    this.length = length
  }
  static displayName() {
  return this.name
  }
}
var shape1 = new Shape('Square',4,10)
shape1.displayName()
  • Class declaration is incorrect.
  • var should be used instead of this in the constructor
  • Properties in the constructor should be separated by commas
  • The function displayName should be called from the class Shape instead of the object shape1.

Quiz 5: Inheritance

Q1. If we create 50 Animal object instances, how many definitions of method getName and property name will we have?

function Animal(name,age ) {
    this.name = name
    this.age = age
}
Animal.prototype.getName = function() {
    return this.name
}
Animal.prototype.getAge = function() {
    return this.age
}
  • getName: 1 , name: 1
  • getName: 1, name: 50
  • getName: 50, name: 1
  • getName: 50, name: 50

Q2. Whenever a property is to be found it is first searched in the object and then the prototype chain is traversed.

  • True
  • False

Q3. Which name will be displayed in the code below?

function Animal(name,age ) {
    this.name = name
    this.age = age
}
Animal.prototype.name = 'Roofy'
var animal = new Animal('Kitty',2)
console.log(animal.name)
  • Roofy
  • Kitty
  • Both of them
  • None of them

Q4. Object.prototype always points to another object.

  • True
  • False

Q5. The __proto__ property is used as a setter/getter for the [[Prototype]] property.

  • True
  • False

Q6. call is used to invoke a method defined anywhere in the current context.

  • True
  • False

Q7. super keyword is used to invoke child class methods and constructors.

  • True
  • False

Q8. In JavaScript, inheritance is implemented through mixins.

  • True
  • False

OOP in JS – Exam 2:

Q1. Which of the following statements describes the role of the constructor?

  • It creates a function
  • It creates a class
  • It initializes the properties of a constructor function
  • It is used to initialize the properties of a class

Q2. Given the code below, which of the following options correctly states the issue in it?

class Shape{
    this.name = name
    this.sides = sides
    this.color = color 
}

var shapeObj = new Shape("Rectangle", 4, "Green");
console.log(`Name is ${shapeObj.name}, sides are ${shapeObj.sides} and color is ${shapeObj.color}`);
  • Inside the class definition, the colon sign should be used instead of the equals sign to define the properties
  • A class object cannot be instantiated with the “new” keyword
  • The constructor is missing inside which the properties need to be defined
  • The properties should be separated by a comma

Q3. What will be the output of the following code?

class Vehicle {
  constructor(name,type,color) {
    var _name = name;
    this.type = type;
    this.color = color;
  }
}
var carObj = new Vehicle("Honda", "Car", "Black");
console.log(carObj._name);
console.log(carObj.type);
console.log(carObj.color);
  • Honda Car Black
  • undefined Car Black
  • undefined undefined undefined
  • Error

Q4. Study the code below, which of the given options is the correct prefix for the greaterNumber function?

class Number {
  constructor(type, sign, value) {
    this.type = type;
    this.sign = sign;
    this.value = value;
  }
  ____ greaterNumber(number1, number2) {
    if (number1.value > number2.value) {
      console.log(`${number1.value} is greater`);
    } else {
      console.log(`${number2.value} is greater`);
    }
  }
}

var number1 = new Number("integer", "positive", 4);
var number2 = new Number("integer", "negative", -3);
Number.greaterNumber(number1, number2);
  • function
  • constructor
  • class
  • static

Q5. What does line 10 in the following code translate to?

1  var Vehicle = {
2   name: 'Car'
3  }
4
5  var Car = {
6   name: "Honda",
7   color: "Blue"
8  } 

10  Car.__proto__ = Vehicle; 
  • Car is the prototype of Vehicle
  • Vehicle is the prototype of Car
  • Vehicle inherits prototypically from Car
  • Vehicle’s [[Prototype]] property is equal to Car

Q6. Given the code below, which of the following options is correct about line 11?

1  var Product = {
2   name : 'Dairy Queen'
3  }
4
5  var IceCream = {
6   price : 15,
7   flavor :'chocolate'
8  }
9
10 IceCream.__proto__ = Product;
11 console.log(IceCream.name); 
  • In the prototypal chain formed, first the IceCream object is searched where the “name” property is found and returned
  • In the prototypal chain formed, first the Product object is searched where the “name” property is found and returned
  • In the prototypal chain formed, the IceCream object is first searched where the “name” property is not found. Next in chain, its prototype, the Product object, is searched where the “name” property is found and returned
  • In the prototypal chain formed, the IceCream object is searched where the “name” property is not found so “null” is returned

Q7. Given the code below, what output is displayed when the ring function is invoked for the bell object?

function Bell() {
  this.ring = function() {
    return 'tring tring';
  };
}

const bell = new Bell();

Bell.prototype.ring = function() {
  return 'ring ring';
};

console.log(bell.ring());
  • tring tring
  • ring ring
  • tring tring ring ring
  • ring ring tring tring

Q8. Given the code below, what will be the output displayed?

function Animal()
{  
   this.name = 'bunny';  
}  

function Rabbit() 
{  
}  

Rabbit.prototype = Object.create(Animal.prototype);  
const rabbit = new Rabbit();  
console.log(rabbit.name);
  • bunny
  • Animal
  • undefined
  • null

Q9. Given the code below, which option correctly describes the issue with it?

class Animal{
  constructor(name,size){
    this.name = name
    this.size = size
  }
}
class Cat{
  constructor(eyes, color){
    super(name,size)
    this.eyes = eyes
    this.color = color
  }
}
  • “super” function should be invoked in the Animal class constructor
  • The “super” function can only be invoked in the Cat constructor if it is a derived constructor
  • The properties “eyes” and “color” should not have been defined after calling the super function
  • There is no issue in this code

Q10. Whenever a method is declared outside of a constructor in a class, it gets defined on the prototype of that class.

  • True
  • False

Q11. Static functions can be accessed by the class and its objects.

  • True
  • False

Q12. The properties and methods defined on the prototype of a constructor function cannot be overridden when inherited by another constructor function.

  • True
  • False

Q13. Every constructor function initialized has a prototype property assigned to it known as the Prototype Object.

  • True
  • False

Q14. Match the terms in the left column with their respective definitions on the right side:

  • Mixin
  • extends keyword
  • super keyword
  • call method
  • property shadowing
  • invokes a method, defined anywhere, in the current context
  • a class that contains various methods implementing different functionalities. Other classes can then inherit those methods without having it be their parent class
  • a property redefined higher up in the prototypal chain wil display the older value when accessed instead of the new one
  • creates a class that is the child of another class
  • acts as the parent class constructor; the parent class constructor parameters can be passed to it. When called with the parameters inside the child class constructor, the parent class properties will get initialized for the child class.

Answers:

  • Mixin –
  • extends keyword –
  • super keyword –
  • call method –
  • property shadowing –

Q15. In this coding exercise, you have to create a Car class with the following properties:

  • name
  • speed
  • color

Please maintain this exact order of the properties in the class constructor.

Next, you need to define a function isFaster that compares the speed of two cars, and displays which car is moving faster.

For example, if there are two cars X and Y with X travelling at a higher speed than Y, the output should display:

"X is faster than Y"

However, if both are travelling at the same speed it should display:

"X and Y have the same speed"

It is left for you to decide what the type of the isFaster function will be.

Sample input
var car1 = new Car("Honda", 50, "Red");
var car2 = new Car("Toyota", 80, "Black");
var car3 = new Car("Audi", 50, "Red");
var car4 = new Car("Nissan", 50, "Black");
Sample output

When the isFaster compares car1 and car2 it will display:

"Toyota is faster than Honda"

And when it compares car3 and car4 it will display:

"Audi and Nissan have the same speed"
Solution:

Q16. In this coding challenge, you are given two classes: Person and Actress. You need to implement inheritance between them using the syntax of the ES6 version. It is upto you to decide which class will be the parent and which one will be the child.

The Person class should have the following properties, initialized in the same order as given below:

  • name
  • age
  • gender

The Actress class should have access to the properties present in the Person class and also have the following additional properties, initialized in the same order as given below:

  • movies
  • awards

It should also have a getDetails function that returns the information about that actress.

Sample input
var actress = new Actress("Rachel", 30, "Female", 20, 5);
actress.getDetails();
Sample output
"Rachel is a 30 years old Female actress, has starred in 20 movies and has won 5 awards"
Solution:
Conclusion:

I hope this Learn Object-Oriented Programming in JavaScript Educative Quiz Answers would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.