Interview Questions and Answers - Order By Newest Q71. Write code for singleton class Design
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=singleton+class&category=code Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  singleton  code  coding  design pattern Asked in 1 Companies Intermediate   frequent Q72. Write code for the usage of Builder Design Pattern
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?&category=code&searchOption&keyword=964 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  builder design pattern  builder pattern  code  coding intermediate Q73. Can we have try statement without catch? If try statement contains return will the finally block be executed? What happens if there is an exception inside finally block? Core Java
Ans. Yes, with finally.
Yes, finally block will be executed even if there is no exception in try block.
If finally throws an exception, the exception gets thrown to the calling module. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exception handling Asked in 1 Companies intermediate Q74. Should a DB Insert Request be a POST or PUT request in Rest Rest
Ans. It depends on whether the request object is persisted as it is
or
its first dismantled, modified or refactored and then inserted into DB.
In first case, it should be a PUT request whereas in second case it should be a POST Request. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Put vs Post   Rest Asked in 2 Companies intermediate   frequent Q75. How to Bulk upload the data into Oracle database? Database
Ans. We can use external table feature of Oracle. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Intermediate Frequently asked Design Pattern interview question. Q76. What is a prototype design pattern ? Design
Ans. The prototype pattern is a creational design pattern. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. Prototype is used when we need duplicate copies of objects. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  design pattern  prototype design pattern  cloning Asked in 11 Companies intermediate Usually asked with Questions related to Generics. Q77. What are Type Erasures in Java ? Core Java
Ans. Type erasure applies to the use of generics. When generics are used, they're pre compiled into compile time checks and execution-time casts. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  type erasures  generics Asked in 2 Companies intermediate   rare Q78. How can one determine if JVM is 32-bit or 64-bit from Java Program ? Core Java
Ans. There is a Java system property "sun.arch.data.model" that can tell if JVM is 32 bit of 64 bit
System.getProperty("sun.arch.data.model") can be used to get that property. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  jvm intermediate   rare Q79. Write an Algorithm for Graph Traversal ? The Graph has a loop. Algorithm
Ans. Please not that all such questions can be easily answered through recursion.
Simple recursive implementation could be
traverse(root);
void traverse(Element element){
if(element.hasNext()){
traverse(element.next());
} else {
System.out.println(element);
}
}
but this algo / code lead to endless loop if there is a loop in graph traversal.
So you can keep a collection to keep track of which elements have laready been traversed
static List<Elements> listOfAlreadyTraversedElements = new ArrayList<Elements>();
main(){
traverse(root);
}
void traverse(Element element){
if(element.hasNext()){
traverse(element.next());
} else {
listOfAlreadyTraversedElements.add(element);
System.out.println(element);
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  graph traversal algorithm  graph traversal algorithm using recursion Asked in 1 Companies intermediate Frequently asked in Alibaba (Based on 2 feedback) Q80. What is the best Memory setting for JVM ? Core Java
Ans. In Java JVM memory settings is done by use the arguments -Xms -Xmx. Use M or G after the numbers for indicating Megs and Gigs of bytes respectively. -Xms indicates the minimum and -Xmx the maximum. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  jvm  memory management  jvm best memory setting Asked in 1 Companies intermediate Frequently asked in high end product companies. Ans. https://www.geeksforgeeks.org/lru-cache-implementation/ Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  cache  LRU cache  coding  code Asked in 10 Companies intermediate Q82. Will finally run if we have return statement and it exits the method early ? Core Java
Ans. finally will execute in all graceful situations - graceful executions as well as graceful exceptions. The only situation when finally block won't execute is when the app is abruptly stopped, killed or unplugged. Sample Code for finally Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  finally Intermediate Ans. Interface that is declared inside the interface or class, is known as nested interface. It is static by default. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  interface  nested interface intermediate Try 1 Question(s) Test Q84. What kind of thread is garbage collection thread ? Core Java
Ans. Daemon thread Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  garbage collection intermediate Try 2 Question(s) Test Q85. What is the difference between the Reader/Writer java.io hierarchy and the Stream class hierarchy? Core Java
Ans. The Reader/Writer hierarchy is character oriented, whereas the Stream class hierarchy is byte oriented. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java io  streams intermediate Ans. It's an object that reads from one stream and writes to another. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java io  io filter intermediate Q87. Can we convert a numeric IP address to a web host name ? Java EE
Ans. Yes, using InetAddress.getByName("<IP Address>").getHostName(); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  ip address to hostname  INETAddress intermediate Q88. how to access the methods of an inner class? Core Java
Ans. It depends on the type of inner class
To access non static inner class method
new OuterClass().new InnerClass().getMethod();
To access static method of static inner class
new OuterClass.InnerClass().getMethod(); Sample Code for inner class Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  nested aclasses Asked in 1 Companies Intermediate   frequent Q89. What is memory leak ? How Java helps countering memory leaks compared to C++ ? Core Java
Ans. Memory Leak is a situation wherein we have a reserved memory location but the program has lost its reference and hence has no way to access it.
Java doesn't have concept of Pointers, Moreover Java has concept of Garbage collection that frees up space in heap that has lost its references. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  memory leak  garbage collection intermediate   frequent Q90. What are the benefits of creating immutable objects ? Core Java
Ans. 1. Security and Safety - They can be shared across multiple threads as they are thread safe. Moreover, it protects then from bad state due to interception by the other code segment. One such problem due to mutability and access by alternate code segment could be the change of hash code and then the impact on its search with hash collections.
2. Reuse - In some cases they can be reused as only one copy would exist and hence it can be relied upon. For example - String Pool Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  immutable  immutability objects Asked in 1 Companies Intermediate   frequent Q91. Can you name some of the Java frameworks in different domains ? Frameworks
Ans. Web Framework - Spring , Struts, Play
Dependency Injection frameworks - Google Guice , PicoContainer and Dagger
ORM Framework - Hibernate
Big Data / ETL Frameworks - Apche Hadoop , Apache Crunch, Apache Spark.
View Frameworks - JSF , Apache Wicket,jtwig.
Java Gui Frameworks - SWT , AWT, JavaFX
Testing - Junit, Mockito, PowerMock, EasyMock, JMock, JMockit
Rest Web services - Jersey , Restlet , RestX, RestEasy ,Restfulie
Here is the big list for reference - http://javasearch.buggybread.com/home2.php?keyword= Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  frameworks Asked in 2 Companies intermediate   frequent Q92. What is the difference between
File f = new File("homexyz.txt");
and
File f = File.createTempFile("xyz",".txt","home"); ? Core Java
Ans. First will not create physical file in storage whereas the second will. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  file handling  File.createTempFile  file handling intermediate   rare Q93. What will be the output of following code ?
Base Interface
public interface BaseInterface {
int i = 4;
}
Derived Interfaces
public interface DerivedInterface1 extends BaseInterface{
int i = 5;
}
public interface DerivedInterface2 extends BaseInterface{
int i=6;
}
Implementing Class
public class Class implements DerivedInterface1,DerivedInterface2 {
public static void main(String[] args){
System.out.println(BaseInterface.i);
}
}
What will be the output upon executing main and Why ? Core Java
Ans. It will print 4 because member elements of an interface are implicitly static and hence the concept of overriding doesn't work.
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  interfaces  coding  code  extending interfaces  diamond interfaces Asked in 1 Companies intermediate Q94. What will be the output of following code ?
Base Interface
public interface BaseInterface {
int i = 4;
}
Derived Interfaces
public interface DerivedInterface1 extends BaseInterface{
int i = 5;
}
public interface DerivedInterface2 extends BaseInterface{
int i=6;
}
Implementing Class
public class Class implements DerivedInterface1,DerivedInterface2 {
int i=10;
public static void main(String[] args){
System.out.println(new Class().i);
}
}
What will be the output upon executing main and Why ? Core Java
Ans. 10 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  interfaces  coding  code intermediate Q95. Name few creational design patterns ? Design
Ans. Factory,Abstract Factory,Singleton,Prototype and Builder Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Design Pattrns  Creational Design Patterns Intermediate Q96. Name few structural Design Patterns ? Design
Ans. Adapter,Bridge,Composite,Decorator,Facade,Flyweight,Proxy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Design Patterns  Structural Design Patterns Intermediate Q97. Name few Behavioral Design Patterns ? Design
Ans. Interpreter,Chain of Responsibility,Command,Iterator,Observer,Mediator,Memento Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Behavioral Design Patterns Intermediate Q98. Which is your favorite Design pattern, each in Creational , Structural and Behavioral and Why ? Design
Ans. [Open Ended Answer] Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Design Patterns Intermediate Q99. What is Exception Wrapping ?
Ans. Exception wrapping is wrapping an exception object within another exception object and then throwing the outer exception. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exception handling  exception wrapping  exceptions intermediate   rare Q100. How to create list that will sort it's elements dynamically? Algorithm
Ans. Override the behavior of collection class to sort the list upon each element addition. Though it's not recommended as list are sorting heavy data structures.
List<MyType> list = new ArrayList<MyType>() {
public boolean add(MyType mt) {
super.add(mt);
Collections.sort(list, comparator);
return true;
}
}; Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies intermediate