Professional Documents
Culture Documents
Java Interview Question by Santosh
Java Interview Question by Santosh
1.JDBC
java data base conectivity
it is jav api to connect and execute the query with the data base
2.JDBC API
to connect and acces the data in database
4.Why JDBC
ODBC is platoform dependent and JDBC is paltform independent and secure
Regex/Regular expression
^[A-Za-z][A-Za-z0-9]{8-19}
Collections
2.Clasees in collection
1.ArrayList
2.Vector
3.Stack
4.HashSet
3.Interfaces incollection
1.List
2.Set
3.Queue
8.Waht is set?
java.Util.Set;
it does'nt contains duplicate elements
it can only include inherited methods of Collection Interface
public Interface Set<E> extends Collection<E>
9.Queue
it is a Collection which defines a queue data structure
which stores the element in the form of FIFO
public Interface Queue<E> extends Collection<E>
java.Util.Queue;
10.Dequeue
it is a double needed queue
it allows the insertion and removal of elements from both ends
it can perform both stack and queue operations LIFO & FIFO
public Interface Dequeue<E> extends Collection<E>
java.Util.Dequeue;
11.Map?
it does not implement with any Collection Interface
it has key value pairs to store the elements
unique key and value may be duplicated
it consiste of two interfacse
1.Map Interfcae
2.Sorted Map
Comparator:
it provides multiple sl
java.Lang
the actual class is not modified
22.blocking queue?
it extends Queue interface.it provides concurrency in operations like
retrievel,deletion,insertion and storing
it is thread safe
public Interface Blockingqueue<E> extends Queue<E>
24.hashCode()
it returns only integer value
it returns same integer number,if two keys are identical by calling equal()
how ever,it is possible that two hashCode numbers can have diff other same
keys
40.Iteartor
It is used in Collections frame work to retrieve the elements one by one
STRINGS
1.String?
it is an object that represents sequence of character values.
the array of characters is same as java string
char[] c={'c','a'}
String s=new string("char");
2.interfaces in string?
Serializable
Comparable
CharSequence
13.compareTo()?
it compares the values lexico graphically and returns the integer value
if(s1==s2)
return 0;
if(s1>s2)
return 1;
if(s1<s2)
return -1;
String s1="Ram"
String s2="Ram"
String s3="kumar"
Sop(s1.compareTo(s2));
Sop(s2compareTo(s3));
15.substring() in java?
a part of string is subString
a subString is subset of another string
String s="java program"
s.subString(6);
s.subString(0,3);
19.StringBuffer append()?
StringBuffer sb=new StringBuffer("java");
sop(sb.append("program"));
20.StringBuffer insert()?
it insert the given string with this string at the given position
StringBuffer sb=new StringBuffer("java");
sop(sb.insert(1,"program"));
O/P:jprogramava
21.StringBuffer replace()?
it replace the given string from the specified begin index and end index
StringBuffer sb=new StringBuffer("java");
sop(sb.replace(1,3,"program"));
22.StringBuffer delet()?
it delet string from specified begin index to end index
StringBuffer sb=new StringBuffer("java");
sop(sb.delet(1,3));
O/P:a
23.StringBuffer reverse()?
it is used to reverse the current string
StringBuffer sb=new StringBuffer("java");
sop(sb.reverse);
O/P:avaj
24.StringBuffer capacity()?
the capacity() of StringBuffer the current capacity of buffer
the default capacity of buffer is 16,
if nbr of characters increases from the current capacity,then it will be old
capacity*2+2=32;
25.What is StringBuilder?
it is used to create mutable(modifiable)
it is same as StringBuffer class but the diff is it is not thread safe
unlike stringBuffer,it is not synchronized
26.StringBiulder append()?
StringBuffer sb=new StringBuffer("java");
sop(sb.append("program"));
27.StringBuilder insert()?
StringBuffer sb=new StringBuffer("java");
sop(sb.insert(1,"program"));
28.StringBuilder replace()?
StringBuffer sb=new StringBuffer("java");
sop(sb.replace(1,3,"program"));
29.StringBuilder delet()?
StringBuffer sb=new StringBuffer("java");
sop(sb.delet(1,3));
30.StringBuilder reverse()?
StringBuffer sb=new StringBuffer("java");
sop(sb.reverse);
31.StringBuilder capacity()?
35.toString()?
if we want to represent any object as a String,this method is used
36.adv of toString()?
to override toString() in object class,we can return the values of the
object.so we no need to write much code
37.StringTokenizer()?
this comes under Util package
it allows to break,the strings ointo Tokens
38.methods in StringTokenizer()?
6 methods
1.hasMoreTokens()
this is used when more tokens are allowed
2.nextToken()
it returns next token
3.nextToken(String delimiter)
it returns next token from delimiter
4.hasMoreElements()
5.nextElement()
6.countTokens()
39.charAtr()?
it returns the character value at the given index nbr
the index nbr starts with 0
String s="java";
sop(s.charAt(2));
40.compareTo()
it compares the values lexico graphically and returns the integer value
if(s1==s2)
return 0;
if(s1>s2)
return 1;
if(s1<s2)
return -1;
41.contains()
it search the sequence of characters in the String
it returns true,if sequence of character values found in the string o/w
returns flase
eg:String s="java is earliest program";
sop(s.contains("java is"));
sop(s.contains("java"));
42.endsWith()
if the strings ends with given suffix,it returns true else return false
String str="java program"
sop(s.endsWith(ram));
43.equals()
refer 11TH question
44.equalsIgnoreCase()
string s1="java";
String s2="java";
String str3="jaVa";
sop(s1.equalsIgnoreCase(s2));
45.format()
to format the representation of the string
String s="java";
sop("the name is %12s",s);
46.getBytes()
it return the byte(sequence of bytes) array of the string
string str="A B C D E"
byte[] b=s.getBytes();
for(int i=0;i<b.length();i++){
sop(b[i]);
}
String s2=new String(b);
sop(s2);
47.getChars()
it copy the content of the string in to character array
String s1="hello java program"
Char[] ch=new Char[16];
sop(s1.getChars(6,9,ch,0));
48.indexOf()
it returns indexOf() gien character value and strats with 0;
String s="How are are always";
sop(s.index("ar"));
49.intern()
It is used to return the intern string.
it can e used to return the string from the string constant fool
if the string is created by new keyword,it create the exact copy of heap
string object in string constant pool
string s1=new String("java");
String s2="java";
String s3=s1.intern();
50.isEmpty()
this method check,if the string is empty or not
string s1="";
string s2="java";
sop(s1.isEmpty());
sop(s2.isEmpty());
51.join()
this method returns string joined with a given delimiter
string res=String.join("-","hello","how","are","you");
sop(res);
O/p:hello-how-are-you
52.lastIndex()
it returns the last index of the given character value
string s="how are you"
sop(s.lastIndexOf("ar"));
53.length()
this method returns the length of the string
string s="java";
sop(s,length());
53.replace()
string s="java is easy";
sop(s.replace('a','e');
O/p:jeve is eesy
54.replaceAll()
it returns a string replacing all the sequence of characters matching regex
and replacement string
replace("\\s","");
55.split()
this is used to split the string into character array or array of characters
String s="java is easy";
string[] words=s.split("\\s",0);
for(string s1:words)
{
sop(s1);
}
56.startsWith()
it checks the given is satrts with given prefix
String str="java";
sop(s.startsWith("j"));
57.toCharArray()
java.array.toChararray() convert the String into charArray()
it returns a newly created character array,it is similar to the String
String s="java";
ch[] ch=s.toCharArray();
for(int i=0;i<ch.length90;i++){
sop(ch[i]);
}
by default string is a character array, but in java it is class
58.toLowerCase()
it converts string from upper case to lower case
String s="JAVA";
sop(s.toLowerCase());
59.toUpperCase()
it converts string from lower to upper
60.trim()
the trim method is used to eliminate leading and trailing space
string s="hello java";
sop(s.trim());
61.valueOf()
it convert diff types of value into string
int a=10;
string s=string.valueOf(a);
sop(s+10);
EXCEPTION
1.exceptions?
they are created when an abnormal situations are araised in pgm
they can be created by JVM
it is an object
it is present in java.lang:
in other words,we caqn say exception as run time error
14.Can we catch more than one Exception with single catch block?
we can catch mopre than 1 exception in single catch
this type of handling reducesz code duplication
eg:
15.What are checked Exceptions?
all the sub clases of throwable class except error,RunTimeExceptions and
subclases are called as checked exceptions
they can be thrown with keyword throws/should be providedd by try catch block
if not doing this,then it will be complier error
SQLException
IOException
FileNotFoundException
InstanciationException
16.Unchecked exception
all subclases of runtimeexceptiuons are called unchecked exception
they are unchecked becz the compiler does not check if a method hadles or
throws exception
if any exception occurs the prgm will terminate
it is very difficult to handle this kind of exception
eg:StringINdexOutOfBoundsEcxeption
NullPointerException
ClassCastException
multi trhereading
1.multi threading
exceutiong multiple threads simultaneously to obtain multi tasking
it is to ovbtain the multi tasking
it consumes less memory and gives fast and effiecient performance
adv:
thtread can share same addrrsesss space
thread is light wait
2.thread?
it is a light wait process
it creates separate path of execution bcz each thread is runs in a diff stack
frame
a process may contain multiple threads
a threads can share all process resources but still they exceuted
independently
16.Deamon thread?
this is a low priority thread that probide a background support and services
to user thread
deamon threads are get automatically terminated by the jvm,if the prgm
remains with in the deamon thread
and all user threads are died
19.Synchronization()
control the access of multiple thread,to any shared resources
20.Synchronized block?
synchronization providing block is known as Synchronized block
21.diff b/w notify nad notifyall()
notify:
unblock only one waiting thread
notifyall():
unblock all the waiting thread
22.thread schedular in java?
after creating the thread,it was superwised by thread schedulkatr which is
aprt of jvm,
thread schedular is only res[ponsible for only which thread is executed
it used two mechanism
time slicing
premitive scheduling
24.Threadpool?
it represent the group of worker thread in thread pool which are waiting for
the service provider who will give task
IO package
1.javaio?
io stands for input and output,used to process th ei/p and produce the o/p
3.Output stream
used to write data in file
it is an abstract class
7.BufferReader class
it is used to read the text from character based i/p stream
it can be read data from readLine();
8.BufferWriter class
it is used to provide write instances
it can be efficient for writing arrays,strings
9.File reader?
this class iss used to read data from file
it returns the data in byte format
it shoiuld be comes with i/p stream reader
10.File writer?
file writer should come with o/p file writer
bf used in io package
it can allocate
readLine() and nextLine()
12.Stream?
it is sequence of data and composed pof bytes
Packages
1.package in java?
package is a mechanism that contains group of realted classes,interfaces
etc., into a single module
2.Naming conventions
package name starts with small letters
we cannot have more than one package statement in source file
4.types of packages?
user defined
system
5.User defined?
we can creatye package with import stataemne
package package name:
can use in another class by importing it into
that