COURSE 8: MOBILE DEVELOPMENT AND JAVASCRIPT QUIZ ANSWERS
Week 2: The Building Blocks of a Program
Meta Android/IOS Developer Professional Certificate
Complete Coursera Answers & Study Guide
Enroll in Coursera Meta IOS Developer Professional Certification
Enroll in Coursera Meta Android Developer Professional Certification
The building Blocks of a Program INTRODUCTION
Here you’ll learn how to use objects, arrays and functions. In addition, you will learn about the most common built-in methods, and the difference between undefined, null and empty strings. And you’ll explore both error handling and defensive programming.
Learning Objectives
- Build and use objects, arrays, and functions
- List some common built-in methods on built-in objects
- Describe handling bugs and errors using try, catch, throw, and defensive programming
- Explain the difference between undefined, null, and empty strings
- Demonstrate how to write basic code using arrays, objects, and functions
SELF REVIEW: PRACTICING WITH FUNCTIONS
1. Did you complete the Practicing with functions exercise?
- Yes (CORRECT)
- No
Well done on completing the exercise!
2. Were there any parts of the Practicing with functions exercise that you found difficult to complete?
- Yes
- No (CORRECT)
Thank you for completing the Practicing with functions exercise.
3. Would you say that you are able to explain, in your own words, how to code functions with loops inside of them?
- Yes (CORRECT)
- No
Great! Being able to describe newly-learned concepts in your own words is a reflection of a better understanding of the subject matter.
4. You are creating a function and write the following code:
function hello() {
console.log(“Hello world”);
}
How do you call your function in your code?
- hello(); (CORRECT)
- hello(){}
- hello;
Correct. In JavaScript, you can call a function by typing the function name followed by the open and close parentheses.
5. You have the following array:
var colors = ["red", "blue", "green", "yellow"];
Which one of the following would you use to call “green” from this array?
- colors[2] (CORRECT)
- colors.green
- colors[“green”]
- colors[3]
Correct! Arrays are zero-indexed, so the third item would be called with index number two.
SELF REVIEW: CREATING ARRAYS AND OBJECTS
1. Did you complete the Creating arrays and objects exercise?
- Yes (CORRECT)
- No
Well done on completing the exercise!
2. Were there any parts of the Creating arrays and objects exercise that you found difficult to complete?
- Yes
- No (CORRECT)
Thank you for completing the Creating arrays and objects exercise.
3. Would you say that you are able to explain, in your own words, how to create arrays and objects in JavaScript?
- Yes (CORRECT)
- No
Great! Being able to describe newly-learned concepts in your own words is a reflection of a better understanding of the subject matter.
4. You have created a “worker” character for your game and would like to assign it several traits. Which of the following best describes the benefits of creating an object for this purpose? Choose all that apply.
- It enables you to use shorter property names. (CORRECT)
- That’s right! Objects tend to allow for shorter names.
- Variable values can’t be changed.
- Objects can hold more types of values than arrays can.
- All properties can be connected to the same object. (CORRECT)
That’s right! Objects can be assigned multiple properties.
5. You have written the following code:
var myNum = Math.ceil(2.49);
console.log(myNum);
When you run this code, what value will be returned? Select the correct answer below:
- 3 (CORRECT)
- 2.49
- 2
- Error
That’s right! The ceil() method only rounds upwards to the nearest whole number, which is 3 in this case.
6. You have the following variables containing string values:
var parting = "Goodbye";
var name = "Robin";
Which of the following can be used to return both of these values as a combined string? Choose all that apply.
- console.log( parting + name ); (CORRECT)
- console.log( parting.concat(name) ); (CORRECT)
- console.log( parting.pop() );
- All of the above will work.
That’s right! The string values can be joined with a plus operator, or by using the concat method.
That’s right. The string values can be joined with a plus operator, or by using the concat method.
KNOWLEDGE CHECK: ARRAYS, OBJECTS AND FUNCTIONS
1. What data type is the variable item ?
var item = [];
- Boolean
- Function
- Array (CORRECT)
That’s correct! [] is the array literal.
2. What is the value of result for the following code?
var result = "Hello".indexOf('l');
- 1
- 2 (CORRECT)
- 3
- 4
That’s correct! Indices start at 0. Therefore the first index of l is 2.
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 (CORRECT)
- 4
That’s correct! 5 items are added (push) to the array and 2 items are removed (pop). The result is 3 items in the array.
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 (CORRECT)
- Donut
That’s correct! Array indices start at 0, so the value at index 1 is Ice cream.
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 (CORRECT)
That’s correct! Additional properties can be assigned after an object is created. In this code, the object is created with three properties (color, height, length) and then a fourth property is assigned.
6. In the following function, the variables a and b are known as _______________.
function add(a, b) {
return a + b;
}
- Parameters (CORRECT)
- Return Values
That’s correct! Parameters are the inputs to a function.
7. Which of the following are functions of the Math object?
- random() (CORRECT)
- round() (CORRECT)
- sqrt() (CORRECT)
- trim()
That’s correct! Math.random() returns a random value between 0.0 and 1.0.
That’s correct! Math.round() rounds a decimal value up to the closest integer.
That’s correct! Math.sqrt() returns the mathematical square root of a number.
8. In your code, you have a value that is stored as ‘18 > 27’. If you use the typeof operator on this value, which one of these outcomes would the console return?
- Boolean (CORRECT)
- String
- Number
- BigInt
Correct! When you compare two numbers using the comparison operator, the return value is either true or false – and the typeof returns ‘Boolean’ as the underlying data type.
SELF REVIEW: ERROR PREVENTION
1. Did you complete the Error Prevention exercise?
- Yes (CORRECT)
- No
Well done on completing the exercise!
2. Were there any parts of the Error Prevention exercise that you found difficult to complete?
- Yes
- No (CORRECT)
Thank you for completing the Error Prevention exercise.
3. Would you say that you are able to explain, in your own words, how to prevent errors in JavaScript?
- Yes (CORRECT)
- No
Great! Being able to describe newly-learned concepts in your own words is a reflection of a better understanding of the subject matter.
4. Which of the following errors occur when you write a piece of code that JavaScript cannot read?
- ReferenceError
- TypeError
- SyntaxError (CORRECT)
Correct. SyntaxError occurs when you write a piece of code that JavaScript cannot read.
5. The try catch JavaScript statement can keep a program running even when it encounters an error. True or false?
- True (CORRECT)
- False
Well done. The try catch statement keeps a program running when it encounters an error.
6. What is the expected output of the following code?
var food;
console.log(food);
- The console.log will display a value of ‘food’.
- The console.log will display a value of ‘undefined’. (CORRECT)
- The console.log will result in the message ‘ReferenceError’.
- Nothing will be displayed.
Correct! When declared with ‘var’, an unassigned variable will be initialized with the value ‘undefined’.
SELF REVIEW: DEFENSIVE PROGRAMMING
1. Did you complete the Defensive Programming exercise?
- Yes (CORRECT)
- No
Well done on completing the exercise!
2. Were there any parts of the Defensive Programming exercise that you found difficult to complete?
- Yes
- No (CORRECT)
Thank you for completing the Defensive Programming exercise.
3. Would you say that you are able to explain, in your own words, how to code a function declaration defensively?
- Yes (CORRECT)
- No
Great! Being able to describe newly-learned concepts in your own words is a reflection of a better understanding of the subject matter.
KNOWLEDGE CHECK: ERROR HANDLING
1. What will be printed when the following code runs?
var result = null;
console.log(result);
- undefined
- null (CORRECT)
- 0
That’s correct! Since the value is initialised with null, null will be output.
2. When the following code runs, what will print out?
try {
console.log('Hello');
} catch(err) {
console.log('Goodbye');
}
- Hello (CORRECT)
- Goodbye
That’s correct! Since there is no error thrown inside the try block, the catch block will not run. Therefore, “Hello” will print out.
3. If you pass an unsupported data type to a function, what error will be thrown?
- RangeError
- SyntaxErrror
- TypeError (CORRECT)
That’s correct! TypeError will be thrown when an incorrect data type is passed to a function.
4. 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 (CORRECT)
- ok
That’s correct! Since the value is not initialised, it will have the undefined data type.
5. What will print out when the following code runs?
throw new Error();
console.log("Hello");
- Hello
- Nothing will print out. (CORRECT)
That’s correct! Throwing an error will stop the execution of the code.
MODULE QUIZ: THE BUILDING BLOCKS OF A PROGRAM
1. What data type is the variable x ?
var x = {};
- Function
- Array
- Object (CORRECT)
That’s correct! {} is the array literal.
2. 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. (CORRECT)
- Caught
Correct, you cannot handle SyntaxErrors in JavaScript using a try catch block.
3. What value is printed when the following code runs?
var burger = ["bun", "beef", "lettuce", "tomato sauce", "onion", "bun"];
console.log(burger[2]);
- bun
- beef (CORRECT)
- lettuce
- tomato sauce
- onion
That’s correct! Indices start at 0. Therefore lettuce will print out as it is at index 2.
4. In the following code, how many methods does the bicycle object have?
var bicycle = {
wheels: 2,
start: function() {
},
stop: function() {
}
};
- 1
- 2 (CORRECT)
- 3
That’s correct! wheels is a property and start and stop are methods.
5. When the following code runs, what will print out?
try {
throw new Error();
console.log('Hello');
} catch(err) {
console.log('Goodbye');
}
- Hello
- Goodbye (CORRECT)
That’s correct! When the error is thrown, the code will execute the code inside the catch statement.
6. If you mis-type some code in your JavaScript, what kind of error will that result in?
- RangeError
- SyntaxErrror
- TypeError (CORRECT)
That’s correct!
7. Will the following code execute without an error?
function add(a, b) {
console.log(a + b)
}
add(3, "4");
- Yes (CORRECT)
- No
That’s correct! Although the code has a bug, it will execute without error. Instead of adding the numbers and printing 7 , it will print 34 .
8. What will be printed when the following code runs?
var result;
console.log(result);
- undefined (CORRECT)
- null
- 0
That’s correct! Since no value was assigned to result , undefined will print out.
9. What will be the output of the following code?
var str = "Hello";
str.match("jello");
- null (CORRECT)
- undefined
- empty string
That’s correct! Since the string “Hello” doesn’t contain the string “jello”, it cannot be matched, and thus an array object with the matching result cannot be created. Hence, the return value is null, signaling the absence of a matching result.
10. 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” (CORRECT)
Correct! The catch block will execute and print because of the Range error.
The building Blocks of a Program CONCLUSION
TBW
Subscribe to our site
Get new content delivered directly to your inbox.
Quiztudy Top Courses
Popular in Coursera
- Meta Marketing Analytics Professional Certificate.
- Google Digital Marketing & E-commerce Professional Certificate.
- Google UX Design Professional Certificate.
- Meta Social Media Marketing Professional Certificate
- Google Project Management Professional Certificate
- Meta Front-End Developer Professional Certificate
Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!