×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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;
	}
}
  1. Hello World
    Hello World
    Hello World
    Hello World
  2. Hello World
    Hello World
    Hello World
    Hello World
    Hello World
  3. Compilation Error
  4. None of the above

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;
	}
}
  1. 0 1 2 3 4
  2. 1 2 3 4 5
  3. 0 1 2 3 4 5
  4. Compilation Error

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
	}
}
  1. 0 2 4
  2. 2 4
  3. Print 2 infinite times
  4. Compilation Error

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")
}
  1. Hello World
  2. Hello World
    Hello World
  3. Compilation Error
  4. None of the above

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")
	}
}
  1. Hello World
  2. Print nothing on the screen
  3. Compilation Error
  4. None of the above

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++;
    }
}
  1. Yes
  2. No

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
	}
}
  1. A B C D E
  2. 65 66 67 68 69
  3. 65 66 67 68
  4. Compilation Error

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
	}
}
  1. 0 1 1 2 3 5 8 13 21 34
  2. 0 1 1 2 3 5 8 12 25 34
  3. 0 1 1 2 3 5 8 13 26 34
  4. Compilation Error

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. 1 5 2 6 3 7 4 8 5 9
  2. 1 2 3 4 5 5 6 7 8 9
  3. Compilation Error
  4. None of the above

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;
    }
}
  1. 0 4
  2. 0 4 1 5
  3. Compilation Error
  4. None of the above


 
 



Comments and Discussions!

Load comments ↻





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