Explain the scope provided for variables in scala.



Explain the scope provided for variables in scala...

Answer / Gopal Prasad Poddar

In Scala, variables follow lexical (static) scoping rules. Variables can be defined inside blocks, functions, classes, objects, and packages. The scope of a variable extends to all parts of the program that are nested within the block where the variable is declared, including nested functions and classes. If a variable with the same name is redefined within a nested block or function, it hides the outer variable. For example, let's consider the following code snippet:nn```scalanval x = 10 // Global variablendef foo(): Unit = {n val x = 20 // Local variable in function foo()n println(x) // Prints 20n}nfoo()nprintln(x) // Prints 10n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Scala Interview Questions

What is Null in Scala? What is null in Scala? What is difference between Null and null in Scala?

1 Answers  


What is an ofdim method in scala?

1 Answers  


Is scala better than java?

1 Answers  


Explain ‘Scala higher order’ functions?

1 Answers  


Explain what is unit in scala?

1 Answers  


What is object in Scala? Is it a singleton object or instance of a class?

1 Answers  


What do you understand by apply and unapply methods in scala?

1 Answers  


Like Java’s java.lang.Object class, what is the super class of all classes in Scala?

1 Answers  


What is the best language to use with Play framework: Scala or Java?

1 Answers  


What is function composition in scala?

1 Answers  


What is bitset in scala?

1 Answers  


What are the differences between Case class and Normal Class?

1 Answers  


Categories