0% found this document useful (0 votes)
71 views7 pages

Java SE 7 Programming Concepts and Challenges

The document contains 15 multiple choice questions related to Java programming concepts. The questions cover topics like object-oriented design, exceptions, JDBC, generics, and more.

Uploaded by

George Petre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views7 pages

Java SE 7 Programming Concepts and Challenges

The document contains 15 multiple choice questions related to Java programming concepts. The questions cover topics like object-oriented design, exceptions, JDBC, generics, and more.

Uploaded by

George Petre
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Given:
import [Link].*;
public class Primes2 {
public static void main(String[] args) {
Integer[] primes = {2, 7, 5, 3};
MySort ms = new MySort();
[Link](primes, ms);
for(Integer p2: primes)
[Link](p2 + " ");
}
static class MySort implements Comparator

{
public int compare(Integer x, Integer y) {
return [Link](x);
}
}
}
What is the result?
A) 2 3 5 7
B) 2 7 5 3
C) 7 5 3 2
D) Compilation fails.

2. Given:
class Class1 {
String v1;
}
class Class2 {
Class1 c1;
String v2;
}
public class Class3 {
Class2 c1;
String i3;
}
Which three options correctly describe the relationship between the classes?
A) Class2 has-a i3
B) Class1 has-a v2
C) Class2 has-a v2
D) Class3 has-a v1
E) Class2 has-a Class3
F) Class2 has-a Class1

3. Given this code snippet:

m = new HashMap();
MyKeys m1 = new MyKeys(1);
MyKeys m2 = new MyKeys(2);
MyKeys m3 = new MyKeys(1);
MyKeys m4 = new MyKeys(new Integer(2));
[Link](m1, "car");
[Link](m2, "boat");
[Link](m3, "plane");
[Link](m4, "hovercraft");
[Link]([Link]());
Map

And this class:


class MyKeys {
Integer key;
MyKeys(Integer k) { key = k; }
public boolean equals(Object o) {
return ((MyKeys)o).key == [Link];
}
}
What is the result?
A) 2
B) 3
C) 4
D) Compilation fails.
E) An exception is thrown at run time.

4. Given:
import [Link].*;
public class MyScan {
public static void main(String[] args) {
String in = "1 a 10 . 100 1000";
Scanner s = new Scanner(in);
int accum = 0;
for(int x = 0; x < 4; x++) {
accum += [Link]();
}
[Link](accum);
}
}

What is the result?


A) 4
B) 10
C) 111

D) 1111
E) Compilation fails.
F) An exception is thrown at run time.

5. Given:
public class Truthy {
public static void main(String[] args) {
int x = 7;
assert(x == 6) ? "x == 6" : "x != 6";
}
}

What is the result if you try to compile [Link] and then run it with assertions enabled?
A) [Link] does NOT compile.
B) [Link] compiles and the output is "x != 6".
C) [Link] compiles and an AssertionError is thrown with no additional output.
D) [Link] compiles and an AssertionError is thrown with "x != 6" as additional output.

[Link] the code fragment:


try {
// assume "conn" is a valid Connection
// assume a valid Statement object is created
// assume rollback invocations will be valid
// use SQL to add 10 to a checking account
Savepoint s1 = [Link]();
// use SQL to add 100 to the same checking account
Savepoint s2 = [Link]();
// use SQL to add 1000 to the same checking account
// insert valid rollback method invocation here
} catch (Exception e) { }

Which two statements are true?


A) If [Link](s1) is inserted, account will be incremented by 10.
B) If [Link](s1) is inserted, account will be incremented by 1010.
C) If [Link](s2) is inserted, account will be incremented by 100.
D) If [Link](s2) is inserted, account will be incremented by 110.
E) If [Link](s2) is inserted, account will be incremented by 1000.

7. Given:

public class Bees {


public static void main(String[] args) {
try {
new Bees().go();
} catch (Exception e ) {
[Link]("thrown to main");
}
}
synchronized void go() throws InterruptedException {
Thread t1 = new Thread();
[Link]();
[Link]("1 ");
[Link](5000);
[Link]("2 ");
}
}

What is the result?


A) 1 then 2 with little delay
B) 1 then 2 after 5 seconds
C) 1 thrown to main
D) 1 2 thrown to main
E) Compilation fails.

8. Given:
import [Link].*;
public class Align {
public static void main(String[] args) {
String[] sa = {"111.234", "222.5678"};
NumberFormat nf = [Link]();
[Link](3);
for(String s: sa)
[Link]([Link](s));
}
}

What is the result?


A) 111.234
222.567
B) 111.234
222.568
C) 111.234
222.5678
D) Compilation fails.
E) An exception is thrown at run time.

9. Given:

class MyResource1 implements AutoCloseable {


public void autoClose() { [Link]("1 "); } // A1
}
class MyResource2 implements AutoCloseable {
public void autoClose() { [Link]("2 "); } // A2
}
public class Triangle {
public static void main(String[] args) {
try ( MyResource1 r1 = new MyResource1(); // B
MyResource2 r2 = new MyResource2();) { // B
[Link]("t ");
}
finally {
[Link]("f ");
}
}
}

What is the result?


A) 1 2 t f
B) 2 1 t f
C) t 1 2 f
D) t 2 1 f
E) Compilation fails due to an error on line B.
F) Compilation fails due to errors on lines A1 and A2.
G) Compilation fails due to errors on lines A1, A2, and B.

10. Given:
1. import [Link].*;
2. public class Forever {
3. public static void main(String[] args) {
4. List x1 = new ArrayList();

5. List x3 = new ArrayList<>();


6. }
7. }

What happens when you compile the code?


A) Compilation succeeds.
B) Compilation fails due to multiple errors.
C) Compilation fails due to an error on line 4.
D) Compilation fails due to an error on line 5.

11. Which is most closely associated with the object-oriented design concept known as "has-a"?
A) DAO
B) factory
C) adapter
D) singleton
E) composition

[Link]
import [Link].*;
public class MyDates {
public static void main(String[] args) {
Calendar c = [Link](); // line A
}
}
Which two statements are true?
A) Line A is an example of using the DAO pattern.
B) Line A is an example of using the factory pattern.
C) Line A is an example of using the singleton pattern.
D) [Link] could be an abstract class.
E) Line A could be replaced with: Calendar c = new Calendar();

13. Which method can be used for establishing a connection to a JDBC 4.1 data source?
A) [Link]()
B) [Link]()
C) [Link]()
D) [Link]()
E) [Link]()

14. Given this code fragment:


// assume "myURL" is a valid URL
try {
Connection conn =
[Link](myURL);
Statement s = [Link]();
// use SQL to add 100 to a checking account
// use more SQL to add 300 to the account
[Link]();
} catch (Exception e) {
[Link]();
}
What is the result?
A) Compilation fails.
B) An exception is thrown at run time.
C) The account's value is not changed.
D) The account's value is incremented by 100.
E) The account's value is incremented by 300.
F) The account's value is incremented by 400.

15. Implementing which method will properly extend [Link]?

A) addContents()

B) getContents()
C) addResources()
D) getResources()
E) getKeys()

Answers
1. D
2. C, D, F
3. C
4. F
5. A
6. A, D
7. C
8. D
9. F
10. A
11. E
12. B, D
13. D
14. F
15. B

Common questions

Powered by AI

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 .

You might also like