JAVA FAQS1

What is JAVA ?
Java is a pure object oriented programming language, which has derived C syntax and C++ object oriented programming features.
Is a compiled and interpreted language and is platform independent and
Can do graphics, networking, multithreading. It was initially called as OAK.

What r the four cornerstones of OOP ?
Abstraction : Can manage complexity through abstraction. Gives the complete overview of a particular task and the details are handled by its derived classes. Ex : Car.
Encapsulation : Nothing but data hiding, like the variables declared under private of a particular class are accessed only in that class and cannot access in any other the class.
Inheritance : Is the process in which one object acquires the properties of another object, ie., derived object.
Polymorphism : One method different forms, ie., method overriding and interfaces are the examples of polymorphism.

what is downcasting ?
Doing a cast from a base class to a more specific class. The cast does not convert the object, just asserts it actually is a more specific extended object.
e.g. Dalamatian d = (Dalmatian) aDog;
Most people will stare blankly at you if you use the word downcast. Just use cast.

What is Java Interpreter ?
It is Java Virtual Machine. ie., a java program compiles the Unicode to intermediary code called as Bytecode which is not an executable code. that is executed by Java interpreter.

What are Java Buzzwords ?Simple : Easy to learn.
Secure : Provided by firewalls between networked applications.
Portable : Can be dynamically downloaded at various platforms connect to internet.
OOP : Four Corner stones.
Multithread : Can perform more than one task concurrently in a single program.
Robust : overcomes problems of de-allocation of memory and exceptions.
Interpreted : Convert into byte code and the executes by JVM.
Distributed : Concept of RMI.
Dynamic : Verifying and accessing objects at run time.


What are public static void main(String args[]) and System.out.println() ?Public keyword is an access specifier.
Static allows main() to be called without having to instantiate a particular instance of class.
Void does not return any value.
main() is the method where java application begins.
String args[] receives any command line arguments during runtime.

System is a predefined class that provides access to the system.
out is output stream connected to console.
println displays the output.

What are identifiers and literals ?Identifiers are the variables that are declared under particular datatype.
Literals are the values assigned to the Identifiers.

What is Typed language ?This means that the variables are at first must be declared by a particular datatype whereas this is not the case with javascript.

Scope and lifetime of variables ?
scope of variables is only to that particular block
lifetime will be till the block ends.
variables declared above the block within the class are valid to that inner block also.

Casting Incompatible types ?Can be done either implicitly and explicitly. ie., int b = (int) c; where c is float.
There are two types of castings related to classes : 1) unicast and 2)multicast.

Declaration of Arrays ?
int a[] = new int[10]; int a[][] = new int[2][2];

What does String define ?String is an array of characters, but in java it defines an object.
and the variable of type string can be assigned to another variable of type String.

Define class ?A class is a one which defines new datatype, and is template of an object, and is a protoype.

Types of Constructors ?
Default Constructor, Parameterized Constructor, Copy Constructors
Garbage collector takes the responsibility releasing the memory of object implicitly.

Define this , finalize, and final( for variables, methods, classes ) ?this is used inside any method to refer to the current object. and is mostly avoided.
finalize method is used to perform the actions specified in this method just before an object is destroyed. ie just before garbage collector process.
final with variables is we cant change the literals of the variables ie nothing but const.
final with method means we cant override that method.
final with class means we cannot have derived classes of that particular class.

What is passed by reference ?
Objects are passed by reference.
In java we can create an object pointing to a particular location ie NULL location by specifying : ;
and also can create object that allocates space for the variables declared in that particular class by specifying : = new ();

Explain about Static ?When a member is declared as static it can be accessed before any objects of its class are created and without any reference to any object.
these are global variables, no copy of these variables can be made.
static can also be declared for methods. and cannot refer to this or super.

What r 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.

Briefly about super() ?This is used to initialize constructor of base class from the derived class and also access the variables of base class like super.i = 10.

method overloading : same method name with different arguments.
method overriding : same method name and same number of arguments.

What is Dynamic Method Dispatch ?this is the mechanism by which a call to an overridden function is resolved at runtime rather than at compile time. And this is how Java implements runtime polymorphism.

What r abstract classes ?
To create a superclass that only defines generalized form that will be shared by all its subclasses, leaving it to each subclass to fill in the details.
• we cannot declare abstract constructors and abstract static methods.
• An abstract class contains at least one abstract method.
• And this abstract class is not directly instantiated with new operator.
• Can create a reference to abstract class and can be point to subclass object.

What is Object class and java.lang ?Object class is the superclass of all the classes and means that reference variable of type object can refer to an object of any other class. and also defines methods like finalise,wait.
java.lang contains all the basic language functions and is imported in all the programs implicitly.

What r packages and why ? how to execute a program in a package ?Package is a set of classes, which can be accessed by themselves and cannot be accessed outside the package. and can be defined as package .
Package name and the directory name must be the same.
And the execution of programs in package is done by : java mypack.account
where mypack is directory name and account is program name.

What does a java source file can contain ?
specifying package name.
importing more than one package
specifying classes and there methods
………………

Why interface and what extends what ?Interface is similar to abstract classes. this define only method declarations and definitions are specified in the classes which implements these interfaces.
and “ one interface multiple methods “ signifies the polymorphism concept.
Here an interface can extend another interface.
and a class implements more than one interface.

How Exception handling is done in Java ?
Is managed via 5 keywords :
try : statements that you want to monitor the exceptions contain in try block.
catch : the exception thrown by try is catched by this.
throw : to manually throw exception we go for this.
throws : Exception that is thrown out of a method must be specified by throws after the method declaration.
finally : this block is executed whether or not an exception is thrown. and also it is executed just before the method returns. and this is optional block.

What r checked and unchecked Exceptions ?* Unchecked Exceptions are those which r not included in throws list and are derived from RuntimeException which are automatically available and are in java.lang.
* Checked Exceptions are those which cannot handle by itself.

What is Thread and multithread ? How is thread created ?
Thread is a small unit of dispatchable code. ie., a single program can perform more than one task simultaneously. This is a light weight process and is of low cost.
MultiThreading writes more efficient programs and make maximum use of CPU ie., it minimizes its idle time.
A Thread is created in two ways :
1) By implementing the runnable interface.
2) By extending the Thread class itself.
First way is used to update only run() method. ie we can use only run() method in this class.
Second way is used to define several methods overridden by derived class.
Generally we override only run() method so we go for runnable interface.

What are the various states and methods of thread ?States : Running, Ready to run, Suspended, Resumed, blocked and terminated.
Methods : getName, getPriority, isAlive, join, run, sleep, start.
Thread priorities are MIN_PRIORITY, MAX_PRIORITY, NORM_PRIORITY.

What is interprocess synchronization ? ( also called Monitor, Semaphore )
Consider a small box namely MONITOR that can hold only one thread. Once a thread enters a monitor, all other threads must wait until that thread exits the monitor.
In this way, a monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.
Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object.

What is Stream ?
It is an Abstraction that either produces or consumes information.
Two types : Byte Stream and Character Stream.
Print Writer is to Print output in Real World Programs.

How to enter data in java ?First specify :
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
then say br.read(), and this is put in do while loop to receive as many as we require.

Define Inline Functions ?In ordinary functions, during the program execution, at any function call the controller jumps to the function definition, performs operation and returns the value, then comes back to the program.
whereas in Inline functions the controller copies the complete function definition into the program and performs operations.

What are the four components in URL ?http:// www. yahoo .com : 8080 / index.html
http: --- > is protocol
www. yahoo .com ---à is IP address
8080 --à port number and is a pointer to memory location.
index.html --à file path to be loaded

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 macros and Inline functions ? Which is best and Difference ?
Inline functions do Parameter passing, where as Macros do Text Substitution.
Its better to go for Inline functions than macros, else you may get different results.

How to Declare a pointer to function ?
int func_name(float) //ordinary declaration of function.
int (*func_name_ptr)(float) // Declaration of pointer.
Assigning the Address can be done as :
func_name_ptr = func_name. // Return type and Signatures must be Same.
Invoking a function pointer : int x = (*func_name_ptr)(values as specified);

What is a file ? What is a Directory ?
File : Describes the properties of file, like permissions, time, date, directory path, and to navigate subdirectory hierarchies.
Directory : Is a file that contains list of other files and directories .

What is Serialization ?The process of writing the state of an object to a byte stream. and can restore these objects by using deserialization.
Is also need to implement RMI, which allows a java object of one machine to invoke java object of another machine.
ie., the object is passed as an argument by serializing it and the receiving machine deserializes it.

Interfaces include by java.lang ?
Java.lang is the package of all the classes.
And is automatically imported into all Java programs.
Intefaces : Cloneable, Comparable, Runnable.

What is user defined exception?To handle situations specific to our applications we go for User Defined Exceptions.
and are defined by User using throw keyword.

What is the difference between process and threads?Process is a heavy weight task and is more cost.
Thread is a light weight task which is of low cost.
A Program can contain more than one thread.
A program under execution is called as process.

What is the difference between CGI and Servlet ?
CGI suffered serious performance problems. servlets performance is better.
CGI create separate process to handle client request. and Servlets do not.
CGI is platform dependent, whereas Servlet is platform-independent b’cauz written in java.
Servlets can communicate with applets, databases and RMI mechanisms.