CIT 171 Chapter 8 Review
In JavaScript, how are an object and an interface related to each other?
An interface allows another program to access an object's properties and methods.
Suppose you have written constructors for two JavaScript object classes, Fruit and Kiwi, and you want the properties and methods of the Fruit class (the base class) to be shared with the Kiwi class. What statement can you use to chain these object classes together in this way?
Kiwi.prototype = new Fruit();
Suppose you have written a JavaScript for loop, and one of the statements that will execute with each iteration of the loop includes an anonymous function that uses the for loop's counter variable, i. Which statement about this loop is true?
The anonymous function will execute when it is called by the program, using the value of i at the time it was encountered in the loop.
Which statement about object-oriented programming in JavaScript is correct?
The code and data within an object cannot be read or modified directly by other programs.
A JavaScript closure is created when _____.
a copy is made of a function that includes the lexical environment of variables used within that function
What should be placed in the blank in the following JavaScript code to add a custom method?let piggyBank = {currentMoney: null,_____};
addMoney: function(amount) {this.currentMoney += amount;return currentMoney;}
Custom objects _____.
are objects created by a programmer for specific tasks
Suppose you have written code to create a JavaScript class called bear. What command should you use to add a method called hibernate to the template for the bear class (not to its constructor)?
bear.prototype.hibernate = function() {return "Zzzzzzz...";}
Suppose the web page you are working on displays several product images, which are represented in your JavaScript code by an array. You want to use a for loop to create an onclick event handler for each item in this array so that you can trigger the execution of a function when any of the product images on the page is clicked. You recognize that _____.
because of closures, you can use the loop counter variable in the function
In JavaScript, the following statement _____.let town = {name: "Helena",county: "Shelby",population: 18500};
creates an associative array containing three key-value pairs
Every JavaScript object has a constructor function that acts as a template for all the properties and methods associated with the object's class, and a prototype, which can be thought of as a machine to instantiate objects.
false
In the following JavaScript statement, "Wizard", "Staff of wisdom", and 115 are called keys.let character = {class: "Wizard",weapon: "Staff of wisdom",healthLevel: 115};
false
Suppose you have written JavaScript constructors for two object classes called ClassA and ClassB and then the statement ClassA.prototype = new ClassB();. ClassB can use the value returned by one of ClassA's methods in one of its own methods because ClassB inherits all the properties and methods of ClassA in this situation.
false
The following JavaScript statements will successfully log the values contained in the snacks array to the console.let snacks = ["almond", "pecan", "walnut", "pistachio", "peanut"];for (let prop in snacks) {console.log(prop + " is " + snacks[prop]);}
false
When you need to store some properties of an object in a JSON text string so that you can supply data to another program in this format, you should use the JSON.parse() method and pass in the list of properties to include as part of the replacer argument.
false
Suppose you want to define a constructor for objects of the custom shoes class. What should you place in the blank to complete the JavaScript command block for this purpose?_____ this.pairQty = shoePairQty;this.style = shoeStyle;this.sellPair() function() {this.pairQty =- 1;return this.pairQty;};}
function shoes(shoePairQty, shoeStyle) {
The lexical scope of variables, functions, and other objects _____.
is based on their physical location within the source code
Suppose you have just written a JavaScript constructor for a button class with the parameters buttonColor, buttonDiameterInches, and buttonHoles. Which statement can you use to create an instance of button to represent a blue, 0.25-inch button with four holes named button1?
let button1 = new button("blue", 0.25, 4);
Imagine that you've written a JavaScript constructor for a custom clownfish object with a position property and then added the following statements: clownfish.prototype.swimForward = function(distanceInCm) { this.position += distanceInCm; }; Now you would like to apply the swimForward() method to a similar custom object called lionfish. What statement(s) could you use to do this?
let spikey = new lionfish(); clownfish.prototype.swimForward.call(spikey, 50);
Which JavaScript code should you use to create an object literal called toyCarColl with numberCars and cars properties?
let toyCarColl = { numberCars: 3, cars: ["Dumptruck", "Mustang", "Rabbit"]};
Which statement is an example of using an object instance of a built-in JavaScript class?
let userInput = new String("");
Suppose you have just written a JavaScript constructor for a button class and then created an instance of this class called button1. Adding the following code to your program _____. function buttonCollection(ownerName) { this.buttonItems = []; this.owner = ownerName; } let myCollection = new buttonCollection("Mel"); myCollection.buttonItems.push(button1);
nests one custom object within another
What type of method is sizeCategory() in the following JavaScript code?function stones(stoneCount) {this.count = stoneCount;function sizeCategory() {if (stoneCount > 10) return "Big";else return "Small";}this.showCategory = function() {console.log("Category: " sizeCategory()};}}
private
What type of method is showCategory() in the following JavaScript code? function stones(stoneCount) {this.count = stoneCount;function sizeCategory() {if (stoneCount > 10) return "Big";else return "Small";}this.showCategory = function() {console.log("Category: " sizeCategory()};}}
privileged
What type of method is the eat() method for iceCream object instances as defined in the following JavaScript statement?iceCream.prototype.eat = function() {return "Yum, yum!";
public
What JavaScript code belongs in the blank to create an object literal named shoes?let shoes = new Object();_____
shoes.pairQty = null;shoes.sellPair = function() { this.pairQty =- 1; return pairQty;};
Suppose you plan to write code for an object literal in JavaScript that includes a method. You can call this method _____.
the same way you call a method for a built-in JavaScript object
Which method inherited by all objects in JavaScript can be used to return a text string representation of an object?
toString()
Once you have created an object literal called toyCarColl with a numberCars property in JavaScript, you can set the value of the numberCars property using the statement(s): _____.
toyCarColl.numberCars = 5; or toyCarColl["numberCars"] = 5;
Attempting to use a variable that is referenced outside its lexical environment, which encompasses functions and the variables they use, will result in an error.
true
Given that a JSON data structure's text has been assigned as the value of the variable dictEntry, the following JavaScript statement will create an object with properties that can be accessed using dot or bracket notation.let dictEntryObj = JSON.parse(dictEntry);
true
In JavaScript, you can use the apply() method to borrow a method from one object class and use it on objects of a different class without defining the second class as an instance of the first class.
true
JavaScript Object Notation is a data interchange format that is not part of JavaScript but employs a similar syntax to organize data as a comma-separated list of key-value pairs.
true
The following JavaScript code will successfully log the phrases "Character class: Wizard," "Character weapon: Staff of wisdom," and "Character healthLevel: 115" to the console.let character = {class: "Wizard",weapon: "Staff of wisdom",healthLevel: 115};for (let prop in character) {console.log("Character " + prop + ": " + character[prop]);}
true
The following JavaScript command adds a method to a built-in class that can be called on any object instance of that class.Array.prototype.scramble = function() {this.sort(function() {return 0.5 - Math.random();});}
true
When you must create several objects that share most of their properties and methods, _____.
you should begin by creating an object class