This is a new series of sharing core Java interview question and answer on Finance domain and mostly on big Investment bank.Many of these Java interview questions are asked on JP Morgan, Morgan Stanley, Barclays or Goldman Sachs. Banks mostly asked core Java interview questions from multi-threading, collection, serialization, coding and OOPS design principles.  Anybody who is preparing for any Java developer Interview on any Investment bank can be benefited from these set of core Java Interview questions and answers. I have collected these Java questions from my friends and I thought to share with you all.  I hope this will be helpful for both of us. It's also beneficial to practice some programming interview questions because in almost all Java interview, there is at-least 1 or 2 coding questions appear. Please share answers for unanswered Java interview questions and let us know how good these Java interview questions are?
 
These Java interview questions are mix of easy, tough and tricky Java questions e.g. Why multiple inheritance is not supported in Java is one of the tricky question in java. Most questions are asked on Senior and experienced level i.e. 3, 4, 5 or 6 years of Java experience e.g. How HashMap works in Java, which is most popular on experienced Java interviews. By the way recently I was looking at answers and comments made on Java interview questions given in this post and I found some of them quite useful to include into main post to benefit all. By the way apart from blogs and articles, you can also take advantage of some books, which are especially written for clearing any programming interviews and some focused on Java programming, two books which comes in minds are programming interview exposed and Java/J2EE interview companion from fellow blogger Arulkumaran. Former is focused on programming in general and lot of other related topics e.g. data structures, algorithms, database, sql, networking and behavioral questions, while later is completely dedicated to Java J2EE concepts.

Core Java Interview Questions Answers in Finance domain

1. What is immutable object? Can you write immutable object?
 
Immutable classes are Java classes whose objects can not be modified once created. Any modification in Immutable object result in new object. For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent sub class from overriding methods in Java which can compromise Immutability. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.
 
2. Does all property of immutable object needs to be final?
Not necessary as stated above you can achieve same functionality by making member as non final but private and not modifying them except in constructor.
 
3. What is the difference between creating String as new() and literal?
When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.
 

String s = new String("Test");
 
does not  put the object in String pool , we need to call String.intern() method which is used to put  them into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" Java automatically put that into String pool.

 
4. How does substring () inside String works?
Another good Java interview question, I think answer is not sufficient but here it is “Substring creates new object out of source string by taking a portion of original string”. see my postHow SubString works in Java for detailed answer of this Java question.
 
 
5. Which two method you need to implement for key Object in HashMap ?
In order to use any object as Key in HashMap, it must implements equals and hashcode method in Java. Read How HashMap works in Java for detailed explanation on how equals and hashcode method is used to put and get object from HashMap. You can also see my post 5 tips to correctly override equals in Java to learn more about equals.
 
6. Where does these  two method comes in picture during get operation?
This core Java interview question is follow-up of previous Java question and candidate should know that once you mention hashCode, people are most likely ask How its used in HashMap. See How HashMap works in Java for detailed explanation.
 
7. How do you handle error condition while writing stored procedure or accessing stored procedure from java?
This is one of the tough Java interview question and its open for all, my friend didn't know the answer so he didn't mind telling me. my take is that stored procedure should return error code if some operation fails but if stored procedure itself fail than catching SQLExceptionis only choice.
 
8. What is difference between Executor.submit() and Executer.execute() method ?
This Java interview question is from my list of Top 15 Java multi-threading question answers, Its getting popular day by day because of huge demand of Java developer with good concurrency skill. Answer of this Java interview question is that former returns an object of Future which can be used to find result from worker thread)
 
By the way @vinit Saini suggested a very good point related to this core Java interview question
 
There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked exception or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will re-throw this exception, wrapped in an ExecutionException.
 
9. What is the difference between factory and abstract factory pattern?
This Java interview question is from my list of 20 Java design pattern interview questionand its open for all of you to answer.

@Raj suggested

Abstract Factory provides one more level of abstraction. Consider different factories each extended from an Abstract Factory and responsible for creation of different hierarchies of objects based on the type of factory. E.g. AbstractFactory extended by AutomobileFactory, UserFactory,RoleFactory etc. Each individual factory would be responsible for creation of objects in that genre.

You can also refer What is Factory method design pattern in Java to know more details.
 
10. What is Singleton? is it better to make whole method synchronized or only critical section synchronized ?
Singleton in Java is a class with just one instance in whole Java application, for example java.lang.Runtime is a Singleton class. Creating Singleton was tricky prior Java 4 but once Java 5 introduced Enum its very easy. see my article How to create thread-safe Singleton in Java for more details on writing Singleton usingenum and double checked locking which is purpose of this Java interview question.
 
 
11. Can you write critical section code for singleton?
This core Java question is followup of previous question and expecting candidate to write Java singleton using double checked locking. Remember to use volatile variable to make Singleton thread-safe. check 10 Interview questions on Singleton Pattern in Java for more details and questions answers

12. Can you write code for iterating over hashmap in Java 4 and Java 5 ?

Tricky one but he managed to write using while and for loop.

13. When do you override hashcode and equals() ?
Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap. check this for writing equals method correctly 5 tips on equals in Java

14. What will be the problem if you don't override hashcode() method ?
You will not be able to recover your object from hash Map if that is used as key in HashMap. 
See here  How HashMap works in Java for detailed explanation.

15. Is it better to synchronize critical section of getInstance() method or whole getInstance() method ?
Answer is critical section because if we lock whole method than every time some one call this method will have to wait even though we are not creating any object)

16. What is the difference when String is gets created using literal or new() operator ?
When we create string with new() its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.

17. Does not overriding hashcode() method has any performance implication ?
This is a good question and open to all , as per my knowledge a poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.

18. What’s wrong using HashMap in multithreaded environment? When get() method go to infinite loop ?
Another good question. His answer was during concurrent access and re-sizing.

19. Give a simplest way to find out the time a method takes for execution without using any profiling tool?
this questions is suggested by @Mohit
Read the system time just before the method is invoked and immediately after method returns. Take the time difference, which will give you the time taken by a method for execution.

To put it in code…

long start = System.currentTimeMillis ();
method ();
long end = System.currentTimeMillis ();

System.out.println (“Time taken for execution is ” + (end – start));

Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing

Read more: http://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html#ixzz2jyvTfnXV

Core Java Interview Question Answer的更多相关文章

  1. Difference Between Arraylist And Vector : Core Java Interview Collection Question

    Difference between Vector and Arraylist is the most common  Core Java Interview question you will co ...

  2. Top 25 Most Frequently Asked Interview Core Java Interview Questions And Answers

    We are sharing 25 java interview questions , these questions are frequently asked by the recruiters. ...

  3. 115 Java Interview Questions and Answers – The ULTIMATE List--reference

    In this tutorial we will discuss about different types of questions that can be used in a Java inter ...

  4. an interview question(1)

    声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...

  5. applet示例 WelcomeApplet.java <Core Java>

    import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Font; import java.awt.Grap ...

  6. Core Java Volume I — 1.2. The Java "White Paper" Buzzwords

    1.2. The Java "White Paper" BuzzwordsThe authors of Java have written an influential White ...

  7. Core Java Volume I — 4.7. Packages

    4.7. PackagesJava allows you to group classes in a collection called a package. Packages are conveni ...

  8. Core Java 学习笔记——1.术语/环境配置/Eclipse汉化字体快捷键/API文档

    今天起开始学习Java,学习用书为Core Java.之前有过C的经验.准备把自己学习这一本书时的各种想法,不易理解的,重要的都记录下来.希望以后回顾起来能温故知新吧.也希望自己能够坚持把自己学习这本 ...

  9. Java Interview Reference Guide--reference

    Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...

随机推荐

  1. 如何查找在CDN下的真实ip

    今天去找了一下www.bilibili.tv的IP(为什么要这样子做见),发现www.bilibili.tv使用了CDN服务直接ping找不到其真实IP(实际上不用找也可以但就是想找一下). 那我们应 ...

  2. 抽象类(abstract)是否可以继承自实体类 ?

    可以. 但是这个实体类必须有无参构造函数(默认的构造函数). 如: public class A { //这个构造函数必须要有(在没有构造函数重载时可以省略,因为运行时会为A添加默认构造函数) pub ...

  3. boost:program_options

    由于系统库getopt和getopt_long用起来不够直观,仔细看了下boost发现Boost.Program_options可以满足我的需求,它和getopt系列函数一样,可以抓起命令行参数,这里 ...

  4. Linux时间相关函数

    相关文件: /etc/localtime  本地时间二级制文件 /etc/sysconfig/clock  时区配置文件 /usr/share/zoneinfo  存储各个时区的二进制文件 时间修改方 ...

  5. Web应用登出后防止浏览器后退

    通常情况下,浏览器会对页面进行缓存,此时可以通过后退访问刚才的页面,如:Web应用登出后后退能够访问刚才被缓存的页面,这样在有些情况下是不够安全的,解决防止后退的办法如下: response.setH ...

  6. UINavigationController 与 UITabBarController

    http://www.cnblogs.com/YouXianMing/p/3756904.html // index start from 1. UITabBarItem *newsItem = [[ ...

  7. 一个统计目录文件大小的php函数

    早上刚到公司,头告诉我,抓紧写一个小函数,用来统计指定目录中文件大小,我了个去,动手吧,还好有点小基础,一会就完工了,哈哈.代码在下面咯. <? /** 统计目录文件大小的函数 @author  ...

  8. Delphi XE5教程1:语言概述

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  9. openerp 常见问题 OpenERP在哪储存附件?(转载)

    OpenERP在哪储存附件? 原文地址:http://cn.openerp.cn/where_to_store_attachement_in_openerp_7/ 我们知道对OpenERP中的每个内部 ...

  10. SQL Server 2014 Always on ON Microsoft Azure New Portal(1)

    以前假如需要在Azure IaaS 创建的SQL Server AlwaysOn 需要参考以下的步骤 From the MVPs: SQL Server High Availability in Wi ...