programming with javascript coursera week 2 quiz answers
Knowledge check: Arrays, Objects and Functions
1. What data type is the variable item ?
var item = [ ];
- Boolean
- Function
- Array
2. What is the value of result for the following code?
var result = "Hello".indexOf('l');
- 1
- 2
- 3
- 4
3. What is the length of the clothes array after this code runs?
var clothes = [];
clothes.push('gray t-shirt');
clothes.push('green scarf');
clothes.pop();
clothes.push('slippers');
clothes.pop();
clothes.push('boots');
clothes.push('old jeans');
- 1
- 2
- 3
- 4
4. What value is printed out by the following code?
var food = [];
food.push('Chocolate');
food.push('Ice cream');
food.push('Donut');
console.log(food[1])
- Chocolate
- Ice cream
Donut
5. How many properties does the dog object have after the following code is run?
var dog = {
color: "brown",
height: 30,
length: 60
};
dog["type"] = "corgi";
- 1
- 2
- 3
- 4
6. In the following function, the variables a and b are known as _______________.
function add(a, b) {
return a + b;
}
- Parameters
- Return Values
7. Which of the following are functions of the Math object?
- random()
- round()
- sqrt()
- trim()
Knowledge check: Error handling
8. What will be printed when the following code runs?
var result = null;
console.log(result);
- undefined
- null
- 0
9. When the following code runs, what will print out?
try {
console.log('Hello');
} catch(err) {
console.log('Goodbye');
}
- Hello
- Goodbye
10. If you pass an unsupported data type to a function, what error will be thrown?
- RangeError
- SyntaxErrror
- TypeError
11. What will print out when the following code runs?
var x;
if(x === null) {
console.log("null");
} else if(x === undefined) {
console.log("undefined");
} else {
console.log("ok");
}
- null
- undefined
- ok
12. What will print out when the following code runs?
throw new Error();
console.log("Hello");
- Hello
- Nothing will print out.
Module quiz: The Building Blocks of a Program
13. What data type is the variable x ?
var x = {};
- Function
- Array
- Object
14. What will be the output of running the following code?
try {
console.log('hello)
} catch(e) {
console.log('caught')
}
- Uncaught SyntaxError: Invalid or unexpected token.
- Caught
15. What value is printed when the following code runs?
var burger = ["bun", "beef", "lettuce", "tomato sauce", "onion", "bun"]
console.log(burger[2]);
- bun
- beef
- lettuce
- tomato sauce
- onion
16. In the following code, how many methods does the bicycle object have?
var bicycle = {
wheels: 2,
start: function() {
},
stop: function() {
}
};
- 1
- 2
- 3
17. When the following code runs, what will print out?
try {
throw new Error();
console.log('Hello');
} catch(err) {
console.log('Goodbye');
}
- Hello
- Goodbye
18. If you mis-type some code in your JavaScript, what kind of error will that result in?
- RangeError
- SyntaxErrror
- TypeError
19. Will the following code execute without an error?
function add(a, b) {
console.log(a + b)
}
add(3, "4");
- Yes
- No
20. What will be printed when the following code runs?
var result;
console.log(result);
- undefined
- null
- 0
21. What will be the output of the following code?
var str = "Hello";
str.match("jello");
- null
- undefined
- empty string
22. What will be the output of the following code?
try {
Number(5).toPrecision(300)
} catch(e) {
console.log("There was an error")
}
- RangeError
- 5
- e
- “There was an error”