What is the difference between continuous deployment and continuous delivery? Please describe in one-two lines
Category Archives: brainteaser
Brainteaser Drools: Testing Objects
This can be a hard one, since it requires from you to be familiar with Drools.
Consider the condition side of the following rules:
[java]
rule "object comparison one"
no-loop
when
$customer1 : Customer( )
$customer2 : Customer(this != $customer1)
then
System.out.println("Rule one – Objects are equal");
end
[/java]
[java]
rule "object comparison two"
no-loop
when
$customer1 : Customer( )
$customer2 : Customer(eval(this != $customer1))
then
System.out.println("Rule two – Objects are equal");
end
[/java]
Question(s):
Which of the two rules does valid comparison of the two Customer instances?
Which of the two rules is invalid? Why?
Looking forward for your answers dear readers
Resources:
Drools JBoss Rules 5.0 Developer’s Guide July 2009
Brainteaser: Overridable methods
Consider the following case of inheritance:
[java]
public class Parent {
public Parent() {
getValue();
}
public void getValue() {
}
}
public class Child extends Parent {
private final Integer integer;
public Child() {
integer = new Integer(888);
}
@Override
public void getValue() {
System.out.println(integer);
}
}
[/java]
Question: What would the following program print, why?
[java]
public class Test {
public static void main(String[] args) {
Child child = new Child();
child.getValue();
}
}
[/java]
Lets assume that getValue() implementation in Child class was changed to:
[java]
@Override
public void getValue() {
System.out.println(integer.toString());
}
[/java]
Question: What would the output of the Test class be now, why?
Brainteaser: Broken Case of Inheritance
Consider the following case of inheritance:
[java]
public class ExtendingHashSet<E> extends HashSet<E> {
private int counter = 0;
public ExtendingHashSet() {
}
@Override
public boolean add(E e) {
counter++;
return super.add(e);
}
@Override
public boolean addAll(Collection<? extends E> c) {
counter += c.size();
return super.addAll(c);
}
public int getCounter() {
return counter;
}
}
[/java]
Created instance:
[java]
ExtendingHashSet<String> s = new ExtendingHashSet<String>();
s.addAll(Arrays.asList("one", "two", "three"));
[/java]
Question: What value would s.getCounter() method return at this point and why?
Looking forward for your answers dear readers
Brainteaser: Broken Comparator
Question: The following program returns result “1″, which indicates that first Integer value is greater than the second, why?
[java]
import java.util.*;
public class Example {
public static void main(String[] args) {
System.out.println(&quot;Result: &quot; +
naturalOrder.compare(new Integer(90),
new Integer(90)));
}
private static Comparator&lt;Integer&gt; naturalOrder =
new Comparator&lt;Integer&gt;() {
public int compare(Integer first, Integer second) {
return first &lt; second ? -1 : (first == second ? 0 :1);
}
};
}
[/java]
Please note:
In this case, comparator for natural order on Integer is written for example only, and in practice there is no need to write it.
Looking forward for your answers dear readers
Resources:
Effective Java
Brainteaser: ArrayList VS TreeSet
When I came across the following example I did not expect the results that the program has printed
hehe…
Question: What does this program print? Why?
[java]
import java.util.*;
public class SetList {
public static void main(String[] args) {
Set<Integer> set =
new TreeSet<Integer>();
List<Integer> list =
new ArrayList<Integer>();
for (int i = -3; i < 3; i++) {
set.add(i);
list.add(i);
}
for (int i = 0; i < 3; i++) {
set.remove(i);
list.remove(i);
}
System.out.println(set + " " + list);
}
}
[/java]
Looking forward for your answers dear readers
Resources:
Effective Java
Brainteaser: Hidden Iterators
… While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection might be iterated. This is trickier than it sounds …
Brian Goetz p.83-84
Question: The following code could throw ConcurrentMofdificationException, even though add() is synchronized, why?
[java]
public class HiddenIterator {
private final Set set = new HashSet();
public synchronized void add(Integer i) {
set.add(i);
}
public synchronized void remove(Integer i) {
set.remove(i);
}
public void addTenThings() {
Random r = new Random();
for (int index = 0; index < 10; index++;) {
add(r.nextInt());
}
System.out.println("Added ten elements to set: " + set);
}
}
[/java]
Looking forward for your answers dear readers