General and former National Security Advisor HR McMaster in his book “Battlegrounds” mentions the two conflicting forces of change and consistency: diametrically opposed concepts that must be…
This Article is for Absolute Beginners in scala , we will try to learn a basic concept and then try to solve a interview question based on that concept.
Let’s try to understand the above concepts with an help of examples
Scala> { Val x=10 ; x+20, 7} //( What will be the output of this code?)
res0: Int = 7 ( so the output is 7 but why )
because 7 is the last statement in the above block of code
Let’s take another example
scala> { Val x =10 ; x+20} // (What will be the output of this code)
res 1: Int = 30 // in Scala output is result , so it shown as res 0 , res1 etc
in the above code since x+20 was the last statement so it beacme the return statement .
scala> Val var1 = { Val x =10 ; x+20; 10.0}
scala> println(var1) // (What will be the output of this code?)
res2: var1: Double =10.0 (Output is 10.0, Double here is data Type for Decimal number 10.0 )
if you know other traditional programming language , C or C++ or Java , then you definitely understand this data type I hope , if you don’t know then you can comment and ask .
The question below was asked in an coding round of interview
What does the following code print
val cool = Map( “a’’-> “aaa” , “b” -> “bbb” , “a’’ -> “ccc”)
println(cool(“a”))
choose the correct option
ccc
4. It will throw an exception
so the correct answer is option number 2
Option 2 is not correct due to the concept that in a block of code last statement is the return statement, it is correct option (i.e option 2) because of below reason ( Don’t worry if you didn't understand now , i will explain in simple and easy manner
its not this (last statement is the return statement) (it is wrong)
it is this (In a block of code last statement is the return statement)
now block of code is always inside a curly braces i.e {}
but in the given problem curly braces was not used so this concept (In a block of code last statement is the return statement) will not apply here
This has been explained below because we can only understand it if we understand Scala Collections (Like Map, There are other collections also but i will explain only Map in this article)
Map is a scala collections i,e data types which can hold multiple values
Array
List
Tuple
Range
set
Map -> A Map is a collection of Key Value pair
let’s understand by example
Scala> Val xy = Map(1->”aaa”, 2->”bbb”) // (here we have taken key as integer type and Value as String data type)
Scala> xy.get(1)
res3: option[String] = Some(aaa) // this is the output
Scala>xy.get(3) // What will be the output of this code
res4: Option[String]= None // None because this key is not there in (Val xy)
Scala> Val cool= Map(“a”->”aaa”, “b”->”bbb”) // (Here we Have Taken key as String and Value as String)
Scala> Val tom=Map(“x” ->24, “y” -> 25, “z”-> 26) //( here we have taken key as String and Value as Integer)
So as you have understood the concept of Map so now you can very easily tell that for question below which we were trying to solver earlier (I only pasted it below again ) , as duplicate key has been used in question below so applying concept from Note :2
val cool = Map( “a’’-> “aaa” , “b” -> “bbb” , “a’’ -> “ccc”) //
println(cool(“a”))
choose the correct option
ccc
4. It will throw an exception
Res: ccc // ( Output will be ccc i.e option 2, which is latest value of key “a”)
I hope i managed to make you understand the concept well , please follow me for more such content on Scala and Big Data.
There are tons of debates about the impacts of different aspects of video games on those that play. Violent tendencies is one that is widely discussed throughout media. But another fairly large…
Imagine a morning where you don’t feel as stressed as you get ready. Where you already feel centered before you walk out the door, both excited and happy to greet the day. You feel lighter because…
Are you struggling with this whole SEO thing? If you’re older like me, it just doesn’t come naturally. We can learn from the twenty-somethings, they’ve got it figured out. Find Your SEO from REO and…