Home »
Aptitude Questions and Answers »
Golang Aptitude Questions and Answers
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")
}
}
- Hello
OK
- Hello
- OK
- Compilation Error
Correct answer: 2
Hello
In the above program, we created a variable val and initialized 10. Then the value of the val variable is matched with case 5+5, and printed the "Hello" message.
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")
}
}
- Hello
- Hello
OK
- Hello
Hii
- Hello
Hii
OK
Correct answer: 3
Hello
Hii
In the above program, we created a variable val and initialized 10. Then the value of the val variable is matched with case 5+5, and print the "Hello" message. Here, we also used the fallthrough statement. The fallthrough statement is used to execute the next immediate case in the switch block, and printed "Hii" message.
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");
}
}
- Sample
- Sample
Hii
- Hii
- Compilation error
Correct answer: 4
Compilation Error
The above program will generate a syntax error because we cannot use fmt.Printf() method without case block in switch block.
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")
}
}
- Hello
- Default
- Hii
- Compilation error
Correct answer: 2
Default
In the above program, we created a variable val, initialized with 7. Here, we defined 2 cases, "case 5-10" that is "case -5", and "case 11-20" that is "case -9". That's why the default case was executed and printed the "Default" message.
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")
}
}
- Hello
- Default
- Hii
- Compilation error
Correct answer: 3
Hii
In the above program, we created a variable val, initialized with 7. Here, we matched the value of the val variable using the "case val==7" statement and printed the "Hii".
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")
}
}
- Hii
- Hello
- Default
- Compilation Error
Correct answer: 1
Hii
In the above program, we created a variable val, initialized with 7. Here, we matched the value of the val variable using the "case 5,6,7,8" statement and printed the "Hii" message.
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");
}
}
- Hii
- Hello
- Default
- Compilation Error
Correct answer: 4
Compilation Error
The above program will generate a syntax error because of the below case statements.
case 5: || case 6:
case 7: || case 8:
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")
}
}
- Hii
- Hello
- Default
- Compilation Error
Correct answer: 2
Hello
In the above program, we created a switch block. Here, we created a variable val and initialized it with a value of 6. Then the "case 6" will be matched with the value of the val variable and printed the "Hello" message.
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")
}
}
- Hii
Default
- Hello
- Hii
- Compilation error
Correct answer: 1
Hii
Default
In the above program, we created a switch block with unsafe.Sizeof("A")/8 statement.
The above statement returns 16/8 that is 2. Then "case 2" is executed and here we used the fallthrough statement then default block is also executed and printed the "Default" message.
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")
}
}
- Hii
Default
- Hello
- Hii
- Compilation error
Correct answer: 2
Hello
In the above program, we created a switch block with an expression.
Now we evaluate the expression:
= (2*2+5)%2
= (4+5)%2
= (9)%2
= 1
Then the case 1 gets executed and printed "Hello" message.
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")
}
}
- Hii
Default
- Hello
- Hii
- Compilation error
Correct answer: 4
Compilation error
The above program will generate a syntax error, because "case 10-5" (that is case 5) and "case 5" are duplicate cases.
12) What will be the output of the following program?
package main
import "fmt"
func main() {
switch true {
default:
fmt.Printf("Default\n")
}
}
- Default
- Compilation error
- None of the above
- No output
Correct answer: 1
Default
In the above program, we created a switch block with the value true. Here, we defined only the default case. So, it gets executed and printed "Default" message.
13) Can we use string values in switch test conditions?
- Yes
- No
Correct answer: 1
Yes
Yes, we can use string value in switch test condition in Golang.