Interview Questions
Interview Questions
nterview Questions:
1.project architecture?
2.spring flow?
3.list out all the annotations in spring?
4.what are all the annotations you used in your project on restful services?
5.in hibernate use of cascade and inverse?
6.first level cache? second level cache in hibernate?
7.waht is diff b/w arraylist and linkedlist?
8.can you explain the internal flow of hashmap?
9.what is the diff b/w hashmap and hashtable?
10.diff b/w array and arraylist?
11.diff b/w arraylist and vector?
12.in your project where you used cuncurrent hashmap?
13.waht is java annoying?
14.diff b/w callable interface adn future interface in concurrent package?
15.class loaders?
16.how can you take list into map?
17.how can you take map into list?
18.when you will get ClassNotFoundException and NoClassDefFoundError?
19.how you implement exception handling in your project?
20.where you implement multi-threading in your project?
21.what are all the design patterens you oberved in spring?
22.which design patters you used in your project?
23.what are all the critial situations you come across in your project?
24.why wait() placed in object class ? why not it is placed in Thread class?
25.waht is use of intern() in spring?
26.what is diff b/w String str="xyz"; and String str2= new String("xyz");
27.expalin about java architectue?
28.explain about jvm architecture?
29.data base queries?
30.i have a compeny table in remote database. by using rest i need to get the table data and print into a
file?
31.how to read book pages on online library by using bookid or author id(by using restful services)?
32.i have a table in remote database, how to update the data in that table using rest?
33.diff b/w rest and web(soap)?
34.agile methodolgy?
35.how to create web-services project and spring project using mavan?
36.what is diff b/w throw and throws?
37.can you tell me java8 features?
38.what are all the contents in wsdl?
39.refer regular expressions?
40.can i add elements to list , if it is defined as final
ex:final List<String> list= new ArrayList<>();?
41.if you pass duplicate key to map what will happen?
42.diff b/w abstract class and interface?
43.diff b/w comparator and comparable?
44.how to compare two database tables(clue: comparator, compare(), you have to compare database
objects.)?
45.how to set timeout for the browser?(clue: restful client api.)?
46.what workflow you used in your project?
47.why java? why not c & c++?
48.In written test they are asking sorting programs(bubblesort,quicksort,...)?
49.what is time complexity? if you are going to implement sorting by your own which sorting you prefer?
and why?
50.what is the use of volatile and synchronized?
51.what is serialization? have you implement serialization in your project?
52.programs on io streams?
53.why we are using @qualifier?
54.diff b/w BeanFactory and ApplicationContext?
55.explain about ioc container?
56.programs on string manipulations?(they are expecting solv by using regular expressions).
57.how you are implemented polymorphism in your project?
58.how you iterate map having key and list<values>?
59.diff b/w Iterator and ListIterator and Enumarator?
60.what are all the collections are supporting ListIterator?
61.how to make non-synchronized map and list as synchronized(by using collection method)?
62.what is diff b/w collection and collections?
63.write the junit test case for the below senario..
-->read array of elements into list<>.
64.what are all the modifiers we can use inside method?(ans: only final)
65.what is diff b/w spring-jdbc and hibernate?
66.what are all the drawbacks of jdbc over hibernate?
67.what are all the problems with inheritance?
68.what is the use of hinernate session?
69.they given one query in sql and they are asking corresponding criteria api query?
70.why we are using @transient in hibernate?
71.what are all the inputs we are giving to SessionFactory?
72.what we are writing in hibernate-mapping file?
73.what we are writing in hibernate-configuration file?
74.senario: in jsp page with 2 buttons, one for addbook and another is for showListOfBooks(by using spring
and hibernate)?
75.what is use of @ComponentScan?
76.what is use of dispather servlet?
77.what are all the pre-processings tasks done by DispatcherServlet?
78.how to render excel and pdf view to the enduser(using poi and itext api's)?
79.how to validate valid username and password in spring?for validating can i directly interact with dao
without service?
80.by defalut servlet container will handle multi-threaded applications , then why you are implementing
multi-threading in your application?
Here the main thing is that to run that piece of code of run(),
so we can re write the code with it in lambda expression.
Generally a function have 4 things
1.Nmae
2.parameter
3.Body
4.Return ype
Among them parameter and body are most important, so as
lambda expressions are anonymous function it has only body
and parameter ,
So let us see the above code with lambda expression.
2.
class Test {
public static void main(String[] args) {
Thread th=new Thread()->System.out.println("Heloo child thread"); // left side of the symbol " -> "
parameter
//and right side is body
th.start();
System.out.println("Heloo main thread");
}
}
3. How lambda fits with java.
Java provides backward compatibility and making the lambda to use with
old codes and also new codes, that means you can happily use lambdas with
old API and also in the new API. As you can see the above threading model
which was an old code but fits with lambda,
Lambdas are backed by Intreface having single abstract method, which is knowas
funcional interface, e.g: Runnable, Callable..
we can use lambda in place of anonymous classes,
So to use lambda extremely devlopers making the api with interface having single abstract method.
4. Why Lambda ?
Here i have highlighted the only onemain point,
When ever we are programming with anonymous inner classes , so it creates one extra anonymous .class
file so for every anonymous class one extra . class file created , so javm loads time to load those classes
and
also create more garbage, so that garbage collector also running on background, which leads to
performance issue,
But in lamda expression no .class file is created like anonymous inner class, so this is simple and
performance is more.
5.Simplicity of Lambda:
Let us take a simple iteration
import java.util.*;
import java.util.function.Consumer;
public class Sample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
//external iterators
// for(int i = 0; i < numbers.size(); i++) {
// System.out.println(numbers.get(i));
// }
// This is the traditonal way to iterate, there are so burden
// to initialize the starting value and setup the boundery conditiion etc..
//external iterators also
// for(int e : numbers) {
// System.out.println(e);
// }
// To reduce the burden, in java 5 for each loop is there ,but still ugly for
// is there .
//internal iterators
// numbers.forEach(new Consumer<Integer>() {
// public void accept(Integer value) {
// System.out.println(value);
// }
// });
// So in java 8 Consumer interface is there in java.lang.function pkg,
// and applying the anonymous class concept we can iterate as shown in above.
// numbers.forEach((Integer value) -> System.out.println(value)); /
//numbers.forEach((value) -> System.out.println(value));
// As we can implement lambdas for anonymous inner classes
// So the implemention is as above.
//Java 8 has type inference, finally, hold your tweets, but only for
//lambda expressions.
//numbers.forEach(value -> System.out.println(value));
//parenthesis is optional, but only for one parameter lambdas.
numbers.forEach(System.out::println); // using method reference
}
}
So 4 to 5 lines of code can be replaced with one simple line i.e
numbers.forEach(System.out::println);
So these are only basics of lambda, but real fun is there with Stream.
with lambda and stream the collection programming become easier.
various methods are there in Stream interface of java.util.stream pkg
which makes the collection claases easier to manipulate data.
6. Method reference:
This is used only , when we receive a parameter and we do not alter
it any way,but pass through it at that time.
Parameter as argument:
-----------------------------------------
numbers.forEach(e->System.out.println(e));
here parameter e is used as argument in System.out.println()
So the above can be written as
numbers.forEach(System.out::println);
Parameter as target
---------------------------------
numbers.stream()
.map(e->toString()) //(maping in to an expression)
.forEach(System::out.println);
we can not use method reference if any manupulation on data.
7.Function composition
import java.util.*;
import java.util.function.Consumer;
public class Sample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
//given the values, double the even numbers and total.
int result = 0;
for(int e : numbers) {
if(e % 2 == 0) {
result += e * 2;
}
}
System.out.println(result);
System.out.println(
numbers.stream() // this is called function composition
.filter(e -> e % 2 == 0)
.mapToInt(e -> e * 2)
.sum());
}
}
Stream() produce a stream of given collection.
filter method filters the in put ,map method maps to an expression,
How can we create a thread without extends from Thread or without implements from Runnable or Callable
interface ?
Solution :-
Create an anonymous thread like below
class AnonymousThread
{
public static void main(String[] args)
{
new Thread(){
public void run()
{
for (int i=0;i <= 20;i++)
{
System.out.println(i);
}
}
}.start();
for (int i=65;i <= 80;i++)
{
System.out.println((char)i);
}
}
}
Sometimes interviewer ask to create own immutable class without using final keyword?
Solution:-
=======
class MyImmutable{
private int i;
public MyImmutable(int i)
{
this.i = i;
}
public MyImmutable modify(int i){
if(this.i==i){
return this;
}else{
return new MyImmutable(i);
}
}
public static void main(String[] args){
MyImmutable m1=new MyImmutable(10);
MyImmutable m2=m1.modify(20);
MyImmutable m3=m1.modify(10);
System.out.println(m1.hashCode()+" "+m2.hashCode()+" :"+(m1==m2));
System.out.println(m1.hashCode()+" "+m3.hashCode()+" :"+(m1==m3));
}
}
May 8
Today's CTS interview question
Technical Round:-
=============
*Core-java
************
1 .Tell me the internal flow of Set implementation class with one example
2 . In HashMap if hashing collision occure then how to resolve it.
3 . can we add duplicate in set and map if yes why write one code
4 . Read data from file find the duplicate word and count them and sort them in desending order
5 . where to use Comparable and where to use Comparator did you ever used in ur project
6 . what is bubble sort can you write one programme. .?
7 . can I write try block single means without using try-catch or try-finally
8 . what is Executor framework
9 . how many way we can create thread and which one best approach and why
10 . jdk version u r using in ur project and why (be care on that question coz they indirectly ask u the
advantages of version or latest features added in New version )
*Jdbc:-
**********
1 . difference between Statement and PreparedStatement
2.they give one db schema and ask me to retrieve data from DB by passing id
*Jsp:-
*********
1.Life cycle
2. list down the implicitly object
3.what is the use of c tag library
*Spring :-
**********
1.What is RowMapper when we have to use it write sample code not completely just give sm hints with
flow
2.what is ResultSet Extractor where exactly we have to use
3.if in my Spring bean configuration file I configure same bean with same id 2 times then what is the
problem and how to resolve it (contender)
4 . Spring Mvc Flow as per your Project
5.Spring transaction, why nd how to work on it
6. How u handle Exception in ur Project just give some brief idea on it with annotation
*Webservices :-
**************
1 . WSDL ,what are the elements and just explain the role of each section verbally
2.what is Rest,
3 . difference between Soap and Rest
4.Write one Resource method using Http method Post
5.which Response u provide to presentation layer and how to bind Json Response
6.Difference between @QuerryParm and @PathPatm which one best and where to use. ..
HR Round:-
=======
1.Tell me about u. .not professional details just tell me personal details
2.tell me about ur payroll company
3.Expected CTC
4.Why that much any reason
5 . Notice Period, is it negotiable?
5.Puzzle --- i have 8lt ,5lt nd 3lt jar ...but i have all total 8 lt water then tell me how to manage 4lt nd 4lt in
first 2 jar ..u can use 3lt jar for measure and balancing
6.i have 9 ball and 1 ball is more weight and i have one measurement device/Weight balancer machine
how can u find out heavy weight ball tell me within 2 step
1)In arraylist we can add duplicate elements rite..now if i want to remove the duplicate elements how can
we remove
2)In string there are repeated characters there if i want to know how many times its repated how can we
get it..condtion is we shouldnt use any loops
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedHashSet;
public class RemoveDuplicates {
// Creating LinkedHashSet
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
1. Diff between spring singleton bean and user defined singleton class.
2. Diff between application context and webcontext.
3. Flow of spring mvc, what is the use of @requestmapping
4. How hibernate interact with application.
5. Diff between enumeration and iteration.
6. Why u use spring transaction?
7. If we create a static method m1, and nonstatic method m2 both are synchronized, in main
method we have 4 thread all are calling m1 and m2, is there any chance one method deadlock the
other. if not then why.
8. diff between error and exception. if checked exception is recoverable exception then how we can
recover it?
9. flow of spring mvc.
2. Tech Mahindra interview question
1. How to create a singleton class ?
2. How to create our own custom Exception ?
3. How to create our own immutable class.?
4. What is autoboxing and unboxing?
5. Why we use generic in collection?
6. What is the difference b/w HashMap and HashSet?
7. Which set insert element in insertion order.?
8. What are the implict object in jsp?
9. In which jsp class all implict objects are available?
10. In ur project which spring annotation are u used?
11. Why hibernate u used in ur project.?
12. Explain ur role in ur project ?
2. You could repeat <class> tag as many times as you want in hibernate mapping files .
e.g.
<hibernate-mapping>
<class name="Employee" table="employee">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="name"/>
</property>
</class>
Write a program to reverse a string by word without using any predefined method.
b) WAP to find mirror of a given matrix. (Explanation: you have to input a square matrix and find its mirror
matrix i.e. left mirror and right mirror.)
c) WAP to find occurrence of particular letter in a string and also find how much time it is occurrence.
d) What is inheritance? Take a real world scenario and explain. Also why it is important in programming
world?
e) Tell me what you understand by polymorphism and abstraction?
f) What is this keyword and what is its use in java? What is the problem which we face when we do not use
this in our program?
g) What is checked and unchecked exception? How will you create your own exception?
h) Why we are using finally block? Tell me a use case of this.
i) Why strings are immutable?
j) What is difference between list and set? Tell me some classes which are implementing these interfaces.
k) What is servlet? What is difference between GenericServlet and HttpServlet? If GenericServlet is there
then what is use of HttpServlet?
l) What is join? Tell me its type.
m) What is normalization?
n) They give me a table and said that find second highest salary of employ from the table.
o) Explain your project. (some cross questions were asked while I was explaining the project)
(Some more questions are from database but I forget…For technical interview follow HARI sir book and
read each line and do all exercise…and never miss the class. This would be sufficient for easily cracking
the interview)
Third and last round:
---------------------------
Followings are questions:
a) Tell me about yourself.
b) What is your goal after 5 years?
c) What is difference between hard work and smart work?
d) Why should I hire you?
how do u ref an element inside ur css by using byname and by type hoe do u do that wt u use
i have a table it have some coloumns i want to swap contents of these two coloumns how will i do that
if i am not intersted to writeing the java program i want to do with query how can i?
JSE
- What do you mean by polymorphism in java? Where did you use it in your experience?
- Can you explain me about Java OOPS?
- Why is abstraction in Java?
- What is encapsulated object?
- Why is java bean used for?
- When do you use an array?
- How many JVMs can we start in a single system?
- What are the new features in Java 7 and 8?
- How do you serialize an object and why?
- Why is the use of garbage collection in java? How does it work? Can we call it explicitly?
- What is the default size of a JVM? Can we increase the size of it? How?
- How do you handle exceptions? Can you explain me the flow of try-catch-finally? Where do you
commit/rollback the transactions?
- How many types of exceptions, are there and did you create any custom exceptions in your project?
- Have you used multi-threading in your project?
- How do you resolve ConcurrentModificationException? Why does it occur?
- When do you use ArrayList, LinkedList collection?
- When do we go for Map collections?
- When do you use Properties collection?
- How many ways, can we create a thread? When for what?
- Can we synchronize a block at object-level?
- Do you have any idea about String Constant Pool? Why is it?
- Why do we use for “forEach” loop?
- What is a singleton class? Why do we create it? Did you create any singleton classes in your project?
What components, did you make singletons?
JEE
- Can you explain the servlet request flow?
- Is servlet a singleton object? If yes, why is it singleton? Can we make it prototype? If yes, how can we
make?
- Can you explain the request flow for the JSP page in server? Where do you place JSP pages in your
project?
- Can we forward a request to a JSP page if it exists in WEB-INF?
- Can we develop web application without web.xml? If yes, how can we?
- What annotations, do you aware of java? What is for what purpose?
- If we want to do some common work for all the components in the application. Where can we do that
and how?
- If we want to load fixed data before first request into the JVM. Where can we write that logic so that it is
loaded before any request to the application?
- Can we replace servlet with JSP? What are the advantages and disadvantages?
- Can we replace JSP with servlet? What are the advantages and disadvantages?
- Is JSP a singleton object in JVM?
- What is the purpose of session object?
- Why application server? Why not webserver?
Spring
- Why IOC? How does it work?
- How can we make bean as singleton and prototype?
- How many scopes, are there in spring? What for what purpose?
- Why did you use spring?
- How do you make class a bean in IOC using annotations?
- What MVC annotations, are you aware of? What for what purpose?
- When do you use @Autowired?
- Did you use AOP? If yes, why is it used for?
- Did you use Spring Jdbc? If yes, why not Java Jdbc and Hibernate?
- What is the use of RowMapper in Spring Jdbc?
- How to use Spring with Hibernate?
- When to go for setter injection over constructor injection?
- How do solve constructor injection confusion?
- What is an aspect in AOP?
- How many advices, are there in Spring AOP? What are those?
- How do you specify a name to a bean in IOC using annotations?
- Did you use java configuration class? If yes, why did you use it? What annotations, did you use on java
configuration class?
- Can we have multiple java configuration classes? If yes, how can we achieve? When should we do that?
- What is the purpose of DispatcherServlet? How can you provide configuration xml file to it?
- Can you develop a spring application by only using annotations including web.xml?
- Did you work on transactions in your project? Why did you use? Where did you use?
- How many transactions, are created for a request? Why?
- Why spring annotations? Why not xml?
- Can you explain your project request flow?
Hibernate
- What does load() method do? What does get() method do?
- Can you make get() method as lazy loader? If yes, how?
- How do mention an attribute as a primary key or composite primary key?
- Is caching available in Hibernate? How many caching levels available in Hibernate? What for what
purpose?
- Did you work on HQL? If yes, why did you use? How do you execute HQL?
- Do you have idea on Criteria in Hibernate? If yes, what is it? Why is it used over HQL?
- What annotations, did you use in your project? What for what purpose?
- Can you write one-to-many relationship xml configuration with an example?
- Do you remember normalizations? Can you explain 2nd normalization?
- Why do we normalize data?
- Did you use Increment ID Generator? How does it work?
- Why not Sequence ID Generator?
- Why Hibernate? Why not Java Jdbc?
- What is the parent exception in Hibernate? What type of exception is it and why?
- How many session objects, do you create for a request?
- Can you tell me the differences between save() and saveOrUpdate()? How does saveOrUpdate() work?
- Can you tell me the differences between update() and merge()?
- Can you write a snippet of code for creating SessionFactory? Where and when do you create
SessionFactory? How many SesionFactory objects, do you have in your project? Why?
Web Services (SOAP)
- How many sections, are there in a SOAP xml? Can you explain their purposes?
- Why SOAP? Why not REST?
- What is the differences between JAXRPC and JAXWS?
- What implementation, did you use in your project? Why?
- How do you secure a Web Service?
- What do you mean by a Web Service?
- Can we send JSON or HTML over SOAP Web Services? If no, why can’t we?
- Assume, there are two systems A and B. A is consuming B’s service. One day, we got to change the
signature of the method in Web Service class by adding new parameter. What changes, might be occurred
in A’s and B’s systems?
- Are you a consumer or producer of Web Service? If both, explain?
- How many Web Services, are you aware of? Explain their advantages/disadvantages?
- How do you share your Web Service to your consumers?
- Can you explain me the use of WSDL in a Web Service? What sections, does it contain and their
purposes?
- How do you make class a Web Service in JAXWS?
- What annotations, are you aware of in JAXWS? What is for what purpose?
XML Technologies
- What is purpose of XML? When do we use XML? What is well-formed XML? How do you validate an
XML?
- What is an XSD?
- How do you convert XML to an object?
- What is SAX/DOM mechanism? Which is good in performance?
RESTful Services
- Why REST? Why not SOAP?
- Can we send HTML code over REST? How?
- What annotations, are you aware of in REST? What is for what purpose?
- What do you mean by resource in REST? How do you make a resource in REST?
- How many places, do you get data from http request?
- What is the purpose of @PathParam?
- What is the purpose of @MatrixParam?
- How do you handle exceptions in REST?
- What are http response codes and their purposes?
- How do share your REST Service info to consumers?
- How do you consume RESTful Service? Can you write a snippet of code?
- Can you explain me GET, POST, PUT methods of HTTP?
- What is a sub-resource?
1. By make our class as immutable what we achieve. In your project where u use.
7. If we create a static method m1, and nonstatic method m2 both are synchronised, in main method we
have 4 thread all are calling m1 and m2, is there any chance of interruption, if not then why
8. diff between error and exception. if checked exception is recoverable exception then how we can recover
it?
9. [10,5,19,11,20,8,13]. Find the second smallest and second highest from the array.
10. If we have same url in two controller. For example @RequestMapping(value="/add") then what wil
Program:-
What is static pointcut and dynamic pointcut ..what is diff between static and dynamic pointcut?
Static point cut: