JavaScript Objects

Last Updated :
Discuss
Comments

Question 1

Which of the following is a valid way to define a function as a property of an object?

  • A

    obj.func = function() { ... }

  • B

    obj.func = () => { ... }

  • C

    Both

  • D

    None of the above

Question 2

Which is the correct way to create an object in JavaScript?

  • A

    let obj = Object();

  • B

    let obj = { key: "value" };

  • C

    let obj = new Object("key", "value");

  • D

    let obj = Object.create();

Question 3

How do you access the property of an object using bracket notation?

  • A

    obj.key

  • B

    obj["key"]

  • C

    obj(key)

  • D

    obj->key

Question 4

Which method is used to merge two objects?

  • A

    Object.assign()

  • B

    Object.merge()

  • C

    Object.create()

  • D

    Object.combine()

Question 5

What is the purpose of the prototype property in JavaScript objects?

  • A

    To create new properties in an object

  • B

    To define methods and properties for inheritance

  • C

    To store the type of the object

  • D

    To delete existing properties

Question 6

Which of the following creates a deep copy of an object?

  • A

    JSON.stringify() and JSON.parse()

  • B

    Object.assign()

  • C

    Object.create()

  • D

    Object.freeze()

Question 7

What will the following code log?

JavaScript
const obj = { a: 1 };
obj.b = 2;
console.log(obj);


  • A

    { a: 1 }

  • B

    { a: 1, b: 2 }

  • C

    Error

  • D

    undefined

Question 8

Which of the following is a valid getter in an object?

  • A

    get name() { return this._name; }

  • B

    getter name() { return this._name; }

  • C

    get: function() { return this.name; }

  • D

    name.get { return this._name; }

Question 9

What will the following code log?

JavaScript
const obj = { a: 1, b: 2 };
delete obj.a;
console.log(obj)


  • A

    { b: 2 }

  • B

    { a: null, b: 2 }

  • C

    { a: undefined, b: 2 }

  • D

    Error

Question 10

What is the purpose of Object.freeze()?

  • A

    To prevent new properties from being added

  • B

    To make the object immutable

  • C

    To prevent existing properties from being changed

  • D

    All of the above

There are 10 questions to complete.

Take a part in the ongoing discussion