Free GoLang Syntax and Simplicity Questions and Answers — Questions and Answers
Question 1: Which of the following are valid ways to declare and initialize a variable in Go?
- var x int = 10 (Correct answer)
- x := 10 (Correct answer)
- int x = 10
- var x = "hello" (Correct answer)
Correct answer: var x int = 10
Go offers several valid ways to declare and initialize variables. `var x int = 10` explicitly declares `x` as an `int` and assigns `10`. `x := 10` is a short variable declaration, where Go infers the type (`int`) from the assigned value. `var x = "hello"` also uses type inference, declaring `x` as a `string` based on its initial value. Option C `int x = 10` is incorrect syntax for Go.
Question 2: Which statement about Go's package declaration is true?
- Every Go file must start with a package declaration. (Correct answer)
- A Go program can have multiple package main declarations.
- The package main is required to create an executable program. (Correct answer)
- The package declaration can be omitted for small programs.
Correct answer: Every Go file must start with a package declaration.
In Go, every source file must begin with a `package` declaration, defining which package the file belongs to. For a program to be executable, it must contain a `main` package, which includes a `main` function as its entry point. While a Go program can consist of multiple files, only one `main` package can exist per executable, serving as the program's starting point.
Question 3: Which of the following if conditions are valid in Go?
- if x := 10; x > 5 {} (Correct answer)
- if (x > 5) (Correct answer)
- if x > 5 then {}
- if x := "test" {}
Correct answer: if x := 10; x > 5 {}
Go's `if` statements support a short statement before the condition, like `if x := 10; x > 5 {}`, which declares and initializes `x` within the scope of the `if` statement. Additionally, `if (x > 5)` is valid, as parentheses around the condition are allowed, though `if x > 5` is more idiomatic. Option C uses `then`, which is not Go syntax, and option D is missing a boolean condition after the short variable declaration.
Question 4: Which of the following are valid Go function definitions?
- func add(a int, b int) int { return a + b } (Correct answer)
- func greet(name string) { fmt.Println("Hello, " + name) } (Correct answer)
- func square(x int) int return x * x
- func divide(a int, b int) (result int, err error) { ... } (Correct answer)
Correct answer: func add(a int, b int) int { return a + b }
Go function definitions start with the `func` keyword, followed by the function name, parameters with their types, and then the return type(s) before the curly braces enclosing the function body. Option A shows a function returning a single integer. Option B shows a function with a string parameter and no explicit return value. Option D demonstrates multiple return values, including a named return value `result` and an `error` type, which is common for robust error handling in Go.
Question 5: Which of the following are valid ways to declare an array or slice in Go?
- var arr [3]int = [3]int{1, 2, 3} (Correct answer)
- arr := []int{1, 2, 3} (Correct answer)
- var arr = make([]int, 3) (Correct answer)
- arr := [3]int{}
Correct answer: var arr [3]int = [3]int{1, 2, 3}
Go provides distinct ways to declare arrays and slices. Option A declares a fixed-size array of 3 integers and initializes it. Option B uses a short variable declaration to create and initialize a slice (dynamic array), where `[]int` indicates a slice type. Option C uses the `make` function to create a slice of a specified length (3) and capacity, initializing elements to their zero value, which is a common way to create slices.
Which of the following are valid ways to declare and initialize a variable in Go?