Explore key concepts of control flow in Go with this quiz covering loops, if-else statements, and switch cases. Assess your understanding of Go’s conditional logic, iterative structures, and branching mechanisms with practical examples.
Which keyword correctly begins an if statement in Go when checking if variable x is greater than 10?
Explanation: The 'if' keyword is used to begin an if statement in Go. 'when', 'unless', and 'check' are not recognized control structures in Go; they are found in other languages or not used at all. Using any keyword other than 'if' would result in a syntax error.
In a Go switch statement, which keyword is used to specify the default case that executes when no other case matches?
Explanation: The 'default' keyword provides a branch for when no case matches in a Go switch statement. 'fallback' and 'otherwise' are not valid keywords in Go, while 'else' is used with if-else structures, not switch statements. Only 'default' has this purpose in switch blocks.
What is the correct way to write a basic for loop in Go to iterate from 0 to 4 with variable i?
Explanation: The syntax 'for i := 0; i u003C 5; i++ {' is the correct Go for loop format. 'foreach' is not a Go reserved word, 'for (i = 0; i u003C 5; i++) {' uses C-style parentheses, and 'loop i := 0 to 4 {' is not valid Go syntax.
Which is a correct usage of an if statement in Go with a short statement to declare variable err?
Explanation: 'if err := doSomething(); err != nil {' uses a short statement to declare 'err' within the if scope, which is valid in Go. The second option uses incorrect parentheses, the third uses improper assignment (no ':='), and the fourth uses 'let', which Go does not recognize.
What keyword is used in Go to allow execution to continue to the next case in a switch statement?
Explanation: The 'fallthrough' keyword allows execution of the code in the next case in a Go switch block. 'continue' skips the rest of a loop iteration, 'next' is not a Go keyword, and 'break' exits the switch entirely.
Where is the variable i declared in 'for i := 0; i u003C 3; i++ {' accessible in Go?
Explanation: A variable declared with ':=' in a for loop's header is only accessible within the for loop's block. It does not exist outside the loop, so it's not visible elsewhere in the function, inside inner functions unless passed, or globally.
What does a switch statement without an explicit value test in Go (e.g., 'switch { ... }')?
Explanation: A switch with no value in Go lets each case use a boolean condition, effectively acting like multiple if-else if branches. It won't cause a syntax error, nor does it default to false or match all cases unconditionally.
What is the correct way to structure multiple conditional branches in Go using if-else if-else?
Explanation: Go uses the 'if ... { } else if ... { } else { }' syntax for chained conditions. 'elseif', 'then', and other options shown are not part of the Go language syntax.
What is the purpose of the break statement inside a Go for loop?
Explanation: 'break' causes immediate exit from the innermost loop in Go. It does not skip to the next iteration (that's 'continue'), does not terminate the entire program (that requires 'return' or 'os.Exit'), and Go for loops do not have an 'else' clause.
How can you specify that a switch case in Go should match multiple values, such as when variable x is 1, 2, or 3?
Explanation: You can group multiple values by separating them with commas as in 'case 1, 2, 3:'. The '||' operator, parentheses, or range syntax ('1-3') are not valid for this purpose in Go switch cases.