Industrial Training

Core Java



What are the advantages of using exception handling?

Exception handling provides the following advantages over "traditional" error management techniques:

* Separating Error Handling Code from "Regular" Code.
* Propagating Errors Up the Call Stack.
* Grouping Error Types and Error Differentiation.


What is transient variable?

  Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.

What are Access Specifiers available in Java?

Access specifiers are keywords that determines the type of access to the member of a class. These are:
1.Public
2.Protected
3.Private
4.Defaults


Explain the Encapsulation principle.

Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

What is method overloading?

Method Overloading means to have two or more methods with same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.
Note:

* Overloaded methods MUST change the argument list
* Overloaded methods CAN change the return type
* Overloaded methods CAN change the access modifier
* Overloaded methods CAN declare new or broader checked exceptions
* A method can be overloaded in the same class or in a subclass


Method overloading refers to using of two methods with the same names in a class.

The names of the methods may be same but they must differ in the number/type of arguements they contain and/or the return type.

Overloaded methods can change the access modifier and their exceptions.

Examples:

ordinary method:
public void doStuff()
{}

Overloaded method:
public string doStuff(String s)throws IOException
{}


How does the Java default constructor be provided?

If a class defined by the code does not have any constructor, compiler will automatically provide one no-parameter-constructor (default-constructor) for the class in the byte code. The access modifier (public/private/etc.) of the default constructor is the same as the class itself.


when we are not create any constructor then java take itself default constructor as(public,private,etc.)class.

What is Constructor?

* A constructor is a special method whose task is to initialize the object of its class.
* It is special because its name is the same as the class name.
* They do not have return types, not even void and therefore they cannot return values.
* They cannot be inherited, though a derived class can call the base class constructor.
* Constructor is invoked whenever an object of its associated class is created.
constuctor name and class name is must same


Describe the wrapper classes in Java.

Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.


Wrapper class is wrapper around a primitive data type(i.e int,float,double,long).Some Examples Of wrapper classes are INTEGER,DOUBLE,LONG etc.
Why we need them because the memory allocated to wrapper class instances is on heap,while all the primitive data types goes on stack.
Conclusion use wrapper when u need an Integer array or a floating point array and thus improve your memory utilization also.


What is the difference between throw and throws?

Throw:it is used to raise exception explicitly
that means it is use when a user defined exception is raised.

Throws:if a method is capable of throwing an exception but
it does not handle the exception that must be specified by using "throws" class.


they are not same.

"throws" declares that your method is capable of throwing an exception.

"throw" actually does the work of throwing the exception.

example :

public void doSomething() throws ApplicationException
{
try{
}
catch(Exception e){
// catch all excpetions n provide some meaningful message
throw ApplicationException("An error occurred while trying connect to DB");
}
}

What is a local, member and a class variable?
Variables declared within a method are ?local? variables. Variables declared within the class i.e not within any methods are ?member? variables (global variables). Variables declared within the class i.e not within any methods and are defined as ?static? are class variables


Variables declared within a method or block is called Local Variables. These will have limited access.

Variables declared outside of a method or a block called as Members of a class. These can be accessed with the help of object.

Variables preceded with access specifier along with static means class variables. Class variables will be created only one time and accessable with the class name. Only one copy of the variable will be shared across the objects.
What is Collection API?

The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.


The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.Explain the user defined Exceptions?

User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.


for eg:
class userExcep extends Exception{userExcep(String msg){super(msg);}}What is runtime polymorphism or dynamic method dispatch?

In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.

What is static block?

Static block which exactly executed exactly once when the class is first loaded into JVM. Before going to the main method the static block will execute.


Hi I am Pluto.