Java Interview Questions – 1

Ques 1. How can you achieve Multiple Inheritance in Java?

Ans. Java’s interface mechanism can be used to implement multiple inheritance, with one important difference from c++ way of doing MI: the inherited interfaces must be abstract. This obviates the need to choose between different implementations, as with interfaces there are no implementations.

 

Ques 2. Replacing Characters in a String

Ans.

// Replace all occurrences of ‘a’ with ‘o’

String newString = string.replace(‘a’, ‘o’);

Replacing Substrings in a String

static String replace(String str,

String pattern, String replace) {

int s = 0;

int e = 0;

StringBuffer result = new StringBuffer();

while ((e = str.indexOf(pattern, s)) >= 0) {

result.append(str.substring(s, e));

result.append(replace);

s = e+pattern.length();

}

result.append(str.substring(s));

return result.toString();

}

Converting a String to Upper or Lower Case

// Convert to upper case

String upper = string.toUpperCase();

// Convert to lower case

String lower = string.toLowerCase();

Converting a String to a Number

int i = Integer.parseInt(“123”);

long l = Long.parseLong(“123”);

float f = Float.parseFloat(“123.4”);

double d = Double.parseDouble(“123.4e10”);

Breaking a String into Words

String aString = “word1 word2 word3”;

StringTokenizer parser =

new StringTokenizer(aString);

while (parser.hasMoreTokens()) {

processWord(parser.nextToken());

Ques 3. Searching a String

Ans. String string = “aString”;

// First occurrence.

int index = string.indexOf(‘S’); // 1

// Last occurrence.

index = string.lastIndexOf(‘i’); // 4

// Not found.

index = string.lastIndexOf(‘z’); // -1

 

Ques 4. Connecting to a Database and Strings Handling

Ans. Constructing a String

If you are constructing a string with several appends, it may be more efficient to construct it using a

StringBuffer and then convert it to an immutable String object.

StringBuffer buf = new StringBuffer(“Initial Text”);

// Modify

int index = 1;

buf.insert(index, “abc”);

buf.append(“def”);

// Convert to string

String s = buf.toString();

Getting a Substring from a String

int start = 1;

int end = 4;

String substr = “aString”.substring(start, end); // Str

 

Ques 5. What is a transient variable?

Ans. A transient variable is a variable that may not be serialized. If you don’t want some field not to be serialized, you can mark that field transient or static.

 

Ques 6. What is the difference between Serializalble and Externalizable interface?

Ans. When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject()two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class’s serialization process.

 

Ques 7. How many methods in the Externalizable interface?

Ans. There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable. These two methods are readExternal() and writeExternal().

 

Ques 8. How many methods in the Serializable interface?

Ans. There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization tools that your class is serializable.

Ques 9. How to make a class or a bean serializable?

Ans. By implementing either the java.io.Serializable interface, or the java.io.Externalizable interface. As long as one class in a class’s inheritance hierarchy implements Serializable or Externalizable, that class is serializable.

Ques 10. What is the serialization?

Ans. The serialization is a kind of mechanism that makes a class or a bean persistence by having its properties or fields and state information saved and restored to and from storage.

 

Ques 11. What are synchronized methods and synchronized statements?

Ans. Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method’s object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

 

Ques 12. What is synchronization and why is it important?

Ans. With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often causes dirty data and leads to significant errors.

 

Ques 13. What is the purpose of finalization?

Ans. The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

 

Ques 14. What classes of exceptions may be caught by a catch clause?

Ans. A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types.

 

Ques 15. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

Ans. The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

 

Ques 16. What happens when a thread cannot acquire a lock on an object?

Ans. If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object’s lock, it enters the waiting state until the lock becomes available.

 

Ques 17. What restrictions are placed on method overriding?

Ans. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.

 

Ques 18. What restrictions are placed on method overloading?

Ans. Two methods may not have the same name and argument list but different return types.

 

Ques 19. How does multithreading take place on a computer with a single CPU?

Ans. The operating system’s task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.

 

Ques 20. How is it possible for two String objects with identical values not to be equal under the == operator?

Ans. The == operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory.

 

Ques 21. How are this() and super() used with constructors?

Ans. this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

 

Ques 22. What class allows you to read objects directly from a stream?

Ans. The ObjectInputStream class supports the reading of objects from input streams.

 

Ques 23. What is the ResourceBundle class?

Ans. The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program’s appearance to the particular locale in which it is being run.

 

Ques 24. What interface must an object implement before it can be written to a stream as an object?

Ans. An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.

 

Ques 25. What is Serialization and deserialization?

Ans. Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.