Skip to content

Instantly share code, notes, and snippets.

@pradhuman7d1
Created December 15, 2021 11:04
Show Gist options
  • Save pradhuman7d1/8ffab6dc2da0e3db121fbcd5ee66cc29 to your computer and use it in GitHub Desktop.
Save pradhuman7d1/8ffab6dc2da0e3db121fbcd5ee66cc29 to your computer and use it in GitHub Desktop.
scala-tuples-maps
Q. What is the difference between arrays ans tuples?
A. Tuples are just like arrays but they are immutable.
Q. Can we traverse a tuple using foreach loop?
A. Yes we can, but we have to use the tuple function .productIterator for doing the same.
Q. What datatypes are allowed for keys or values in a map?
A. No limitations whatsoever, you can store any datatype for keys as well as values.
Q. Can we edit or add new values to a map?
A. No by default maps are immutable, for such operations you have to create a mutable map.
Q. For a tuple ("hi", "hello", "hola", "namaste"), what will be the output of this line of code?
println(s"$tup._2 ; $tup(3)")
a. hola ; namaste
b. hello ; hola
c. hola ; hola
d. hello ; namaste
A. d. hello ; namaste
Q. What will be the output for this snippet?
val map = Map(1 -> 1, 2 -> 2, 3 -> 3)
map("1") = "5"
println(map)
a. HashMap(1 -> 1, 1 -> 5, 2 -> 2, 3 -> 3)
b. HashMap(1 -> 5, 2 -> 2, 3 -> 3)
c. HashMap(1 -> 1, 2 -> 2, 3 -> 3, 1 -> 5)
d. None of these
A. d. None of these
Q. Which is the correct code to create a mutable map?
a. val map = Map("PEP" -> "CODING", 10 -> "TEN", "two" -> 2)
b. val map = collection.MutableMap("PEP" -> "CODING", 10 -> "TEN", "two" -> 2)
c. val map = collection.Map.mutable("PEP" -> "CODING", 10 -> "TEN", "two" -> 2)
d. val map = collection.mutable.Map("PEP" -> "CODING", 10 -> "TEN", "two" -> 2)
A. d. val map = collection.mutable.Map("PEP" -> "CODING", 10 -> "TEN", "two" -> 2)
Q. Which statement is able to print the values of a tuple "tup"?
a. tup.productIterator.foreach{i => print(s"$i" + ", ")}
b. tup.foreach.productIterator{i => print(s"$i" + ", ")}
c. productIterator.tup.foreach{i => print(s"$i" + ", ")}
d. None of these
A. a. tup.productIterator.foreach{i => print(s"$i" + ", ")}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment