Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
Q1. Object-oriented programming is a tool used to break a program into segments of objects that can communicate with each other.
Q2. OOP in JavaScript is exactly the same as OOP in C++, Java and C#.
Q3. OOP in JavaScript follows prototype-based programming.
Q4. Prototype-based programming involves making a class that acts as a blueprint with all the properties encapsulated within it.
Q5. Class-based programming is more flexible and transparent than Prototyped-based programming.
Q6. Which of the following is not true for the ES6 version?
class
keyword is used in order to define a classextends
keyword is used for inheritancefunction
keyword.static
keyword is used to declare static membersQ1. An object’s set of properties include data values and functions.
Q2. What does the create()
method do?
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?
Q4. What is the error in the following code:
var human = {
name : 'Penny',
age : 32
}
console.log(human[name])
name
propertyname
should be written as a string while accessing i.e., it should be human['name']
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']()
Q6. Properties, including both data values and functions, can be deleted or removed from an object using the delete
operator.
Q7. What should be the return statement in the display
function?
var human = {
name : 'Penny',
display() {
//function returns the name of the human
}
}
return name
return human.name
return this.name
return
Q8. get
and set
keywords allow the function to be accessed as a property.
Q1. All objects created from a constructor function contain all the properties in it?
Q2. Point out the error in the code below:
function Shape(name, sides) {
name: name
sides: sides
}
var square = new Shape('Square', 4)
new
keyword should not be used to create a new object instanceQ3. Code in the constructor function executes every time a new object is instantiated.
Q4. this
refers to the new object created and is used to set its property values.
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)
Length is: 0
Length is: 4
Length is: undefined
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.
Q7. Declaring properties using the var
keyword makes them accessible outside the constructor function.
Q1. Which of the following features were introduced in the ES6 version of JavaScript?
Q2. Given the following code, which of the following statements is correct?
const house = {
bedrooms : 3,
washrooms : 2,
basement : 'no',
}
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"
}
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);
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
Q6. Given the code below, which option correctly shows the output displayed on the console?
var shape = {
sides: 4
}
console.log(shape.color);
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());
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);
Q9. How is a constructor function 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();
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);
Q12. When a function is used a constructor function, the new objects created from it share its _____ .
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);
Q14. The ES5 version of JavaScript allowed the use of both static and super keywords.
Q15. In JavaScript, functions cannot have their own properties and methods.
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]);
Q17. Match the terms in the left column with their respective definitions on the right side:
Answer:
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.
var parallelogram = new Parallelogram(4,5);
parallelogram.printArea();
20
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.
Q1. The constructor
is used to initialize the properties of the class.
Q2. The class declaration creates a function as an end result.
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)
class shape = { }
insteadthis
should be replaced with var
in the body of the constructordisplayName
function cannot access name
directlyQ4. Methods defined inside the class are assigned to the prototype
of the class.
Q5. Methods inside the class that are assigned to the prototype
of the class are known as static methods.
Q6. Static methods cannot be inherited by the objects of the class.
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()
var
should be used instead of this
in the constructordisplayName
should be called from the class Shape
instead of the object shape1
.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
: 1getName
: 1, name
: 50getName
: 50, name
: 1getName
: 50, name
: 50Q2. Whenever a property is to be found it is first searched in the object and then the prototype chain is traversed.
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)
Q4. Object.prototype
always points to another object.
Q5. The __proto__
property is used as a setter/getter for the [[Prototype]]
property.
Q6. call
is used to invoke a method defined anywhere in the current context.
Q7. super
keyword is used to invoke child class methods and constructors.
Q8. In JavaScript, inheritance is implemented through mixins.
Q1. Which of the following statements describes the role of the constructor
?
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}`);
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);
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);
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 }
9
10 Car.__proto__ = Vehicle;
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);
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());
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);
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
}
}
Q10. Whenever a method is declared outside of a constructor in a class, it gets defined on the prototype of that class.
Q11. Static functions can be accessed by the class and its objects.
Q12. The properties and methods defined on the prototype of a constructor function cannot be overridden when inherited by another constructor function.
Q13. Every constructor function initialized has a prototype property assigned to it known as the Prototype Object.
Q14. Match the terms in the left column with their respective definitions on the right side:
Answers:
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.
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");
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"
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.
var actress = new Actress("Rachel", 30, "Female", 20, 5);
actress.getDetails();
"Rachel is a 30 years old Female actress, has starred in 20 movies and has won 5 awards"
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 >>