Recall & Review
beginner
What does the
var keyword do in Go?It declares a variable with a specified type and optionally assigns it a value.
Click to reveal answer
beginner
How do you declare an integer variable named
age using var?You write:
var age intThis creates a variable
age of type int with a default value 0.Click to reveal answer
intermediate
Can you declare multiple variables at once using
var? How?Yes. Use parentheses and list variables:
var ( x int y string )This declares
x as int and y as string.Click to reveal answer
beginner
What is the default value of a variable declared with
var but not assigned?It depends on the type: <br>- Numbers: 0 <br>- Strings: empty string <br>- Booleans: false <br>- Pointers: nil
Click to reveal answer
beginner
How to declare and assign a variable in one line using
var?Write:
var name string = "Alice"This declares
name as string and assigns "Alice".Click to reveal answer
What is the correct way to declare a variable
count of type int using var?✗ Incorrect
The correct syntax is
var count int to declare an int variable.What is the default value of a string variable declared with
var but not assigned?✗ Incorrect
Strings default to an empty string "" when declared but not assigned.
Which of these declares multiple variables correctly?
✗ Incorrect
Both
var x, y int and using parentheses with new lines: var (
x int
y string
) are correct.How do you declare and assign a variable
name with value "Bob" using var?✗ Incorrect
The full declaration with type and assignment is
var name string = "Bob".What happens if you declare a variable with
var but do not assign a value?✗ Incorrect
Variables declared with
var get a zero value based on their type automatically.Explain how to declare a variable using
var in Go and what happens if you don't assign a value.Think about how Go handles variables when you only declare them.
You got /4 concepts.
Describe how to declare multiple variables at once using
var and why it might be useful.Consider grouping declarations for neatness.
You got /4 concepts.