A constant is a fixed value that cannot be changed during the program's execution. It is declared using the keyword const.
Click to reveal answer
beginner
How do you declare a constant in Go?
Use the <code>const</code> keyword followed by the name, optional type, and value. Example: <code>const Pi = 3.14</code>
Click to reveal answer
beginner
Can you change the value of a constant after it is declared?
No, constants cannot be changed once declared. Trying to assign a new value will cause a compile-time error.
Click to reveal answer
intermediate
What types of values can constants hold in Go?
Constants can hold boolean, numeric (integer, float, complex), and string values.
Click to reveal answer
beginner
What is the difference between a constant and a variable in Go?
A constant's value is fixed and cannot change, while a variable's value can be changed during program execution.
Click to reveal answer
Which keyword is used to declare a constant in Go?
Aconst
Bvar
Clet
Dstatic
✗ Incorrect
The const keyword is used to declare constants in Go.
What happens if you try to change the value of a constant after declaration?
AThe value changes successfully
BCompile-time error occurs
CThe program compiles and runs normally
DRuntime error occurs
✗ Incorrect
Constants cannot be changed after declaration; trying to do so causes a compile-time error.
Which of these can be a constant value in Go?
AA slice
BA struct
CA map
DAn integer
✗ Incorrect
Constants can hold numeric values like integers, but not slices, maps, or structs.
How do you declare multiple constants at once in Go?
AUsing multiple <code>const</code> statements
BUsing <code>var</code> with <code>const</code>
CUsing a <code>const</code> block with parentheses
DUsing <code>let</code> keyword
✗ Incorrect
You can declare multiple constants inside a const block using parentheses.
Which of the following is a valid constant declaration in Go?
Aconst greeting = "Hello"
Bconst number = []int{1,2,3}
Cconst pi = math.Pi
Dconst flag = true
✗ Incorrect
A (string literal) and D (boolean literal) are valid constant declarations. Slices (B) and variables like math.Pi (C) cannot be used as constant initializers.
Explain what a constant is in Go and how it differs from a variable.
Think about whether the value can change after declaration.
You got /3 concepts.
Describe how to declare multiple constants together in Go and give an example.
Use parentheses after the const keyword.
You got /2 concepts.
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
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.
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.
Final Answer:
To declare a value that cannot be changed -> Option A
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
Step 1: Recall Go syntax for constants
In Go, constants are declared using the const keyword followed by the name and value.
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.
Final Answer:
const Pi = 3.14 -> Option A
Quick Check:
const keyword = correct declaration [OK]
Hint: Use const keyword for fixed values, not var or := [OK]