×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Looping – Aptitude Questions and Answers

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

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

import "fmt"

func main() {
	for {
		fmt.Println("Hello World")
	}
}
  1. Infinite loop
  2. Compilation Error
  3. Runtime exception
  4. None of the above

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

import "fmt"

func main() {
	for true {
		fmt.Println("Hello World")
	}
}
  1. Infinite loop
  2. Compilation Error
  3. Runtime exception
  4. None of the above

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

import "fmt"

func main() {

	var flag = true

	for i := 1; flag; i++ {
		if i == 10 {
			flag = false
		}
		fmt.Printf("%d ", i)
	}
}
  1. Infinite loop
  2. Compilation Error
  3. 1 2 3 4 5 6 7 8 9
  4. 1 2 3 4 5 6 7 8 9 10

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

import "fmt"

func main() {

	var flag = true

	for i := 1; flag; i++ {
		if i == 10 {
			flag = false
			continue
		}
		fmt.Printf("%d ", i)
	}
}
  1. Infinite loop
  2. Compilation Error
  3. 1 2 3 4 5 6 7 8 9
  4. 1 2 3 4 5 6 7 8 9 10

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

func main() {
    
    var flag=true;
    
    i:=1;
    while flag {
        if(i==10){
            flag=false;
        }
        fmt.Printf("%d ",i);
        i++;
    }
}
  1. Infinite loop
  2. Compilation Error
  3. 1 2 3 4 5 6 7 8 9
  4. 1 2 3 4 5 6 7 8 9 10

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

func main() {   
    i:=1;
    for ;; {
        if(i==10){
            break;
        }
        fmt.Printf("%d ",i);
        i++;
    }
}
  1. Infinite loop
  2. Compilation Error
  3. 1 2 3 4 5 6 7 8 9
  4. 1 2 3 4 5 6 7 8 9 10

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

func main() {
    var name = "Includehelp";
    for index, _ := range name  {
		fmt.Printf("%c ",name[index]);
	}
}
  1. I n c l u d e h e l p
  2. Includehelp
  3. Compilation Error
  4. None of the above

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

func main() {
    for _, val := range "includehelp"  {
		fmt.Printf("%c ",val);
	}
}
  1. I n c l u d e h e l p
  2. Includehelp
  3. Compilation Error
  4. None of the above

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

func main() {
    for _, val := range "ABCD"  {
		fmt.Printf("%d ",val*5);
	}
}
  1. I n c l u d e h e l p
  2. 325 330 335 340
  3. Includehelp
  4. Compilation Error

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

func main() {
    emps := map[int]string{
		101: "Amit Kumar",
		102: "Arun Kumar",
	}

	for _, X := range emps {
		fmt.Println(X);
	}
}
  1. 101
    102
  2. Amit Kumar
    Arun Kumar
  3. Compilation Error
  4. None of the above


 
 



Comments and Discussions!

Load comments ↻





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