Home »
Aptitude Questions and Answers »
Golang Aptitude Questions and Answers
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")
}
}
- Infinite loop
- Compilation Error
- Runtime exception
- None of the above
Correct answer: A
Infinite loop
The above program will print the "Hello World" message.
2) What will be the output of the following program?
package main
import "fmt"
func main() {
for true {
fmt.Println("Hello World")
}
}
- Infinite loop
- Compilation Error
- Runtime exception
- None of the above
Correct answer: 1
Infinite loop
The above program will print the "Hello World" message.
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)
}
}
- Infinite loop
- Compilation Error
- 1 2 3 4 5 6 7 8 9
- 1 2 3 4 5 6 7 8 9 10
Correct answer: 4
1 2 3 4 5 6 7 8 9 10
In the above program, we created a flag variable initialized with "true". Then we used the "for" loop to print numbers from 1 to 10. Here, we terminated the loop when the value of i becomes 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)
}
}
- Infinite loop
- Compilation Error
- 1 2 3 4 5 6 7 8 9
- 1 2 3 4 5 6 7 8 9 10
Correct answer: 3
1 2 3 4 5 6 7 8 9
In the above program, we created a flag variable initialized with "true". Then we used the "for" loop to print numbers from 1 to 10. Here, we changed the value of flag from "true" to "false" when the value of i is 10, But here we used the continue statement, that's why 10 is not printed and the loop gets terminated.
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++;
}
}
- Infinite loop
- Compilation Error
- 1 2 3 4 5 6 7 8 9
- 1 2 3 4 5 6 7 8 9 10
Correct answer: 2
Compilation Error
The above program will generate a syntax error because the "while" keyword is not available in Golang.
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++;
}
}
- Infinite loop
- Compilation Error
- 1 2 3 4 5 6 7 8 9
- 1 2 3 4 5 6 7 8 9 10
Correct answer: 3
1 2 3 4 5 6 7 8 9
The above program will print numbers from 1 to 9 using the "for" loop. Here loop gets terminated, when the value of "i” becomes 10, and the "break" statement is executed.
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]);
}
}
- I n c l u d e h e l p
- Includehelp
- Compilation Error
- None of the above
Correct answer: 1
I n c l u d e h e l p
In the above program, we created a variable name initialized with "Includehelp". Then we accessed the value string character by character based on an index using "range" and printed the "I n c l u d e h e l p" message.
8) What will be the output of the following program?
package main
import "fmt"
func main() {
for _, val := range "includehelp" {
fmt.Printf("%c ",val);
}
}
- I n c l u d e h e l p
- Includehelp
- Compilation Error
- None of the above
Correct answer: 1
I n c l u d e h e l p
In the above program, we accessed the value string character by character using "range" in the "for" loop and printed the "I n c l u d e h e l p" message.
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);
}
}
- I n c l u d e h e l p
- 325 330 335 340
- Includehelp
- Compilation Error
Correct answer: 2
325 330 335 340
In the above program, we accessed the value string character by character using "range" in the "for" loop and multiply the value of ASCII value of character and printed result.
ABCD
A- 65 – (65*5) = 325
B- 66 – (66*5) = 330
C- 67 – (67*5) = 335
D- 68 – (68*5) = 340
Then the result is 325 330 335 340.
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);
}
}
- 101
102
- Amit Kumar
Arun Kumar
- Compilation Error
- None of the above
Correct answer: 2
Amit Kumar
Arun Kumar
In the above program, we created a map for employee details. Then we printed the name of employees using "range" in the "for" loop.