Variablels Flashcards

1
Q

Declare a variable in go

A

use keyword “var” followed by “name” followed by “type”

e.g. var myString string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

End of statement in go-lang

A
  1. By using new line character

2. By using semicolon

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Declare variable with datatype

A

var myVar string = “asdfa”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Declare variable without datatype

A
var myVar = "stasdf"
// here the type is identified using the literal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Declare variable without var keyword

A
myVar := "asdf"
// here the type is identified using the literal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is lexical scoping of variables in Go?

A

It means, the variable can be accessed only inside the curly braces where it was declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
is it going to work?
myVar := "asd"
func main(){
    fmt.Println("hello world" + myVar)
}
A

No, outside the body function, only “non-declarative” statements are allowed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is created here?

const myVar string = “asdfasf”

A

We created a constant here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is there any difference between scoping of a constant and a variable?

A

No, there is no difference between them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is it correct syntax?

var ( a = 23; b = “asd”; c= 234.23)

A

Yes, here we are creating multiple variables using single var keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What this function will do?

fmt.Scanf()

A

This will read input from standard inputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How many loops are there in Go-lang?

A

Only one, for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Syntax of for loop?

A
for  {
  // all codes goes here
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is it necessary to provide for expression inside a parenthesis?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Syntax of if-else

A
if  {
 // if block
} else {
 // else block
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is wrong here
if x == 5
fmt.Println(“5 is incorrect”)

A

After If statement, curly braces are mandatory

17
Q
What is wrong here
if x==5 {
// if block
}
else {
// else block
}
A

The else statement must be started where “if” ends.

18
Q

Switch case syntax in go lang

A

switch {
case 0 :

case 1:

}

19
Q

Is it mandatory to use “break” statement in switch-case

A

No