Home Interview Questions and AnswersTechnical Interview Questions and AnswersGo Go Interview Questions and Answers For Freshers Part-2

Go11.Can you declared multiple types of variables in single declaration in Go?
Yes Variables of different types can be declared in one go using type inference.

var a, b, c = 3, 4, “foo”

12.How to print type of a variable in Go?
Following code prints the type of a variable −

var a, b, c = 3, 4, “foo”
fmt.Printf(“a is of type %T\n”, a)

13.What is a pointer?
It’s a pointer variable which can hold the address of a variable.

For example −

var x = 5
var p *int
p = &x
fmt.Printf(“x = %d”, *p)
Here x can be accessed by *p.

14.What is the purpose of break statement?
break terminates the for loop or switch statement and transfers execution to the statement immediately following the for loop or switch.

15.What is the purpose of continue statement?
continue causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

16.What is the purpose of goto statement?
goto transfers control to the labeled statement.

17.Explain the syntax for ‘for’ loop.
The syntax of a for loop in Go programming language is −

for [condition | ( init; condition; increment ) | Range]
{
statement(s);
}
Here is the flow of control in a for loop −

if condition is available, then for loop executes as long as condition is true.

if for clause that is ( init; condition; increment ) is present then

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

if range is available, then for loop executes for each item in the range.

18.Explain the syntax to create a function in Go.
The general form of a function definition in Go programming language is as follows −

func function_name( [parameter list] ) [return_types]
{
body of the function
}
A function definition in Go programming language consists of a function header and a function body. Here are all the parts of a function −

func func starts the declaration of a function.

Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.

Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

Return Type − A function may return a list of values. The return_types is the list of data types of the values the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the not required.

Function Body − The function body contains a collection of statements that define what the function does.

19.Can you return multiple values from a function?
A Go function can return multiple values. For example −

package main
import “fmt”
func swap(x, y string) (string, string) {
return y, x
}
func main() {
a, b := swap(“Mahesh”, “Kumar”)
fmt.Println(a, b)
}

20.In how many ways you can pass parameters to a method?
While calling a function, there are two ways that arguments can be passed to a function −

Call by value − This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

Call by reference − This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

You may also like

Leave a Comment