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.
if statement in Go?if x > 10 {}if (x > 10) {}if x > 10 then {}if x > 10 : {}x := 10 if x > 5 { fmt.Println("Greater") } else { fmt.Println("Smaller") } a) Greaterelse if block in Go?else ifelseifelseelse if is the correct syntaxx := 3 if x > 5 { fmt.Println("Greater") } else if x == 3 { fmt.Println("Equals 3") } else { fmt.Println("Smaller") } if block in Go?{}begin and end;switch statement in Go?switch (x) {}switch x {}switch {x}case x {}case in a switch statement matches the expression?default case if definedswitch statement in Go?switch x { case 1: fmt.Println("One") }switch { x case 1: fmt.Println("One") }switch x: case 1: fmt.Println("One")switch: case x { fmt.Println("One") }switch without an expression in Go?case against truefalsex := 2 switch x { case 1: fmt.Println("One") case 2: fmt.Println("Two") default: fmt.Println("Other") } for loop in Go?for i := 0; i < 10; i++ {}for i in 0..10 {}for i = 0, i < 10, i++ {}for i := 0 to 10 {}for loop in Go?exitbreakreturncontinuecontinue statement in a for loop in Go?for {}for i := 0; i < 10; i++ {}for i := 0; i < 10 {}for i := 0; i-- {}for i := 0; i < 3; i++ { if i == 2 { break } fmt.Println(i) } for loops in Go?exitreturnbreak with labelsfor i := 0; i < 3; i++ { if i == 1 { continue } fmt.Println(i) } for loop for an exact number of iterations in Go?for loop with a set limitwhile statement with a conditiondo loopforwhileforeachdo-whilefor rangefor i := 0; i < len(arr); i++ {}errors package in Go?errors package is used to create a new error?errors.New()errors.Create()errors.Error()errors.Generate()errors.New() return?stringerrorfmt.Errorf()nilnil in Go?if err == nil {}if error == null {}if err.isNil() {}if err != nil {}fmt.Errorf()error.format()errors.Format()fmt.Error()return errorreturn errors.New("error message")return fmt.Errorf("error message")defer in error handling in Go?fmt.Errorf() with %werrors.Wrap()errors.New()nil and handle accordinglytry-catch blocks| QNo | Answer (Option with the text) |
|---|---|
| 1 | a) if x > 10 {} |
| 2 | a) Greater |
| 3 | d) else if is the correct syntax |
| 4 | b) Equals 3 |
| 5 | a) Using curly braces {} |
| 6 | b) switch x {} |
| 7 | b) It defaults to the default case if defined |
| 8 | a) switch x { case 1: fmt.Println("One") } |
| 9 | b) It matches each case against true |
| 10 | b) Two |
| 11 | a) for i := 0; i < 10; i++ {} |
| 12 | b) break |
| 13 | b) It skips the current iteration and moves to the next one |
| 14 | a) for {} |
| 15 | b) 0 1 |
| 16 | c) break with labels |
| 17 | b) 0 2 |
| 18 | d) Both a and b |
| 19 | c) foreach |
| 20 | c) Both a and b |
| 21 | a) To create new errors |
| 22 | a) errors.New() |
| 23 | b) error |
| 24 | a) if err == nil {} |
| 25 | b) It causes a runtime panic |
| 26 | a) fmt.Errorf() |
| 27 | d) Both b and c |
| 28 | a) To delay the execution of a function |
| 29 | a) Using fmt.Errorf() with %w |
| 30 | a) Check if the error is nil and handle accordingly |