Bird
Raised Fist0
Goprogramming~20 mins

Variable declaration using var in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Var Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of variable declaration with var and default value
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var x int
    fmt.Println(x)
}
Aundefined
Bnil
C0
D1
Attempts:
2 left
๐Ÿ’ก Hint
Variables declared with var get a default zero value.
โ“ Predict Output
intermediate
2:00remaining
Output of multiple variable declaration using var
What will this Go program print?
Go
package main
import "fmt"
func main() {
    var a, b, c = 1, "hello", true
    fmt.Println(a, b, c)
}
A1 0 true
B1 hello true
C0 hello false
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
var can declare multiple variables with initial values.
โ“ Predict Output
advanced
2:00remaining
Output of var declaration with explicit type and no initial value
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var flag bool
    fmt.Println(flag)
}
Afalse
Btrue
Cnil
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Boolean variables default to false if not initialized.
โ“ Predict Output
advanced
2:00remaining
Output of var declaration with short variable declaration inside function
What will this Go program print?
Go
package main
import "fmt"
func main() {
    var x int = 10
    y := 20
    fmt.Println(x + y)
}
A30
B1020
CCompilation error
D0
Attempts:
2 left
๐Ÿ’ก Hint
The + operator adds integers.
๐Ÿง  Conceptual
expert
2:00remaining
Error caused by var declaration with conflicting types
What error will this Go code produce?
Go
package main
func main() {
    var x int = 5
    var x string = "hello"
}
Ano error, compiles fine
Btype mismatch error
Csyntax error: missing semicolon
Dcannot redeclare x
Attempts:
2 left
๐Ÿ’ก Hint
You cannot declare two variables with the same name in the same scope.

Practice

(1/5)
1. What does the var keyword do in Go?
easy
A. Starts a loop
B. Creates a function
C. Imports a package
D. Declares a variable with a name and type

Solution

  1. Step 1: Understand the role of var

    The var keyword is used to declare variables in Go, giving them a name and a type.
  2. Step 2: Differentiate from other keywords

    Other keywords like func, import, and for serve different purposes, not variable declaration.
  3. Final Answer:

    Declares a variable with a name and type -> Option D
  4. Quick Check:

    var declares variables [OK]
Hint: Remember: var means variable declaration [OK]
Common Mistakes:
  • Confusing var with func or import
  • Thinking var creates loops
  • Assuming var is for constants
2. Which of the following is the correct way to declare an integer variable named count using var in Go?
easy
A. var count int
B. var count = int
C. count var int
D. int var count

Solution

  1. Step 1: Recall Go variable declaration syntax

    In Go, the correct syntax is var variableName type, so var count int is correct.
  2. Step 2: Check other options for syntax errors

    Options A, C, and D do not follow Go's syntax rules for variable declaration.
  3. Final Answer:

    var count int -> Option A
  4. Quick Check:

    var name type = var count int [OK]
Hint: Use 'var name type' format for declaration [OK]
Common Mistakes:
  • Placing var after variable name
  • Using '=' without value
  • Swapping type and var keywords
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
    var x int = 10
    var y int
    y = 5
    fmt.Println(x + y)
}
medium
A. 5
B. 10
C. 15
D. 0

Solution

  1. Step 1: Identify variable values

    Variable x is declared with value 10, y is declared then assigned 5.
  2. Step 2: Calculate the sum printed

    The program prints x + y which is 10 + 5 = 15.
  3. Final Answer:

    15 -> Option C
  4. Quick Check:

    10 + 5 = 15 [OK]
Hint: Add assigned values of variables to find output [OK]
Common Mistakes:
  • Assuming uninitialized y is zero without assignment
  • Confusing variable declaration with initialization
  • Ignoring the assignment to y
4. Find the error in this Go code snippet:
package main
func main() {
    var name string = "Go"
    var age
    age = 10
}
medium
A. Missing type for variable 'age' in declaration
B. Cannot assign value after declaration
C. String value assigned to integer variable
D. Variable 'name' declared incorrectly

Solution

  1. Step 1: Check variable declarations

    The variable age is declared without a type or initial value, which is invalid in Go.
  2. Step 2: Understand Go's requirement for var declarations

    Go requires a type or an initial value when using var. Here, age has neither.
  3. Final Answer:

    Missing type for variable 'age' in declaration -> Option A
  4. Quick Check:

    var needs type or value [OK]
Hint: Always specify type or initial value with var [OK]
Common Mistakes:
  • Declaring var without type or value
  • Assuming assignment after declaration fixes type
  • Confusing var with := shorthand
5. You want to declare two variables, a as an integer with value 5 and b as a string with value "hello", using var. Which is the correct way to do this in Go?
hard
A. var a, b int = 5, "hello"
B. var a int = 5; var b string = "hello"
C. var a = 5, b = "hello"
D. var a int, b string = "hello", 5

Solution

  1. Step 1: Understand type declaration for multiple variables

    In Go, when declaring multiple variables with different types, each must be declared separately or with explicit types.
  2. Step 2: Evaluate each option

    var a, b int = 5, "hello" tries to declare both as int but assigns a string to b, which is invalid. var a = 5, b = "hello" uses shorthand but mixes types without explicit declaration. var a int, b string = "hello", 5 mixes types incorrectly in one statement. var a int = 5; var b string = "hello" correctly declares each variable with its type and value.
  3. Final Answer:

    var a int = 5; var b string = "hello" -> Option B
  4. Quick Check:

    Separate var declarations for different types [OK]
Hint: Declare different types separately with var [OK]
Common Mistakes:
  • Trying to declare different types in one var line
  • Mixing types without explicit declaration
  • Using shorthand := incorrectly with var
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard