Golang switch – Aptitude Questions and Answers

Golang switch – Aptitude Questions and Answers: This section contains aptitude questions and answers on Golang switch.
Submitted by Nidhi, on March 02, 2022

1) What will be the output of the following program?
package main

import "fmt"

func main() {
	var val = 10

	switch val {
	case 5 + 5:
		fmt.Printf("Hello\n")
	default:
		fmt.Printf("OK\n")
	}
}
  1. Hello
    OK
  2. Hello
  3. OK
  4. Compilation Error

2) What will be the output of the following program?
package main

import "fmt"

func main() {
	var val = 10

	switch val {
	case 5 + 5:
		fmt.Printf("Hello\n")
		fallthrough
	case 11:
		fmt.Printf("Hii\n")
	default:
		fmt.Printf("OK\n")
	}
}
  1. Hello
  2. Hello
    OK
  3. Hello
    Hii
  4. Hello
    Hii
    OK

3) What will be the output of the following program?
package main

import "fmt"

func main() {
    var val=10;
    
    switch val{
        fmt.Printf("Sample\n");
        default:
            fmt.Printf("Default\n");
        case 5:
            fmt.Printf("Hello\n");
        case 10:
            fmt.Printf("Hii\n");
    }
}
  1. Sample
  2. Sample
    Hii
  3. Hii
  4. Compilation error

4) What will be the output of the following program?
package main

import "fmt"

func main() {
	var val = 7

	switch val {
	default:
		fmt.Printf("Default\n")
	case 5 - 10:
		fmt.Printf("Hello\n")
	case 11 - 20:
		fmt.Printf("Hii\n")
	}
}
  1. Hello
  2. Default
  3. Hii
  4. Compilation error

5) What will be the output of the following program?
package main

import "fmt"

func main() {
	var val = 7

	switch {
	case val == 5:
		fmt.Printf("Hello\n")
	case val == 7:
		fmt.Printf("Hii\n")
	default:
		fmt.Printf("Default\n")
	}
}
  1. Hello
  2. Default
  3. Hii
  4. Compilation error

6) What will be the output of the following program?
package main

import "fmt"

func main() {
	var val = 7

	switch val {
	case 1, 2, 3, 4:
		fmt.Printf("Hello\n")
	case 5, 6, 7, 8:
		fmt.Printf("Hii\n")
	default:
		fmt.Printf("Default\n")
	}
}
  1. Hii
  2. Hello
  3. Default
  4. Compilation Error

7) What will be the output of the following program?
package main

import "fmt"

func main() {
    var val=7;
    
    switch val{
        case 5: || case 6:
            fmt.Printf("Hello\n");
        case 7: || case 8:
            fmt.Printf("Hii\n");
        default:
            fmt.Printf("Default\n");
    }
}
  1. Hii
  2. Hello
  3. Default
  4. Compilation Error

8) What will be the output of the following program?
package main

import "fmt"

func main() {

	switch val := 6; val {
	case 6:
		fmt.Printf("Hello\n")
		break
	case 7:
		fmt.Printf("Hii\n")
		break
	default:
		fmt.Printf("Default\n")
	}
}
  1. Hii
  2. Hello
  3. Default
  4. Compilation Error

9) What will be the output of the following program?
package main

import (
	"fmt"
	"unsafe"
)

func main() {
	switch unsafe.Sizeof("A") / 8 {
	case 1:
		fmt.Printf("Hello\n")
		fallthrough
	case 2:
		fmt.Printf("Hii\n")
		fallthrough
	default:
		fmt.Printf("Default\n")
	}
}
  1. Hii
    Default
  2. Hello
  3. Hii
  4. Compilation error

10) What will be the output of the following program?
package main

import (
	"fmt"
	"unsafe"
)

func main() {
	switch unsafe.Sizeof("A") / 8 {
	case 1:
		fmt.Printf("Hello\n")
		fallthrough
	case 2:
		fmt.Printf("Hii\n")
		fallthrough
	default:
		fmt.Printf("Default\n")
	}
}
  1. Hii
    Default
  2. Hello
  3. Hii
  4. Compilation error

11) What will be the output of the following program?
package main

import "fmt"

func main() {

	var val = 5
	switch val {
	case 10 - 5:
		fmt.Printf("Hello\n")
	case 5:
		fmt.Printf("Hii\n")
	default:
		fmt.Printf("Default\n")
	}
}
  1. Hii
    Default
  2. Hello
  3. Hii
  4. Compilation error

12) What will be the output of the following program?
package main

import "fmt"

func main() {
	switch true {
	default:
		fmt.Printf("Default\n")
	}
}
  1. Default
  2. Compilation error
  3. None of the above
  4. No output

13) Can we use string values in switch test conditions?
  1. Yes
  2. No


 
 




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.