programming with javascript coursera week 3 quiz answers
Knowledge check: Introduction to Functional Programming
1. What will print out when the following code runs?
var globalVar = 77;
function scopeTest() {
var localVar = 88;
}
console.log(localVar);
- 77
- 88
- null
- undefined
2. Variables declared using const can be reassigned.
- true
- false
3. When a function calls itself, this is known as _____________.
- Recursion
- Looping
- Higher-order Function
4. What will print out when the following code runs?
function meal(animal) {
animal.food = animal.food + 10;
}
var dog = {
food: 10
};
meal(dog);
meal(dog);
console.log(dog.food);
- 10
- 20
- 30
- 40
5. What value will print out when the following code runs?
function two() {
return 2;
}
function one() {
return 1;
}
function calculate(initialValue, incrementValue) {
return initialValue() + incrementValue() + incrementValue();
}
console.log(calculate(two, one));
- 1
- 2
- 3
- 4
Knowledge check: Introduction to Object-Oriented Programming
6. What will print out when the following code runs?
class Cake {
constructor(lyr) {
this.layers = lyr + 1;
}
}
var result = new Cake(1);
console.log(result.layers);
- 1
- 2
- 3
- 4
7. When a class extends another class, this is called ____________.
- Inheritance
- Extension
8. What will print out when the following code runs?
class Animal {
constructor(lg) {
this.legs = lg;
}
}
class Dog extends Animal {
constructor() {
super(4);
}
}
var result = new Dog();
console.log(result.legs);
- 0
- undefined
- null
- 4
9. What will print out when the following code runs?
class Animal {
}
class Cat extends Animal {
constructor() {
super();
this.noise = "meow";
}
}
var result = new Animal();
console.log(result.noise);
- undefined
- null
- “”
- meow
10. What will print out when the following code runs?
class Person {
sayHello() {
console.log("Hello");
}
}
class Friend extends Person {
sayHello() {
console.log("Hey");
}
}
var result = new Friend();
result.sayHello();
- Hello
- Hey
Knowledge check: Advanced JavaScript Features
11. What will print out when the following code runs?
const meal = ["soup", "steak", "ice cream"]
let [starter] = meal;
console.log(starter);
- soup
- ice cream
- steak
12. The for-of loop works for Object data types.
- true
- false
13. What will print out when the following code runs?
let food = "Chocolate";
console.log(`My favourite food is ${food}`);
- My favourite food is Chocolate
- My favourite food is ${food}
14. What values will be stored in the set collection after the following code runs?
let set = new Set();
set.add(1);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
- 1, 2, 3, 2, 1
- 1, 2, 3
15. What value will be printed out when the following code runs?
let obj = {
key: 1,
value: 4
};
let output = { ...obj };
output.value -= obj.key;
console.log(output.value);
- 1
- 2
- 3
- 4
16. What value will be printed out when the following code runs?
function count(...basket) {
console.log(basket.length)
}
count(10, 9, 8, 7, 6);
- 10, 9, 8, 7, 6
- 1, 2, 3, 4, 5
- 6
- 5
Knowledge Check - JavaScript in the browser
17. In the following code, the type attribute can be omitted.
< script type="text/javascript">
//Comment
< /script >
- true
- false
18. What does the document variable return in JavaScript?
console.log(document);
- The entire body tag of the webpage in the browser’s memory, as a JavaScript object.
- The entire webpage in the browser’s memory, as a JavaScript object.
- The HTML code of the downloaded webpage, as a JavaScript string.
19. What does the following function return?
getElementById('main-heading')
- It doesn’t return anything.
- All elements that have the class attribute with a value main-heading
- The first element that has the id attribute with a value main-heading
- The last element that has the id attribute with a value main-heading
20. After the following code is run, what will happen when the user clicks on a p element in the browser?
document.querySelector('h1').addEventListener('click',
function() {
console.log('clicked');
});
- ‘clicked’ is printed to the console log.
- Nothing.
21. What will be printed when the following code runs?
var result = {
value: 7
};
console.log(JSON.stringify(result));
- {}
- {value: 7}
- {“value”: 7}
Module quiz: Programming Paradigms
22. Variables declared using 'let' can be reassigned.
- true
- false
23. What will print out when the following code runs?
function scopeTest() {
var y = 44;
console.log(x);
}
var x = 33;
scopeTest();
- null
- undefined
- 33
- 44
24. What will print out when the following code runs?
class Cake {
constructor(lyr) {
this.layers = lyr;
}
getLayers() {
return this.layers;
}
}
class WeddingCake extends Cake {
constructor() {
super(2);
}
getLayers() {
return super.getLayers() * 5;
}
}
var result = new WeddingCake();
console.log(result.getLayers());
- 0
- 2
- 5
- 10
25. What will print out when the following code runs?
class Animal {
}
class Dog extends Animal {
constructor() {
this.noise = "bark";
}
makeNoise() {
return this.noise;
}
}
class Wolf extends Dog {
constructor() {
super();
this.noise = "growl";
}
}
var result = new Wolf();
console.log(result.makeNoise());
- bark
- growl
- undefined
26. Consider this code snippet: 'const [a, b] = [1,2,3,4] '. What is the value of b?
- undefined
- 1
- 2
- [1,2,3,4]
27. What value will be printed out when the following code runs?
function count(...food) {
console.log(food.length)
}
count("Burgers", "Fries", null);
- 2
- 3
- “Burgers”, “Fries”, null
- “Burgers”, “Fries”, undefined
28. Which of the following are JavaScript methods for querying the Document Object Model? Select all that apply.
- getElementsByClassName
- getElementsById
- getElementById
- getElementByClassName
- queryAllSelectors
- querySelector
29. Which of the following methods convert a JavaScript object to and from a JSON string?
- JSON.parse
- JSON.stringify
- JSON.fromString
JSON.toString
30. What will be the result of running this code?
const letter = "a"
letter = "b"
- Uncaught TypeError: Assignment to constant variable
- b
- a
- Uncaught SyntaxError: Invalid or unexpected token
31. What is a constructor?
- A function that is called to create an instance of an object.
- An instance of a class.
- A specific object that has been created using the class name.
- An object literal