Home »
Aptitude Questions and Answers »
Golang Aptitude Questions and Answers
Golang goto – Aptitude Questions and Answers
Golang goto – Aptitude Questions and Answers: This section contains aptitude questions and answers on Golang goto statement.
Submitted by Nidhi, on March 26, 2022
1) What will be the output of the following program?
package main
import "fmt"
func main() {
var val int = 0;
XYZ:
fmt.Println("Hello World");
val = val + 1;
if val < 5 {
GOTO XYZ;
}
}
- Hello World
Hello World
Hello World
Hello World
- Hello World
Hello World
Hello World
Hello World
Hello World
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate a syntax error. Because we used "GOTO" in capital letters. The correct "goto" statement can be used in small letters.
2) What will be the output of the following program?
package main
import "fmt"
func main() {
var val int = 0;
XYZ:
fmt.Printf("%d ",val);
val = val + 1;
if val < 5 {
goto XYZ;
}
}
- 0 1 2 3 4
- 1 2 3 4 5
- 0 1 2 3 4 5
- Compilation Error
Correct answer: 1
0 1 2 3 4
In the above program, we created a variable val initialized with 0. Then we printed numbers from 0 to 4 using the "goto" statement.
3) What will be the output of the following program?
package main
import "fmt"
func main() {
var val int = 0
XYZ:
val, _ = fmt.Printf("%d ", val)
if val < 5 {
goto XYZ
}
}
- 0 2 4
- 2 4
- Print 2 infinite times
- Compilation Error
Correct answer: 3
Print 2 infinite times
The above program will print number 2 infinite times. Here fmt.Printf() methods returns 2. So if condition will never false, and print "2" infinite times.
4) What will be the output of the following program?
package main
import "fmt"
func main() {
var val int = 0
val = val + 2
if val < 5 {
goto XYZ
}
}
func PrintHello() {
XYZ:
fmt.Printf("Hello World\n")
}
- Hello World
- Hello World
Hello World
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate a syntax error because we cannot transfer program control from one function to another function using the "goto" statement in the GO program.
5) What will be the output of the following program?
package main
import "fmt"
func main() {
var val int = 0
XYZ:
val = val + 2
if val < 5 {
goto XYZ
fmt.Printf("Hello World\n")
}
}
- Hello World
- Print nothing on the screen
- Compilation Error
- None of the above
Correct answer: 2
Print nothing on the screen
The above program will print nothing on the console screen because fmt.Printf() statement will never be executed in the above program.
6) We can perform iterative activities using the "goto" statement?
package main
import "fmt"
func main() {
i:=1;
for ;; {
if(i==10){
break;
}
fmt.Printf("%d ",i);
i++;
}
}
- Yes
- No
Correct answer: 1
Yes
Yes, we can perform iterative activities using the "goto" statement.
7) What will be the output of the following program?
package main
import "fmt"
func main() {
var val int = 0
var str string = "ABCDE"
XYZ:
fmt.Printf("%d ", str[val])
val = val + 1
if val < 5 {
goto XYZ
}
}
- A B C D E
- 65 66 67 68 69
- 65 66 67 68
- Compilation Error
Correct answer: 2
65 66 67 68 69
In the above program, we created a string. Then we printed the ASCII value of each character using the goto statement on the console screen.
8) What will be the output of the following program?
package main
import "fmt"
func main() {
var num1 int = 0
var num2 int = 1
var num3 int = 0
var num4 int = 3
fmt.Printf("%d", num1)
fmt.Printf(" %d", num2)
Repeat:
num3 = num1 + num2
fmt.Printf(" %d", num3)
num1 = num2
num2 = num3
num4 = num4 + 1
if num4 <= 10 {
goto Repeat
}
}
- 0 1 1 2 3 5 8 13 21 34
- 0 1 1 2 3 5 8 12 25 34
- 0 1 1 2 3 5 8 13 26 34
- Compilation Error
Correct answer: 1
0 1 1 2 3 5 8 13 21 34
The above program will print the Fibonacci series using the "goto” statement on the console screen.
9) What will be the output of the following program?
package main
import "fmt"
func main() {
var num int = 0;
ABC:
num = num + 1;
fmt.Printf("%d ", num);
goto XYZ:
fmt.Printf("%d ", num + 2);
XYZ:
fmt.Printf("%d ", num + 4);
if(num<5){
goto ABC;
}
}
- 1 5 2 6 3 7 4 8 5 9
- 1 2 3 4 5 5 6 7 8 9
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate a syntax error because of the below statement.
goto XYZ:
In the above statement, we used colon ":" instead of the semicolon ";".
10) What will be the output of the following program?
package main
import "fmt"
func main() {
var num int = 0;
ABC:
goto XYZ;
fmt.Printf("%d ", num++);
XYZ:
fmt.Printf("%d ", num + 4);
if(num<5){
goto ABC;
}
}
- 0 4
- 0 4 1 5
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate a syntax error because we cannot use the increment "++" operator in fmt.Printf() function.