Go Loops MCQ

1. What is the basic loop construct in Go?

a) for
b) while
c) do-while
d) loop

Answer:

a) for

Explanation:

Go uses the 'for' keyword as its basic and only loop construct.

2. How do you create an infinite loop in Go?

a) for {}
b) for true {}
c) while(true) {}
d) Both a and b

Answer:

d) Both a and b

Explanation:

In Go, an infinite loop can be created using 'for {}' or 'for true {}'.

3. How do you exit a loop in Go?

a) exit()
b) stop
c) break
d) end

Answer:

c) break

Explanation:

The 'break' statement is used to exit a loop in Go.

4. What is the purpose of the 'continue' statement in Go loops?

a) To pause the loop temporarily
b) To skip the current iteration and continue with the next
c) To exit the loop immediately
d) To restart the loop from the beginning

Answer:

b) To skip the current iteration and continue with the next

Explanation:

The 'continue' statement in Go skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

5. How do you write a for loop in Go that iterates from 0 to 9?

a) for i := 0; i <= 9; i++ {}
b) for i := 0; i < 10; i++ {}
c) for (i = 0; i < 10; i++) {}
d) for i in range(0, 10) {}

Answer:

b) for i := 0; i < 10; i++ {}

Explanation:

In Go, a for loop that iterates from 0 to 9 is written as 'for i := 0; i < 10; i++ {}'.

6. Can you declare multiple variables in the initialization statement of a Go for loop?

a) Yes
b) No
c) Only if they are of the same type
d) Only if they are of different types

Answer:

a) Yes

Explanation:

Go allows the declaration of multiple variables in the initialization statement of a for loop.

7. How do you iterate over an array or slice in Go using a loop?

a) Using a for loop with an index
b) Using a while loop
c) Using the range keyword with a for loop
d) Both a and c

Answer:

d) Both a and c

Explanation:

Iteration over an array or slice in Go can be done using a traditional for loop with an index or using the range keyword with a for loop.

8. What is the use of the range keyword in a Go loop?

a) To specify the range of values for a loop variable
b) To iterate over elements of a data structure like an array or map
c) To create a range of numbers
d) To limit the execution time of a loop

Answer:

b) To iterate over elements of a data structure like an array or map

Explanation:

The range keyword in Go is used to iterate over elements of data structures like arrays, slices, maps, and channels.

9. How do you write a loop in Go that continues as long as a condition is true?

a) for condition {}
b) while(condition) {}
c) do {} while(condition);
d) loop while(condition) {}

Answer:

a) for condition {}

Explanation:

In Go, a loop that continues as long as a condition is true is written using the for keyword, e.g., 'for condition {}'.

10. Is it possible to omit all three components (initialization, condition, post) of a Go for loop?

a) Yes
b) No
c) Only the initialization and post can be omitted
d) Only the condition can be omitted

Answer:

a) Yes

Explanation:

In Go, it's possible to omit all three components of the for loop, resulting in an infinite loop.

11. Can a for loop in Go have multiple post statements?

a) Yes, separated by commas
b) No
c) Yes, but only if they are of the same type
d) Yes, separated by semicolons

Answer:

a) Yes, separated by commas

Explanation:

In Go, a for loop can have multiple post statements, separated by commas.

12. How do you nest loops in Go?

a) By writing one loop inside another
b) Using the 'nest' keyword
c) Go does not support nested loops
d) By calling a function with a loop inside another loop

Answer:

a) By writing one loop inside another

Explanation:

Nested loops in Go are created by writing one loop inside another loop.

13. What happens if you omit the loop condition in a Go for loop?

a) The loop becomes an infinite loop
b) The loop will not compile
c) The loop runs once
d) The loop condition defaults to true

Answer:

a) The loop becomes an infinite loop

Explanation:

Omitting the loop condition in a Go for loop turns it into an infinite loop.

14. Can the range keyword in a Go loop return the index or key only?

a) Yes, by omitting the value variable
b) No, it always returns both index and value
c) Yes, but only for arrays
d) Yes, but only for maps

Answer:

a) Yes, by omitting the value variable

Explanation:

When using the range keyword in a Go loop, you can choose to get the index or key only by omitting the value variable.

15. In a Go for loop, is it mandatory to use the post statement?

a) Yes
b) No
c) It depends on the loop condition
d) Only in nested loops

Answer:

b) No

Explanation:

The post statement in a Go for loop is not mandatory. You can omit it if not needed for the logic of your loop.

Leave a Comment

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

Scroll to Top