Test Your JavaScript Skills : ES14 - 32

Part 32

Introduction

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

  1. Identify the output of the below program

     let arr=[11,2];
     let arr2= arr.toSorted();
     console.log(arr);
    

    a.[11,2]

    b.[2,11]

    c.error

    d.11

    e.2

    Answer:a

    Reason:The host array is not sorted.

  2. Identify the output of the below program

     let arr=[11,2];
     let arr2= arr.toReversed();
     console.log(arr2);
    

    a.[11,2]

    b.[2,11]

    c.[1,12]

    d.error

    e.no output

    Answer:b

    Reason:The reversed array is returned.

  3. Identify the output of the below program

     let arr=["ram","Hari"];
     let arr2= arr.toReversed();
     console.log(arr);
    

    a.["Hari","ram"]

    b.error

    c.["ram", "Hari"]

    d.no output

    e.Hari

    Answer:c

    Reason:The host array is not reversed

  4. Identify the output of the below program

     let arr=[11,2];
     let arr2= arr.toReversed();
     console.log(arr);
    

    a.error

    b.[2,11]

    c.no output

    d.[11,2]

    e.11

    Answer:d

    Reason:The host array is not reversed

  5. Identify the output of the below program

     let arr=[11,2];
     let arr2= arr.toSorted();
     console.log(arr2);
    

    a.[11,2]

    b.[2.11]

    c.11

    d.error

    e.2

    Answer:a

    Reason:The sorting is string based.

  6. Identify the output of the below program

     let arr=[20,10,30];
     let arr2= arr.toSorted((a,b)=> a-b);
     console.log(arr2);
    

    a.[30,20,10]

    b.[10,30,20]

    c.[10,20,30]

    d.error

    e.[30,10,20]

    Answer:c

    Reason:The sorting is done using predicate.

  7. Identify the output of the below program

     let arr=["ram","Hari"];
     let arr2= arr.toSorted();
     console.log(arr2);
    

    a.["ram","Hari"]

    b.error

    c.no output

    d.["Hari", "ram"]

    e.ram

    Answer: d

    Reason: The sorting is string based i.e based on ASCII value.

  8. Identify the output of the below program

     let arr=["ram",2,"Hari"];
     let arr2= arr.toSorted();
     console.log(arr2);
    

    a.[2,'Hari','ram']

    b.['ram',2,'Hari']

    c.['ram','Hari']

    d.error

    e.no output

    Answer:a

    Reason:The sorting is string based i.e based on ASCII value.

  9. Identify the output of the below program

     let arr=[20,10,30];
     let arr2= arr.toSorted((a,b)=> b-a);
     console.log(arr2);
    

    a.[10,20,30]

    b.[10,30,20]

    c.[30,20,10]

    d.error

    e.[30,10,20]

    Answer:c

    Reason:The sorting is done using predicate.

  10. Identify the output of the below program

    let arr=[20,,30];
    let arr2= arr.toSorted((a,b)=> a-b);
    console.log(arr2);
    

    a.[20,30]

    b.[20,30,null]

    c.[20,30,undefined]

    d.[20,undefined,30]

    e.error

    Answer:c

    Reason:The empty value is converted to undefined and the array is sorted using predicate.

Did you find this article valuable?

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