Go Control Flow: Loops, If-Else, and Switch Essentials Quiz Quiz

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.

  1. Basic If Statement Syntax

    Which keyword correctly begins an if statement in Go when checking if variable x is greater than 10?

    1. when
    2. check
    3. if
    4. unless

    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.

  2. Default Value in Switch Statements

    In a Go switch statement, which keyword is used to specify the default case that executes when no other case matches?

    1. else
    2. otherwise
    3. default
    4. fallback

    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.

  3. For Loop in Go

    What is the correct way to write a basic for loop in Go to iterate from 0 to 4 with variable i?

    1. for (i = 0; i u003C 5; i++) {
    2. foreach i in 0..4 {
    3. loop i := 0 to 4 {
    4. for i := 0; i u003C 5; 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.

  4. Short Statement in If

    Which is a correct usage of an if statement in Go with a short statement to declare variable err?

    1. if (err := doSomething()) err != nil {
    2. if err := doSomething(); err != nil {
    3. if let err = doSomething(); err != nil {
    4. if err = doSomething(); err != nil {

    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.

  5. Switch Case Fallthrough

    What keyword is used in Go to allow execution to continue to the next case in a switch statement?

    1. break
    2. continue
    3. next
    4. fallthrough

    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.

  6. Loop Variable Scope

    Where is the variable i declared in 'for i := 0; i u003C 3; i++ {' accessible in Go?

    1. Globally in the package
    2. Only inside the for loop
    3. Everywhere in the function
    4. Only inside any inner function

    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.

  7. Switch Without Condition

    What does a switch statement without an explicit value test in Go (e.g., 'switch { ... }')?

    1. It always results in syntax error
    2. It matches all cases unconditionally
    3. It tests each case as a boolean expression
    4. It defaults all cases to false

    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.

  8. If-Else Chain Format

    What is the correct way to structure multiple conditional branches in Go using if-else if-else?

    1. if ... { } elseif ... { } else { }
    2. if ... then { } else if ... { }
    3. if ... elseif ... else ...
    4. 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.

  9. Loop Termination with Break

    What is the purpose of the break statement inside a Go for loop?

    1. To immediately exit the loop
    2. To skip the rest of the loop body and start next iteration
    3. To skip to the loop's else clause
    4. To end the entire program

    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.

  10. Switch with Multiple Cases

    How can you specify that a switch case in Go should match multiple values, such as when variable x is 1, 2, or 3?

    1. case 1, 2, 3:
    2. case (1, 2, 3):
    3. case 1 || 2 || 3:
    4. case 1-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.