Exception

                                                          Exception 

1) What is an Exception?
An exception is an abnormal condition that arises in a code sequence at run time. In other
words, an exception is a run-time error.

2) What is a Java Exception?

A Java exception is an object that describes an exceptional condition i.e., an error condition
that has occurred in a piece of code. When this type of condition arises, an object
representing that exception is created and thrown in the method that caused the error by
the Java Runtime. That method may choose to handle the exception itself, or pass it on.
Either way, at some point, the exception is caught and processed.

3) What are the different ways to generate and Exception?

There are two different ways to generate an Exception.
1. Exceptions can be generated by the Java run-time system.
Exceptions thrown by Java relate to fundamental errors that violate the rules of the
Java language or the constraints of the Java execution environment.
2. Exceptions can be manually generated by your code.
Manually generated exceptions are typically used to report some error condition to
the caller of a method.

4) Where does Exception stand in the Java tree hierarchy?

• java.lang.Object
• java.lang.Throwable
• java.lang.Exception
• java.lang.Error

5) Is it compulsory to use the finally block?

It is always a good practice to use the finally block. The reason for using the finally block is,
any unreleased resources can be released and the memory can be freed. For example while
closing a connection object an exception has occurred. In finally block we can close that
object. Coming to the question, you can omit the finally block when there is a catch block
associated with that try block. A try block should have at least a catch or a finally block.

6) How are try, catch and finally block organized?

A try block should associate with at least a catch or a finally block. The sequence of try,
catch and finally matters a lot. If you modify the order of these then the code won’t
compile. Adding to this there can be multiple catch blocks associated with a try block. The
final concept is there should be a single try, multiple catch blocks and a single finally block
in a try-catch-finally block.

7) What is a throw in an Exception block?

“throw” is used to manually throw an exception (object) of type Throwable class or a
subclass of Throwable. Simple types, such as int or char, as well as non-Throwable
classes, such as String and Object, cannot be used as exceptions. The flow of execution
stops immediately after the throw statement; any subsequent statements are not
executed.
throw ThrowableInstance;
ThrowableInstance must be an object of type Throwable or a subclass of Throwable.
throw new NullPointerException("thrownException");

8) What is the use of throws keyword?

If a method is capable of causing an exception that it does not handle, it must specify this
behavior so that callers of the method can guard themselves against that exception. You do
this by including a throws clause in the method’s declaration. A throws clause lists the
types of exceptions that a method might throw.
type method-name(parameter-list) throws exception-list
{
// body of method
}

9) What are Checked Exceptions and Unchecked
Exceptions?
The types of exceptions that need not be included in a methods throws list are called
Unchecked Exceptions.
• ArithmeticException
• ArrayIndexOutOfBoundsException
• ClassCastException
• IndexOutOfBoundsException
• IllegalStateException
• NullPointerException
• SecurityException
The types of exceptions that must be included in a methods throws list if that method can
generate one of these exceptions and does not handle it itself are called Checked
Exceptions.
• ClassNotFoundException
• CloneNotSupportedException
• IllegalAccessException
• InstantiationException
• InterruptedException
• NoSuchFieldException
• NoSuchMethodException

10) What are Chained Exceptions?

The chained exception feature allows you to associate another exception with an exception.
This second exception describes the cause of the first exception. Lets take a simple
example. You are trying to read a number from the disk and using it to divide a number.
Think the method throws an ArithmeticException because of an attempt to divide by zero
(number we got). However, the problem was that an I/O error occurred, which caused the
divisor to be set improperly (set to zero). Although the method must certainly throw an
ArithmeticException, since that is the error that occurred, you might also want to let the
calling code know that the underlying cause was an I/O error. This is the place where
chained exceptions come in to picture.
Throwable getCause( )
Throwable initCause(Throwable causeExc)

11)What is Exception Handling ?
The process of converting system error messages into user friendly error message is
known as Exception handling.

12)Which is super class for any Exception class ?

Object class is super class for any Exception class.

13)Can any statement is possible between try and catch block ?
Each and every try block must be immediately followed by catch block that is no
intermediate statements are allowed between try and catch block.

14)Can any try block contain another try block ?
Yes, One try block can contains another try block that is nested or inner try block can
be possible.

15)When IOException is thrown ?
IOException is thrown in following conditions which is given below;
· Try to transfer more data but less data are present.
· Try to read data which is corrupted
· Try to write but file is read only.

16)When ArithmeticException is thrown ?

The ArithmeticException is thrown when integer is divided by zero or taking the
remainder of a number by zero. It is never thrown in floating-point operations.

17)Difference between throw and throws ?

18)Difference between checked Exception and un-checked Exception ?


19)Difference between Error and Exception?


Inheritance

                                                            Inheritance


What is Inheritance ?
   The process of obtaining the data members and methods from one class to another
class is known as inheritance. It is one of the fundamental features of object-oriented
programming.

What are advantage of Inheritance ?
   If we develop any application using concept of Inheritance than that application have
following advantages,
· Application development time is less.
· Application take less memory.
· Application execution time is less.
· Application performance is enhance (improved).
· Redundancy (repetition) of the code is reduced or minimized so that we get
consistence results and less storage cost.

Why use Inheritance ?
· For Method Overriding (used for Runtime Polymorphism).
· It's main uses are to enable polymorphism and to be able to reuse code for different
classes by putting it in a common super class
· For code Re-usability

You can decrease scope of access modifier in inheritance ?
No, In Inheritance the scope of access modifier increasing is allow but decreasing is
not allow. Suppose in parent class method access modifier is default then it's present
in child class with default or public or protected access modifier but not private.

How many type of inheritance are allow in java ?
In Java following inheritance are allow;
· Single inheritance
· Multilevel inheritance
· Hierarchical inheritance
· Hybrid inheritance

Which inheritance is not supported by Java ?
Multiple Inheritance, are not supported by java.

Why multiple inheritance is not supported in java?
Due to ambiguity problem java does not support mutiple inheritance at class level.

How to achieve multiple inheritance in java ?
Java can not support multiple inheritance at class level but you can achieve multiple
inheritance in java by using Interface concept.

Difference between Inheritance and package ?
->  Package keyword is always used for creating the undefined package and placing
common classes and interfaces.
-> import is a keyword which is used for referring or using the classes and interfaces of a specific package.

 Give real life example of class and object?
In real world many examples of object and class like dog, cat, and cow are belong to
animal's class. Each object has state and behaviors.
  For example a dog has state:-color, name, height, age as well as behaviors:- barking, eating, and sleeping.

Java Features

                                          Java Features

1. Simple
2. Object Oriented
3. Platform Independent
4. Architectural Neutral
5. Portable
6. Robust
7. Secure
8. Dynamic
9. Distributed
10. Multithread
11. Interpretive
12. High Performance

1. Simple:-

Java is a simple programming language because,

 Java technology has eliminated all the difficult and confusion oriented concepts like pointers, multiple inheritance in the java language.
 Java uses c,cpp syntaxes mainly hence who knows C,CPP for that java is simple language.

2. Object Oriented:-

 Java is object oriented technology because it is representing total data of the class in the form of object.

 Object oriented programming (OOPs) is a methodology that simplifies software development and maintenance by providing some rules.
Basic concept of Oops are
 Object
 Class
 Inheritance
 Polymorphism
 Encapsulation
 Abstraction

3. Platform Independent :-

When we compile the application by using one operating system (windows) that Compiled file can execute only on the same operating system(windows) this behavior is called platform dependency.

Example :- C,CPP …etc
When we compile the application by using one operating system (windows) that Compiled file can execute in all operating systems(Windows,Linux,Mac…etc) this behavior is called platform independency.
Example :- java,Ruby,Scala,PHP …etc

4. Architectural Neutral:-

Java tech applications compiled in one Architecture/hardware (RAM, Hard Disk) and that Compiled program runs on any architecture (hardware) is called Architectural Neutral.
5. Portable:-
In Java the applications are compiled and executed in any OS (operating system) and any Architecture (hardware) hence we can say java is a portable language.

6. Robust:-


Any technology good at two main areas that technology is robust technology.

a. Exception Handling
b. Memory Allocation
Java is providing predefined support to handle the exceptions.
Java provides Garbage collector to support memory management.

7. Secure:-


 To provide implicit security Java provides one component inside JVM called Security Manager.

 To provide explicit security for the Java applications we are having very good predefined library in the form of java.security package.
8. Dynamic:-
Java is dynamic technology it follows dynamic memory allocation (at runtime the memory is allocated).
9. Distributed:-
By using java it is possible to develop distributed applications & to develop distributed applications java uses RMI,EJB…etc


10. Multithreaded: -

 Thread is a light weight process and a small task in large program.

 In java it is possible to create user thread & it possible to execute simultaneously is called multithreading.
 The main advantage of multi threading is it shares the same memory & threads are important at multimedia, gaming, web application.

11. Interpretive:-

JAVA is both interpretive and completive by using Interpreter we are converting source code into byte code and it a interpreter is a part of JVM.


12. High Performance:-


If any technology having features like Robust, Security, Platform Independent, Dynamic and so on then that technology is high performance

Java keywords

Java keywords:-

Java keywords are 50

Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. 

Data Types (8)
Flow-Control:-(10)
Predefined constants 
byte
short
int
long
float
double
char
boolean

if
else
switch
case
default
break
for
while
do
continue

method-level:-(2)
void
return

Object-level:- (4)
new
this
super
instanceof

Modifiers:-(11)
Exception handling:-(5)
source-file:-(6)
public
private
protected
abstract
final
static
strictfp
native
transient
volatile
synchronized
try
catch
finally
throw
throws

class
extends
interface
implements
package
import

1.5 version:-(2)
Predefined Constants:-(3)
Unused:-(2)
enum
assert

True
 False
 null

goto
const



Syntax for java

Syntax for a standalone application in Java:

********************************************************* class <classname>
{
public static void main(String args[])
{
statements;
————————;
————————;
}
}

*********************************************************

Steps to run the above application:


1. Type the program in the DOS editor or notepad. Save the

file with a .java extension.
2. The file name should be the same as the class, which has the
main method.
3. To compile the program, using javac compiler, type the
following on the command line:
Syntax: javac <filename.java>
Example: javac abc.java
4. After compilation, run the program using the Java
interpreter.
Syntax: java <filaname> (without the .java
extension)
Example: java abc
5. The program output will be displayed on the command line.

Interface

                                                  Interface       

What is an interface ?
Interface is similar to class which is collection of public static final variables
(constants) and abstract methods.

Why use interface in java ?
In java interface are used for achieving multiple inheritance.

Can an Interface extend another Interface?
Yes an Interface can inherit another Interface.

How interface is similar to class ?
Whenever we compile any Interface program it generate .class file. That means the
bytecode of an interface appears in a .class file.

How interface is different from class ?
· we cannot instantiate an interface.
· An interface does not contain any constructors.
· All methods in an interface are abstract.
· An interface cannot contain instance fields. Interface only contains public static final
variables.
· An interface is can not extended by a class; it is implemented by a class.
· An interface can extend multiple interfaces. That means interface support multiple
Inheritance.

Why interface have no constructor ?
Because, constructor are used for eliminate the default values by user defined values,
but in case of interface all the data members are public static final that means all are
constant so no need to eliminate these values.
Other reason because constructor is like a method and it is concrete method and
interface does not have concrete method it have only abstract methods that's why
interface have no constructor.

What is Marker or tagged interface ?
An interface that have no member is known as marker or tagged interface. For
example: Serializable, Cloneable, Remote etc. They are used to provide some essential
information to the JVM so that JVM may perform some useful operation.

Why Method Overloading is not possible by changing the return type of method?
In java, method overloading is not possible by changing the return type of the method
because there may occur ambiguity.

Why Interface have no Constructor ?
Because, constructor are used for eliminate the default values by user defined values,
but in case of interface all the data members are public static final that means all are
constant so no need to eliminate these values.
Other reason because constructor is like a method and it is concrete method and
interface does not have concrete method it have only abstract methods that's why
interface have no constructor.

Why not use abstract and final modifier together ?
In java, abstract and final both modifiers are not allowed for a class at a time the
reason is abstract and final are opposite keywords. If a class is an abstract, then that
class must be extended (inherited). If a class is final then that class can not be
extended. So both keyword can not be use at a time

Why use Abstract class ?
Abstract class are used for fulfill common requirement. If we use concrete classes for
fulfill common requirements than such application will get the following limitations.
· Application will take more amount of memory space (main memory).
· Application execution time is more.
· Application performance is decreased.
To overcome above limitation you can use abstract class.

Difference between abstract class and concrete class ?

Difference between Abstraction and Encapsulation ?
Encapsulation is not provides fully security because we can access private member of
the class using reflation API, but in case of Abstraction we can't access static, abstract
data member of class.

Give Real life example of Abstraction
Abstraction shows only important things to the user and hides the internal details for
example when we ride a bike, we only know about how to ride bike but can not know
about how it work ? and also we do not know internal functionality of bike.

Difference between Encapsulation and Abstraction
Encapsulation is not provides fully security because we can access private member of
the class using reflation API, but in case of Abstraction we can't access static, abstract
data member of class.

How to achieve Encapsulation in java
In java you can achieve Encapsulation by using class concept.


Difference between Abstract and Interface ?

Multi-Threading

                                 Multi-Threading

What is thread ?
Thread is a lightweight components and it is a flow of control. In other words a flow of
control is known as thread.

What is multithreading ?

Multithreading in java is a process of executing multiple threads simultaneously.

Explaing State or Life cycle of thread?
State of a thread are classified into five types they are
· New State
· Ready State
· Running State
· Waiting State
· Halted or dead State

How to achieve multithreading in java ?

In java language multithreading can be achieve in two different ways.
· Using thread class
· Using Runnable interface

In which state no memory is available for thread ?

If the thread is in new or dead state no memory is available but sufficient memory is
available if that is in ready or running or waiting state.

Difference between sleep() and suspend() ?
Sleep() can be used to convert running state to waiting state and automatically thread
convert from waiting state to running state once the given time period is completed.
Where as suspend() can be used to convert running state thread to waiting state but it
will never return back to running state automatically.

What is Thread Synchronization ?
Allowing only one thread at a time to utilized the same resource out of multiple
threads is known as thread synchronization or thread safe.

Why use Thread Synchronization ?

Whenever multiple threads are trying to use same resource than they may be chance
to of getting wrong output, to overcome this problem thread synchronization can be
used.

How to achieve Thread Synchronization in java ?
In java language thread synchronization can be achieve in two different ways.
· Synchronized block
· Synchronized method