Skip to content

Instantly share code, notes, and snippets.

@pradhuman7d1
Created December 15, 2021 10:14
Show Gist options
  • Save pradhuman7d1/8900089255a0235e7578a63cb631dd3d to your computer and use it in GitHub Desktop.
Save pradhuman7d1/8900089255a0235e7578a63cb631dd3d to your computer and use it in GitHub Desktop.
scala-arrays
Q. What is the difference between array and arraybuffer?
A. Arrays are fixed in size and insert or remove operations are not possible, while arraybuffer is dynamic.
Q. How we initialize an array in scala?
A. val arr = new Array[Int](10) this is the syntax to create an array of size 10.
Q. What we need to import to create an ArrayBuffer?
A. the import we need is - scala.collection.mutable.ArrayBuffer
Q. Can we update the value of an element in an array?
A. Yes, we can update the values but can't add or remove new values.
Q. What is the use of mkstring?
a. To make multiple strings from an array
b. To make an array of strings
c. To join elements of an array into a string
d. None of these
A. c. To join elements of an array into a string
Q. Which elements will get removed from an array buffer (30,40,10,50,60,20) with .remove(1,4)?
a. 30,40,10
b. 30,40,10,50
c. 40,10,50
d. 40,10,50,60
A. d. 40,10,50,60
Q. What will be the elements in arr2 if arr = 10,20,30,40,50 ?
var arr2 = arr.map(x => x - x)
a. 10,20,30,40,50
b. 50,40,30,20,10
c. 0,10,20,30,40
d. 0, 0, 0, 0, 0
A. d. 0, 0, 0, 0, 0
Q. What will be the sortArr if arr = 40,10,30,20,50?
var sortArr = arr.sortWith(_ > _)
a. 10,20,30,40,50
b. 50,40,30,20,10
A. b. 50,40,30,20,10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment