Answer: A) A function assigned as a property of an object
Explanation: An object method is simply a function that is defined as a property of an object.
let obj = {methodName: function() { return "hello"; }};
let obj = {methodName: "hello"};
let obj = {methodName: "function() { return 'hello'; }"};
Answer: A) let obj = {methodName: function() { return "hello"; }};
Explanation: This is how you define a method in an object using function syntax.
greet()
from the object person
?let person = {greet: function() { return "Hello!"; }};
person.greet();
greet();
person["greet"]();
Answer: D) Both A and C
Explanation: Methods can be called using both dot notation (person.greet()
) and bracket notation (person["greet"]()
).
let person = {name: "Alice", greet: function() { return "Hello, " + this.name; }};
console.log(person.greet());
Answer: A) Hello, Alice
Explanation: The this
keyword refers to the object itself, so this.name
will return the value "Alice".
object["methodName"]()
object.methodName()
object["methodName"]
object.methodName
Answer: A) object["methodName"]()
Explanation: Bracket notation allows you to access a method using the key name inside quotes.
let car = {make: "Toyota", model: "Corolla", describe: function() { return this.make + " " + this.model; }};
console.log(car.describe());
Answer: A) Toyota Corolla
Explanation: The method describe()
uses the this
keyword to access the object's properties and returns them as a string.
this
refer to in a method?
Answer: C) The object that the method is called on
Explanation: In a method, this
refers to the object that the method is called on, allowing access to its properties and other methods.
Answer: A) Yes
Explanation: Methods can be assigned to variables just like any other function. These variables can be called like normal functions.
this
refer to in the following method?let person = {name: "Bob", greet: function() { return "Hello, " + this.name; }};
Answer: B) The person object
Explanation: In this case, this
refers to the person
object, and it accesses the name
property.
let obj = {method: function() { return "Hello"; }};
let obj = {method() { return "Hello"; }};
let obj = {method: () => { return "Hello"; }};
Answer: B) let obj = {method() { return "Hello"; }};
Explanation: Shorthand method syntax is supported in JavaScript, where you can directly define the method without using the function
keyword.
Answer: A) A function used to create and initialize objects
Explanation: A constructor is a special function used to create and initialize objects in JavaScript. It is called using the new
keyword.
let obj = new Object();
let obj = Object();
let obj = {};
Answer: D) Both A and B
Explanation: Both new Object()
and Object()
can be used to create an empty object in JavaScript.
function MyConstructor() {}
let MyConstructor = function() {}
function MyConstructor() { this.property = value; }
Answer: D) Both A and C
Explanation: The constructor function can be defined either with function MyConstructor()
or using the this
keyword to initialize properties.
let obj = new MyConstructor();
let obj = MyConstructor();
let obj = create MyConstructor();
Answer: A) let obj = new MyConstructor();
Explanation: The new
keyword is used to create an object from a constructor function, initializing the properties and methods defined in it.
this
keyword in a constructor?
Answer: C) It refers to the newly created object
Explanation: In a constructor function, this
refers to the newly created object, allowing access to its properties and methods.
function Car(make, model) { this.make = make; this.model = model; }
let myCar = new Car("Toyota", "Corolla");
console.log(myCar.make);
Answer: A) Toyota
Explanation: The constructor function Car()
initializes the properties make
and model
, and myCar.make
accesses the make
property of the object.
MyConstructor.prototype.methodName = function() {}
MyConstructor.methodName = function() {}
MyConstructor.prototype = function() {}
Answer: A) MyConstructor.prototype.methodName = function() {}
Explanation: You can add methods to a constructor's prototype by using MyConstructor.prototype.methodName = function() {}
.
Answer: A) They allow multiple instances of objects with the same properties and methods
Explanation: Constructor functions allow you to create multiple instances of objects with the same properties and methods, enabling code reuse.
typeof obj
instanceof Constructor
obj.constructor
Answer: D) All of the above
Explanation: You can check the type of an object using typeof
, instanceof
, or by accessing the constructor
property.
function Car(make, model) { this.make = make; this.model = model; }
Car.prototype.describe = function() { return this.make + " " + this.model; };
let myCar = new Car("Honda", "Civic");
console.log(myCar.describe());
Answer: A) Honda Civic
Explanation: The method describe()
is added to the prototype of the constructor function Car
, and it correctly outputs the make
and model
of the object.
Answer: A) To create methods specific to each instance of an object
Explanation: Constructors allow you to create methods that are specific to each instance of an object, providing functionality that operates on the object's properties.
this.methodName = function() {}
methodName = function() {}
constructor.methodName = function() {}
function methodName() {}
Answer: A) this.methodName = function() {}
Explanation: The method is defined using this
keyword inside the constructor, ensuring it is attached to the instance of the object created by that constructor.
object.methodName()
methodName()
object::methodName()
this.methodName()
Answer: A) object.methodName()
Explanation: To call a method created in a constructor, you access it through the object instance using object.methodName()
.
undefined
Answer: A) It will throw an error
Explanation: If you try to call a method before creating an instance of an object, it will throw an error because the method is defined on an instance of the object.
this.methodName = function() { return this.property; }
methodName = function() { return this.property; }
function methodName() { return this.property; }
this.methodName = function() { return property; }
Answer: A) this.methodName = function() { return this.property; }
Explanation: You can create a method inside a constructor that uses the this
keyword to access and return the properties of the instance.
function Person(name, age) { this.name = name; this.age = age; this.greet = function() { return "Hello " + this.name; }; }
function Person(name, age) { this.name = name; this.age = age; greet = function() { return "Hello " + this.name; }; }
let Person = { name: "", age: "", greet: function() { return "Hello " + this.name; } };
Answer: A) function Person(name, age) { this.name = name; this.age = age; this.greet = function() { return "Hello " + this.name; }; }
Explanation: The correct way is to define a method inside the constructor using this
to refer to the object and access its properties.
MyConstructor.prototype.methodName = function() {}
this.methodName = function() {}
MyConstructor.methodName = function() {}
Object.prototype.methodName = function() {}
Answer: A) MyConstructor.prototype.methodName = function() {}
Explanation: You can add methods to an object constructor's prototype by using the MyConstructor.prototype.methodName
syntax.
Answer: A) It makes the method available to all instances created from that constructor
Explanation: Defining methods on a constructor's prototype ensures that all instances share the same method, reducing memory usage.
function Dog(name) { this.name = name; }
Dog.prototype.speak = function() { return this.name + " barks!"; };
let myDog = new Dog("Rex");
console.log(myDog.speak());
Answer: A) Rex barks!
Explanation: The method speak()
is added to the prototype of the Dog
constructor and is accessed through the myDog
instance.
Answer: B) By creating reusable methods for multiple instances of objects
Explanation: Constructors for methods allow you to define methods that can be reused across multiple instances of objects, improving code reusability and efficiency.
Answer: A) A blueprint for creating objects
Explanation: A prototype is essentially a template for objects, where shared properties and methods are defined. Objects created from the same constructor share the prototype.
Object.prototype
object.__proto__
object.prototype
object.prototype()
Answer: B) object.__proto__
Explanation: The __proto__
property allows access to the prototype of an object.
Answer: C) A series of objects connected via their prototypes
Explanation: The prototype chain is the series of objects linked together through the __proto__
property, where each object inherits properties and methods from its prototype.
object.prototype.property = value;
object.prototype = { property: value };
Constructor.prototype.property = value;
Constructor.prototype = value;
Answer: C) Constructor.prototype.property = value;
Explanation: You add a property to an object prototype by modifying the constructor's prototype directly.
null
undefined
Answer: C) It returns undefined
Explanation: If a property doesn't exist on an object, JavaScript will search the object's prototype chain. If it can't find the property, it will return undefined
.
__proto__
directly
Answer: C) By adding properties to the constructor's prototype
Explanation: To modify the prototype of an object, you can add new properties or methods to the constructor's prototype, which will be available to all instances created from that constructor.
Object.create()
method do?
Answer: B) It creates a new object that has the specified prototype
Explanation: The Object.create()
method creates a new object with the specified prototype, allowing for more control over the object's inheritance.
Answer: B) Objects can inherit properties from other objects
Explanation: Prototype-based inheritance allows objects to inherit properties and methods from other objects, which is the basis of inheritance in JavaScript.
object.hasOwnProperty(property)
object.property !== undefined
property in object
object.property == null
Answer: C) property in object
Explanation: The in
operator checks if a property exists in an object's prototype chain, including the object's prototype.
Object.seal(object)
Object.freeze(object)
Object.preventExtensions(object)
Answer: B) Object.freeze(object)
Explanation: Object.freeze()
prevents any modifications to the object's prototype, including adding, removing, or changing properties.
object.hasOwnProperty(property)
property.in(object)
object.checkProperty(property)
object.propertyExists(property)
Answer: A) object.hasOwnProperty(property)
Explanation: The hasOwnProperty()
method checks whether an object has a specified property as its own (not inherited).
object.property in object
return if the property is found in the object's prototype chain?false
undefined
true
null
Answer: C) true
Explanation: The in
operator returns true
if the property exists anywhere in the object's prototype chain.
object.isInstanceOf(constructor)
constructor.instanceof(object)
object instanceof constructor
object.isPrototypeOf(constructor)
Answer: C) object instanceof constructor
Explanation: The instanceof
operator checks if an object is an instance of a constructor function (or class).
object.property === undefined
check for?undefined
undefined
Answer: B) Whether the property exists and has a value of undefined
Explanation: This check is used to determine if a property is either not present or has been set to undefined
.
object.hasOwnProperty()
method do?
Answer: B) It checks if the object has the property as its own
Explanation: hasOwnProperty()
checks if the property belongs directly to the object, excluding inherited properties from the prototype chain.
object.hasOwnProperty('prototype')
on a constructor function object?false
undefined
true
prototype
Answer: A) false
Explanation: The prototype
property is not a direct property of the constructor function object; it's part of the function's prototype chain.
object.methodExists()
method in object
object.method !== undefined
method.hasOwnProperty(object)
Answer: C) object.method !== undefined
Explanation: You can check if a method exists on an object by verifying if its value is not undefined
.
hasOwnProperty()
in
operatorobject.property === null
object.property instanceof Function
Answer: B) By using in
operator
Explanation: The in
operator checks if a property exists anywhere in the prototype chain, including inherited methods.
Object.keys(object)
method return?
Answer: B) An array of all enumerable property names
Explanation: Object.keys(object)
returns an array of the object's own enumerable property names (not including properties from the prototype chain).
delete object.property
?undefined
Answer: B) The property is completely removed from the object
Explanation: The delete
operator removes the property from the object, making it undefined and inaccessible.
window.url
document.url
window.location.href
document.location.url
Answer: C) window.location.href
Explanation: window.location.href
returns the full URL of the current page.
window.location.href = 'newURL';
document.location = 'newURL';
location.href = 'newURL';
Answer: D) All of the above
Explanation: You can use window.location.href
, document.location
, or simply location.href
to change the URL.
window.location.reload()
document.location.reload()
location.reload()
Answer: D) All of the above
Explanation: You can use window.location.reload()
, document.location.reload()
, or location.reload()
to reload the page.
window.location.hostname
window.location.href
document.location.hostname
window.location.protocol
Answer: A) window.location.hostname
Explanation: The hostname
property returns the domain name or IP address of the server.
Answer: B) Replaces the current page with the new URL but does not update the browser history
Explanation: window.location.replace()
replaces the current page in the browser's history, so you cannot go back to the previous page.
window.location.href
window.location.pathname
window.location.search
window.location.protocol
Answer: A) window.location.href
Explanation: The href
property returns the full URL of the current page, including the query string.
window.location.pathname
window.location.href
document.location.pathname
window.location.protocol
Answer: A) window.location.pathname
Explanation: The pathname
property returns the part of the URL that comes after the hostname, including the path to the page.
window.location.origin
return?
Answer: A) The protocol, hostname, and port of the URL
Explanation: origin
returns the protocol, hostname, and port of the URL, essentially the base URL.
window.location.reload(true)
window.location.reload()
window.location.replace()
document.location.reload(true)
Answer: A) window.location.reload(true)
Explanation: The reload(true)
method reloads the page and forces the browser to bypass cache.
window.location.hash = 'newSection'
?
Answer: A) Changes the URL and scrolls to the section with ID 'newSection'
Explanation: The hash
property changes the fragment identifier in the URL and scrolls the page to the corresponding section with that ID.
window.location.href = 'newURL';
document.location.replace('newURL');
location.assign('newURL');
Answer: D) All of the above
Explanation: All these methods can set the URL of a page.
window.location.replace()
and window.location.assign()
?replace()
replaces the current page in history, while assign()
does notreplace()
opens a new page, while assign()
reloads the current pagereplace()
reloads the current page, while assign()
navigates to the new page
Answer: A) replace()
replaces the current page in history, while assign()
does not
Explanation: replace()
removes the current page from history, so you can't go back to it, while assign()
adds the page to the history.
window.open('newURL');
window.location.href = 'newURL';
window.location.replace('newURL');
Answer: D) Both B and C
Explanation: Both href
and replace()
can navigate to a new URL in the same window, while open()
typically opens a new window or tab.
window.location.href += '?param=value';
window.location.assign('?param=value');
window.location.reload('?param=value');
window.location.replace('?param=value');
Answer: A) window.location.href += '?param=value';
Explanation: You can append a query string to the current URL using window.location.href
.
window.open('newURL');
document.open('newURL');
location.open('newURL');
window.location.href = 'newURL';
Answer: A) window.open('newURL');
Explanation: The window.open()
method opens a new browser window or tab with the specified URL.
window.location.reload(true);
window.location.reload();
window.location.replace('currentPage');
window.location.href = 'currentPage';
Answer: A) window.location.reload(true);
Explanation: The reload(true)
method forces the browser to bypass the cache and reload the page.
window.location.origin
return?
Answer: A) The protocol, hostname, and port of the URL
Explanation: origin
provides the base part of the URL, including the protocol, hostname, and port number (if any).
window.location.hash
window.location.href
window.location.pathname
window.location.protocol
Answer: A) window.location.hash
Explanation: The hash
property returns the fragment identifier (the part after the '#') from the URL.
window.location.search = '';
window.location.replace(window.location.pathname);
window.location.href = window.location.pathname;
Answer: D) All of the above
Explanation: All these methods can be used to remove the query string from the URL by setting the search or href to the pathname.
window.location.protocol
window.location.href
window.location.hostname
window.location.port
Answer: A) window.location.protocol
Explanation: The protocol
property returns the protocol used by the current URL (e.g., 'http:' or 'https:').
window.history.forward();
window.history.back();
window.history.go(1);
Answer: D) Both A and C
Explanation: forward()
or go(1)
both navigate forward in the browser's history.
window.history.back()
do?
Answer: A) Moves to the previous page in history
Explanation: The back()
method moves the browser one step backward in the history list.
window.history.forward(2);
window.history.go(2);
window.history.go(-2);
window.history.forward();
Answer: B) window.history.go(2);
Explanation: The go(2)
method moves forward two steps in the browser's history.
window.history.back();
window.history.forward();
window.history.go();
window.location.href;
Answer: C) window.history.go();
Explanation: The go()
method allows navigation both forward and backward through the history depending on the parameter passed.
window.history.length
property return?
Answer: A) The total number of pages in the history list
Explanation: The length
property returns the number of entries in the session history.
window.history.replaceState()
method?
Answer: A) To replace the current page in the browser's history with a new one
Explanation: The replaceState()
method replaces the current history entry with a new one without creating a new history entry.
window.history.back(2);
window.history.go(-2);
window.history.forward(2);
window.history.go(2);
Answer: B) window.history.go(-2);
Explanation: The go(-2)
method moves back two steps in the browser's history.
window.history.pushState()
?
Answer: B) It creates a new history entry without reloading the page
Explanation: The pushState()
method adds a new entry to the browser's history stack without reloading the page.
window.history.forward()
?
Answer: C) It navigates forward in the history list
Explanation: The forward()
method navigates the browser forward by one step in the history list.
window.history.state
return?
Answer: B) The state object associated with the current history entry
Explanation: The state
property returns the state object associated with the current history entry (if any).
window.document.write()
window.location.href
document.createElement()
window.location.replace()
Answer: A) By using window.document.write()
Explanation: The write()
method allows you to directly write content to the document of the current window.
window.open()
document.write()
window.location.href
window.document.write()
Answer: A) window.open()
Explanation: The open()
method is used to open a new window and you can use document.write()
to add content to it.
document.write()
method do?
Answer: B) It writes content to the browser's window
Explanation: document.write()
writes content directly into the document of the current window or new window.
document.open()
window.open()
document.write()
window.location.replace()
Answer: C) document.write()
Explanation: document.write()
will replace the entire content of the window with the new content passed to it.
window.location.replace()
?
Answer: C) It navigates the browser to a new URL
Explanation: replace()
replaces the current page with the new one specified by the URL.
window.open()
window.location.replace()
window.location.reload()
window.document.write()
Answer: A) window.open()
Explanation: The open()
method is used to open a new window in the browser.
window.document.write()
on an already loaded page?
Answer: A) It replaces the existing content
Explanation: Calling document.write()
on an already loaded page will overwrite the existing content.
document.write()
?
Answer: C) It can write HTML tags, including images, links, etc.
Explanation: document.write()
can insert text and HTML tags into the document.
window.open()
window.location.reload()
document.write()
innerHTML
of elements
Answer: D) By modifying the innerHTML
of elements
Explanation: After the page loads, you can modify the content using the innerHTML
property of DOM elements.
window.location.replace()
over window.location.href
?
Answer: B) It navigates to a new page without saving the current page in history
Explanation: replace()
will replace the current page in history, preventing the user from using the "back" button to return to the previous page.
window.resizeTo()
window.open()
window.location.replace()
window.document.write()
Answer: A) window.resizeTo()
Explanation: resizeTo()
is used to resize a window to the specified width and height.
window.moveTo()
window.open()
window.location.href
window.scrollTo()
Answer: A) window.moveTo()
Explanation: moveTo()
allows you to move the window to a specific location on the screen.
window.resizeBy()
method do?
Answer: B) It resizes the window relative to its current size.
Explanation: resizeBy()
changes the size of the window by the given width and height relative to its current dimensions.
window.location.replace()
window.resizeTo()
window.scrollTo()
window.open()
Answer: A) window.location.replace()
Explanation: window.location.replace()
is used to replace the current page with a new one by updating the URL.
window.moveBy()
?
Answer: A) It moves the window by the specified amount from its current position.
Explanation: moveBy()
moves the window by the given horizontal and vertical offsets from its current position.
window.innerWidth
window.outerWidth
window.width
window.getWidth()
Answer: A) window.innerWidth
Explanation: window.innerWidth
returns the width of the browser's viewport including scrollbars.
window.resizeTo()
or window.resizeBy()
.
Answer: C) The window can be resized using window.resizeTo()
or window.resizeBy()
.
Explanation: These methods allow resizing of the window either to specific dimensions or by specified increments.
window.outerWidth
property?
Answer: A) It returns the width of the window including the browser chrome (border, toolbar, etc.).
Explanation: window.outerWidth
includes the entire window area, including the chrome elements of the browser.
window.resizeTo()
and window.resizeBy()
?resizeTo()
sets the size of the window, while resizeBy()
resizes it relative to its current size.resizeBy()
sets the size, while resizeTo()
resizes it relative to its current size.resizeTo()
can only be used with pop-up windows.
Answer: A) resizeTo()
sets the size of the window, while resizeBy()
resizes it relative to its current size.
Explanation: resizeTo()
resizes the window to the exact specified width and height, while resizeBy()
resizes the window by the given amount.
Answer: B) Only the window opener can resize the window.
Explanation: Most modern browsers restrict resizing windows that were not opened via JavaScript to prevent abusive behavior.