Core Java Basic Definitions:
CoreJAVA
BASIC QUSTIONS
1.
What if the main method is declared as private?
String Litiral
Array
Annonymous Array
Primitive variable
Reference variable
Instance variable
Static variable
Local Variable
Var-Arg method
Command line arguments
OOPS
Data Hiding
Abstraction
Encapsulation
Tightly Encapsulated class
What is the difference between StringBuffer and String Bulder?
OverLoading:
Overring:
Method Hiding
Pholymorphism(flexibility)
Constructor:
Serialization:
DeSerialization:
Type-Casting:
Collections:
Array
Collection
Collection FrameWork:
Generic Classes
MultiThreading Interview Qustions
Thread
Process-Based Multi-Tasking:
Thread-Based Multi-Tasking:
Exception:
CoreJAVA
BASIC QUSTIONS
1.
What if the main method is declared as private?
The program compiles properly but at runtime it will give
“Main method not public.” message.
KeyWords:
Public
|
Protected
|
Static
|
Synchronized
|
Native
|
Transient
|
Volatile
|
Strictfb
|
private
|
Abstract
|
Final
|
Byte
|
Short
|
Int
|
Long
|
Float
|
Double
|
Char
|
Boolean
|
If
|
Else
|
Swtch
|
Case
|
Default
|
For
|
While
|
Do
|
Break
|
Continue
|
Return
|
Package
|
Import
|
Class
|
Instance
|
Extends
|
Implements
|
This
|
Super
|
Try
|
Cache
|
Throw
|
Throws
|
Finally
|
New
|
Assert
|
Void
|
Enum
|
Instanceof
|
Goto
|
Const
|
False
|
True
|
null
|
|
Definitions
String Litiral
Any sequence of character with in double codes “ ” is called
string litiral.
Array
An array is an indexed collection of fixed no of homogeneous
data elements
Annonymous Array
Sometimes we can create an array with out name also suchtype
of nameless arrays called Annonumus arrays ex: new int[] {10,20, 30}
Primitive variable
It can be used to represent the primitive values ex: int x=10;
Reference variable
It can be used to refer objects ex: Student s=new Student();
Instance variable
If the value of the variable is varied from Object to Object
such type of variable is called instance variable. Also called Object level
variables or attributes.
Static variable
If the value of a variable is not varied from object to
object then it is never recommended to declare that variable at object level, we
have to declare such type of variables
at class level by using static modifier. Also called Class level variables or
fields
Local Variable
To meat temporary requirement of the programmer sometimes we
create variables inside the method, block and constructor. Such type of
variables are called local variables. Also called stack variables, automatic
variables and temporary variables.
Var-Arg method
In 1.5 anwords, we can declare a method with variable no.of
arguments such type of methods are called var-arg methods. Ex: m1(int… x)
Command line arguments
The arguments which are passing from command prompt are
called Command line arguments. We customize the behavior of the main().
OOPS
Data Hiding
Hiding of the data. So that Outside person cant access
ourdata directly.
By using private modifier we can acheive data Hiding.
Abstraction
Hiding internal implementation details & just highylate
the set of services what we are offering is called “Abstraction”.
By using Abstract classes and interfaces we can achieve Abstraction.
Encapsulation
Encapsulating data & corresponding methods (behavior)
into single module is called “Encapsulation”.
If any java class follows Data Hiding and Abstaction such
type of class is said to Encapsulated class.
Encapsulated=DataHiding+Abstraction
Hiding data behind methods is the central concept of
Encapsulation
Tightly Encapsulated class
A class is said to be tightly encapsulate iff every data
member decleared as the private.
Getter or Setter methods are not checked.
What is the difference between StringBuffer and String Bulder?
If the Content will change frequently then it is never
recommended to go for String. Because for every change compulsory a new object
will be created.
-to handel this requirement compulsory we should go for StringBuffer
where all changes will be performend in existing object only instead of
creating new object.
StringBulder:
-every method present in string buffer is synchronized.
Hence at a time only one thread is allowed to access StringBuffer object. It
increases waiting time of the thread and Effects performance of the system.
-in 1.5v, StringBuilder is Exactly same as StringBuffer (including methods & Constructors).
Diff:
StringBuffer
|
String Builder
|
1. Every
method is synchronized
|
1. No
method is synchronized
|
2. SBuffer
object is thread safe Because SB object can be accessed by only one thread at
a time.
|
3. SBuilderis
not thread safe Because it can be accessed by multiple threads
simultaneously.
|
2. Relatively
performance is low
|
3. Relatively
performance is High
|
4. Intorduced
in 1.0
|
5. It
is in 1.5
|
OverLoading:
-Two methods are said to Overloaded iff method names are
same but arguments are different.
Overring:
In Overring resolution is also known as “ Dynamic method
dispatch”, also called runtime polymorphism and dynamic polymorphism or late
binding.
Method Hiding
Method Hiding
|
Overriding
|
1.Both methods should be a static
|
1.Both methods shoud be non-static
|
2.method resolution takes care by compiler based on reference type.
|
2.method resolution always takes care by jvmbased on Runtime object.
|
3.It is considerd as compile time polymorphism, or static
polymorohism, or early binding.
|
3.It considerd as Runtime polymorphism or dynamic Pholymorphsmor Late
binding.
|
|
|
Property
|
OverLoading
|
OverRiding
|
Method names
|
Must be same
|
Must be same
|
Arguments
|
Must be different(at least order)
|
Must be same (including order)
|
Method signature
|
Must be different
|
Must be same
|
Return type
|
No restrictions
|
Must be same until 1.4 but from 1.5v onwards co-varient return types
are allowed.
|
Private, static final methods
|
Canbe overloaded
|
Can nt be overriding
|
Access modifiers
|
No restrictions
|
We cant decrease the scope.
|
Throws clause
|
No restructions
|
Size & level of checked exceptions we can decrease. But no
restrictions for unchecked exceptions.
|
Method resolutions
|
Alwas take care by compiler based on reference type
|
Alwas takecare by JVM based on runtime object.
|
Also known as
|
Compile time poly, static poly, or early binding
|
Runtime poly or dynamic poly or late binding
|
Pholymorphism(flexibility)
We can use same name to represent multiple forms in
polymorphism.
Poly-many , morphs-forms
-two types of polymorphisms 1,Compile time
poly(overloading,method Hiding) 2. Run time poly(overring)
-Innheritance(Reusability),encapsulation(Security),polymorphism(flexibility).
Constructor:
The Constructor is to perform the initialization for the
newly created object.
Serialization:
-The process of writing state of an object to a file is
called serialization.
DeSerialization:
-The Process of reading state of an object from a file is
called de-serialization.
Enum:java.lang Class. It is base class for all java enums
enum: key word. Used to define a group of named constants.
Enumeration: Interface in java.util .used to retrieving objects
from collection one by one.
Type-Casting:
Parent class reference can be used to hold child class
object.
Interface
|
Abstract class
|
If we don’t know anything about implementation just we have requirement
specification. Then we should go for interface.
|
If we are talking about implementation but not completely (partially
implemented) the we should go for abstract class
|
Every method declared inside interfase is by default public
&abstract
|
Every method present inside abstract class need not be public &
abstract. We can take concreate methods also.
|
The following modifiers are not allowed for interface methods… strctfp, protected, static, native,
private, final, synchronized.
|
We can use any modifier
|
Variable inside interface is public , static final , by default
wheather we are declare (or) not.
|
Abstract class variable need not be public , final , static.
|
For the interface variables we declare the following modifiers
private, protected , transient, volatile
|
No restrictions for abstract class variable modifiers.
|
For the interface variable compulsory we should perform
initialization at the time of declaration only.
|
For the abstract class variables there is no restriction like
performing initialization at the time of declaration.
|
Inside interface we cant take instance, static blocks.
|
Inside abstract class we can take static block and instance blocks.
|
Inside interface we cant take constructor
|
Inside abstract class we can take constructor.
|
Collections:
Array
An array is an indexed collection of fixed no of homogeneous
data elements
Collection
A group of individual objects as a Single entity is called
“Collection”.
Collection FrameWork:
It defines several Classes & Interfaces, Which can be
used to represent a group of objects as a Single Entity.
1)Collection(
Interface)
If we want to represent a group of individual objects as a
Single entity then we should go for collection.
2) List (i)
If we want to represent a group of individual objects where
insertion order is preserves & duplicates are allowed . then we should go
for list.
3) Set (i)
If we want to represent a group of individual objects where
duplicates are not allowed & insertion order is not preserved. Then we
should go for “Set”.
4) SortedSet (i)
It is child interface of Set. If we want to represent a
group of individual objects, according to some sorted order. Then we should go
for SortedSet.
5) NavigableSet (i)
It is child interface of SortedSet, to provide several
methods for Navigation Purposes. 1.6
6)Queue (i)
If we want to represent a group of individual objects, prior
to processing then we should go for Queue.
7) Map (i)
If we want to represent a group of objects as key-value
pairs then we should go for Map. Duplicate keys are not allowed but values can
be duplicated. Each key-value pair is called entry.
8) SortedMap (i)
If we want to represent a group of objects as Key-value
paires , according to some sorted Order(based on key but not on value) . Then
we should go for SortedMap.
9) Navigable Map (i)
It is the child interface of SortedMap & define several
methods for Navigation purposes.
ArrayList
|
Vector
|
No method is synchronized
|
Every method is Syncronized
|
Multiple threads can access ArrayList simultaneously, hence ArrayList objects are not thread safe.
|
At any point only one thread is allowed to operate on vector Object
at a time. Hence vector Object is Thread Safe.
|
Threads are not required to wait & hence Performance is high.
|
It increases waiting time of Threads & Hence performance is Low
|
Introduced in 1.2 version & hence not –Legacy
|
Introduced in 1.0 version hence It is Legacy.
|
CURSORs
Enumaretion(1.0)
|
Iterator(1.2)
|
ListIterator(1.2)
|
It is a CURSOR to retrive Objects one by one from the collection.
|
|
|
It is legacy
|
No
|
No
|
It is applicable only for legacy classes
|
For any collection objects
|
Only for list objects
|
Movement single direction(only forward)
|
Single direction (forward )
|
Bi-directional (forward and backward)
|
How to get it?
By using elements()
Method
|
By using iterator()
Method
|
By using listIterator()
|
Accessibility
Only read
|
Read and remove
|
Read , remove, replace, add
|
Methods
B hasMoreElements()
O nextElement()
|
B hasNext(), O next(),
V remove()
|
3+, I nextIndex(),
B hasPrevious(),
O previous(),
int previousIndex(),
v set(o),
v add(o)
|
Comparable
|
Comparator
|
We can use comparable to define default
natural sorting order
|
We can use comparator to define
customized sorting order
|
This interface is in package: java.lang
|
Tis interface is in package : java.util
|
Methods:
compareTo()
|
compare(),
equal()
|
All Wrapper classes & String class
implements comparable interface.
|
No predefined class implements comparator
interface.
|
HashSet (c)
|
LinkedHashSet (c)
|
TreeSet (c)
|
Underlying DataStructure
HashTable
|
HashTable+LinkedList
|
BalancedTree
|
Insersion order
Not preserved
|
Preserved
|
Not preserved
|
Sorted Order
N.A
|
N.A
|
Preserved
|
Heterojgeneous objects
Allowed
|
Allowed
|
Not allowed
|
Duplicate objects
Not allowed
|
Not allowed
|
Not allowed
|
Null acceptance
Allowed (1)
|
Allowed (1)
|
For the empty TreeSet add the first element null insertion is
possible in all other cases we will get NPE.
|
HashMap
|
HashTable
|
No method is synchronized
|
Every method is synchronized
|
Multiple threads can operates simultaneously and hence HashMap object
is not Thread Safe.
|
Every Time only one Thread is allowed to operate on HashTable Object.
Hence it is Thread Safe.
|
Threads are not required to wait & hence relatively performance
is High.
|
It increases waiting time of the Thread & Hence performance is
low
|
Null is allowed for both key and value
|
Null is not allowed for both key and values. Otherwise we will get
NullPointrException
|
Introduced in 1.2 & it is not legacy.
|
Introduced in 1.0 version & it is legacy
|
HashMap
|
LinkedHashMap
|
The Underlying Data Structure is HashTable
|
The Underlying D.S is
HashTable+LinkedList
|
Insertion Order is not Preserved
|
Insertion order is preserved
|
Introduced in 1.2 version
|
Introduced in 1.4 version
|
Generic Classes
In 1.5v a Generici’s introduced. To getting type-safety of
an Object. Based On our runtime Requirement Type Parameter ‘T” will be replaced
with Corresponding provided type.
<String> <String,Integer> <T extends Runnable
& Comparable> , we cant extend morethan one class at a time<T
exctendsNumber&Thread>
MultiThreading Interview Qustions
Thread
Thread is an independent path of execution.
Process-Based Multi-Tasking:
Executing several tasks simultaneously, where each task is a
separate independent process, is called process based multitasking.
Thread-Based Multi-Tasking:
Executing several tasks simultaneously where each task is a
separate independent part of the same program is called “Thread-Based
Multi-Tasking”. And each independent part is called “Thread “.
Thread Creation:
Two ways to define Instanciate & start a new Thread
i)extending Thread Class. Ii)implementing Runnable Interface.
Thread Priority:
Every Thread in java has some priority but the range of
Thread priorities is “1 to 10”. (Thread.MIN_PRIORITYà1 ) MAX,NORM default is
5.
The methods to
prevent Thread Execution:
1)
yield() 2) join() 3) sleep()
1)
yield(): yield() method causes , to pause
current executing Thread for giving the chance to remaining waiting threads of
same priority.
public
static native void yield()
2)
Join(): If a thread wants to wait until completing some other Thread then we should
go for join() method.
If Thread t1 Executes t2.join()
then t1 Thread will entered into waiting state until t2 will continue its
execution.
join()throws
InterruptedException. join(long ms, int ns)t IE, join(long ms)t IE.
3)
Sleep(): If a Thread don’t want to perform any
operation for a particular amount of time (Just pausing) Then we should go for
sleep().
Sleep(long
ms)throws InterruptedException. Sleep(long ms,int ns)t IE
inturupt()-A thread can Interrupt another sleeping or
waiting Thread by using this method.
Exception:
When unwanted , unexpected that
disturbs normal flow of program is called exception.
-Throwable class acts as a root for
entire java Exception hierarchy. Child classes 1) Exception, 2) Error.
1)Exceptions: these are caused by
our program and these are recoverable.
2)Errors: most of the cases Errors
are not caused by our program ,there are due to lack of system resources,
errors are not recoverable.
Checked Exceptions: The Exceptions
which are checked by compiler for smooth exegution of the program at runtime
are called Checked Exceptions.
Un-Checked Exceptions: The
Exceptions which are not checked by the compiler are called “un-checked
exceptions.
Try- to maintain Risky code
Catch- to maintain Handling code
Finally- to maintain clean-up code
Throw- To hand-over our created Exception object to the JVM
manually
Throws-to delegate the responsibility
No comments :
Post a Comment