This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Write a Go code to compare two slices of a byte. |
|
Answer» We can do this by USING the Compare() method from the bytes package. package mainimport ( "bytes" "fmt")func main() { sl1 := []byte{'I', 'N', 'T', 'E', 'R' , 'V', 'I', 'E', 'W'} sl2 := []byte{'B', 'I', 'T'} // Use Compare function to compare slices res := bytes.Compare(sl1, sl2) if res == 0 { fmt.Println("Equal Slices") } else { fmt.Println("Unequal Slices") }}The output of this code is: Unequal SlicesConclusionGolang was developed with the promise of code efficiency for faster software development. Companies have recognized the scope and benefits of Golang and have started to adapt to this language. Some of the notable companies that have already shifted to Golang are Google, Apple, Facebook, Docker, BBC etc. Furthermore, Golang has raised the excitement level of developers in the open-source community as it’s been a while since a NEW language for the backend has been created. DUE to these reasons, the scope of Golang is growing rapidly. According to the data in Golang Cafe from 2021, the average salary of golang developers in India starts from ₹819,565 to ₹1,617,391 per annum. The prospects and benefits are amazing! ReferencesTo Learn Golang: |
|
| 2. |
Write a Golang code for checking if the given characters are present in a string. |
|
Answer» We can do this by using the CONTAINS() method from the STRINGS package. package main IMPORT ( "fmt" "strings") // Main functionfunc main() { // Create and initialize string1 := "WELCOME to Interviewbit" string2 := "Golang Interview Questions" // Check for presence Using Contains() method of strings package res1 := strings.Contains(string1, "Interview") res2 := strings.Contains(string2, "Go") // Displaying the result fmt.Println("Is 'Interview' PRESENT in string1 : ", res1) fmt.Println("Is 'Go' present in string2: ", res2) }The output of this code is: Is 'Interview' present in string1 : trueIs 'Go' present in string2: true |
|
| 3. |
Write a Go program to find the nth Fibonacci number. |
|
Answer» To find the NTH Fibonacci number, we have to add the previous 2 Fibonacci numbers as shown below. fib(0)=0fib(1)=1fib(2)=1+0 = 1fib(3)=1+1 = 2fib(4)=2+1 = 3: : fib(n)=fib(n-1)+fib(n-2)Code: package mainimport "fmt"//nth fibonacci number functionfunc fibonacci(n int) int { if n < 2 { return n } return fibonacci(n-1) + fibonacci(n-2)}func MAIN() { fmt.Println(fibonacci(7))}The output of this code WOULD be: 13 |
|
| 4. |
Write a GO Program to find factorial of a given number. |
|
Answer» Factorial of a number is the product of multiplication of a number N with every PRECEDING number till it reaches 1. Factorial of 0 is 1. Example:FACT(1) = 1fact(3) = 3 * 2 * 1 = 6fact(5) = 5 * 4 * 3 * 2 * 1 = 120CODE: package mainimport "fmt"//factorial functionfunc factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1)}FUNC main() { fmt.Println(factorial(7))}The output of this code would be: 5040 |
|
| 5. |
Write a Go program to swap variables in a list? |
|
Answer» CONSIDER we have num1=2, num2=3. To swap these two numbers, we can just WRITE: num1,num2 = num2, num1 The same logic can be extended to a list of variables as shown below: package mainimport "fmt"func swapContents(listObj []int) { for i, j := 0, len(listObj)-1; i < j; i, j = i+1, j-1 { listObj[i], listObj[j] = listObj[j], listObj[i] }}func MAIN() { listObj := []int{1, 2, 3} swapContents(listObj) fmt.Println(listObj)}The code results in the output: [3 2 1] |
|