Q1. 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.
Q2. Given:
From what threading problem does the program suffer?
A. deadlock
B. livelock
C. starvation D. race condition
Answer: B
Explanation:
A thread often acts in response to the action of another thread. If the other thread's action is also a response tothe action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to makefurther progress.
However, the threads are not blocked -- they are simply too busy responding to each other to resume work. This is comparable to two people attempting to pass each other in a corridor: Alphonse moves to his left to let Gaston pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still blocking eachother, Alphone moves to his right, while Gaston moves to his left. They'restill blocking each other, so.
Q3. Given three resources bundles with these values set for menu1: (the default resource bundle in US English.)
English US Resource Bundle Menu1 = small French Resource Bundle Menu1 = petit Chinese Resource Bundle Menu1 = And given the code fragment: Locale.setDefault(new Locale("es", "ES")); // Set default to Spanish and Spain
Locale loc1 = Locale.getDefault();
ResourceBundle message = ResourceBundle.getBundle("MessageBundle", loc1);
System.out.println(message.getString("menu1"));
What is the result?
A. No message is printed
B. petit
C. small
D. A runtime error is produced
Answer: D
Explanation: Compiles fine, but runtime error when trying to access the Spanish Resource bundle (which doesnot exist): Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name messageBundle, locale es_ES
Q4. You have been asked to create a ResourceBundle file to localize an application.
Which code example specifies valid keys menu1 and menu2 with values of File Menu and View Menu?
A. <key name ="menu1">File Menu</key> <key name ="menu1">View Menu</key>
B. <key> menu1</key><File Menu>File Menu </value> <key> menu1</key><File Menu>View Menu </value>
C. menu1m File menu, menu2, view menu
D. menu1 = File Menu menu2 = View Menu
Answer: D
Explanation:
A properties file is a simple text file. You can create and maintain a properties file with just aboutany text editor.
You should always create a default properties file. The name of this file begins with the base name of your ResourceBundle and ends with the .properties suffix. In the PropertiesDemo program the base name is LabelsBundle. Therefore the default properties file is called LabelsBundle.properties. The following examplefilecontains the following lines: # This is the default LabelsBundle.properties file s1 = computer s2 = disk s3 = monitor s4 = keyboard Note that in the preceding file the comment lines begin with a pound sign (#). The other lines contain key-valuepairs. The key is on the left side of the equal sign and the value is on the right. For instance, s2 is the key thatcorresponds to the value disk. The key is arbitrary. We could have called s2 something else, like msg5 ordiskID. Once defined, however, the key should not change because it is referenced in the source code. Thevalues may be changed. In fact, when your localizers create new properties files to accommodate additionallanguages, they will translate the values into various languages.
Q5. Given a language code of fr and a country code of FR, which file name represents a resource bundle file namethat is not the default?
A. MessageBundle_fr_FR.properties
B. MessageBundle_fr_FR.profile
C. MessageBundle_fr_FR.xinl
D. MessageBundle__fr__FR.Java
E. MessageBundle__fr__FR.Locale
Answer: A
Explanation:
The default file is MessageBundle.properties. The non-default file name is
MessageBundle_fr_FR.properties
Note 0:.properties is a file extension for files mainly used in Java related technologies to
store the configurableparameters of an application. They can also be used for storing
strings for Internationalization and localization;these are known as Property Resource
Bundles. Each parameter is stored as a pair of strings, one storing thename of the
parameter (called the key), and the other storing the value.Note 1:You can obtain an
instance of ResourceBundle by calling its static getBundle method.public static
ResourceBundle getBundle(java.lang.String baseName) public static ResourceBundle
getBundle(java.lang.String baseName, Locale locale) For example:
ResourceBundle rb = ResourceBundle.getBundle("MyResources", Locale.US); This will
load theResourceBundle object with the values in the corresponding properties file.1.If a
suitable properties file is not found, the ResourceBundle object will use the default
properties file, whichwill be the one whose name equals the base name and has the
properties extension. In this case, the defaultfile would be MyResources.properties. 2.If this
file is not found, a java.util.MissingResourceException will bethrown.
Note2:java.util.ResourceBundle class enables you to choose and read the properties file
specific to the user'slocale and look up the values.
A ResourceBundle object has a base name. In order for a ResourceBundle object to pick
up a properties file,the filename must be composed of the ResourceBundle base name,
followed by an underscore, followed bythe language code, and optionally followed by
another underscore and the country code.
The format for the properties file name is as follows:
basename_languageCode_countryCode
For example, suppose the base name is MyResources and you define the following three
locales:
US-en DE-de CN-zh Then you would have these three properties files: MyResources_en_US.properties MyResources_de_DE.properties MyResources_zh_CN.properties
Reference:Reading Properties Files using ResourceBundle
Q6. Given: Which two statements concerning the OO concepts "IS-A" and "HAS-A" are true?
A. Flimmer is-a Glommer.
B. Flommer has-a String.
C. Tagget has-a Glommer.
D. Flimmer is-a ArrayList.
E. Tagget has-a doStuff()
F. Tagget is-a Glommer.
Answer: B,F
Explanation:
B: The relationship modeled by composition is often referred to as the "has-a" relationship.
Here Flommer hasaString.
E: The has-a relationship has an encapsulation feature (like private or protected modifier
used before eachmember field or method).
Here Tagget has-a method doStuff()
F: Tagget implements Glommer.
Tagget is-a Glommer.
Note: The has-a relationship has an encapsulation feature (like private or protected
modifier used before eachmember field or method).
Q7. 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).
Q8. Given this code fragment:
Assume that the SQL query returns records.
What is the result?
A. Compilation fails due to error at line 17
B. The program prints Error
C. The program prints each record
D. Compilation fails at line 14
Answer: C
Q9. Given: What is the result?
A. woof arf
B. woof woof
C. arf arf
D. A RuntimeException is generated
E. The code fails to compile
Answer: E
Explanation:
class Dog {
protected String bark()
public class Beagle extends Dog {
private String bark()
Cannot reduce the visibility of the inherited method from Dog
Q10. Given: What is the result?
A. false false
B. true false
C. true true
D. Compilation fails
E. An exception is thrown at runtime
Answer: A
Explanation:
(this == obj) is the object implementation of equals() and therefore FALSE, if the reference
points to variousobjectsand then the super.equals() is invoked, the object method equals()
what still result in FALSEbetter override of equals() is to compare the attributes like:
public boolean equals (Object obj) {
if (obj != null){
Product p = (Product)obj;
return this.id == p.id;
}
return false;
}