Multiple Choice Question for Grand JS Quiz



(Chap # 71 - Objects Methods)

(1) What is an object method in JavaScript?

A) A function assigned as a property of an object
B) A variable inside an object
C) A loop inside an object
D) A string inside an object

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.



(2) How do you define a method in an object?

A) let obj = {methodName: function() { return "hello"; }};
B) let obj = {methodName: "hello"};
C) let obj = {methodName: "function() { return 'hello'; }"};
D) None of the above

Answer: A) let obj = {methodName: function() { return "hello"; }};

Explanation: This is how you define a method in an object using function syntax.



(3) How would you call the method greet() from the object person?

let person = {greet: function() { return "Hello!"; }};

A) person.greet();
B) greet();
C) person["greet"]();
D) Both A and C

Answer: D) Both A and C

Explanation: Methods can be called using both dot notation (person.greet()) and bracket notation (person["greet"]()).



(4) What will this code output?

let person = {name: "Alice", greet: function() { return "Hello, " + this.name; }};
console.log(person.greet());


A) Hello, Alice
B) Hello, undefined
C) Alice
D) Error

Answer: A) Hello, Alice

Explanation: The this keyword refers to the object itself, so this.name will return the value "Alice".



(5) How can you access the method of an object using bracket notation?

A) object["methodName"]()
B) object.methodName()
C) object["methodName"]
D) object.methodName

Answer: A) object["methodName"]()

Explanation: Bracket notation allows you to access a method using the key name inside quotes.



(6) What will the following code print?

let car = {make: "Toyota", model: "Corolla", describe: function() { return this.make + " " + this.model; }};
console.log(car.describe());


A) Toyota Corolla
B) Toyota
C) Corolla
D) Error

Answer: A) Toyota Corolla

Explanation: The method describe() uses the this keyword to access the object's properties and returns them as a string.



(7) What does this refer to in a method?

A) The global object
B) The function itself
C) The object that the method is called on
D) None of the above

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.



(8) Can a method be assigned to a variable?

A) Yes
B) No
C) Only if it has a return value
D) Only if it's anonymous

Answer: A) Yes

Explanation: Methods can be assigned to variables just like any other function. These variables can be called like normal functions.



(9) What does this refer to in the following method?

let person = {name: "Bob", greet: function() { return "Hello, " + this.name; }};

A) The global object
B) The person object
C) The function itself
D) Undefined

Answer: B) The person object

Explanation: In this case, this refers to the person object, and it accesses the name property.



(10) Which of the following correctly assigns a method to an object using shorthand syntax?

A) let obj = {method: function() { return "Hello"; }};
B) let obj = {method() { return "Hello"; }};
C) let obj = {method: () => { return "Hello"; }};
D) All of the above

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.



(Chap # 72 - Objects Constructor)

(1) What is a constructor in JavaScript?

A) A function used to create and initialize objects
B) A variable that holds an object
C) A method that manipulates an object
D) A loop that iterates over object properties

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.



(2) How do you create an object using a constructor?

A) let obj = new Object();
B) let obj = Object();
C) let obj = {};
D) Both A and B

Answer: D) Both A and B

Explanation: Both new Object() and Object() can be used to create an empty object in JavaScript.



(3) How do you define a constructor function?

A) function MyConstructor() {}
B) let MyConstructor = function() {}
C) function MyConstructor() { this.property = value; }
D) Both A and C

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.



(4) How do you use a constructor function to create an object?

A) let obj = new MyConstructor();
B) let obj = MyConstructor();
C) let obj = create MyConstructor();
D) None of the above

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.



(5) What is the purpose of the this keyword in a constructor?

A) It refers to the function itself
B) It refers to the global object
C) It refers to the newly created object
D) It refers to the properties of the function

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.



(6) What will the following code output?

function Car(make, model) { this.make = make; this.model = model; }
let myCar = new Car("Toyota", "Corolla");
console.log(myCar.make);


A) Toyota
B) Corolla
C) Car
D) Error

Answer: A) Toyota

Explanation: The constructor function Car() initializes the properties make and model, and myCar.make accesses the make property of the object.



(7) How can you add methods to a constructor's prototype?

A) MyConstructor.prototype.methodName = function() {}
B) MyConstructor.methodName = function() {}
C) MyConstructor.prototype = function() {}
D) None of the above

Answer: A) MyConstructor.prototype.methodName = function() {}

Explanation: You can add methods to a constructor's prototype by using MyConstructor.prototype.methodName = function() {}.



(8) What is the main advantage of using constructor functions in JavaScript?

A) They allow multiple instances of objects with the same properties and methods
B) They make the code less reusable
C) They prevent method inheritance
D) They limit the number of properties in an object

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.



(9) How can you check the type of an object created by a constructor?

A) typeof obj
B) instanceof Constructor
C) obj.constructor
D) All of the above

Answer: D) All of the above

Explanation: You can check the type of an object using typeof, instanceof, or by accessing the constructor property.



(10) What is the output of the following code?

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());


A) Honda Civic
B) undefined
C) Car Civic
D) Error

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.



(Chap # 73 -Objects – Constructors for Methods)

(1) What is the purpose of using constructors for methods in JavaScript?

A) To create methods specific to each instance of an object
B) To create global functions
C) To create reusable methods across different objects
D) To increase the memory usage of objects

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.



(2) How would you define a method inside a constructor?

A) this.methodName = function() {}
B) methodName = function() {}
C) constructor.methodName = function() {}
D) 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.



(3) How would you call a method that is created in the constructor function?

A) object.methodName()
B) methodName()
C) object::methodName()
D) 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().



(4) What will happen if you try to call a method from an object before creating an instance?

A) It will throw an error
B) It will return undefined
C) The method will be executed
D) The method will be automatically created

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.



(5) How would you create a method that uses a constructor's properties?

A) this.methodName = function() { return this.property; }
B) methodName = function() { return this.property; }
C) function methodName() { return this.property; }
D) 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.



(6) Which of the following is the correct way to create a constructor with a method for an object?

A) function Person(name, age) { this.name = name; this.age = age; this.greet = function() { return "Hello " + this.name; }; }
B) function Person(name, age) { this.name = name; this.age = age; greet = function() { return "Hello " + this.name; }; }
C) let Person = { name: "", age: "", greet: function() { return "Hello " + this.name; } };
D) None of the above

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.



(7) How do you add a method to an object constructor's prototype?

A) MyConstructor.prototype.methodName = function() {}
B) this.methodName = function() {}
C) MyConstructor.methodName = function() {}
D) 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.



(8) What is the advantage of defining methods on a constructor's prototype?

A) It makes the method available to all instances created from that constructor
B) It increases memory usage
C) It ensures that each instance gets its own method
D) None of the above

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.



(9) What will the following code output?

function Dog(name) { this.name = name; }
Dog.prototype.speak = function() { return this.name + " barks!"; };
let myDog = new Dog("Rex");
console.log(myDog.speak());


A) Rex barks!
B) undefined barks!
C) Dog barks!
D) Error

Answer: A) Rex barks!

Explanation: The method speak() is added to the prototype of the Dog constructor and is accessed through the myDog instance.



(10) How can constructors for methods improve code reusability?

A) By allowing each object to have its own unique method
B) By creating reusable methods for multiple instances of objects
C) By reducing the number of instances created
D) By making the code less modular

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.



(Chap # 74 -bObjects prototypes )

(1) What is a prototype in JavaScript?

A) A blueprint for creating objects
B) A method used to initialize objects
C) A built-in function that returns object properties
D) A constructor for arrays

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.



(2) How do you access an object's prototype?

A) Object.prototype
B) object.__proto__
C) object.prototype
D) object.prototype()

Answer: B) object.__proto__

Explanation: The __proto__ property allows access to the prototype of an object.



(3) What is the prototype chain in JavaScript?

A) The mechanism for allowing functions to inherit properties
B) A chain of constructors that creates objects
C) A series of objects connected via their prototypes
D) A stack of object methods

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.



(4) How can you add a property to an object prototype?

A) object.prototype.property = value;
B) object.prototype = { property: value };
C) Constructor.prototype.property = value;
D) 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.



(5) What happens when you try to access a property that doesn't exist on an object?

A) It throws an error
B) It returns null
C) It returns undefined
D) It returns the prototype value

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.



(6) How can you modify the prototype of an object created from a constructor?

A) By changing the constructor itself
B) By changing the object's __proto__ directly
C) By adding properties to the constructor's prototype
D) By overriding the object's methods

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.



(7) What does the Object.create() method do?

A) It creates an object from a constructor
B) It creates a new object that has the specified prototype
C) It clones an object
D) It updates an object's prototype

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.



(8) Which of the following is a characteristic of prototype-based inheritance in JavaScript?

A) It requires classes to be defined first
B) Objects can inherit properties from other objects
C) It only works with primitive values
D) The prototype is always a function

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.



(9) How do you check if a property exists on an object's prototype chain?

A) object.hasOwnProperty(property)
B) object.property !== undefined
C) property in object
D) 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.



(10) How can you prevent further modification of an object's prototype?

A) Object.seal(object)
B) Object.freeze(object)
C) Object.preventExtensions(object)
D) You cannot prevent modification of the prototype

Answer: B) Object.freeze(object)

Explanation: Object.freeze() prevents any modifications to the object's prototype, including adding, removing, or changing properties.



(Chap # 75 - Objects : Checking for Properties and Methods )

(1) Which method is used to check if an object has a specific property?

A) object.hasOwnProperty(property)
B) property.in(object)
C) object.checkProperty(property)
D) 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).



(2) What will object.property in object return if the property is found in the object's prototype chain?

A) false
B) undefined
C) true
D) null

Answer: C) true

Explanation: The in operator returns true if the property exists anywhere in the object's prototype chain.



(3) Which method can be used to check if an object is an instance of a specific constructor?

A) object.isInstanceOf(constructor)
B) constructor.instanceof(object)
C) object instanceof constructor
D) 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).



(4) What does object.property === undefined check for?

A) Whether the property is a function
B) Whether the property exists and has a value of undefined
C) Whether the property exists
D) Whether the property is explicitly set to 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.



(5) What does the object.hasOwnProperty() method do?

A) It checks if the property is defined in the prototype chain
B) It checks if the object has the property as its own
C) It checks if the object contains the property in its methods
D) It checks if the property is inherited from the prototype

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.



(6) What is the result of calling object.hasOwnProperty('prototype') on a constructor function object?

A) false
B) undefined
C) true
D) 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.



(7) How would you check if an object has a specific method?

A) object.methodExists()
B) method in object
C) object.method !== undefined
D) 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.



(8) How can you check if an object has an inherited method?

A) By using hasOwnProperty()
B) By using in operator
C) By using object.property === null
D) By using 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.



(9) What does the Object.keys(object) method return?

A) An array of all property values
B) An array of all enumerable property names
C) A list of property names in the prototype chain
D) An array of property methods

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).



(10) What is the outcome of delete object.property?

A) The property is set to undefined
B) The property is completely removed from the object
C) The property is moved to the prototype
D) It causes an error

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.



(Chap # 76 -Browser Control – Getting and Setting the URL )

(1) How can you retrieve the current URL of the webpage in JavaScript?

A) window.url
B) document.url
C) window.location.href
D) document.location.url

Answer: C) window.location.href

Explanation: window.location.href returns the full URL of the current page.



(2) Which property would you use to change the URL of the current page programmatically?

A) window.location.href = 'newURL';
B) document.location = 'newURL';
C) location.href = 'newURL';
D) All of the above

Answer: D) All of the above

Explanation: You can use window.location.href, document.location, or simply location.href to change the URL.



(3) Which of the following will reload the current page?

A) window.location.reload()
B) document.location.reload()
C) location.reload()
D) All of the above

Answer: D) All of the above

Explanation: You can use window.location.reload(), document.location.reload(), or location.reload() to reload the page.



(4) How can you retrieve just the hostname from the current URL?

A) window.location.hostname
B) window.location.href
C) document.location.hostname
D) window.location.protocol

Answer: A) window.location.hostname

Explanation: The hostname property returns the domain name or IP address of the server.



(5)What does window.location.replace('newURL') do?

A) Replaces the current page with the new URL in the browser's history
B) Replaces the current page with the new URL but does not update the browser history
C) Opens a new tab with the new URL
D) Redirects to a new URL and closes the current tab

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.



(6) Which property provides the full URL including the query string?

A) window.location.href
B) window.location.pathname
C) window.location.search
D) window.location.protocol

Answer: A) window.location.href

Explanation: The href property returns the full URL of the current page, including the query string.



(7) How can you retrieve the path of the current URL?

A) window.location.pathname
B) window.location.href
C) document.location.pathname
D) 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.



(8) What does window.location.origin return?

A) The protocol, hostname, and port of the URL
B) The full URL
C) The path of the URL
D) The domain of the URL

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.



(9) Which method would you use to reload the page with an option to bypass cache?

A) window.location.reload(true)
B) window.location.reload()
C) window.location.replace()
D) 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.



(10) What is the effect of using window.location.hash = 'newSection'?

A) Changes the URL and scrolls to the section with ID 'newSection'
B) Changes the URL without affecting the page
C) Scrolls to the section with ID 'newSection' without changing the URL
D) None of the above

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.



(Chap # 77 - Browser Control – Getting and Setting the URL Another Way)

(1) Which of the following methods can be used to set the URL of a page in JavaScript?

A) window.location.href = 'newURL';
B) document.location.replace('newURL');
C) location.assign('newURL');
D) All of the above

Answer: D) All of the above

Explanation: All these methods can set the URL of a page.



(2) What is the difference between window.location.replace() and window.location.assign()?

A) replace() replaces the current page in history, while assign() does not
B) replace() opens a new page, while assign() reloads the current page
C) There is no difference
D) replace() 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.



(3) Which of the following will navigate to a new URL in the same browser window?

A) window.open('newURL');
B) window.location.href = 'newURL';
C) window.location.replace('newURL');
D) Both B and C

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.



(4) How can you append a query string to the current URL using JavaScript?

A) window.location.href += '?param=value';
B) window.location.assign('?param=value');
C) window.location.reload('?param=value');
D) 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.



(5) How can you use JavaScript to open a new tab or window with a URL?

A) window.open('newURL');
B) document.open('newURL');
C) location.open('newURL');
D) 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.



(6) Which method would you use to reload the current page and force it to bypass the cache?

A) window.location.reload(true);
B) window.location.reload();
C) window.location.replace('currentPage');
D) 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.



(7) What does window.location.origin return?

A) The protocol, hostname, and port of the URL
B) The entire URL
C) The path of the URL
D) The domain of the URL

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).



(8) Which property would you use to access the hash (fragment identifier) part of the current URL?

A) window.location.hash
B) window.location.href
C) window.location.pathname
D) window.location.protocol

Answer: A) window.location.hash

Explanation: The hash property returns the fragment identifier (the part after the '#') from the URL.



(9) How can you remove the query string from the URL using JavaScript?

A) window.location.search = '';
B) window.location.replace(window.location.pathname);
C) window.location.href = window.location.pathname;
D) All of the above

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.



(10) How can you get the protocol of the current URL (e.g., 'http', 'https')?

A) window.location.protocol
B) window.location.href
C) window.location.hostname
D) 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:').



(Chap # 78 - Browser Control – Forward and Reverse)

(1) Which method is used to navigate forward in the browser's history?

A) window.history.forward();
B) window.history.back();
C) window.history.go(1);
D) Both A and C

Answer: D) Both A and C

Explanation: forward() or go(1) both navigate forward in the browser's history.



(2) What does window.history.back() do?

A) Moves to the previous page in history
B) Reloads the current page
C) Navigates to the next page in history
D) Opens the home page

Answer: A) Moves to the previous page in history

Explanation: The back() method moves the browser one step backward in the history list.



(3) How can you move forward by two steps in the browser's history?

A) window.history.forward(2);
B) window.history.go(2);
C) window.history.go(-2);
D) window.history.forward();

Answer: B) window.history.go(2);

Explanation: The go(2) method moves forward two steps in the browser's history.



(4) Which method allows you to navigate both forward and backward through the browser's history?

A) window.history.back();
B) window.history.forward();
C) window.history.go();
D) 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.



(5) What does the window.history.length property return?

A) The total number of pages in the history list
B) The length of the current page URL
C) The total number of browser tabs open
D) The number of steps the browser has navigated

Answer: A) The total number of pages in the history list

Explanation: The length property returns the number of entries in the session history.



(6) What is the purpose of the window.history.replaceState() method?

A) To replace the current page in the browser's history with a new one
B) To navigate to a new page
C) To go back to the previous page
D) To reload the current page

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.



(7) Which method would you use to navigate back two steps in the browser's history?

A) window.history.back(2);
B) window.history.go(-2);
C) window.history.forward(2);
D) window.history.go(2);

Answer: B) window.history.go(-2);

Explanation: The go(-2) method moves back two steps in the browser's history.



(8) What happens when you call window.history.pushState()?

A) It replaces the current history entry
B) It creates a new history entry without reloading the page
C) It navigates to a new page
D) It clears the browser's history

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.



(9) Which of the following statements is true regarding window.history.forward()?

A) It navigates backward in history
B) It only works on web pages in the same domain
C) It navigates forward in the history list
D) It reopens the last closed window

Answer: C) It navigates forward in the history list

Explanation: The forward() method navigates the browser forward by one step in the history list.



(10) What does window.history.state return?

A) The URL of the current page
B) The state object associated with the current history entry
C) The length of the history stack
D) The domain of the current page

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).



(Chap # 79 - Browser Control – Filling the Window with Content)

(1) How can you fill the browser window with content using JavaScript?

A) By using window.document.write()
B) By using window.location.href
C) By using document.createElement()
D) By using 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.



(2) Which method allows you to open a new window and fill it with content?

A) window.open()
B) document.write()
C) window.location.href
D) 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.



(3) What does the document.write() method do?

A) It creates a new document
B) It writes content to the browser's window
C) It updates the current URL
D) It opens a new window

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.



(4) Which of the following will replace the content of the current window?

A) document.open()
B) window.open()
C) document.write()
D) 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.



(5) What happens if you use window.location.replace()?

A) The content of the current page is replaced
B) It opens a new window
C) It navigates the browser to a new URL
D) It reloads the current page

Answer: C) It navigates the browser to a new URL

Explanation: replace() replaces the current page with the new one specified by the URL.



(6) Which method would you use to open a new browser window?

A) window.open()
B) window.location.replace()
C) window.location.reload()
D) window.document.write()

Answer: A) window.open()

Explanation: The open() method is used to open a new window in the browser.



(7) What is the effect of using window.document.write() on an already loaded page?

A) It replaces the existing content
B) It adds new content without affecting the existing content
C) It opens a new window
D) It redirects the browser to a new URL

Answer: A) It replaces the existing content

Explanation: Calling document.write() on an already loaded page will overwrite the existing content.



(8) Which of the following is true about using document.write()?

A) It can only write text content
B) It is not supported in modern browsers
C) It can write HTML tags, including images, links, etc.
D) It opens a new document

Answer: C) It can write HTML tags, including images, links, etc.

Explanation: document.write() can insert text and HTML tags into the document.



(9) How can you change the content of a webpage after it has loaded?

A) By using window.open()
B) By using window.location.reload()
C) By using document.write()
D) By modifying the 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.



(10) What is the main advantage of using window.location.replace() over window.location.href?

A) It opens a new tab
B) It navigates to a new page without saving the current page in history
C) It redirects to the previous page
D) It reloads the current page

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.



(Chap # 80 - Browser Control – Controlling the Window's Size and Location)

(1) Which method allows you to resize a window in JavaScript?

A) window.resizeTo()
B) window.open()
C) window.location.replace()
D) window.document.write()

Answer: A) window.resizeTo()

Explanation: resizeTo() is used to resize a window to the specified width and height.



(2) Which method is used to move a window to a specific position on the screen?

A) window.moveTo()
B) window.open()
C) window.location.href
D) window.scrollTo()

Answer: A) window.moveTo()

Explanation: moveTo() allows you to move the window to a specific location on the screen.



(3) What does the window.resizeBy() method do?

A) It resizes the window to the specified dimensions.
B) It resizes the window relative to its current size.
C) It opens a new window.
D) It moves the window to a new location.

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.



(4) Which of the following methods is used to change the URL of the current window?

A) window.location.replace()
B) window.resizeTo()
C) window.scrollTo()
D) 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.



(5) What happens when you use window.moveBy()?

A) It moves the window by the specified amount from its current position.
B) It moves the window to a fixed position on the screen.
C) It resizes the window.
D) It refreshes the window.

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.



(6) Which property is used to get the width of the current window?

A) window.innerWidth
B) window.outerWidth
C) window.width
D) window.getWidth()

Answer: A) window.innerWidth

Explanation: window.innerWidth returns the width of the browser's viewport including scrollbars.



(7) Which of the following is true about resizing a window with JavaScript?

A) The window can be resized only if the user allows it.
B) Resizing a window automatically opens a new tab.
C) The window can be resized using window.resizeTo() or window.resizeBy().
D) Resizing is not allowed in modern browsers.

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.



(8) What is the purpose of the window.outerWidth property?

A) It returns the width of the window including the browser chrome (border, toolbar, etc.).
B) It returns the width of the browser's viewport.
C) It changes the width of the window.
D) It returns the width of the content area only.

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.



(9) What is the main difference between window.resizeTo() and window.resizeBy()?

A) resizeTo() sets the size of the window, while resizeBy() resizes it relative to its current size.
B) resizeBy() sets the size, while resizeTo() resizes it relative to its current size.
C) Both methods work the same way.
D) 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.



(10) Which of the following statements about window size control is true?

A) You cannot resize a window once it is opened.
B) Only the window opener can resize the window.
C) Modern browsers allow the resizing of any window.
D) Resizing is restricted for all windows by default.

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.




Prepared by "Ismail Shah"