Home » 
        Aptitude Questions and Answers » 
        Golang Aptitude Questions and Answers
    
    Golang if-else – Aptitude Questions and Answers
    
    
    
    
	    Golang if-else – Aptitude Questions and Answers: This section contains aptitude questions and answers on Golang if-else.
	    
		    Submitted by Nidhi, on February 26, 2022
	    
    
       
    
        
        1) What will be the output of the following program?
package main
import "fmt"
func main() {
	var xyz = 0
	xyz, _ = fmt.Printf("")
	if xyz == 0 {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
        
            - Hiii
- Okkk
- Compilation Error
- None
            Correct answer: 2
Okkk
            In the above program, we created a variable xyz and initialized 0. Then we assigned the value to the xyz variable returned from fmt.Printf() method. Then fmt.Printf() method returns the number of a printed character. Here, we did not print anything. After that, we checked the value of xyz which is 0, and printed the appropriate message.
         
    
   
        
        
        2) What will be the output of the following program?
package main
import "fmt"
func main() {
    var xyz = 0;
    
    xyz,_= fmt.Printf("ABC ");
    
    if( xyz==4)
        fmt.Printf("Okkk");
    else
        fmt.Printf("Hiii");
}
        
            - ABC Hiii
- ABC Okkk
- Compilation Error
- None
            Correct answer: 3
Compilation Error
            The above program will generate a syntax error because we did not use curly braces "{}" with "if and else" statements. The correct program is given below.
package main
import "fmt"
func main() {
	var xyz = 0
	xyz, _ = fmt.Printf("ABC ")
	if xyz == 4 {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
         
    
      
   
        
        
        3) What will be the output of the following program?
package main
import "fmt"
func main() {
    var xyz = 0;
    
    xyz,_= fmt.Printf("ABC ");
    
    if( xyz==4)
    {
        fmt.Printf("Okkk");
    }
    else
    {
        fmt.Printf("Hiii");
    }
}
        
            - ABC Hiii
- ABC Okkk
- Compilation Error
- None
            Correct answer: 3
Compilation Error
            The above program will generate a syntax error because we did not use curly braces "{}" with "if and else" statements in the proper manner. The correct program is given below.
package main
import "fmt"
func main() {
	var xyz = 0
	xyz, _ = fmt.Printf("ABC ")
	if xyz == 4 {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
         
    
   
    
        
        4) What will be the output of the following program?
package main
import "fmt"
func main() {
    var xyz = 0;
    
    xyz,_= fmt.Printf("ABC ");
    
    if( xyz==4){
        fmt.Printf("Okkk");
    }
    else{
        fmt.Printf("Hiii");
    }
}
        
            - ABC Hiii
- ABC Okkk
- Compilation Error
- None
            Correct answer: 3
Compilation Error
            The above program will generate a syntax error because we did not use curly braces "{}" with "if and else" statements in the proper manner. The correct program is given below.
package main
import "fmt"
func main() {
	var xyz = 0
	xyz, _ = fmt.Printf("ABC ")
	if xyz == 4 {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
         
    
   
 
        
        
        5)  What will be the output of the following program?
package main
import "fmt"
func main() {
    var xyz = 10;
    
    if( xyz=4){
        fmt.Printf("Okkk");
    }else{
        fmt.Printf("Hiii");
    }
}
        
            - Hiii
- Okkk
- Compilation Error
- None
            Correct answer: 3
Compilation Error
            The above program will generate syntax error because we used the assignment operator "=" instead of equal to "==" operator in the "if" condition.
         
    
   
        
        
        6) What will be the output of the following program?
package main
import "fmt"
func main() {
    var xyz = 10;
    
    if(--xyz==10){
        fmt.Printf("Okkk");
    }else{
        fmt.Printf("Hiii");
    }
}
        
            - Hiii
- Okkk
- Compilation Error
- None
            Correct answer: C
Compilation error
            The above program will generate a syntax error because we cannot use the decrement "- -" operator in the "if" condition like that.
         
    
   
        
        
        7) What will be the output of the following program?
package main
import (
	"fmt"
	"unsafe"
)
func main() {
	var X int = 10
	var Y int64 = 20
	if unsafe.Sizeof(X) == unsafe.Sizeof(Y) {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
        
            - Hiii
- Okkk
- Compilation Error
- None
            Correct answer: 2
Okkk
            In this program, we created two variables X, Y using data types "int" and "int64". In Golang, variables created using "int" and "int64", occupy 64-bit memory space. That's why, the size of X, Y are the same.
         
    
   
        
        
        8) What will be the output of the following program?
package main
import "fmt"
func main() {
	var X int = 10
	if X == 10 {
		fmt.Printf("Hello...")
		break
		fmt.Printf("Ok")
	} else {
		fmt.Printf("Hiii")
	}
}
        
            - Hiii
- Hello
- Hello Ok
- Compilation Error
            Correct answer: 4
Compilation Error
            The above program will generate a syntax error because we cannot use the "break" statement within the "if" block.
         
    
   
        
        
        9) What will be the output of the following program?
package main
import (
	"fmt"
	"unsafe"
)
func main() {
	var X = 10
	var Y int32 = 20
	if unsafe.Sizeof(X) == unsafe.Sizeof(Y) {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
        
            - Hiii
- Okkk
- Compilation Error
- None of the above
            Correct answer: 1
Hiii
            In this program, we created a variable X without a specified data type but an initial value of 10. If we create a variable with an initial integer value, then created variable occupies 64-bit memory space. That's why, the size of X, Y are not the same, so printed "Hiii" message.
         
    
   
        
        
        10) What will be the output of the following program?
package main
import "fmt"
func main() {
	if true {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
        
            - Hiii
- Okkk
- Compilation Error
- None of the above
            Correct answer: 2
Okkk
            In this program, we checked the "if" condition, here we used "true", that's why the "Okkk" message will be printed.
         
    
   
    
    11) What will be the output of the following program?
package main
import "fmt"
func main() {
	var flag = true
	if flag {
		fmt.Printf("Okkk")
	} else {
		fmt.Printf("Hiii")
	}
}
    
        - Hiii
- Okkk
- Compilation Error
- None of the above
        Correct answer: 3
Compilation Error
        The above program will generate a syntax error because we imported an "unsafe" package but we did not use it throughout the program.
     
 
    
    12) What will be the output of the following program?
package main
import "fmt"
func main() {
	var val = 100
	if val > 20 {
		if val < 20 {
			fmt.Printf("Heyyy")
		} else {
			fmt.Printf("Hiii")
		}
	}
}
    
        - Hiii
- Heyyy
- HiiiHeyyy
- Compilation Error
        Correct answer: 1
Hiii
        In the above program, we used nested "if", here the value of the "val" variable is greater than 20 or less than 20, that's why the "Hiii" message is printed.
     
 
    
    13) What will be the output of the following program?
package main
import "fmt"
func main() {
	var val = 100
	if val == 100 {
		fmt.Printf("ABC ")
	}
	if 100 == val {
		fmt.Printf("XYZ ")
	}
}
    
        - ABC
- XYZ
- ABC XYZ
- Compilation Error
        Correct answer: 3
ABC XYZ
        In the above program, we created a variable “val" with an initial value of 100. Then we checked the "if" condition and printed "ABC " and "XYZ " messages.
     
 
    
    
    
    
    
    	
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement