The Building Block of a Program

Course 2 – Programming with JavaScript quiz answers

Week 2: The Building Block of a Program

Meta Front-End Developer Professional Certificate

Complete Coursera Answers & Study Guide

Click to Enroll in Coursera Meta Front-End Professional Certificate

The Building Block of a Program INTRODUCTION

The Coursera Meta Front-End Developer Professional Certificate: The Building Block of a Program module explores the fundamentals of objects, arrays, and functions. You will learn how to use them in programming and create your own programs. You’ll also gain an understanding of the most common built-in methods related to these building blocks, as well as develop skills in error handling and defensive programming. This module will also help you differentiate between undefined, null, and empty strings.

By the end of this course, you will have a thorough understanding of how objects, arrays, and functions are used to create programs. You will be able to confidently write programs that are robust in terms of 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

Correct: Well done on completing the exercise!

2. Were there any parts of the Practicing with functions exercise that you found difficult to complete?

  • Yes (Correct)
  • No

Correct: If you’ve found any particular part of the exercise as being difficult to complete, think about why that was the case. What was the hardest part of the task? Pin-pointing the most difficult concept will help you understand areas that potentially need to be worked on more.

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

Correct: Great! Being able to describe newly-learned concepts in your own words is a reflection of a better understanding of the subject matter.

SELF REVIEW: CREATING ARRAYS AND OBJECTS

1. Did you complete the Creating arrays and objects exercise?

  • Yes (Correct)
  • No

Correct: 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 (Correct)
  • No

Correct: If you’ve listed any particular part of the exercise as being difficult to complete, think about why that was the case. What was the hardest part of the task? Pin-pointing the most difficult concept will help you understand areas that potentially need to be worked on more.

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

Correct: 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: ARRAYS, OBJECTS AND FUNCTIONS

1. What data type is the variable item ?

  var item = [];
  • Boolean
  • Function
  • Array (Correct)

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

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

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

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)

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

Correct: 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)

Correct: That’s correct! Math.random() returns a random value between 0.0 and 1.0.

Correct: That’s correct! Math.round() rounds a decimal value up to the closest integer.

Correct: That’s correct! Math.sqrt() returns the mathematical square root of a number.

8. 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;
  • hello(); (Correct)
  • hello(){};

Correct: In JavaScript, you can call a function by typing the function name followed by the open and close parentheses.

9. 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.green
  • colors[3]
  • colors[“green”]
  • colors[2] (Correct)

Correct: Arrays are zero-indexed, so the third item would be called with index number two.

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

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

Correct: That’s right! The ceil() method only rounds upwards to the nearest whole number, which is 3 in this case.

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

  • All properties can be connected to the same object. (Correct)
  • Variable values can’t be changed.
  • It enables you to use shorter property names. (Correct)
  • Objects can hold more types of values than arrays can.

13. 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 + name ); (Correct)
  • console.log( parting.pop() );
  • All of the above will work.

Correct: That’s right! The string values can be joined with a plus operator, or by using the concat method.

Correct: That’s right. The string values can be joined with a plus operator, or by using the concat method.

14. Which of the following errors occur when you write a piece of code that JavaScript cannot read?

  • TypeError
  • ReferenceError
  • SyntaxError (Correct)

Correct: SyntaxError occurs when you write a piece of code that JavaScript cannot read.

15. The try catch JavaScript statement can keep a program running even when it encounters an error. True or false?

  • True
  • False (Correct)

Correct: Well done. The try catch statement keeps a program running when it encounters an error.

16. What is the expected output of the following code?

 var food;
 console.log(food);
  • Nothing will be displayed.
  • The console.log will result in the message ‘ReferenceError’.
  • The console.log will display a value of ‘food’.
  • The console.log will display a value of ‘undefined’. (Correct)

Correct: When declared with ‘var’, an unassigned variable will be initialized with the value ‘undefined’.

SELF REVIEW: ERROR PREVENTION

1. Did you complete the Error Prevention exercise?

  • Yes (Correct)
  • No

Correct: Well done on completing the exercise!

2. Were there any parts of the Error Prevention exercise that you found difficult to complete?

  • Yes (Correct)
  • No

Correct: If you’ve listed any particular part of the exercise as being difficult to complete, think about why that was the case. What was it that was hard to complete? Pin-pointing the most difficult concept will help you understand areas that potentially need to be worked on more.

3. Would you say that you are able to explain, in your own words, how to prevent errors in JavaScript?

  • Yes (Correct)
  • No

Correct: Great! Being able to describe newly-learned concepts in your own words is a reflection of a better understanding of the subject matter.

SELF REVIEW: DEFENSIVE PROGRAMMING

1. Did you complete the Defensive Programming exercise?

  • Yes (Correct)
  • No

Correct: Well done on completing the exercise!

2. Were there any parts of the Defensive Programming exercise that you found difficult to complete?

  • Yes (Correct)
  • No

Correct: If you’ve listed any particular part of the exercise as being difficult to complete, think about why that was the case. What was it that was hard to complete? Pin-pointing the most difficult concept will help you understand areas that potentially need to be worked on more.

3. Would you say that you are able to explain, in your own words, how to code a function declaration defensively?

  • Yes (Correct)
  • No

Correct: 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

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

Correct: 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)

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

Correct: 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)

Correct: Throwing an error will stop the execution of the code.

Coursera Meta Front-End Developer Professional Certificate Answers and Study Guide

Liking our content? Then don’t forget to ad us to your bookmarks so you can find us easily!

Weekly Breakdown | Meta Study Guides | Back to Top

MODULE QUIZ: THE BUILDING BLOCKS OF A PROGRAM

1. What data type is the variable  x ?

  var x = {};
  • Function
  • Array
  • Object (Correct)

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
  • lettuce (Correct)
  • tomato sauce
  • onion

Correct: 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

Correct: 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)

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 (Correct)
  • 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

Correct: 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

Correct: 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

Correct: 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 Block of a Program CONCLUSION

Now that you’ve reached the end of this course, you should be comfortable working with objects, arrays and functions in JavaScript. You know how to use the most common built-in methods, and can differentiate between undefined, null and empty strings. Finally, you understand both error handling and defensive programming – two essential skills for any programmer.

If you’re interested in learning more about programming with JavaScript, we encourage you to join Coursera now!

Subscribe to our site

Get new content delivered directly to your inbox.

Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!