Question

In: Computer Science

Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed...

Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1?

var x;
function  sub1() {
  document.write("x = " + x + "");
}
function  sub2() {
   var x;
   x = 10;
   sub1();
}
x = 5;
sub2();

Solutions

Expert Solution

Using Static Scoping rules:

  • In static scoping, the variable will be always referred to the top-level environment. With the help of the static scoping, it becomes very easy to develop a modular code and also figure out the scope just by analyzing the code.
  • So, looking at the above function it is clear that while running the code by looking at the static scoping rules it will display X = 5.
  • The reason behind it is simply because this represents the global value of x which is the top-level environment of the variable that the local assignment inside the function.

Using Dynamic Scoping rules:

  • In dynamic scoping, the variable will refer to the most recent assignment to the global identifier and will consider each of the identifier with the global stack of the bindings and the occurrence of the identifiers that are being searched and managed in the most recent binding.
  • Hence, in simpler terms, it will display x = 10 as it is later on assigned inside the function changing the global value of x and then will refer to the value assigned by the function.
  • Hence, dynamic scoping makes the code tougher to read but has many use cases in the modern programming languages and is actively used as it helps in saving memory spaces.

Hence, these are the output of the programs according to the different scopes suggested.


Related Solutions

What information should be provided in the scoping section of an audit program? What is the...
What information should be provided in the scoping section of an audit program? What is the importance of each?
(4 pts) What does the function fun print if the language uses static scoping? What does...
(4 pts) What does the function fun print if the language uses static scoping? What does it print with dynamic scoping? x: integer <-- global procedure Assignx(n:integer) x:= n proc printx write_integer(x) proc foo Assignx(1) printx procedure boo x: integer Assignx(2) printx Assignx(0) foo() printx boo() printx
using javascript and without using javascript sort. Sort an array from lowest to highest const x...
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5] ​function sort(array){ // put your code here return //the answer } ​ sort(x) // output: [-5,28,30,201]
Using JavaScript You must submit a file, called indentation.js indentation.js: program to indent The following program...
Using JavaScript You must submit a file, called indentation.js indentation.js: program to indent The following program is difficult to follow because of its indentation: print ( "binary"); var n = Math.pow (2, 31) - 1; var bits = 0; while (n> 0) { bits + = n & 1; n >> = 1; } print ("Number of bits at 1:" + bits); for (var i = 1; i <= bits; i ++) { var str = ""; if (i% 3...
JavaScript Given the following object, log every property name and value to the console using a...
JavaScript Given the following object, log every property name and value to the console using a loop. let myObj = { id: 12 name: 'My Object', class: 'obj', height: 65, likeJavascript: true, data: [1, 53, 23] };
Use a JavaScript program to: Stimulate a coin tossing environment using random functions and using a...
Use a JavaScript program to: Stimulate a coin tossing environment using random functions and using a for loop - toss the coin 100 times and print the number of heads and tails. A detailed answer would be appreciated, I would like to learn from it rather than just know the answer. Thanks in advance!
using Java progam Q1: What will be the value of x after the following code is...
using Java progam Q1: What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 100; } Q2: What will be the value of x after the following statements are executed? int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; break; default: x *= 3; } Q3: What is the output of the following progm? public static void main(String...
Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm...
Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm with the substitution algorithm. Specify the min and max of the message user can enter to encrypt. Specify the length of the key user can enter to encrypt and decrypt the message. document your design by words or diagrams.
11. What is the difference between Interpreted and Compiled program languages? Describe briefly the advantages and...
11. What is the difference between Interpreted and Compiled program languages? Describe briefly the advantages and disadvantages between these two categories of language. Under which category the language Python falls? 12. What is an identifier? List the rules for writing identifiers in Python language. 13. What is a Python statement? Give examples of different ways you can write multiline statements. Explain, with examples, the use of indentation in Python language. 14. How do you write comments in Python? How many...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT