Test Your JavaScript Skills : ES11 (JavaScript 2020) - 41

Part 41

Table of contents

Introduction

Namaste, In this blog I will discuss 8 code scenarios on Javascript .

  1. Identify the output of the below program

     // mymath.mjs
     export function add(a,b) {
         return a+b;
     }
    
     // program.js
     let mod = './mymath.mjs'
     import(mod).then (arg => {
         console.log("module loaded");
     }).catch(err => {
         console.log("Error");
     });
    

    a. 10

    b. 0

    c.module loaded

    d.crash

    e.Error

    Answer:c

    Reason:The given module is loaded.

  2. Identify the output of the below program

     let prm = [
         Promise.resolve("success")
     ];
    
     Promise.allSettled(prm).then (arg => {
         console.log(arg[0].value);
     });
    

    a.success

    b.failure

    c.fulfilled

    d.error

    e.undefined

    Answer:a

    Reason:The promise returns the value success.

  3. Identify the output of the below program

     let prm = [
         Promise.resolve("success"),
         Promise.reject("failed")
     ];
    
     Promise.allSettled(prm).then (arg => {
         console.log(arg[1].reason);
     });
    

    a.success

    b.failed

    c.fulfilled

    d.undefined

    e.error

    Answer:b

    Reason:The promise returns the reason - failed.

  4. Identify the output of the below program

    
     Promise.allSettled([
         Promise.resolve("ram"),
         new Promise((resolve) => setTimeout(() => resolve(100), 4000)),
         true,
         Promise.reject(new Error("an error")),
       ]).then((values) => {
         console.log(values[2].value);    
       });
    

    a.ram

    b.100

    c.true

    d.undefined

    e.an error

    Answer:c

    Reason:The promise returns the value true.

  5. Identify the output of the below program when the program is executed on Node.js

     console.log(globalThis===global);
    

    a.true

    b.false

    c.error

    d.undefined

    e.0

    Answer:a

    Reason:Both the objects are identical

  6. Identify the output of the below program

    
     Promise.allSettled([
         Promise.resolve("ram"),
         new Promise((resolve) => setTimeout(() => resolve(100), 4000)),
         true,
         Promise.reject(new Error("an error")),
       ]).then((values) => {
         console.log(values[1].value);    
       });
    

    a.100

    b.true

    c.ram

    d.an error

    e.undefined

    Answer:a

    Reason:The promise returns the value 100.

  7. Identify the output of the below program

     // mymath.mjs
     export function add(a,b) {
         return a+b;
     }
    
     // program.js
     let mod = './math.mjs'
     import(mod).then (arg => {
         console.log("module loaded");
     }).catch(err => {
         console.log("error");
     });
    

    a.module loaded

    b.error

    c.undefined

    d.no output

    e.1

    Answer:b

    Reason:The name of the module is incorrect hence there is an error.

  8. Identify the output of the below program

    
     Promise.allSettled([
         Promise.resolve("ram"),
         new Promise((resolve) => setTimeout(() => resolve(100), 4000)),
         true,
         Promise.reject("error"),
       ]).then((values) => {
         console.log(values[3].reason);    
       });
    

    a.ram

    b.100

    c.true

    d.error

    e.undefined

    Answer:d

    Reason:The promise fails hence the reason is error.

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!