Q1. Given: Which of the four are valid modifications to synchronize access to the valid list between threads t1 and t2?
A. Replace line 1 with:
Synchronized (t2) (t1.start();) synchronized(t1) (t2.start(); )
korrekte Schreibweise: synchronized (t2) {t1.start();} synchronized(t1) { t2.start();}
B. Replace Line 2 with:
static CopyWriteArrayList<Integer> list = new CopyWriteArrayList<>();
korrekte Schreibweise: static CopyOnWriteArrayList<Integer> list = new
CopyOnWriteArrayList<>();
C. Replace line 3 with:
synchronized public static void addItem () {
korrekte Schreibweise: synchronized public static void addItem () {
D. Replace line 4 with:
synchronized (list) (list.add(1);)
korrekte Schreibweise: synchronized (list) { (list.add(1); }
E. Replace line 5 with:
Synchronized public void run () {
korrekte Schreibweise: synchronized public void run () {
F. replace line 6 with:
Synchronized (this) {for (in i = 0, i<5000, i++) WorkPool.addItem(); }
korrekte Schreibweise: synchronized (this) {for (int i = 0; i<500; i++) WorkPool.addItem(); }
G. Replace line 6 with:
synchronized (bar) {for (int i= 0; i<5000; i++) WorkPool.addItem(); }
korrekte Schreibweise: synchronized (bar) {for (int i= 0; i<500; i++) WorkPool.addItem(); }
Answer: B,C,D
Explanation:
Away to create synchronized code is with synchronized statements.
Unlike synchronized methods, synchronized statements must specify the object that
provides theintrinsic lock:
For example:
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
In this example, the addName method needs to synchronize changes to lastName and
nameCount, but alsoneeds to avoid synchronizing invocations of other objects' methods.
Without synchronized statements, therewould have to be a separate, unsynchronized
method for the sole purpose of invoking nameList.add.
Reference: The Java Tutorial,Intrinsic Locks and Synchronization
Q2. Given: What is the result?
A. 1 1 1 1 1
B. 1 2 3 4 5
C. 0 1 2 3 4
D. 0 1 4 3 4
Answer: A
Explanation:
first for-loop set 0 0 0 0 0 second for-loop increments each to 1 1 1 1 1 if condition is not given
Q3. Given the code fragment:
What is the result?
A. Java 7
B. Java 6
C. Java 7, Java 6
D. Java 7 java 6
E. Java
Answer: C
Explanation:
regex: Java / one or more anything !!! / ends with a digit so it is the source string
Q4. Which type of ExecutorService supports the execution of tasks after a fixed delay?
A. DelayedExecutorService
B. ScheduledExecutorService
C. TimedExecutorService D. FixedExecutorService
E. FutureExecutorService
Answer: B
Explanation:
The ScheduledExecutorService interface supplements the methods of its parent
ExecutorService withschedule, which executes a Runnable or Callable task after a
specified delay. In addition, the interface definesscheduleAtFixedRate and
scheduleWithFixedDelay, which executes specified tasks repeatedly, at definedintervals.
Note:The java.util.concurrent package defines three executor interfaces:
*Executor, a simple interface that supports launching new tasks. *ExecutorService, a
subinterface of Executor,which adds features that help manage the lifecycle, both of the
individual tasks and of the executor itself.
*ScheduledExecutorService, a subinterface of ExecutorService, supports future and/or
periodic execution oftasks.
Reference: The Java Tutorials,Executor Interfaces
Q5. Which code fragment demonstrates the proper way to handle JDBC resources?
A. try {
ResultSet rs = stmt.executeQuery (query);
statement stmt = con.createStatement();
while (rs.next()) (/* . . . */)
} catch (SQLException e) {}
B. try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery (query);
while (rs.next()) (/* . . . */)
} catch (SQLException e) {}
C. try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery (query);
while (rs.next()) (/* . . . */)
} finally {
rs.close();
stmt.close();
}
D. try {
ResultSet rs = stmt.executeQuery (query);
Statement stmt = con.createStatement();
while (rs.next()) (/* . . . */)
} finally {
rs.close();
stmt.close();
}
Answer: C
Q6. Which two demonstrate the valid usage of the keyword synchronized?
A. interface ThreadSafe {
synchronized void doIt();
}
B. abstract class ThreadSafe {
synchronized abstract void doIt();
}
C. class ThreadSafe {
synchronized static void soIt () {}
}
D. enum ThreadSafe {
ONE, TWO, Three;
synchronized final void doIt () {}
}
Answer: C
Explanation:
The Java programming language provides two basic synchronization idioms:
synchronized methods and synchronized statements.
To make a method synchronized, simply add the synchronized keyword to its declaration.
Q7. Which two forms of abstraction can a programmer use in Java?
A. enums
B. interfaces
C. primitives
D. abstract classes
E. concrete classes
F. primitive wrappers
Answer: B,D
Explanation:
When To Use Interfaces An interface allows somebody to start from scratch to implement your interface or implement your interface insome other code whose original or primary purpose was quite different from your interface. To them, yourinterface is only incidental, something that have to add on to thetheir code to be able to use your package. Thedisadvantage is every method in the interface must be public. You might not want to expose everything.
*When To Use Abstract classes An abstract class, in contrast, provides more structure. It usually defines some default implementations andprovides some tools useful for a full implementation. The catch is, code using it must use your class as thebase. That may be highly inconvenient if the other programmers wanting to use your package have alreadydeveloped their own class hierarchy independently. In Java, a class can inherit from only one base class.*When to Use Both You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore yourabstract class if they choose. The only drawback of doing that is calling methods via their interface name isslightly slower than calling them via their abstract class name.
Reference:http://mindprod.com/jgloss/interfacevsabstract.html
Q8. Given the incomplete pseudo-code for a fork/join framework application:
And given the missing methods: Process, submit, and splitInHalf Which three insertions properly complete the pseudo-code?
A. Insert submit at line X.
B. Insert splitInHalf at line X.
C. Insert process at line X.
D. Insert process at line Y.
E. Insert splitInHalf at line Y.
F. Insert process at line Z.
G. Insert submit at line Z.
Answer: C,E,G
Explanation:
C: If data is small enough then process it. Line X
E: If data is not small enough then split it half. Line Y
G: After the data has been split (line Y) then recursively submit the splitted data (Line z).
Q9. Given two classes in separate files:
Which two import statements can make the a.b.parent class compliable?
A. import a.b.c.Parent;
B. import a.b.c.Child;
C. import a.b.c.*;
D. import a.b.*;
E. import a.*;
Answer: B,C
Explanation:
To import a specific member into the current file, put an import statement at the beginning of thefile before any type definitions but after the package statement, if there is one.C:To import all the types contained in a particular package, use the import statement with the asterisk (*)wildcard character.
Reference: The Java Tutorials,Using Package Members
Q10. Given:
What is the result?
A. Cue sports, Cue sports
B. Compilation fails at line 9
C. Compilation fails at line 11
D. Compilation fails at line 12
E. Compilation fails at line 13
Answer: B
Explanation:
Class Snooker is public. Should be declared in a separate file. // Line 9 getCategory() >>> GetCategory() Line 13
Q11. Which three are true?
A. A setAutoCommit (False) method invocation starts a transaction context.
B. An instance of Savepoint represents a point in the current transaction context.
C. A rollback () method invocation rolls a transaction back to the last savepoint.
D. A rollback () method invocation releases any database locks currently held by this connection object.
E. After calling rollback (mysavepoint), you must close the savepoint object by calling mySavepoint.close() .
Answer: A,B,C
Explanation:
A:The way to allow two or more statements to be grouped into a transaction is to disable the auto-commitmode. After the auto-commit mode is disabled, no SQL statements are committed until you call the methodcommit explicitly. All statements executed after the previous call to the method commit are included in thecurrent transaction and committed together as a unit. Note:When a connection is created, it is in auto-commit mode. This means that each individual SQL statementis treated as a transaction and is automatically committed right after it is executed. (To be more precise, thedefault is for a SQL statement to be committed when it is completed, not when it is executed. A statement iscompleted when all of its result sets and update counts have been retrieved. In almost all cases, however, astatement is completed, and therefore committed, right after it is executed.)
B:The method Connection.setSavepoint, sets a Savepoint object within the current transaction. The Connection.rollback method is overloaded to take a Savepoint argument. When a transaction is rolled back toa savepoint all changes made after that savepoint are undone.
C: calling the method rollback terminates a transaction and returns any values that were modified to theirprevious values. If you are trying to execute one or more statements in a transaction and get a SQLException, call the method rollback to end the transaction and start the transaction all over again.
Q12. Given:
Which two are true?
A. Thread is printed
B. Runnable is printed
C. No output is produced
D. No new threads of execution are started within the main method
E. One new thread of execution is started within the main method
F. Two new threads of exclusion are started within the main method
Answer: C,D
Q13. To provide meaningful output for:
System.out.print( new Item ()):
A method with which signature should be added to the Item class?
A. public String asString()
B. public Object asString()
C. public Item asString()
D. public String toString()
E. public object toString()
F. public Item toString()
Answer: D
Explanation:
Implementing toString method in java is done by overriding the Object's toString method.
The javatoString() method is used when we need a string representation of an object. It is
defined in Object class. Thismethod can be overridden to customize the String
representation of the Object.
Note:
Below is an example shown of Overriding the default Object toString() method. The
toString() method must bedescriptive and should generally cover all the contents of the
object.
class PointCoordinates {
private int x, y;
public PointCoordinates(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
// Custom toString() Method.
public String toString() {
return "X=" + x + " " + "Y=" + y;
}}
Q14. Which is a factory method from the java.text.NumberFormat class?
A. format (long number)
B. getInstance()
C. getMaxiraumFractionDigits ()
D. getAvailableLocales ()
E. isGroupingUsed()
Answer: B
Explanation:
To obtain a NumberFormat for a specific locale, including the default locale, call one ofNumberFormat's factory methods, such as getInstance(). Reference:java.textClass DecimalFormat
Q15. Given: Which three statements concerning the OO concepts "is-a" and "has-a" are true?
A. Flimmer is-a Plinkable
B. Flommer has-a Tagget
C. Flommer is-a Glommer
D. Tagget has-a String
E. Flommer is-a Plinkable
F. Flimmer is-a Flommer
G. Tagget is-a Plinkable
Answer: A,B,E
Explanation:
A: Flimmer implements Plinkable.
Flimmer is-a plinkable.
D:The relationship modeled by composition is often referred to as the "has-a" relationship.
HereTaggethasaString.
F: Flommer extends Flimmer
So there is an "is-a relationship between Flommer and Flimmer .
Note: Thehas-a relationship has anencapsulation feature (like private or protected modifier
used before eachmember field or method).