Test Your JavaScript Skills : ES13 (JavaScript 2022) - 36

Part 36

Table of contents

Introduction

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

  1. Identify the output of the below program

     class Myclass {
         static eno;
         static name="ram";    
    
     };
     console.log(Myclass.name);
    

    a.ram

    b.undefined

    c.error

    d.null

    e.1

    Answer:a

    Reason:The public static field can be accessed.

  2. Identify the output of the below program

     class Myclass {
         #eno=10;
         #ename="ram";    
         static show(){
             console.log(Myclass.eno);
         }
     };
     Myclass.show();
    

    a.Show

    b.10

    c.ram

    d.undefined

    e.error

    Answer:d

    Reason:Instance variable cannot be accessed on a class.

  3. Identify the output of the below program

    
     class Myclass2 {
         #data=100;    
         #show() {
            console.log(this.#data);
         }    
         show2(){
             this.#show();
         }
     };
     let obj2 = new Myclass2();
     obj2.show2();
    

    a.100

    b.undefined

    c.error

    d.null

    e.1

    Answer:a

    Reason:Private method can be called internally within a class.

  4. Identify the output of the below program

     class Myclass {
         static eno;
         name="ram";    
         static show() {
             console.log(Myclass.name); 
         }
     };
     Myclass.show();
    

    a.ram

    b.undefined

    c.null

    d.Myclass

    e.error

    Answer:d

    Reason:The class name property is used which returns the name of the class.

  5. Identify the output of the below program

     class Myclass3 {
         static {
             console.log("Static block");
         }
     };
    

    a.Undefined

    b.No output

    c.error

    d.Static block

    e. null

    Answer:d

    Reason:The static block is executed when the class code is parsed.

  6. Identify the output of the below program

     class Myclass2 {
         static #data=100;    
         #show() {
            console.log(Myclass2.#data);
         }    
         show2(){
             this.#show();
         }
     };
     let obj2 = new Myclass2();
     obj2.show2();
    

    a.undefined

    b.error

    c.null

    d.10

    e.100

    Answer:e

    Reason:The private static element can be accessed within the class.

  7. Identify the output of the below program

     class Myclass {
         static eno;
         static #name="ram";    
    
     };
     console.log(Myclass.name);
    

    a.ram

    b.Myclass

    c.error

    d.undefined

    e.null

    Answer:b

    Reason:The default name property of the class is used which identifies the class.

  8. Identify the output of the below program

    
     class Myclass2 {
         static #data=100;    
         #show() {
            console.log(this.#data);
         }    
         show2(){
             this.#show();
         }
     };
     let obj2 = new Myclass2();
     obj2.show2();
    

    a.error

    b.100

    c.undefined

    d.null

    e.1

    Answer:a

    Reason:Static element cannot be accessed on this keyword. The data does not exists on the object.

  9. Identify the output of the below program

     class Myclass3 {
         static #i=10;
         static {
             Myclass3.#i=20;
         }
         static {
             console.log(Myclass3.#i);
         }
     };
    

    a.10

    b.20

    c.error

    d.no output

    e.undefined

    Answer:b

    Reason:The class can have multiple static blocks. They are executed when the class code is parsed in top down order.

  10. Identify the output of the below program

    class Myclass {
        static eno;
        static #ename="ram";    
    
    };
    console.log(Myclass.#ename);
    

    a.ram

    b.error

    c.null

    d.undefined

    e.0

    Answer:b

    Reason:Static private field cannot be accessed outside the class.

Did you find this article valuable?

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