Test Your JavaScript Skills : ES8 (JavaScript 2017) - 45

Part 45

Table of contents

Introduction

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

  1. Identify the output of the below program

     function fx() {
         return new Promise(resolve => {
           setTimeout(() => {
             resolve('resolved');
           }, 1000);
         });
       }
    
       async function asyncFx() {    
         const result = await fx();
         console.log(result); 
       }
    
       asyncFx();
    

    a.resolved

    b.reject

    c.undefined

    d.error

    e.null

    Answer:a

    Reason:The promise is successfully completed.

  2. Identify the output of the below program

     function fx() {
         return new Promise((resolve,reject) => {
           setTimeout(() => {
             reject('resolved');
           }, 1000);
         });
       }
    
       async function asyncFx() {    
         const result = await fx();
         console.log(result); 
       }
    
       asyncFx();
    

    a.reject

    b.error

    c.resolve

    d.undefined

    e.null

    Answer:b

    Reason:The program gives an error.

  3. Identify the output of the below program

    
     async function getData() {
         try {
             let response = await  fetch("jsontest.com/key/value");
             let data = await response.json();
             console.log(data);
         }catch (err) {
                 console.log("problem");
          }
     }
     getData();
    

    a.problem

    b.{key:value}

    c.json

    d.undefined

    e.null

    Answer:a

    Reason:The incorrect url yields into an error.

  4. Identify the output of the below program

     let city = {
         "delhi":1000,    
       };
       console.log(Object.entries(city));
    

    a. [ 'delhi', 1000 ]

    b. { 'delhi', 1000 }

    c. error

    d. [ [ 'delhi', 1000 ] ]

    e. 'delhi', 1000

    Answer:d

    Reason:The output is [ [ 'delhi', 1000 ] ].

  5. Identify the output of the below program

    
     let city = {
         "delhi":1000,  
       };
    
       for (let [k,v] of Object.entries(city))
         console.log(k,v);
    

    a.[ 'delhi', 1000 ]

    b. { 'delhi', 1000 }

    c. error

    d. [ [ 'delhi', 1000 ] ]

    e. 'delhi', 1000

    Answer:e

    Reason:Individual values are extracted and printed.

  6. Identify the output of the below program

     let city = {    
         "mumbai":2000,
         "bangalore":1500
       };
    
     console.log(Object.values(city));
    

    a.[2000,1500]

    b.[2000]

    c.[1500]

    d.[mumbai:2000]

    e.[bangalore:1500]

    Answer:a

    Reason:The values() function only extracts the values.

  7. Identify the output of the below program

     let city = {    
         "mumbai":2000,
         "bangalore":1500
       };
    
       let sum=0;
     for (let obj of Object.values(city)) {
        sum = obj + sum;
     }
     console.log(sum);
    

    a.error

    b.undefined

    c.3500

    d.2000

    e.1500

    Answer:c

    Reason:The values are summed up.

  8. Identify the output of the below program

     let city = {
         "delhi":1000,
      };
     let obj = Object.getOwnPropertyDescriptors(city);
     console.log(obj.delhi.enumerable);
    

    a.true

    b.false

    c.delhi

    d.1000

    e.error

    Answer:a

    Reason:The enumerable is true by default.

  9. Identify the output of the below program

    
     async function getData() {
         try {
             let response = await  fetch("http://echo.jsontest.com/key/value");
             let data = await response.json();
             console.log(data);
         }catch (err) {
                 console.log("error ");
          }
     }
    
     getData();
    

    a.{key:value}

    b.error

    c.echo

    d.undefined

    e.10

    Answer:a

    Reason:The value returned is {key:value}.

  10. Identify the output of the below program

    function fx() {
        return new Promise((resolve,reject) => {
          setTimeout(() => {
            reject('resolved');
          }, 1000);
        });
      }
    
      async function asyncFx() {    
        try {
        const result = await fx();
        }catch (err) {
          console.log(err); 
        }
      }
    
      asyncFx();
    

    a.resolved

    b.reject

    c.error

    d.undefined

    e.null

    Answer:a

    Reason:When error occurs catch block is called and prints resolved.

Did you find this article valuable?

Support Mahavir DS Rathore by becoming a sponsor. Any amount is appreciated!