Golang Basics & Syntax Quiz

Golang
0 Passed
0% acceptance

40 in-depth questions covering Go's fundamentals, packages, imports, syntax rules, variables, constants, comments, and formatting — with 10 code examples to solidify understanding.

40 Questions
~80 minutes
1

Question 1

What is the primary design goal of the Go programming language?

A
To be a modern replacement for C++ with garbage collection and concurrency built-in
B
To create the fastest possible compiled language
C
To be a scripting language for web development
D
To replace Java in enterprise applications
2

Question 2

Which statement about Go packages is correct?

A
Every Go file must belong to a package, and the package name matches the directory name
B
Packages are optional and only used for libraries
C
Go uses classes instead of packages for organization
D
Package names must be unique globally
3

Question 3

What is the correct syntax for importing a package in Go?

A
import "fmt"
B
#include <fmt>
C
using fmt;
D
require 'fmt'
4

Question 4

Which of these is a valid Go identifier?

A
_myVar123
B
123invalid
C
my-var
D
var
5

Question 5

What is the difference between var and := in Go?

A
:= is short declaration syntax that infers type and can only be used inside functions
B
var is for constants, := is for variables
C
They are identical and can be used interchangeably
D
var requires explicit type, := cannot be used with types
6

Question 6

What will this code print?

go
package main

import "fmt"

func main() {
    var x int = 10
    y := 20
    fmt.Println(x + y)
}
A
30
B
1020
C
Compilation error
D
No output
7

Question 7

Which of these constant declarations is valid?

A
const Pi = 3.14159
B
const var MaxInt = 2147483647
C
const x int = 42
D
const Pi := 3.14159
8

Question 8

What does gofmt do?

A
Automatically formats Go code according to standard conventions
B
Compiles Go code to machine language
C
Runs Go tests
D
Manages Go dependencies
9

Question 9

What is the output of this program?

go
package main

import "fmt"

func main() {
    const x = 10
    const y = x * 2
    fmt.Println(y)
}
A
20
B
Compilation error
C
10
D
0
10

Question 10

Which comment style is preferred in Go?

A
// for single-line comments
B
/* for multi-line */
C
# for comments
D
-- for comments
11

Question 11

What happens when you declare a variable without initializing it?

A
It gets the zero value for its type
B
Compilation error
C
Runtime error
D
Undefined behavior
12

Question 12

What is the purpose of the main package in Go?

A
It defines the entry point for executable programs
B
It contains all standard library functions
C
It is used for testing only
D
It manages imports
13

Question 13

What will this code output?

go
package main

import "fmt"

func main() {
    var a int
    b := a
    fmt.Printf("%d %d\n", a, b)
}
A
0 0
B
0 undefined
C
Compilation error
D
Garbage values
14

Question 14

Which import style allows renaming a package?

A
import f "fmt"
B
import "fmt" as f
C
import fmt f
D
Cannot rename imports
15

Question 15

What is the output of this constant expression?

go
package main

import "fmt"

func main() {
    const (
        a = iota
        b
        c
    )
    fmt.Println(a, b, c)
}
A
0 1 2
B
1 2 3
C
0 0 0
D
Compilation error
16

Question 16

Why does Go require explicit package imports?

A
To make dependencies clear and compilation fast
B
To allow implicit conversions
C
To support dynamic loading
D
To match Python's style
17

Question 17

What will this program print?

go
package main

import "fmt"

func main() {
    x := 10
    {
        x := 20
        fmt.Print(x, " ")
    }
    fmt.Println(x)
}
A
20 10
B
10 20
C
20 20
D
Compilation error
18

Question 18

Which of these is NOT a basic syntax rule in Go?

A
Statements do not need semicolons (usually)
B
Curly braces are required for blocks
C
Parentheses are required around if conditions
D
Functions can return multiple values
19

Question 19

What is the result of this variable declaration?

go
package main

import "fmt"

func main() {
    var x, y int = 1, 2
    fmt.Println(x, y)
}
A
1 2
B
0 0
C
Compilation error
D
1 0
20

Question 20

What does the blank identifier _ do in Go?

A
Ignores values you don't need, avoiding 'unused variable' errors
B
Declares a private variable
C
Creates an anonymous function
D
Imports all exported names
21

Question 21

What is the output of this import example?

go
package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println("Hello")
}
A
Hello
B
Compilation error
C
No output
D
Runtime error
22

Question 22

Which constant declaration is invalid?

A
const MaxValue = 1000
B
const Pi float64 = 3.14159
C
const Debug = true
D
const x = runtime.GOMAXPROCS(0)
23

Question 23

What will this code print?

go
package main

import "fmt"

func main() {
    const x = 5
    var y = x
    fmt.Println(y)
}
A
5
B
Compilation error
C
0
D
undefined
24

Question 24

Why does Go have gofmt?

A
To eliminate debates about code style and ensure consistency
B
To make code run faster
C
To compress source files
D
To generate documentation
25

Question 25

What is the output of this scoping example?

go
package main

import "fmt"

var x = 10

func main() {
    fmt.Println(x)
    x := 20
    fmt.Println(x)
}
A
10\n20
B
20\n20
C
Compilation error
D
10\n10
26

Question 26

Which of these package names is conventional for an executable?

A
main
B
executable
C
program
D
app
27

Question 27

What will this code output?

go
package main

import "fmt"

func main() {
    var x int
    x = 5
    fmt.Println(x)
}
A
5
B
0
C
Compilation error
D
undefined
28

Question 28

What is the purpose of the init function in Go?

A
To perform package-level initialization before main runs
B
To initialize variables
C
To replace main
D
To handle errors
29

Question 29

What is the output of this constant group?

go
package main

import "fmt"

func main() {
    const (
        a = 1 << iota
        b
        c
    )
    fmt.Println(a, b, c)
}
A
1 2 4
B
0 1 2
C
1 1 1
D
Compilation error
30

Question 30

Why are semicolons optional in Go?

A
The compiler inserts them automatically at the end of lines
B
Go doesn't use semicolons
C
They are required but hidden
D
Only for constants
31

Question 31

What will this program print?

go
package main

import "fmt"

func main() {
    x := 10
    if x > 5 {
        fmt.Println("Big")
    }
}
A
Big
B
Compilation error
C
No output
D
Small
32

Question 32

Which of these is a valid comment in Go?

A
// This is a comment
B
/* This is also a comment */
C
# This is not a comment
D
Both A and B
33

Question 33

What is the output of this variable shadowing scenario?

go
package main

import "fmt"

func main() {
    x := 1
    {
        x := 2
        fmt.Print(x)
    }
    fmt.Println(x)
}
A
21
B
11
C
22
D
Compilation error
34

Question 34

What makes Go's syntax different from C-like languages?

A
No parentheses required around if conditions, automatic semicolon insertion
B
Uses indentation instead of braces
C
No keywords at all
D
Requires semicolons everywhere
35

Question 35

What will this code output?

go
package main

import "fmt"

func main() {
    const a = "Hello"
    const b = a + " World"
    fmt.Println(b)
}
A
Hello World
B
Hello
C
Compilation error
D
HelloWorld
36

Question 36

Why does Go require packages for all files?

A
To organize code into reusable units and manage dependencies
B
To make compilation slower
C
To replace functions
D
To create executables
37

Question 37

What is the output of this import alias example?

go
package main

import f "fmt"

func main() {
    f.Println("Hello")
}
A
Hello
B
Compilation error
C
No output
D
Runtime error
38

Question 38

Which of these variable declarations is invalid at package level?

A
var x int = 10
B
var y = 20
C
z := 30
D
Both A and B
39

Question 39

What is the purpose of gofmt in the Go toolchain?

A
To format code consistently and resolve formatting debates
B
To optimize code performance
C
To generate machine code
D
To manage memory
40

Question 40

What fundamental concept does Go's basic syntax emphasize?

A
Simplicity, clarity, and explicitness in code organization and declarations
B
Maximum expressiveness with minimal syntax
C
Object-oriented programming only
D
Functional programming paradigms

QUIZZES IN Golang