Groovy Quiz 7 | Bitwise, Conditional and Ternary O


Reference video | Groovy Playground


1 Point for every correct answer | Let’s Get Started

1.

Bitwise Operators – Check if the symbol and description matches

&: bitwise “and”

|: bitwise “or”

^: bitwise “xor” (exclusive “or”)

~: bitwise negation

 
 

2.

Bitwise Operators – Guess the output

(Can run and check code here – https://groovy-playground.appspot.com/)

int a = 20
int b = 25
print (a & b)

hint: Convert 20 and 25 to their binary form

println Integer.toBinaryString(20)  // 10100
println Integer.toBinaryString(25)  //  11001

Apply & operation

10100

11001

———-

10000

Covert back to Integer

println Integer.parseInt("10000", 2)

(to the base 2 or decimal base)

 
 
 

3.

Conditional Operators – Guess the output

println ((!true)    == false)                   
println ((!'foo')   == false)                   
println ((!'')      == true)
 
 

4.

Ternary Operator “…” ? “…” : “…”

Guess the output

result = (5>6) ? "5 is greater" : "6 is greater"
print result
 
 
 

9