You are planning to do an indexed search in a
list of objects. Which of the two Java collections should you use: ArrayList or
LinkedList?
ArrayList
How can you minimize the need of garbage
collection and make the memory use more effective?
Use object pooling and weak object references.
There are two classes: A and B. The class B need
to inform a class A when some important event has happened. What Java technique
would you use to implement it?
If these classes are threads I’d consider
notify() or notifyAll(). For regular classes you can use the Observer
interface.
What access level do you need to specify in the
class declaration to ensure that only classes from the same directory can
access it?
You do not need to specify any access level, and
Java will use a default package access level.
If Runnable interface is better than Thread
class, than why we are using Thread class? What is the need for Thread class?
What are Encapsulation, Inheritance and
Polymorphism?
Encapsulation is the mechanism that binds
together code and data it manipulates and keeps both safe from outside
interference and misuse.
Inheritance is the process by which one object
acquires the properties of another object.
Polymorphism is the feature that allows one
interface to be used for general class actions.
What is the difference between Assignment and
Initialization?
Assignment can be done as many times as desired
whereas initialization can be done only once.
What are Class, Constructor and Primitive data
types?
Class is a template for multiple objects with
similar features and it is a blue print for objects. It defines a type of
object according to the data the object can hold and the operations the object
can perform.
Constructor is a special kind of method that
determines how an object is initialized when created.
Primitive data types are 8 types and they are:
byte, short, int, long, float, double, boolean, char.
What is the difference between an argument and a
parameter?
While defining method, variables passed in the
method are called parameters. While using those methods, values passed to those
variables are called arguments.
What is final, finalize() and finally?
final : final keyword can be used for class,
method and variables. A final class cannot be subclassed and it prevents other
programmers from subclassing a secure class to invoke insecure methods. A final
method can’t be overridden. A final variable can’t change from its initialized
value.
finalize() : finalize() method is used just
before an object is destroyed and can be called just prior to garbage
collection.
finally : finally, a key word used in exception
handling, creates a block of code that will be executed after a try/catch block
has completed and before the code following the try/catch block.
The finally block will execute whether or not an
exception is thrown.
What is the difference between this() and
super()?
this() can be used to invoke a constructor of
the same class whereas super() can be used to invoke a super class constructor.
What is the difference between superclass and
subclass?
A super class is a class that is inherited
whereas sub class is a class that does the inheriting.
What are inner class and anonymous class?
Inner class : classes defined in other classes,
including those defined in methods are called inner classes. An inner class can
have any accessibility including private.
Anonymous class : Anonymous class is a class
defined inside a method without a name and is instantiated and declared in the
same place and cannot have explicit constructors.
What is the difference between Integer and int?
a) Integer is a class defined in the java. lang
package, whereas int is a primitive data type defined in the Java language
itself. Java does not automatically convert from one to the other.
b) Integer can be used as an argument for a
method that requires an object, whereas int can be used for calculations.
What is a cloneable interface and how many
methods does it contain?
It is not having any method because it is a
TAGGED or MARKER interface.
What is the difference between abstract class
and interface?
a) All the methods declared inside an interface
are abstract whereas abstract class must have at least one abstract method and
others may be concrete or abstract.
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.
What is the difference between exception and
error?
What is the difference between process and
thread?
Process is a program in execution whereas thread
is a separate path of execution in a program.
What is synchronization?
Synchronization is the mechanism that ensures
that only one thread is accessed the resources at a time.
Are there any global variables in Java, which
can be accessed by other part of your program?
No, it is not the main method in which you
define variables. Global variables is not possible because concept of
encapsulation is eliminated here.