Java SE 7 Programming Concepts and Challenges
Java SE 7 Programming Concepts and Challenges
If `conn.rollback(s1)` is executed, the account will only reflect the addition of the initial 10 as it's rolled back to the state just before adding the 100 and 1000. If `conn.rollback(s2)` is executed, it rolls back to after the first transaction, retaining the addition made for the initial 100, leading to a total increment of 110. Therefore, A) If `conn.rollback(s1)` is inserted, account will be incremented by 10, and D) If `conn.rollback(s2)` is inserted, account will be incremented by 110 .
The code uses a ternary operator in an assertion, which is syntactically incorrect, causing the code to not compile. Assertions need a boolean expression and an optional error message; the use of a ternary expression as the assertion statement itself is invalid. Therefore, an assertion error does not occur as the code fails to compile initially .
The given code sorts an array of integers in descending order using the `MySort` class, which implements `Comparator`. The `compare` method in `MySort` returns the result of `y.compareTo(x)`, reversing the natural order. The correct output will be `7 5 3 2` .
The Java `HashMap` uses both `equals` and `hashCode` to determine key uniqueness. The provided `MyKeys` class implements `equals` but not `hashCode`, leading to potential multiple instances being considered equal and still stored separately. As a result, the map size is 4 because all keys are treated as unique due to lack of a consistent `hashCode()` implementation .
The "has-a" relationship in the provided classes refers to composition, where classes contain references to objects of other classes. Class2 has a reference to an instance of Class1, therefore it 'has-a' Class1. Class3 contains an object of Class2, so it 'has-a' Class2. Class3 does not directly 'have-a' Class1 since it is an indirect relationship through Class2. The correct statements are: C) Class2 has-a v2, D) Class3 has-a v1, and F) Class2 has-a Class1 .
The `Calendar.getInstance()` method is an example of the Factory pattern, which provides a mechanism to create instances of classes, typically outlined by abstract classes or interfaces. It is not a DAO or Singleton pattern because it doesn't focus on data access or ensuring a single instance, respectively. Option B is correct and it's also possible that `Calendar` is an abstract class, fulfilling option D .
The `wait` method can only be used when the current thread holds the lock for the object upon which `wait` is applied. In the provided code, `wait()` is called on a `Thread` object without synchronizing on it, leading to an `IllegalMonitorStateException`. Hence, "1 thrown to main" will be printed, indicating the exception caught in the `catch` block is thrown to the main method .
The `NumberFormat` class, as used in the `Align` class, requires handling potential parsing exceptions. The `parse` method can throw a `ParseException`, so it should be wrapped in a try-catch block or the method `main` should declare to throw this exception. The absence of exception handling leads to compilation errors .
The `MyResource1` and `MyResource2` classes correctly implement `AutoCloseable`, but they must provide an `overridden close()` method for the try-with-resources statement to compile successfully. Instead, they should have `public void close()` methods. The absence of these results in compilation failure due to errors on the specified lines .
The "has-a" relationship is most closely associated with composition in object-oriented design. This involves creating complex objects by combining simpler ones. Among the provided options, composition is often used to represent "has-a" relationships, where an object contains another object as part of its state .