MCQs on Control Flow | Go

Control flow is fundamental to programming. Dive into Go’s control flow structures, including if-else, switch statements, loops (for, break, continue), and error handling with the errors package through these 30 MCQs.


Chapter 3: Control Flow in Go – MCQs

1. If-else and Switch Statements (10 Questions)

  1. Which of the following is the correct syntax for an if statement in Go?
    a) if x > 10 {}
    b) if (x > 10) {}
    c) if x > 10 then {}
    d) if x > 10 : {}
  2. What will be the output of the following code?
    x := 10 if x > 5 { fmt.Println("Greater") } else { fmt.Println("Smaller") }
    a) Greater
    b) Smaller
    c) Compilation error
    d) Nothing
  3. Which keyword is used to start an else if block in Go?
    a) else if
    b) elseif
    c) else
    d) else if is the correct syntax
  4. What will the following code print?
    x := 3 if x > 5 { fmt.Println("Greater") } else if x == 3 { fmt.Println("Equals 3") } else { fmt.Println("Smaller") }
    a) Greater
    b) Equals 3
    c) Smaller
    d) Compilation error
  5. How can you execute multiple statements in an if block in Go?
    a) Using curly braces {}
    b) Using begin and end
    c) Separating statements with commas
    d) Using a semicolon ;
  6. Which of the following is the correct syntax for a switch statement in Go?
    a) switch (x) {}
    b) switch x {}
    c) switch {x}
    d) case x {}
  7. In Go, what happens if no case in a switch statement matches the expression?
    a) It causes a runtime error
    b) It defaults to the default case if defined
    c) It does nothing
    d) It throws a syntax error
  8. Which of the following is a valid switch statement in Go?
    a) switch x { case 1: fmt.Println("One") }
    b) switch { x case 1: fmt.Println("One") }
    c) switch x: case 1: fmt.Println("One")
    d) switch: case x { fmt.Println("One") }
  9. What is the behavior of switch without an expression in Go?
    a) It acts as an infinite loop
    b) It matches each case against true
    c) It causes a compile-time error
    d) It matches only false
  10. What will the following Go code print?
    x := 2 switch x { case 1: fmt.Println("One") case 2: fmt.Println("Two") default: fmt.Println("Other") }
    a) One
    b) Two
    c) Other
    d) Compilation error

2. Loops: for, break, continue (10 Questions)

  1. What is the correct syntax for a for loop in Go?
    a) for i := 0; i < 10; i++ {}
    b) for i in 0..10 {}
    c) for i = 0, i < 10, i++ {}
    d) for i := 0 to 10 {}
  2. How do you break out of a for loop in Go?
    a) exit
    b) break
    c) return
    d) continue
  3. What is the effect of the continue statement in a for loop in Go?
    a) It stops the loop
    b) It skips the current iteration and moves to the next one
    c) It restarts the loop
    d) It terminates the program
  4. Which of the following loops is an infinite loop in Go?
    a) for {}
    b) for i := 0; i < 10; i++ {}
    c) for i := 0; i < 10 {}
    d) for i := 0; i-- {}
  5. What will be the output of the following code?
    for i := 0; i < 3; i++ { if i == 2 { break } fmt.Println(i) }
    a) 0 1 2
    b) 0 1
    c) 0
    d) 1 2
  6. How do you exit multiple nested for loops in Go?
    a) exit
    b) return
    c) break with labels
    d) You cannot exit nested loops
  7. What will the following code output? for i := 0; i < 3; i++ { if i == 1 { continue } fmt.Println(i) }
    a) 0 1 2
    b) 0 2
    c) 1 2
    d) 1
  8. How would you execute a for loop for an exact number of iterations in Go?
    a) Use a for loop with a set limit
    b) Use while statement with a condition
    c) Use do loop
    d) Both a and b
  9. Which of the following is not a valid type of loop in Go?
    a) for
    b) while
    c) foreach
    d) do-while
  10. How can you loop over an array or slice in Go?
    a) Using for range
    b) Using for i := 0; i < len(arr); i++ {}
    c) Both a and b
    d) Only a

3. Error Handling Basics (Errors Package) (10 Questions)

  1. What is the purpose of the errors package in Go?
    a) To create new errors
    b) To print error messages
    c) To handle exceptions
    d) To define error types
  2. Which function in the errors package is used to create a new error?
    a) errors.New()
    b) errors.Create()
    c) errors.Error()
    d) errors.Generate()
  3. What type does errors.New() return?
    a) string
    b) error
    c) fmt.Errorf()
    d) nil
  4. How do you check if an error is nil in Go?
    a) if err == nil {}
    b) if error == null {}
    c) if err.isNil() {}
    d) if err != nil {}
  5. What will happen if an error is returned but not handled in Go?
    a) The program will continue running
    b) It causes a runtime panic
    c) It will be logged automatically
    d) The function will be skipped
  6. Which function is commonly used for error formatting in Go?
    a) fmt.Errorf()
    b) error.format()
    c) errors.Format()
    d) fmt.Error()
  7. How do you return an error in a Go function?
    a) return error
    b) return errors.New("error message")
    c) return fmt.Errorf("error message")
    d) Both b and c
  8. What is the purpose of defer in error handling in Go?
    a) To delay the execution of a function
    b) To handle errors
    c) To skip the error
    d) To retry a function
  9. How can you wrap an existing error with more context in Go?
    a) Using fmt.Errorf() with %w
    b) Using errors.Wrap()
    c) Using errors.New()
    d) You cannot wrap errors in Go
  10. What is the typical pattern for handling errors in Go?
    a) Check if the error is nil and handle accordingly
    b) Always throw errors
    c) Use try-catch blocks
    d) Ignore errors and proceed

Answers

QNoAnswer (Option with the text)
1a) if x > 10 {}
2a) Greater
3d) else if is the correct syntax
4b) Equals 3
5a) Using curly braces {}
6b) switch x {}
7b) It defaults to the default case if defined
8a) switch x { case 1: fmt.Println("One") }
9b) It matches each case against true
10b) Two
11a) for i := 0; i < 10; i++ {}
12b) break
13b) It skips the current iteration and moves to the next one
14a) for {}
15b) 0 1
16c) break with labels
17b) 0 2
18d) Both a and b
19c) foreach
20c) Both a and b
21a) To create new errors
22a) errors.New()
23b) error
24a) if err == nil {}
25b) It causes a runtime panic
26a) fmt.Errorf()
27d) Both b and c
28a) To delay the execution of a function
29a) Using fmt.Errorf() with %w
30a) Check if the error is nil and handle accordingly

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top