JavaScript Objects and Prototypes Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/10

What does `Object.getPrototypeOf(obj)` return for an object `obj` created using `Object.create(protoObj)`?
let protoObj = { type: 'example' };
let obj = Object.create(protoObj);
Object.getPrototypeOf(obj);

Select your answer

Question 2/10

Which of the following correctly describes how to make an object `cat` have a non-enumerable property `legs`?
let cat = { name: 'Whiskers' };

Select your answer

Question 3/10

How can you add a new property `bark` to all objects created from the `Dog` constructor function without modifying the constructor itself?
function Dog(name) {
  this.name = name;
}

const myDog = new Dog('Rex');

Select your answer

Question 4/10

How do you determine if an object literal `{}` is the root prototype of a variable `someVar`?
let someVar = {};
Object.getPrototypeOf(someVar) === Object.prototype;

Select your answer

Question 5/10

How can you check if an object `duck` has a property `quack` as its own property, not inherited from its prototype?
let duck = { quack: true };
let rubberDuck = Object.create(duck);

Select your answer

Question 6/10

What is the output of `console.log(shape.toString())` for an object created from `Shape` constructor with a custom `prototype` toString method?
function Shape(name) {
  this.name = name;
}

Shape.prototype.toString = function() {
  return `Shape: ${this.name}`;
};

const shape = new Shape('Circle');
console.log(shape.toString());

Select your answer

Question 7/10

Given an object `bird` with `fly` method added to its prototype, what is the correct way to override this method with a new implementation on a specific instance `parrot`?
function Bird() {}
Bird.prototype.fly = function() {
  return 'Flying';
};
let parrot = new Bird();

Select your answer

Question 8/10

What method can you use to extend all objects of a given type with a new method without modifying individual object instances?
function Person(name) {
  this.name = name;
}

Select your answer

Question 9/10

What will `console.log(obj.prop)` output if `prop` does not exist on `obj`, which is an object created with `Object.create(null)`?
let obj = Object.create(null);
console.log(obj.prop);

Select your answer

Question 10/10

If `farm = Object.create(duck)`, what will `console.log(farm.swim)` output when `duck` does not have a property `swim` but its prototype does?
let protoDuck = { swim: 'Glide' };
let duck = Object.create(protoDuck);
let farm = Object.create(duck);
console.log(farm.swim);

Select your answer

Your Results

You did not answer any questions correctly.

Your Answers

Question 1/10
😊 Your answer was correct 🙁 Your answer was incorrect
What does `Object.getPrototypeOf(obj)` return for an object `obj` created using `Object.create(protoObj)`?
let protoObj = { type: 'example' };
let obj = Object.create(protoObj);
Object.getPrototypeOf(obj);

Available answers

`Object.getPrototypeOf(obj)` returns the prototype from which `obj` was created. Since `obj` is created with `Object.create(protoObj)`, its prototype is `protoObj`.
Question 2/10
😊 Your answer was correct 🙁 Your answer was incorrect
Which of the following correctly describes how to make an object `cat` have a non-enumerable property `legs`?
let cat = { name: 'Whiskers' };

Available answers

To make a property non-enumerable, you can use `Object.defineProperty` with the `enumerable` attribute set to `false`.
Question 3/10
😊 Your answer was correct 🙁 Your answer was incorrect
How can you add a new property `bark` to all objects created from the `Dog` constructor function without modifying the constructor itself?
function Dog(name) {
  this.name = name;
}

const myDog = new Dog('Rex');

Available answers

Adding the method to the `Dog` prototype with `Dog.prototype.bark = function() { return 'Woof!'; };` ensures that all instances of `Dog` can inherit the `bark` method without changing the constructor function itself.
Question 4/10
😊 Your answer was correct 🙁 Your answer was incorrect
How do you determine if an object literal `{}` is the root prototype of a variable `someVar`?
let someVar = {};
Object.getPrototypeOf(someVar) === Object.prototype;

Available answers

`Object.getPrototypeOf(someVar) === Object.prototype` checks if `someVar` was directly created from `Object.prototype`, indicating it's a root object.
Question 5/10
😊 Your answer was correct 🙁 Your answer was incorrect
How can you check if an object `duck` has a property `quack` as its own property, not inherited from its prototype?
let duck = { quack: true };
let rubberDuck = Object.create(duck);

Available answers

`hasOwnProperty` checks if `quack` is a property of `duck` itself and not inherited from the prototype chain.
Question 6/10
😊 Your answer was correct 🙁 Your answer was incorrect
What is the output of `console.log(shape.toString())` for an object created from `Shape` constructor with a custom `prototype` toString method?
function Shape(name) {
  this.name = name;
}

Shape.prototype.toString = function() {
  return `Shape: ${this.name}`;
};

const shape = new Shape('Circle');
console.log(shape.toString());

Available answers

`Shape.prototype.toString` is overridden to return `Shape: `. Since `shape` is created with `name` as `Circle`, it returns `Shape: Circle`.
Question 7/10
😊 Your answer was correct 🙁 Your answer was incorrect
Given an object `bird` with `fly` method added to its prototype, what is the correct way to override this method with a new implementation on a specific instance `parrot`?
function Bird() {}
Bird.prototype.fly = function() {
  return 'Flying';
};
let parrot = new Bird();

Available answers

To override a method for a specific instance, directly assign a new method to the instance, like `parrot.fly = function() { return 'Soaring'; };`. This does not affect other instances of `Bird`.
Question 8/10
😊 Your answer was correct 🙁 Your answer was incorrect
What method can you use to extend all objects of a given type with a new method without modifying individual object instances?
function Person(name) {
  this.name = name;
}

Available answers

Adding a method to `Person.prototype` will ensure all instances of `Person` have access to it without modifying each instance.
Question 9/10
😊 Your answer was correct 🙁 Your answer was incorrect
What will `console.log(obj.prop)` output if `prop` does not exist on `obj`, which is an object created with `Object.create(null)`?
let obj = Object.create(null);
console.log(obj.prop);

Available answers

Since `obj` is created using `Object.create(null)`, it has no properties of its own and no prototype. Accessing a non-existent property `prop` on such an object will return `undefined` by default.
Question 10/10
😊 Your answer was correct 🙁 Your answer was incorrect
If `farm = Object.create(duck)`, what will `console.log(farm.swim)` output when `duck` does not have a property `swim` but its prototype does?
let protoDuck = { swim: 'Glide' };
let duck = Object.create(protoDuck);
let farm = Object.create(duck);
console.log(farm.swim);

Available answers

`farm` is part of the prototype chain starting from `duck` to `protoDuck`. `farm.swim` resolves to `protoDuck.swim` which is `Glide`, due to prototype chain lookup.