Go Control structures MCQ

1. Which keyword is used for conditional execution in Go?

a) if
b) switch
c) case
d) check

Answer:

a) if

Explanation:

The 'if' keyword is used for conditional execution in Go, allowing the program to execute certain code only if a specified condition is true.

2. What is the syntax for a switch statement in Go?

a) switch variable {}
b) switch (variable) {}
c) switch case variable {}
d) switch variable case {}

Answer:

a) switch variable {}

Explanation:

In Go, a switch statement is written using the 'switch' keyword followed by the variable being switched on, and the cases inside curly braces.

3. How do you specify a default case in a Go switch statement?

a) default:
b) else:
c) otherwise:
d) case default:

Answer:

a) default:

Explanation:

The 'default:' case in a Go switch statement is used to specify the code to run if no other case matches.

4. Can the 'if' statement in Go be used without an 'else'?

a) Yes
b) No
c) Only inside a function
d) Only outside a function

Answer:

a) Yes

Explanation:

In Go, the 'if' statement can be used without an accompanying 'else' statement.

5. How do you write a loop in Go that repeats a block of code while a condition is true?

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

Answer:

d) for condition {}

Explanation:

In Go, the 'for' statement is used for looping, and a 'for' loop with a condition acts like a while loop in other languages.

6. What does a 'break' statement do in a Go loop?

a) Pauses the loop
b) Stops the loop and exits it
c) Breaks the computer
d) Skips the current iteration

Answer:

b) Stops the loop and exits it

Explanation:

The 'break' statement in Go is used to exit a loop immediately, stopping its execution.

7. How do you write an infinite loop in Go?

a) while true {}
b) loop {}
c) for {}
d) forever {}

Answer:

c) for {}

Explanation:

An infinite loop in Go is written using 'for {}', omitting the initialization, condition, and post statements.

8. Is the 'else if' clause available in Go?

a) Yes
b) No
c) Only with a switch statement
d) Only if 'else' is not used

Answer:

a) Yes

Explanation:

Go supports 'else if' for multiple conditional branches in an 'if-else' statement.

9. How do you group multiple cases in a single case clause in a switch statement in Go?

a) By separating them with a comma
b) By using the 'group' keyword
c) By listing them in separate lines under one case
d) It's not possible in Go

Answer:

a) By separating them with a comma

Explanation:

In Go, multiple expressions can be grouped in a single case in a switch statement by separating them with commas.

10. What is the use of the 'continue' statement in Go?

a) Exits the loop
b) Skips the rest of the current loop iteration
c) Continues execution without pausing
d) Restarts the loop

Answer:

b) Skips the rest of the current loop iteration

Explanation:

The 'continue' statement in Go skips the remaining code in the current loop iteration and continues with the next iteration.

11. How is the 'fallthrough' keyword used in Go?

a) To fall to the next case in a switch statement
b) To exit a switch statement
c) In loops, to continue to the next iteration
d) To check for errors

Answer:

a) To fall to the next case in a switch statement

Explanation:

The 'fallthrough' keyword in Go is used in switch statements to indicate that the execution should continue into the next case clause.

12. Can you nest control structures in Go?

a) Yes
b) No
c) Only if they are of the same type
d) Only in functions

Answer:

a) Yes

Explanation:

In Go, you can nest control structures, such as an 'if' statement inside a 'for' loop, or a 'switch' inside an 'if', etc.

13. What does the 'goto' statement do in Go?

a) Sends the program to a labeled statement
b) Exits the program
c) Skips to the next iteration of a loop
d) Throws an error

Answer:

a) Sends the program to a labeled statement

Explanation:

The 'goto' statement in Go transfers control to a statement labeled with the corresponding label.

14. How do you execute different cases based on the type of a variable in Go?

a) Using the 'type' keyword
b) Using a type switch
c) It's not possible in Go
d) Using multiple if-else statements

Answer:

b) Using a type switch

Explanation:

A type switch in Go allows you to compare the type of a variable and execute different cases based on that type.

15. Can a 'for' loop in Go be used to iterate over elements of a slice or an array?

a) Yes, using the 'range' keyword
b) No, 'for' loops are only for conditions
c) Only with arrays
d) Only with slices

Answer:

a) Yes, using the 'range' keyword

Explanation:

In Go, the 'for' loop can be used with the 'range' keyword to iterate over the elements of a slice or an array.

Leave a Comment

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

Scroll to Top