Bird
Raised Fist0
Goprogramming~20 mins

Constants in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Constants Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of constant iota usage
What is the output of this Go program?
Go
package main
import "fmt"
const (
  a = iota
  b
  c = 5
  d
  e = iota
)
func main() {
  fmt.Println(a, b, c, d, e)
}
A0 1 5 5 4
B0 1 5 5 5
C0 1 5 6 4
D0 1 5 6 5
Attempts:
2 left
๐Ÿ’ก Hint
Remember that iota increments on each line, but explicit assignments reset it.
โ“ Predict Output
intermediate
2:00remaining
Constant expression evaluation
What will this Go program print?
Go
package main
import "fmt"
const (
  x = 3
  y = x * 2
  z = y + 4
)
func main() {
  fmt.Println(x, y, z)
}
A3 6 10
B3 6 7
C3 5 9
D3 6 12
Attempts:
2 left
๐Ÿ’ก Hint
Constants can be expressions using other constants.
โ“ Predict Output
advanced
2:00remaining
Constant block with mixed types
What is the output of this Go program?
Go
package main
import "fmt"
const (
  a = 1
  b
  c = "hello"
  d
  e = 3.14
  f
)
func main() {
  fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
}
A1 2 hello hello 3.14 3.15
B1 1 hello hello 3.14 3.14
C1 1 hello hello 3.14 0
D1 1 hello hello 3.14 <nil>
Attempts:
2 left
๐Ÿ’ก Hint
Constants repeat the previous value if no new value is given.
โ“ Predict Output
advanced
2:00remaining
Using iota with bit shifting
What does this Go program print?
Go
package main
import "fmt"
const (
  _ = 1 << (iota * 2)
  A
  B
  C
)
func main() {
  fmt.Println(A, B, C)
}
A2 8 32
B1 4 16
C4 16 64
D0 4 16
Attempts:
2 left
๐Ÿ’ก Hint
iota starts at 0, but the first constant is ignored with underscore.
๐Ÿง  Conceptual
expert
2:00remaining
Why are constants preferred over variables for fixed values?
Which is the best reason to use constants instead of variables for fixed values in Go?
AConstants allow dynamic typing which variables do not.
BConstants use less memory than variables because they are stored on the stack.
CConstants can be changed during program execution for flexibility.
DConstants improve code readability and prevent accidental changes at runtime.
Attempts:
2 left
๐Ÿ’ก Hint
Think about safety and clarity in code.

Practice

(1/5)
1. What is the purpose of using const in Go programming?
easy
A. To declare a value that cannot be changed
B. To declare a variable that can change
C. To create a function
D. To start a loop

Solution

  1. Step 1: Understand the role of const

    The const keyword is used to declare constants, which are fixed values that cannot be changed after being set.
  2. Step 2: Compare options with the definition

    To declare a value that cannot be changed correctly states that const declares a value that cannot be changed, while other options describe unrelated concepts.
  3. Final Answer:

    To declare a value that cannot be changed -> Option A
  4. Quick Check:

    Constants = fixed values [OK]
Hint: Constants hold fixed values, variables do not change [OK]
Common Mistakes:
  • Confusing constants with variables
  • Thinking const declares functions
  • Assuming const starts loops
2. Which of the following is the correct way to declare a constant named Pi with value 3.14 in Go?
easy
A. const Pi = 3.14
B. let Pi = 3.14
C. Pi := 3.14
D. var Pi = 3.14

Solution

  1. Step 1: Recall Go syntax for constants

    In Go, constants are declared using the const keyword followed by the name and value.
  2. Step 2: Identify the correct syntax

    const Pi = 3.14 uses const Pi = 3.14, which is the correct syntax. Options A and C declare variables, and B uses invalid Go syntax.
  3. Final Answer:

    const Pi = 3.14 -> Option A
  4. Quick Check:

    const keyword = correct declaration [OK]
Hint: Use const keyword for fixed values, not var or := [OK]
Common Mistakes:
  • Using var or := instead of const
  • Using let which is not Go syntax
  • Missing the const keyword
3. What will be the output of this Go program?
package main
import "fmt"
const greeting = "Hello"
func main() {
    fmt.Println(greeting)
}
medium
A. greeting
B. Compilation error
C. Hello\n
D. Hello

Solution

  1. Step 1: Understand constant usage in the program

    The constant greeting is declared with value "Hello" and printed in main.
  2. Step 2: Predict the output of fmt.Println

    fmt.Println(greeting) prints the string value of the constant, which is "Hello" without quotes.
  3. Final Answer:

    Hello -> Option D
  4. Quick Check:

    Printing constant value = Hello [OK]
Hint: Print constant name to get its fixed value output [OK]
Common Mistakes:
  • Expecting the name 'greeting' to print
  • Thinking it prints with quotes
  • Assuming a compile error
4. Identify the error in this Go code snippet:
package main
import "fmt"
const x = 10
func main() {
    x = 20
    fmt.Println(x)
}
medium
A. Variable 'x' not declared
B. Cannot assign to constant 'x' after declaration
C. Missing import of fmt package
D. No error, code runs fine

Solution

  1. Step 1: Check constant reassignment

    The code declares x as a constant with value 10, then tries to assign 20 to it, which is not allowed.
  2. Step 2: Verify other possible errors

    There are no other compilation errors; the only issue is reassignment of the constant.
  3. Final Answer:

    Cannot assign to constant 'x' after declaration -> Option B
  4. Quick Check:

    Constants cannot be reassigned [OK]
Hint: Constants cannot be changed after declaration [OK]
Common Mistakes:
  • Trying to assign new value to a constant
  • Ignoring missing imports
  • Confusing constants with variables
5. Given the following Go code, what will be the output?
package main
import "fmt"
const (
    A = iota
    B
    C = 5
    D
)
func main() {
    fmt.Println(A, B, C, D)
}
hard
A. 0 1 2 3
B. 0 1 5 5
C. 0 1 5 3
D. Compilation error due to iota usage

Solution

  1. Step 1: Understand iota behavior in constant blocks

    iota starts at 0 and increments by 1 for each line unless overridden.
  2. Step 2: Analyze each constant value

    A = iota (0), iota increments to 1; B = iota (1), iota to 2; C = 5 (explicit), iota to 3; D = iota (3).
  3. Step 3: Confirm output

    Printing A, B, C, D outputs: 0 1 5 3
  4. Final Answer:

    0 1 5 3 -> Option C
  5. Quick Check:

    iota increments except when overridden [OK]
Hint: iota increments unless constant value is explicitly set [OK]
Common Mistakes:
  • Assuming D equals C's value (5)
  • Thinking iota resets after explicit value
  • Expecting compilation error
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