Java25 07/10/2008

What is a task’s priority and how is it used in scheduling?

A task’s priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.

To what value is a variable of the String type automatically initialized?

The default value of a String type is null.

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.

Can an unreachable object become reachable again?

An unreachable object may become reachable again. This can happen when the object’s finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.

How do I serialize an object to a file?

The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a Fileoutputstream. This will save the object to a file.

Which methods of Serializable interface should I implement?

The serializable interface is an empty interface, it does not contain any methods. So we do not implement any methods

Java24 07/10/2008

How can I customize the seralization process? i.e. how can one have a control over the serialization process?

Yes it is possible to have control over serialization process. The class should implement Externalizable interface. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.

What is the common usage of serialization?

Whenever an object is to be sent over the network, objects need to be serialized. Moreover if the state of an object is to be saved, objects need to be serilazed.

When you serialize an object, what happens to the object references included in the object?

The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized alongwith the original obect.

What one should take care of while serializing the object?

One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.

What happens to the static fields of a class during serialization?

There are three exceptions in which serialization doesnot necessarily read and write to the stream. These are
1. Serialization ignores static fields, because they are not part of any particular state.
2. Base class fields are only handled if the base class itself is serializable

Java23 07/10/2008

Does importing a package imports the subpackages as well?

No you will have to import the subpackages explicitly.
For eg: Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of it’s subpackage.

What are some alternatives to inheritance?

Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

If I write return at the end of the try block, will the finally block still execute?

Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.

Is it necessary that each try block must be followed by a catch block?

It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

What are the different ways to handle exceptions?

There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions

Java22 07/10/2008

How does an exception permeate through the code?

An unhandled exception moves up the method stack in search of a matching When an exception is thrown from a code which is wrapped in a try block followed by one or more catch blocks, a search is made for matching catch block. If a matching type is found then that block will be invoked. If a matching type is not found then the exception moves up the method stack and reaches the caller method. Same procedure is repeated if the caller method is included in a try catch block. This process continues until a catch block handling the appropriate type of exception is found. If it does not find such a block then finally the program terminates.

What are runtime exceptions?

Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.

What are checked exceptions?

Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions.

Does Java provide any construct to find out the size of an object?

No there is not sizeof operator in Java. So there is not direct way to determine the size of an object directly in Java.

What is Externalizable interface?

Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods

Java21 07/10/2008

How can one prove that the array is not null but empty using one line of code?

Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

What is the purpose of garbage collection in Java, and when is it used?

The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

What is the difference between a constructor and a method?

A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

What is final?

A final class can’t be extended ie., final class may not be subclassed. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant).

What is static in java?

Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can’t override a static method with a nonstatic method. In other words, you can’t change a static method into an instance method in a subclass

Java20 07/10/2008

What if the main method is declared as private?

The program compiles properly but at runtime it will give “Main method not public.” message.

What is an Iterator interface?

The Iterator interface is used to step through the elements of a Collection .

Describe the Garbage Collection process in Java ?

The JVM spec mandates automatic garbage collection outside of the programmers control. The System.gc() or Runtime.gc() is merely a suggestion to the JVM to run the GC process but is NOT guaranteed.

How many static init can you have ?

As many as you want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope.

What is constructor chaining and how is it achieved in Java ?

A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement

Java19 07/10/2008

What methods can be overridden in Java?

In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods can be overwritten in subclasses except those that are declared final, static, and private.

Describe java’s security model.

Java’s security model is one of the most interesting and unique aspects of the language. For the most part it’s broken into two pieces: the user adjustable security manager that checks various API operations like file access, and the byte code verifier that asserts the validity of compiled byte code. public abstract class SecurityManager java.lang.SecurityManager is an abstract class which different applications subclass to implement a particular security policy. It allows an application to determine whether or not a particular operation will generate a security exception.

What is daemon thread and which method is used to create the daemon thread?

Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.

What are Transient and Volatile Modifiers?

Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.
Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.

What is the difference between a public and a non-public class?

A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

What is the difference between instanceof and isInstance?

instanceof is used to check to see if an object can be cast into a specified type without throwing a classcastexception.
isInstance() determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof
operator. The method returns true if the specified Object argument is non-null and can
be cast to the reference type represented by this Class object without raising a
ClassCastException. It returns false otherwise

Java18 07/10/2008

What does the “abstract” keyword mean in front of a method? A class?

Abstract keyword declares either a method or a class. If a method has a abstract keyword in front of it,it is called abstract method.Abstract method has no body.It has only arguments and return type.Abstract methods act as placeholder methods that are implemented in the subclasses.
Abstract classes can’t be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract

What is JDBC? Describe the steps needed to execute a SQL query using JDBC.

The JDBC is a pure Java API used to execute SQL statements. It provides a set of classes and interfaces that can be used by developers to write database applications.
The steps needed to execute a SQL query using JDBC:
1. Open a connection to the database.
2. Execute a SQL statement.
3. Process th results.
4. Close the connection to the database.

What are native methods? How do you use them?

Native methods are methods that are defined as public static methods within a java class, but whose implementation is provided in another programming language such as C.

What does the keyword “synchronize” mean in java. When do you use it? What are the

Synchronize is used when u want to make ur methods thread safe. The disadvantage of synchronise is it will end up in slowing down the program. Also if not handled properly it will end up in dead lock.
1. Only use (and minimize it’s use)synchronization when writing multithreaded code as there is a speed (up to five to six time slower, depending on the execution time of the synchronized/non-synchronized method ) cost associated with its use.
2. In case of syncronized method modifier, the byte code generated is the exact same as non-syncronized method. The only difference is that a flag called ACC_SYNCRONIZED property flag in method’s method_info structure is set if the syncronized method modifier is present.
3. Also, syncronized keyword can make the code larger in size if used in the body of the method as bytecode for monitorenter/monitorexit is generated in addition to any exception handling.

Java says “write once, run anywhere”. What are some ways this isn’t quite true?

Any time you use system calls specific to one operating system and do not create alternative calls for another operating system, your program will not function correctly.
Solaris systems and Intel systems order the bits of an integer differently. (You may have heard of little endian vs. big endian)
If your code uses bit shifting, or other binary operators, they will not work on systems that have opposide endianism

Java17 07/10/2008

How can you force all derived classes to implement a method present in the base class?

Creating and implementing an interface would be the best way for this situation. Just create an interface with empty methods which forces a programmer to implement all the methods present under it.
Another way of achieving this task is to declare a class as abstract with all its methods abstract.

What’s the difference between == and equals method?

The equals method can be considered to perform a deep comparison of the value of an object, whereas the == operator performs a shallow comparison.
The equals() method compares the characters inside a string object. == operator compares two object references to check whether they refer to the same instances or not.

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.

What are the two basic ways in which classes that can be run as threads may be defined?

A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.

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.

What is the highest-level event class of the event-delegation model?

The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy.

What restrictions are placed on the values of each case of a switch statement?

During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value

Java16 07/10/2008

How is rounding performed under integer division?

The fractional part of the result is truncated. This is known as rounding toward zero.

What is the Dictionary class?

The Dictionary class provides the capability to store key-value pairs.

What are order of precedence and associativity, and how are they used?

Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left

What is a StringTokenizer ?

String Tokenizer provide parsing process in which it identifies the delimiters provided by the user , by default delimiters are spaces, tab, newline etc. and separates them from the tokens. Tokens are those which are separated by delimiters.

What are nested classes ?

There are two types : static and non-static.
static class means the members in its enclosing class (class within class) can be accessed by creating an object and cannot be accessed directly without creating the object.
non-static class means inner class and can be accessed directly with the object created for the outer class no need to create again an object like static class.

What is the difference between abstract class and interface?

a) All the methods declared inside an interface are abstract whereas abstract class may or may not contain abstract methods.
b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods.
c)Abstract class must have subclasses whereas interface can’t have subclasses

    Java Sets


    July 2008

    RSS Feed