Saturday, May 28, 2011

JAVA FUNDAMENTALS

Certain concepts of java which are considerably the basics of java are the concept of strings, serialization, synchronization, threads, method overriding, method overloading, multiple inheritance are discussed.
Strings :
1. Searching a String
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
2. Connecting to a Database and Strings Handling
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
Serialization :
3. What is the difference between Serializalble and Externalizable interface?
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.
4. How many methods in the Externalizable interface?
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().
5. How many methods in the Serializable interface?
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.
6. How to make a class or a bean serializable?
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.
7. What is the serialization?
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.
8. What is Serialization and deserialization?
Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.
9 How many methods do u implement if implement the Serializable Interface?
The Serializable interface is just a "marker" interface, with no methods of its own to implement.
10. What is a transient variable?
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.
11. How many methods do u implement if implement the Serializable Interface?
The Serializable interface is just a "marker" interface, with no methods of its own to implement.

Synchronization :
12. What are synchronized methods and synchronized statements?
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.
13. What is synchronization and why is it important?
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.
14. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.
Thread :
15. What are the disadvantages of using threads?
Deadlocks.
16. What happens when a thread cannot acquire a lock on an object?
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.
17. How does multithreading take place on a computer with a single CPU?
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.
Method Overloading & Overriding :
18. What restrictions are placed on method overriding?
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.
19. What restrictions are placed on method overloading?
Two methods may not have the same name and argument list but different return types.
20. How are this() and super() used with constructors?
this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.
21. What are the Object and Class classes used for?
The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.
Multiple Inheritance :
22. Can you write Java code for declaration of multiple inheritance in Java ?
Class C extends A implements B
{
}
23. What do you mean by multiple inheritance in C++ ?
Multiple inheritance is a feature in C++ by which one class can be of different types. Say class teachingAssistant is inherited from two classes say teacher and Student.
24. How can you achieve Multiple Inheritance in Java?
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.
25. What do you mean by virtual methods?
virtual methods are used to use the polymorphism feature in C++. Say class A is inherited from class B. If we declare say function f() as virtual in class B and override the same function in class A then at runtime appropriate method of the class will be called depending upon the type of the object.
26. What do you mean by static methods?
By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f()
function as A.f(). There is no need of creating an object of class A.
27. What do mean by polymorphism, inheritance, encapsulation?
Polymorphism: is a feature of OOPL that at run time depending upon the type of object the appropriate method is called.
Inheritance: is a feature of OOP that represents the "is a" relationship between different objects(classes).
Say in real life a manager is a employee. So in OOP manger class is inherited from the employee class.
Encapsulation: is a feature of OOP that is used to hide the information.
28. Are there any other 'marker' interfaces?
java.rmi.Remote
java.util.EventListener
29. What does the "final" keyword mean in front of a variable?
A method? A class?
FINAL for a variable : value is constant
FINAL for a method : cannot be overridden
FINAL for a class : cannot be derived.
30. Name four methods every Java class will have.
public String toString();
public Object clone();
public boolean equals();
public int hashCode();
 

No comments:

Post a Comment