Java FAQ's
Java FAQ's
A. Pass By Reference means the passing the address itself rather than
passing the value. Passby Value means passing a copy of the value to be
passed.
Q. What is an Iterator?
A. 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.
Q. What is final?
A. 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).
· A class may implement several interfaces where as it can extend only one abstract class.
· An interface cannot provide any code at all, much less default code where as an abstract class can provide
complete code, default code, and/or just stubs that has to be overridden.
· Static final constants can use the interfaces without qualification in classes that implement the interface
where as both static constants and instance variables can use the abstract classes.
· Interface implementation can be added to any existing third part classes where as third party classes must
be rewritten to extend from the abstract classes.
· Interfaces are used to often represent peripheral ability of a class, not central identity where as abstract
class defines core identity of its descendants.
· If various implementations share is method signature then Interface works best where as If the various
implementations are all of a kind and share a common status and behavior, usually an abstract class works
best.
· If you add a new method to an interface, you must track down all implementations of that interface in the
universe and provide them with a concrete implementation of that method where as in case of an abstract
class if you add a new method to an abstract class, you have the option of providing a default
implementation of it. Then all existing code will continue to work without change.
· You can put shared code into an abstract class, where you cannot into an interface.
· To write user-defined exception in java, you exception class should extend Exception base class have call
the super class methods in your own methods.
· This means one object having multiple parents, which is not truly supported in java but supported in C++.
But overall design of java suggests that we can implement multiple inheritances in java using interfaces.
· A non-static inner class may have object instances that are associated with instances of the class's outer
class. A static inner class does not have any object instances.
· Inner class is class, which is defined inside a class as private class and always bears a reference to the
outer class.
· Scope of inner class is the entire enclosing class in which the inner class is nested.
· Inner classes can contain methods that return handles to inner class instances.
· Major advantage of inner classes is the ability to create adaptor classes that implement an interface.
o Rather than handle classes returning inner classes, they return interfaces.
o Wrapped classes are classes that allow primitive types to be accessed as objects.
· An immutable object is an object and any object it references that does not change after construction. The
object is, therefore, immutable for its lifetime.
· Immutable classes are commonly used to represent strings, colors, and numeric values.
Advantage:
· They guarantee that their state cannot change after construction, they are inherently thread-safe.
· Implement a deep clone if the default shallow clone is not correct for a properly behaved immutable object.
· A Java package is a naming context for classes and interfaces. A package is used to create a separate
name space for groups of classes and interfaces. Packages are also used to organize related classes and
interfaces into a single API unit and to control accessibility to these classes and interfaces
· 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.
· Incase of constructors, this() is used to invoke a constructor of the same class. super() is used to invoke a
superclass constructor.
· A static variable is associated with the class as a whole rather than with specific instances of a class. Non-
static variables take on unique values with each object instance.
1. Method overloading
1) We all know that linkedHashMap maintains the ordering of elements as they are
inserted. What happens when an already inserted element is reinserted? Will the
ordering change?
Ans) A weak reference is one that does not prevent the referenced object from being
garbage collected. You might use them to manage a HashMap to look up a cache of
objects. A weak reference is a reference that does not keep the object it refers to alive. A
weak reference is not counted as a reference in garbage collection. If the object is not
referred to elsewhere as well, it will be garbage collected.
Ans) The code sleep(1000); puts thread aside for exactly one second. The code
wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it
receives the notify() or notifyAll() call. The method wait() is defined in the class Object
and the method sleep() is defined in the class Thread.
4) What will be the output on executing the following code.
public class MyClass {
public static void main (String args[] ) {
int abc[] = new int [5];
System.out.println(abc);
}
}
Ans) D
It will print some junk characters to the output. Here it will not give any compile
time or runtime error because we have declared and initialized the array properly.
Event if we are not assigning a value to the array, it will always initialized to its
defaults.
Ans) C. Here it will not give any compile time or runtime error because we have
declared and initialized the array properly. Event if we are not assigning a value to
the array, it will always initialized to its defaults. So the array will be initialized
with values zero.
9)What is JVM?
Ans)JVM stands for Java Virtual Machine. It is the run time for java programs. All are
java programs are running inside this JVM only. It converts java byte code to OS specific
commands. In addition to governing the execution of an application's byte codes, the
virtual machine handles related tasks such as managing the system's memory, providing
security against malicious code, and managing multiple threads of program execution.
10)What is JIT?
Ans)JIT stands for Just In Time compiler. It compiles java byte code to native code.
A. Triggers are special kind of stored procedures that get executed automatically when an
INSERT, UPDATE or DELETE operation takes place on a table.
Triggers can't be invoked on demand. They get triggered only when an associated action
(INSERT, UPDATE, DELETE) happens on the table on which they are defined.
Triggers are generally used to implement business rules, auditing. Triggers can also be
used to extend the referential integrity checks, but wherever possible, use constraints for
this purpose, instead of triggers, as constraints are much faster.
Q. What are cursors? Explain different types of cursors. What are the
disadvantages of cursors? How can you avoid cursors?
Types of cursors: Static, Dynamic, Forward-only, Keyset-driven. See books online for more
information.
Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a
network roundtrip, where as a normal SELECT query makes only one rowundtrip, however
large the resultset is. Cursors are also costly because they require more resources and
temporary storage (results in more IO operations). Furthere, there are restrictions on the
SELECT statements that can be used with some types of cursors.Most of the times, set
based operations can be used instead of cursors.
Q. What's the difference between a primary key and a unique key?
A. Dropping : (Table structure + Data are deleted), Invalidates the dependent objects
,Drops the indexes
Truncating: (Data alone deleted), Performs an automatic commit, Faster than delete
A. You can sort the results and return the sorted results to your program
by using ORDER BY keyword thus saving you the pain of carrying out the
sorting yourself. The ORDER BY keyword is used for sorting.
A. 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.
A. One can not do anytihng in this scenarion. Because Java does not allow
multiple inheritance and does not provide any exception interface as well.
A. The class should extend from Exception class. Or you can extend your
class from some more precise exception type also.
A. Your class should extend class Exception, or some more specific type thereof.
A. If the array is an array of primitive types, then all the elements of the array will
be initialized to the default value corresponding to that primitive type. e.g. All the
elements of an array of int will be initialized to 0, while that of boolean type will be
initialized to false. Whereas if the array is an array of references (of any type), all the
elements will be initialized to null.
Q. Can I import same package/class twice? Will the JVM load the package
twice at runtime?
A. One can import the same package or same class multiple times. Neither compiler
nor JVM complains abt it. And the JVM will internally load the class only once no
matter how many times you import the same class.
A. No the program fails to compile. The compiler says that the main method is
already defined in the class.
A. Yes it is possible. While starting the application we mention the class name to be
run. The JVM will look for the Main method only in the class whose name you have
mentioned. Hence there is not conflict amongst the multiple classes having main
method.
Q. How can one prove that the array is not null but empty using one line of
code?
A. 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.
Q. What if the main method is declared as private?
A. The program compiles properly but at runtime it will give "Main method not
public." message.
Q. What if the static modifier is removed from the signature of the main
method?
Q. What is final?
A. 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).
A. The program compiles properly but at runtime it will give "Main method not
public." message.
Q. What is the default value of an object reference declared as an instance
variable?
A. In declaration we just mention the type of the variable and it's name. We do not
initialize it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String ("abcd"); Or String s =
"abcd"; are both definitions.
A. No. A top level class can not be private or protected. It can have either "public" or
no modifier. If it does not have a modifier it is supposed to have a default access.If a
top level class is declared as private the compiler will complain that the "modifier
private is not allowed here". This means that a top level class can not be private.
Same is the case with protected.
A. Java only supports pass by value. With objects, the object reference itself is
passed by value and so both the original reference and parameter copy both refer to
the same object .
Q. What is serialization?
LANGUAGE STUFF
JSP
Q.What will be the default values of all the elements of an array defined as
an instance variable?
A. If the array is an array of primitive types, then all the elements of the array will
be initialized to the default value corresponding to that primitive type. e.g. All the
elements of an array of int will be initialized to 0, while that of boolean type will be
initialized to false. Whereas if the array is an array of references (of any type), all the
elements will be initialized to null.
Q. Can I import same package/class twice? Will the JVM load the package
twice at runtime?
A. One can import the same package or same class multiple times. Neither compiler
nor JVM complains abt it. And the JVM will internally load the class only once no
matter how many times you import the same class.
A. No the program fails to compile. The compiler says that the main method is
already defined in the class.
A. Yes it is possible. While starting the application we mention the class name to be
run. The JVM will look for the Main method only in the class whose name you have
mentioned. Hence there is not conflict amongst the multiple classes having main
method.
Q. How can one prove that the array is not null but empty using one line of
code?
A. 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.
Q. What if the main method is declared as private?
A. The program compiles properly but at runtime it will give "Main method not
public." message.
Q. What if the static modifier is removed from the signature of the main
method?
A. 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).
A. The program compiles properly but at runtime it will give "Main method not
public." message.
A. In declaration we just mention the type of the variable and it's name. We do not
initialize it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String ("abcd"); Or String s =
"abcd"; are both definitions.
A. No. A top level class can not be private or protected. It can have either "public" or
no modifier. If it does not have a modifier it is supposed to have a default access.If a
top level class is declared as private the compiler will complain that the "modifier
private is not allowed here". This means that a top level class can not be private.
Same is the case with protected.
A. Java only supports pass by value. With objects, the object reference itself is
passed by value and so both the original reference and parameter copy both refer to
the same object .
Q. What is serialization?
A. Serialization is a mechanism by which you can save the state of an object by
converting it to a byte stream.