-
-
Save pradhuman7d1/1eeba0bde0f5c6d38af348e0fa485326 to your computer and use it in GitHub Desktop.
Kotlin Arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q. What should be the datatype of elements stored in an array? | |
A. In Kotlin you can store multiple datatypes into an array, so it can be any type of element. | |
Q. How to find if an item is present in an array or not? | |
A. We can use the contains function to check if an item is present or not. It return true/false. | |
Q. What is the output of indexOf function? | |
A. indexOf function as defined by the name returns the index of the element that the user search for and return -1 if element is not present in array. | |
Q. Can we construct an array that can auto assign values of elements corrosponding to its index? | |
A. Yes, we can do that by using Array function i.e. val arr = Array(size, {x -> x * x}), so this will initialize the values as squares of the index, this function can be defined as per the requirement. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q. What is the syntax to declare and assign values to an array? | |
a) val arr = {1, 2, 3, 4, 5} | |
b) val arr = ArrayOf(1, 2, 3, 4, 5) | |
c) val arr = arrayOf(1, 2, 3, 4, 5) | |
d) val arr = Array(1, 2, 3, 4, 5) | |
A. c) val arr = arrayOf(1, 2, 3, 4, 5) | |
Q. How to find the number of elements in an array arr? | |
a) arr.length | |
b) arr.size | |
c) arr.length() | |
d) arr.size() | |
A. b) arr.size | |
Q. The elements of this array are : val arr = Array(5, {x -> x * x}) | |
a) (0, 1, 2, 3, 4) | |
b) (1, 2, 3, 4, 5) | |
c) (0, 1, 4, 9, 16) | |
d) (1, 4, 9, 16, 25) | |
A. C) (0, 1, 4, 9, 16) | |
Q. How to create a new array using the elements from a given array arr containing values (5, 10, 15, 20, 25, 30, 35, 40, 45, 50) that has the values (10, 15, 20, 25)? | |
a) var ans = copyOfRange(arr{2, 5}) | |
b) var ans = arr.copyOfRange(2, 6) | |
c) var ans = arr.copyOfRange(1, 5) | |
d) var ans = arr.copyOfRange(1, 4) | |
A. c) var ans = arr.copyOfRange(1, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment