Docsity

JavaScript Fundamentals, Exams of Programming Languages

Partial preview of the text

Download JavaScript Fundamentals and more Exams Programming Languages in PDF only on Docsity!

Full Stack JavaScript

Introduction to JavaScript

Knowledge Assessment

Q & S

  1. Multiple Choice: What is the purpose of the 'reduce' method in JavaScript? a) To execute a reducer function on each element of the array, resulting in a single output value. b) To filter out elements in an array based on a condition. c) To find the first element in the array that satisfies a condition. d) To execute a function on each element of the array without creating a new array.

Correct Answer: a) The 'reduce' method in JavaScript is used to execute a

reducer function on each element of the array, resulting in a single output value. It is particularly useful for accumulating values, such as summing numbers or concatenating strings.

  1. Fill-in-the-Blank: The _______ operator is used to import modules in JavaScript ES6.

Correct Answer: The 'import' operator is used to import modules in

JavaScript ES6, allowing for modular and reusable code.

  1. True/False: In JavaScript, 'null' is considered to be the same as '0'.

Correct Answer: False. In JavaScript, 'null' is a primitive value that

represents the intentional absence of any object value, while '0' is a number.

  1. Multiple Choice: Which statement about JavaScript Promises is true? a) A Promise is an object representing the eventual completion or failure of an asynchronous operation.

d) The function that is currently being processed.

Correct Answer: b) The 'this' keyword in JavaScript refers to the object

that owns the currently executing code, and its value is determined by how the function is called.

  1. Fill-in-the-Blank: To handle multiple promises, we can use 'Promise._______' which takes an iterable of promises as an input.

Correct Answer: To handle multiple promises, we can use 'Promise.all'

which takes an iterable of promises as an input, and it is resolved when all of the promises are resolved, or rejected if any promise is rejected.

  1. True/False: The 'forEach' method returns a new array after executing a provided function on every element in the calling array.

Correct Answer: False. The 'forEach' method executes a provided

function on every element in the calling array but does not return a new array; it returns 'undefined'.

  1. Multiple Choice: What is event delegation in JavaScript? a) Assigning a single event listener to manage all events of a particular type for child elements. b) Creating a new event that will trigger other events to execute. c) The process of adding multiple event listeners to a single element. d) The method of preventing default behavior for certain events.

Correct Answer: a) Event delegation in JavaScript involves assigning a

single event listener to manage all events of a particular type for child elements, which helps in managing dynamic elements and improves performance.

  1. Fill-in-the-Blank: '_______' is a JavaScript runtime built on Chrome's V8 JavaScript engine.

Correct Answer: 'Node.js' is a JavaScript runtime built on Chrome's V

JavaScript engine, allowing JavaScript to be run on the server-side.

  1. True/False: The 'map' method in JavaScript calls the provided function once for each element in an array, in order.

Correct Answer: True. The 'map' method calls the provided function

once for each element in an array, in order, and creates a new array with the results.

  1. Multiple Choice: What is the main use of the 'async' keyword in JavaScript? a) To indicate that a function can run in parallel with other functions. b) To make a function return a Promise. c) To pause the execution of a function until a 'Promise' is resolved. d) To declare that a function will automatically execute at the end of the current operation.

Correct Answer: b) The 'async' keyword is used to make a function

return a Promise, simplifying the syntax for working with promises and asynchronous code.

  1. Fill-in-the-Blank: '_______' is the process of converting a JavaScript object into a string with JSON.stringify().

Correct Answer: False

Rationale: JavaScript is a loosely typed language, meaning variables do not have a fixed data type. Multiple Choice: Which function is used to output data to the console in JavaScript? a) print() b) log() c) write() d) console.log()

Correct Answer: d) console.log()

Rationale: console.log() is commonly used to display output in the console. Fill-in-the-Blank: The process of combining two or more arrays in JavaScript is known as __.

Correct Answer: concatenation

Rationale: Concatenation is the process of joining arrays together in JavaScript. True/False: In JavaScript, the === operator performs type coercion.

Correct Answer: False

Rationale: The === operator in JavaScript performs strict comparison without type coercion.

Multiple Choice: Which statement correctly defines a JavaScript function? a) Functions are objects b) Functions cannot return values c) Functions can only be declared within loops d) Functions do not accept parameters

Correct Answer: a) Functions are objects

Rationale: In JavaScript, functions are first-class objects. Fill-in-the-Blank: The process of stopping the further propagation of an event in JavaScript is called __.

Correct Answer: event.stopPropagation()

Rationale: The event.stopPropagation() method prevents the propagation of an event in JavaScript. True/False: JavaScript is a synchronous language by default.

Correct Answer: True

Rationale: JavaScript is single-threaded and synchronous by default. Multiple Choice: Which statement about arrow functions in JavaScript is correct? a) Arrow functions do not have a 'this' keyword b) Arrow functions cannot accept arguments

Rationale: The push() method adds an element to the end of an array in JavaScript. Fill-in-the-Blank: The process of executing the same block of code repeatedly in JavaScript is known as __.

Correct Answer: looping

Rationale: Looping is used to repeat a block of code multiple times in JavaScript. True/False: JavaScript allows for multiple inheritance.

Correct Answer: False

Rationale: JavaScript does not support multiple inheritance, as a class can only inherit from one other class.

  1. Which of the following methods can be used to merge two or more arrays into a new array? a. Array.prototype.concat() b. Array.prototype.join() c. Array.prototype.splice() d. Array.prototype.merge()

Answer: a. Array.prototype.concat()

Rationale: The concat() method is used to merge two or more arrays, returning a new array containing the combined elements.

  1. What is the output of the following code snippet?
    const a = [1, 2, 3]; const b = a; b.push(4); console.log(a); 
    a. [1, 2, 3] b. [1, 2, 3, 4] c. [4, 1, 2, 3] d. [1, 2, 3, undefined]

Answer: b. [1, 2, 3, 4]

Rationale: Arrays in JavaScript are objects and are passed by reference, so b and a point to the same array.

  1. Which of the following is true about JavaScript promises? a. Promises can only be in the pending state.

Rationale: In JavaScript, the type of undefined is 'undefined', and the type of null is 'object' due to a historical bug.

  1. Which JavaScript method is used to parse a JSON string into an object? a. JSON.parse(string) b. JSON.stringify(string) c. JSON.toObject(string) d. JSON.parseObject(string)

Answer: a. JSON.parse(string)

Rationale: JSON.parse(string) is used to convert a JSON string into a JavaScript object.

Fill-in-the-Blank Questions

  1. The keyword _____ is used to declare a block-scoped variable.

Answer: let

Rationale: 'let' is used to declare variables that are limited to the scope of a block statement.

  1. In JavaScript, the _____ method is used to create a shallow copy of an array.

Answer: slice

Rationale: The slice() method returns a shallow copy of a portion of an array into a new array object.

  1. The _____ statement is used to destroy a specific attribute of an object in JavaScript.

Answer: delete

Rationale: The delete statement is used to remove a property from an object.

  1. When using the spread operator for arrays, the syntax is represented by _____.

Answer: ...

Rationale: The spread operator is represented by three dots (...).

  1. A function defined as _____ in JavaScript retains its lexical scope.

Answer: Arrow function

Rationale: Arrow functions retain the lexical scope of the context in which they were defined.

Answer: False

Rationale: The let and const keywords are block-scoped, not function-scoped.

  1. JavaScript does not have block scope until ECMAScript 6.

Answer: True

Rationale: ECMAScript 6 (ES6) introduced block scope with the let and const keywords.

Additional Multiple Choice Questions

  1. Which of the following statements about closures in JavaScript is true? a. Closures can only be created inside loops. b. Closures allow functions to access variables from an enclosing scope even after that scope has finished execution. c. Closures are limited to the function scope and cannot access global variables. d. Closures cannot retain their lexical scope.

Answer: b. Closures allow functions to access variables from an

enclosing scope even after that scope has finished execution.

Rationale: Closures let a function access variables from an enclosing scope even after the enclosing function has returned.

  1. What gets logged to the console with the following code?
    var x = 10; (function() { console.log(x); var x = 20; console.log(x); })(); 
    a. 10, 20 b. 10, undefined c. undefined, 20 d. ReferenceError

Answer: c. undefined, 20

Rationale: Due to variable hoisting, var x inside the function is hoisted to the top and initialized to undefined.

Rationale: Redux implements a predictable state container based on unidirectional data flow.