Home »
Aptitude Questions and Answers »
Golang Aptitude Questions and Answers
Golang Operators – Aptitude Questions and Answers
Golang Operators – Aptitude Questions and Answers: This section contains aptitude questions and answers on Golang Operators.
Submitted by Nidhi, on February 19, 2022
1) Which is not a bitwise operator?
- &&
- |
- <<
- &
Correct answer: 1
&&
The && is not a bitwise operator. It is a "Logical AND" operator.
2) What will be the output of the following program?
package main
import "fmt"
func main() {
var X = 10
var Y = 2
var Z = 0
Z = X & Y
fmt.Printf("%d", Z)
}
- 0
- 2
- 10
- 12
Correct answer: 2
2
In the above program, we created 3 variables X, Y, Z initialized with 10, 2, 0. Then we performed the "bitwise AND" (&) operation.
X=10 //0000 1010
Y=2 //0000 0010
Now we perform BITWISE AND operation.
0000 1010
0000 0010
=========
0000 0010 That is 2.
3) What will be the output of the following program?
package main
import "fmt"
func main() {
var TV = 0x01
var MOBILE = 0x02
var item = 0x00
var temp = 0x00
item = item | TV
item = item | MOBILE
fmt.Printf("I have purchased ...:")
temp = (item & MOBILE)
if temp != 0 {
fmt.Printf(" MOBILE, ")
}
temp = (item & TV)
if temp != 0 {
fmt.Printf(" TV")
}
}
- I have purchased ...:
- I have purchased ...: MOBILE, TV
- I have purchased ...: MOBILE,
- I have purchased ...: TV
Correct answer: 2
I have purchased ...: MOBILE, TV
In the above program, we created 4 variables TV, MOBILE, item, temp, that was initialized with 1,2, 0,0 respectively. Then we perform BITWISE OR operation and check condition accordingly and print "I have purchased ...: MOBILE, TV" message.
4) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = 0x04
val = val | 0x04
fmt.Printf("%d,", val)
val |= 0x01
fmt.Printf("%d", val)
}
- 4,5
- 4,4
- 4,8
- 8,8
Correct answer: 1
4,5
In the above program, we created a variable val initialized 0x04.
Now we evaluate the expression:
val = 4 that is 0100
val = val | 0x04;
val = 0x04 | 0x04;
0100
0100
====
0100 That is 4
val = val | 0x01;
val = 0x04 | 0x01;
0100
0001
=====
0101 That is 5
5) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = 10 ^ 2
fmt.Printf("val: %d", val)
}
- Compilation error
- val: 10
- val: 2
- val: 8
Correct answer: 4
val: 8
In the above program, we performed the BITWISE XOR operation.
1010
0010
====
1000 That is 8.
6) What will be the output of the following program?
package main
import "fmt"
func main() {
var val = 0x0f;
val &= ~0x02;
fmt.Printf("%d",val);
}
- Compilation error
- 13
- 22
- 10
Correct answer: 1
Compilation error
The above program will generate a syntax error because the tilde "~" operator is not available in GOLANG.
7) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?
- &
- |
- ^
- &&
Correct answer: 1
&
The Bitwise AND (&) Operator can be used to check whether a number is EVEN or ODD quickly, consider the statement (val & 1), this statement will return 1 if the first bit of the number is High (1) else it will return 0. All ODD numbers have their first bit 1 and ODD numbers have 0.
8) Left shift (<<) and Right shift (>>) operators are equivalent to ___ by 2.
- Multiplication and Division
- Division and Multiplication
- Multiplication and Remainder
- Remainder and Multiplication
Correct answer: 1
Multiplication and Division
Left shift by 1 return the multiplication by 2 and Right shift by 1 return the division by 2.
9) What will be the output of the following program?
package main
import "fmt"
func main() {
var A int = 65;
var B char = (char)A;
fmt.Printf("%c\n",B);
}
- A
- 65
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate will a compilation error because "char" is not a built-in datatype.
10) What will be the output of the following program?
package main
import "fmt"
func main() {
var A float32 = 65.12
var B int = int(A)
fmt.Printf("%d\n", B)
}
- 65.12
- 65
- Compilation Error
- None of the above
Correct answer: 2
65
In the above created two variables A, B. Then we performed type casting from 32-bit floating-point number to integer. After that, we printed the result.
11) What will be the output of the following program?
package main
import "fmt"
func main() {
var A float32 = 65.12;
var B int = (int)A;
fmt.Printf("%d\n",B);
}
- 65.12
- 65
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate a compile-time error because of the wrong syntax for typecasting.
var B int = (int)A;
In the above statement, we did not perform proper typecasting. The correct statement is given below.
var B int = int(A);
12) What will be the output of the following program?
package main
import "fmt"
func main() {
var A float32 = 65.12
var B float64 = float(A)
fmt.Printf("%f\n", B)
}
- 65.12
- 65.120000
- Compilation Error
- None of the above
Correct answer: 3
Compilation Error
The above program will generate a compile-time error because we cannot type caste 32-bit float into 64-bit float using float(). The correct statement is given below:
var B float64 = float64(A);
13) What will be the output of the following program?
package main
import "fmt"
func main() {
var num int = int('A' + 2)
fmt.Printf("%d\n", num)
}
- 67
- C
- Compilation Error
- None of the above
Correct answer: 1
67
In the above program, we created a variable num and initialized it with 'A'+2, that is 67. Then we printed the result.