Skip to content

Instantly share code, notes, and snippets.

@pavelburov
Last active January 27, 2017 14:33
Show Gist options
  • Save pavelburov/e48f785693018b93076f to your computer and use it in GitHub Desktop.
Save pavelburov/e48f785693018b93076f to your computer and use it in GitHub Desktop.
Groovy Puzzlers
// https://www.youtube.com/watch?v=k6vXQwxk7N8
//***
-3.abs()
// A. 3
// B. NoSuchMethodError
// C. -3
// D. Execution Failure
//***
println (-3).abs()
// A. print -3
// B. print 3
// C. print 3 and NullPointerException
// D. print -3 and NullPointerException
//***
def range = 1.0..10.0
assert range.contains(5.0)
println range.contains(5.6)
// A. Assertion Failed
// B. print false
// C. print true
// D. NullPointerException
//***
def key = 'x'
def map = [key: 'treasure']
def value = map.get(key)
println value
// A. NoSuchElementException
// B. print null
// C. print treasure
// D. Execution Failure
//***
def map = [2: 'treasure']
def key = 2
def value = map."$key"
println value
// A. NoSuchElementException
// B. print null
// C. print treasure
// D. Execution Failure
//***
List<Integer> list = [1, 2, 3]
def now = new Date()
list << now
println list
// A. Compile Failure
// B. [1, 2, 3, Mon Jul 28 11:19:11 CDT 2014]
// C. [1, 2, 3, 1406564351463]
// D. ClassCastException
//***
[0..9].each { println(it - 1) }
// A. -1 | B. 0 | C. [0,2,3,4,5,6,7,8,9] |
// 0 | 1 | |
// 1 | 2 | |
// 2 | 3 | D. [-1,0,1,2,3,4,5,6,7,8] |
// 3 | 4 | |
// 4 | 5 | |
// 5 | 6 | |
// 6 | 7 | |
// 7 | 8 | |
// 8 | 9 | |
//*** double the PI
double value = 3
println "$value.14".isDouble()
// A. true
// B. MissingPropertyException
// C. false
// D. MissingMethodException
//***
class Invite {
int attending = 1
}
def invite = new Invite()
def attendees = (invite.attending) + 1
println attendees
// A. Compile Failure
// B. 1
// C. 2
// D. MissingPropertyException
//***
// All problems in computer science can be solved by another pair of parentheses (John McCarthy, the inventor of LISP))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment