What will be printed out as the result of the following code execution?
val a = 1;
{
  val a = 2
  print(a)
}
print(a)
Explanation
Once a variable is defined, you can't define a new variable with the same name in the same scope. For example, the following script would not compile:
val a = 1
val a = 2 // Does not compile
print(a)
You can, on the other hand, define a variable in an inner scope that has the same name as a variable in an outer scope. The following script would compile and run:
val a = 1;
{
  val a = 2 // Compiles just fine
  print(a)
}
print(a)
However, it is usually better to choose a new, meaningful variable name rather than to shadow an outer variable.
Source: Variable scope

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback