Q1. How many Threads are created when passing task to an Executor instance?
A. A new Thread is used for each task.
B. A number of Threads equal to the number of CPUs Is used to execute tasks.
C. A single Thread Is used to execute all tasks.
D. A developer-defined number of Threads is used to execute tasks.
E. A number of Threads determined by system load is used to execute tasks.
F. The method used to obtain the Executor determines how many Threads are used to execute tasks.
Answer: F
Explanation:
The Executor interface provides a single method, execute, designed to be a drop-in replacementfor a common thread-creation idiom. If r is a Runnable object, and e is an Executor object you can replace(new Thread(r)).start();
with e.execute(r); However, the definition of execute is less specific. The low-level idiom creates a new thread and launches it immediately. Depending on the Executor implementation, execute may do the same thing, but is more likely to use an existing worker thread to run r, or to place r in a queue to wait for a worker thread to become available.
Reference: The Java Tutorial,The Executor Interface
Q2. Given:
Which two statements, inserted independently at line ***, enable the program to produce the following output:
We have 002 Blue pants that cost $24.99.
A. System.out.printf("We have %03d %s pants that cost $%3.2f.\n",quantity, color, price);
B. System.out.printf("We have$03d$s pants that cost $$3.2f.\n",quantity, color, price);
C. String out = String.format ("We have %03d %s pants that cost $%3.2f.\n",quantity,
color,price);
System.out.println(out);
D. String out = System.out.format("We have %03d %s pants that cost $%3.2f.",quantity,
color, price);
System.out.println(out);
E. System.out.format("We have %s%spants that cost $%s.\n",quantity, color, price);
Answer: A,C
Q3. 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
Q4. Given the Greetings.properties file, containing:
What is the result?
A. Compilation fails
B. HELLO_MSG
C. GOODGYE_NSG
D. Hello, everyone!
E. Goodbye everyone!
Answer: A
Explanation:
The code will not compile.
The problem is the following line:
System.out.println(resource.getObject(1));
In particular getObject(1) throws the following error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -Erroneous sym type:
<any>.loadResourceBundle
Note:getObject(String key) !!! String keyGets an object for the given key from this resource
bundle or one of its parents.
Q5. Which four are syntactically correct?
A. package abc; package def; import Java.util . * ; public class Test { }
B. package abc; import Java.util.*; import Java.util.regex.* ; public class Test { }
C. package abc; public class Test {} import Java.util.* ;
D. import Java.util.*; package abc; public class Test {}
E. package abc; import java.util. *; public class Test{}
F. public class Test{} package abc; import java.util.*{}
G. import java.util.*; public class Test{}
H. package abc; public class test {}
Answer: B,E,G,H
Q6. Given the code fragment: What is the result when infected() is invoked?
A. before try catch finally after
B. before catch finally after
C. before catch after
D. before catch finally
E. before catch
Answer: D
Explanation:
The following line throws and exception:
int i = 1/0;
This exception is caught by:
catch(Exception e) {
System.out.print("catch ");
throw e;
Lastly, the finally statement is run as the finally block always executes when the try block
exits. This ensuresthat the finally block is executed even if an unexpected exception
occurs.
Reference: Java Tutorial,The finally Block
Q7. Given: What is the result?
A. tolting cantering tolting
B. cantering cantering cantering
C. compilation fails
D. an exception is thrown at runtime
Answer: C
Explanation:
Compiler says: Cannot reduce the visibility of the inherited method from Rideable. müssen
PUBLIC sein
public String ride() { return "cantering "; }
public String ride() { return "tolting "; }
if this is given then the result would be:
A : tolting cantering tolting
Q8. Given:
String s = new String("3");
System.out.print(1 + 2 + s + 4 + 5);
What is the result?
A. 12345
B. 3345
C. 1239
D. 339
E. Compilation fails.
Answer: B
Explanation:
1 and 2 are added.
Then the string s is concatenated.
Finally 3 and 4 are concatenated as strings.
Q9. Given: What is the result?
A. p001 Widget p002 X-Large Widget
B. p002 Large Widget p001 Widget
C. p002 X-large Widget p001 Widget
D. p001 Widget p002 Large Widget
E. compilation fails
Answer: A
Explanation: Compiles fine. Output is: P001 Widget P002 X-Large Widget Line: partList.put("P002", "X-Large Widget"); >> overwrites >> line:partList.put("P002", "Large Widget"); put V put(K key, V value) Associates the specified value with the specified key in this map (optional operation). If the map previouslycontained a mapping for the key, the old value is replaced by the specified value. (Amap m is said to contain amapping for a key k if and only if m.containsKey(k) would return true.)
Parameters: key - key with which the specified value is to be associated value - value to be associated with the specified key Returnsthe previous value associated with key, or null if there was no mapping for key. (A null return can alsoindicate that the map previously associated null with key, if the implementation supports null values.)
Q10. Which two actions can be used in registering a JDBC 3.0 driver?
A. Add the driver class to the META-INF/services folder of the JAR file.
B. Set the driver class name by using the jdbc.drivers system property.
C. Include the JDBC driver class in a jdbcproperties file.
D. Use the java.lang.class.forName method to load the driver class.
E. Use the DriverManager.getDriver method to load the driver class.
Answer: A,D
Explanation:
A: if your JDBC Driver is NOT JDBC 4-compliant then we can update the driver using "jar"-utility by adding the "META-INF /services/java.sql.Driver" inside it. as following: D:Dynamic loading of Java classes at runtime provides tremendous flexibility in the development of enterprisesystems. It provides for the basis of "application servers", and allows even simpler, lighter-weight systems toaccomplish some of the same ends. Within Java, dynamic-loading is typically achieved by calling the forNamemethod on the class java.lang.ClassAn example provided by the standard Java SE API is the ServiceLoader. Amongothers, the JDBC 4.0compatible drivers implement this. This way just dropping the JDBC driver JAR file folder will automatically loadthe driver class during Java application's startup/initialization without the need for any manual Class.forName("com.example.Driver") line in your code.