Home »
Aptitude Questions and Answers »
Golang Aptitude Questions and Answers
Golang Basics – Aptitude Questions and Answers
Golang Basics – Aptitude Questions and Answers: This section contains aptitude questions and answers on Golang Basics.
Submitted by Nidhi, on February 17, 2022
1) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 25
var val2, _ = fmt.Printf("%d ", val1)
fmt.Printf("%d", val2)
}
- Compilation Error
- 25 2
- 25 3
- None of these
Correct answer: 3
25 3
The fmt.Printf() returns total number of printed characters, the statement var val2,_=fmt.Printf("%d ",val1); will print 25 (25 and one space) and return 3. Thus, output will be 25 3 [25<space>3].
2) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 25
var val2, _ = fmt.Print(val1)
fmt.Print(val2)
}
- Compilation Error
- 252
- 25 2
- 25 3
Correct answer: 2
252
The fmt.Print() returns total number of printed characters, the statement var val2,_=fmt.Print(val1); will print 25 and return 2. Thus, output will be 252.
3) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 25
var val2, _ = fmt.Println(val1)
fmt.Println(val2)
}
- Compilation Error
- 252
- 25
2
- 25
3
Correct answer: 4
25
3
The fmt.Println() returns total number of printed characters, the statement var val2,_=fmt.Println(val1); will print 25 (25 with new line character) and return 3.
Thus, the output will be:
25
3
4) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 'A'
fmt.Println(val1)
}
- A
- 65
- Compilation Error
- None of the these
Correct answer: 2
65
The above program will print 65, which is the ASCII value of character 'A'.
5) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 0x400
fmt.Println(val1)
}
- 400
- 0x400
- Compilation Error
- 1024
Correct answer: 4
1024
The above program will print 1024, which is the decimal value of 0x400. Here 0x400 is a hexadecimal number.
6) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 0b1011
fmt.Println(val1)
}
- 1011
- 11
- Compilation Error
- 0b1011
Correct answer: 2
11
The above program will generate a compilation error because we cannot represent a binary number using the "ob" prefix.
7) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 10*6 + 6
fmt.Printf("%c", val1)
}
- 66
- Runtime Error
- Compilation Error
- B
Correct answer: 4
B
The above program will print character 'B'. Because statement var val1 = 10*6+6 assigned the value 66 into val1 variable. After that, we printed the corresponding character value of 66.
8) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 255
var val2 = -1
fmt.Printf("%02X %02X", val1, val2)
}
- 255 255
- FF FF
- FF -1
- 255 -1
Correct answer: 3
FF -1
The above program will print "FF -1". Because it will print hexadecimal corresponding 255, that is FF But there is no corresponding number for -1. That's why it prints as it is.
9) Which is the correct name for a Golang variable?
(1) -var (2) var-1 (3) _var (4) var_1
- Only (1)
- Only (2)
- Both (1) and (2)
- Both (3) and (4)
Correct answer: 4
Both (3) and (4)
As per the conventions of Golang, option 4 is correct.
10) Which special character can be used in GOLANG to separate two parts (words) of a variable/identifier name?
- - (Hyphen)
- _ (Underscore)
- $ (Dollar)
- # (Hash)
Correct answer: 2
_ (Underscore)
Underscore (_) is a symbol, which is used to separate two parts of a variable name.
11) What will be the output of the following program?
import "fmt"
func main() {
var val = 'AB';
fmt.Printf("%c",val);
}
- AB
- A
- B
- Compilation Error
Correct answer: 4
Compilation Error
The above program will generate an error because we cannot assign more than 1 character in a variable.
12) What will be the output of the following program?
package main
import "fmt"
func main() {
var val char = 'A'
fmt.Printf("%c", val)
}
- A
- 65
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate an error because "char" is not a built-in datatype in Golang.
13) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 int = 20;
fmt.Printf("%d",++val1);
}
- Compilation Error
- 20
- 21
- None of the above
Correct answer: 1
Compilation Error
The above program will generate an error because we cannot use the increment operator in fmt.Printf() function.
14) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 int = (10,20);
fmt.Printf("%d",val1);
}
- Compilation Error
- 10
- 20
- None of the above
Correct answer: 1
Compilation Error
The above program will generate an error because we cannot initialize a variable like this.
15) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1,var2 int = (10,20);
fmt.Printf("%d %d",val1, val2);
}
- Compilation Error
- 10 20
- 20 10
- None of the above
Correct answer: 1
Compilation Error
The above program will generate an error because we cannot initialize a variable like this.
16) What will be the output of the following program?
package main
import "fmt"
func main() {
fmt.Printf("includehelp.com\rHello\n")
}
- Compilation Error
- includehelp.comHello
- Hellodehelp.com
- None of the above
Correct answer: 3
Hellodehelp.com
The '/r' is an escape sequence that means carriage return. Carriage return takes back the cursor to the leftmost side in a line.
Thus in the statement fmt.Printf("includehelp.com\rHello\n");
The "includehelp" is printed ( not still displayed) then the cursor moves to the leftmost position ("i" here) and starts printing "Hello" which results in overwriting of first 5 characters of "includehelp".
Thus the final output is "Hellodehelp.com" and then the cursor moves to the next line due to character feed \n.
17) What will be the output of the following program?
package main
import "fmt"
func main() {
fmt.Printf("includehelp.com\b\b\bHello\n")
}
- Compilation Error
- includehelp.comHello
- includehelp.Hello
- None of the above
Correct answer: 3
includehelp.Hello
The '/b' escape character is used which is equivalent to backspacing the cursor. Overwrite also took place here due to three backspaces.
18) What will be the output of the following program?
package main
import "fmt"
func main() {
var val int = 0
val = 5 | 2 | 1
fmt.Printf("%d", val)
}
- Compilation Error
- 7
- 6
- 5
Correct answer: 2
7
The above program will print 7. Now we will perform bitwise OR operations.
5 - 0101
2 - 0010
1 - 0001
========
0111
That is 7.
19) What will be the output of the following program?
package main
import "fmt"
func main() {
static var val int = 0;
val = 5 | 2 | 1;
fmt.Printf("%d",val);
}
- Compilation Error
- 7
- 6
- 5
Correct answer: 1
Compilation Error
The above program will generate a compilation error because we cannot use the "static" keyword in Golang.
20) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 int = 0x10
var val2 int = 010
fmt.Printf("%d", val1+val2)
}
- 20
- 26
- 24
- None of the above
Correct answer: 3
24
In the above program, we created two variables val1, val2 initialized with 0x10, 010 respectively. 0x10 is a hexadecimal number, the corresponding decimal is 16. 010 is an octal number, the corresponding decimal number is 8. Then the sum of 16+8 is 24.
21) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 int = 013
var val2 int = 010
fmt.Printf("%d", val1+val2)
}
- 19
- 23
- 22
- None of the above
Correct answer: 1
19
In the above program, we created two variables val1, val2 initialized with 013, 010 respectively. 013, 010 are octal numbers, the corresponding decimal numbers are 11, 8. Then the sum of 11+8 is 19.
22) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 int = 13;
var val2 int = 10;
;
;
;
fmt.Printf("%d",val1+val2);
}
- 19
- 23
- 24
- None of the above
Correct answer: 2
23
In the above program, we created two variables val1, val2 initialized with 13, 10 respectively. Here we used 3 empty statements using the semicolon ';'. Then we printed the sum of both variables, which is 23.
23) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 float32 = 13.24f;
var val2 float32 = 10.34f;
fmt.Printf("%f",val1+val2);
}
- 23.580000
- Compilation error
- 23.58
- None of the above
Correct answer: 2
Compilation error
The above program will generate a syntax error because suffix 'f' for floating-point numbers is not allowed in golang.
24) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 float = 13.24
var val2 float = 10.34
fmt.Printf("%f", val1+val2)
}
- 23.580000
- Compilation error
- 23.58
- None of the above
Correct answer: 2
Compilation error
The above program will generate a syntax error because "float" is not a built-in datatype. We can use "float32" or "float64" to declare a floating-point variable in Golang.
25) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 float32 = 13.24
var val2 float32 = 10.34
fmt.Printf("%f", val1+val2)
}
- 23.580000
- Compilation error
- 23.58:
- None of the above
Correct answer: 1
23.580000
In the above program, we created two variables val1, val2 initialized with 13.24, 10.34 respectively. Then we printed the sum of both variables, which is 23.580000.
26) What will be the output of the following program?
package main
import "fmt"
func main() {
var val1 = 10
var val2 = 20
var val3 = 30
fmt.Printf("%d%d%d")
}
- Compilation Error
- Garbage Value
- No output
- None of these
Correct answer: 1
Compilation Error
The above program will generate a syntax error because here we created 3 variables but we did not use created variables in the program.
27) What will be the output of the following program?
package main
import "fmt"
func main() {
var str = "%shello "
fmt.Printf(str, str)
}
- Garbage value
- hello %shello
- %shello hello
- None of the above
Correct answer: 3
%shello hello
In the above program, we created a string variable str, initialized with "%shello ". Then we used created variable inside fmt.Printf() function and printed "%shello hello" message.
28) What will be the output of the following program?
package main
import "fmt"
func main() {
var a = 65
fmt.Printf("%d %x %o %c", a, a, a, a)
}
- 65 41 101 A
- 66 41 Garbage A
- Compilation Error
- None of the above
Correct answer: 1
65 41 101 A
In the above program, we created a variable 'a', initialized with 65. Then we printed the value of the variable in decimal, hexadecimal, octal, and character.
29) What will be the output of the following program?
package main
import "fmt"
func main() {
var str = "Hello"
fmt.Printf("%d", fmt.Printf("%s", str))
}
- Hello5
- 5Hello
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate a syntax error because we used multiple-value fmt.Printf() in single-value context. The fmt.Printf() returns 2 values.
30) What will be the output of the following program?
package main
import "fmt"
func main() {
var str = "Hello"
var n = 0
n, _ = fmt.Printf("%s", str)
for i := 1; i < n; i++ {
n, _ = fmt.Printf("%s", str)
}
}
- HelloHelloHelloHelloHello
- HelloHelloHelloHelloHelloHello
- Compilation Error
- None of the above
Correct answer: 1
HelloHelloHelloHelloHello
In the above program, we created two variables str, n, that was initialized with "Hello", 0 respectively. Then we printed the "Hello" message 5 times using fmt.Printf() function.
31) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = 'A'
val++
val++
fmt.Printf("%c", val)
}
- 67
- C
- Compilation Error
- None of the above
Correct answer: 2
C
In the above program, we created a variable val initialized with 'A'. Then we increased the value of val by 2 using the increment operator. After that, we printed the corresponding character that is 'C'.
32) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = 'A'
val++
var val1 = val
val++
var val2 = val
val = val1 + val2
fmt.Printf("%d", val)
}
- 132
- 134
- 133
- Compilation Error
Correct answer: 3
133
In the above program, we created a variable val initialized with 'A', which is 65. Then we increased the value of val using the increment operator and initialized variables in val1, val2 with 66,67 respectively. After that, we added values of val1, val2 and assigned the result 133 into variable val and printed the result.
33) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = 0
var a = 10
var b = 2
val = a + b*a + 10/2*a
fmt.Printf("%d", val)
}
- 1250
- 80
- 125
- Error
Correct answer: 2
80
In the above program, we created variables val, a, b, that was initialized with 0, 10, 2 respectively. Then we calculated the expression and printed the result.
34) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = 67
val += 2
val++
fmt.Printf("%c", val)
}
- 70
- Compilation Error
- Runtime Error
- F
Correct answer: 4
F
In the above program, we created a variable val initialized with 67. Then we increased the value of variable val using compound assignment and increment operator. After that, we printed the corresponding character of the value of val that is 'F'.
35) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = (6 | 8) & (8)
fmt.Printf("%d", val)
}
- 8
- Compilation Error
- Runtime Error
- F
Correct answer: 1
8
(6 || 8) ... both are non-zero values, will return 14.
(14) && 8 ... both are non-zero values, hence output will be 8.