Go Arrays MCQ

1. How do you declare an array of 10 integers in Go?

a) var arr [10]int
b) var arr int[10]
c) int arr[10]
d) array arr[10]int

Answer:

a) var arr [10]int

Explanation:

In Go, arrays are declared by specifying the type of the elements and the number of elements within square brackets before the variable name.

2. What is the length of the array declared as var myArray [5]int in Go?

a) 4
b) 5
c) 6
d) 0

Answer:

b) 5

Explanation:

The length of the array 'myArray' is 5, as defined by the number in the square brackets.

3. How do you access the third element in an array named 'data' in Go?

a) data[3]
b) data[2]
c) data(2)
d) data(3)

Answer:

b) data[2]

Explanation:

Arrays in Go are zero-indexed, so the third element is accessed with index 2.

4. In Go, are arrays passed by value or by reference to functions?

a) By value
b) By reference
c) Depends on the array size
d) Depends on the array type

Answer:

a) By value

Explanation:

In Go, arrays are passed by value to functions, meaning a copy of the array is passed.

5. What happens if you try to access an index out of bounds in a Go array?

a) It returns nil.
b) It returns the default value of the array type.
c) It causes a runtime panic.
d) It automatically extends the array.

Answer:

c) It causes a runtime panic.

Explanation:

Accessing an index out of bounds in a Go array causes a runtime panic.

6. How are uninitialized elements in a Go array set?

a) To nil
b) To the zero value of the array's type
c) To an arbitrary memory value
d) The array cannot have uninitialized elements

Answer:

b) To the zero value of the array's type

Explanation:

In Go, uninitialized elements in an array are automatically set to the zero value of the array's type.

7. What is the correct way to initialize an array with specific values in Go?

a) var arr = [5]int{1, 2, 3, 4, 5}
b) var arr [5]int = {1, 2, 3, 4, 5}
c) arr := [5]int(1, 2, 3, 4, 5)
d) [5]int arr = [1, 2, 3, 4, 5]

Answer:

a) var arr = [5]int{1, 2, 3, 4, 5}

Explanation:

In Go, arrays can be initialized with specific values using curly braces following the type and size.

8. Can you change the size of an array after its declaration in Go?

a) Yes, by using append function
b) Yes, by reassigning with a new size
c) No, arrays have a fixed size
d) Yes, by using resize function

Answer:

c) No, arrays have a fixed size

Explanation:

In Go, the size of an array is fixed upon declaration and cannot be changed.

9. What does the 'len' function return when applied to an array in Go?

a) The capacity of the array
b) The number of initialized elements in the array
c) The total size of the array in bytes
d) The number of elements in the array

Answer:

d) The number of elements in the array

Explanation:

The 'len' function in Go returns the number of elements in the array.

10. How can you iterate over an array in Go?

a) Using the 'foreach' loop
b) Using the 'while' loop
c) Using the 'range' keyword
d) Arrays in Go cannot be iterated

Answer:

c) Using the 'range' keyword

Explanation:

The 'range' keyword is used in Go to iterate over elements of an array.

11. What is the default value of elements in an array of type bool in Go?

a) true
b) false
c) nil
d) 0

Answer:

b) false

Explanation:

The default zero value for boolean types in Go, including elements in a boolean array, is 'false'.

12. How do you declare a multi-dimensional array in Go?

a) var arr [][]int
b) var arr [2][3]int
c) var arr (2)(3)int
d) var arr 2×3 int

Answer:

b) var arr [2][3]int

Explanation:

Multi-dimensional arrays in Go are declared by specifying the size of each dimension in square brackets.

13. Is it possible to compare two arrays directly in Go?

a) Yes, if they are of the same type and size
b) No, arrays cannot be compared
c) Yes, regardless of their type and size
d) Only if they are string arrays

Answer:

a) Yes, if they are of the same type and size

Explanation:

In Go, two arrays can be compared directly using the '==' operator if they are of the same type and size.

14. How can you copy an array into another array in Go?

a) Using the '=' operator
b) Using the copy() function
c) Arrays cannot be copied
d) Using the clone() method

Answer:

b) Using the copy() function

Explanation:

The 'copy()' function is used in Go to copy elements from one array to another.

15. What is the output of the 'cap' function when applied to an array in Go?

a) The capacity of the array
b) The number of non-nil elements in the array
c) The number of elements in the array
d) The total size of the array in bytes

Answer:

a) The capacity of the array

Explanation:

The 'cap' function in Go returns the capacity of the array, which is the number of elements it can contain.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top