Thursday, July 29, 2021

Top 10 Java Swing Interview Questions Answers asked in Investment banks

The Swing  API and toolkit are heavily used in developing trading application front end in Java on both large Investment banks and small broker firms. Doesn’t matter whether it's a front office or middle office application, you will find Java Swing GUI everywhere. The Swing-based GUI is used to develop an Order entry system, Order monitoring GUI, and other tools that trader or operations can use on different trade life cycles in the front office. middle office and back-office space. Due to its heavy usage, there are a lot of requirements of Java Swing developers in investment banks like Barclays, Citibank, Deutsche Bank, Nomura, JP Morgan, Morgan Stanley, and Goldman Sachs, etc.

Though in the era of Java 8, Java FX is positioned to take over from Swing but there are still a lot of legacy applications which means the requirements for Java Swing developers will not dry out soon.

In this post, you will see some Java Swing interview questions asked from various interviews. These swing interview questions are based on my experience as well as collected from friends and colleagues who have been interviewed in the finance and insurance domain, commonly known as the BFSI domain in India.

So, if you are going for any Java Swing development job interview you can use these java swing interview questions for practice, answers to some swing questions are also given. For those which I haven't provided answers, you can do some research on the internet to find out the right answers.







10 Java Swing Interview Questions Answers

Let’s start with some simple questions and then we'll move towards more difficult and complex ones. In general, this is the pattern most of the interviewer will follow on Java interviews. That’s how it goes in swing interviews as well.

Btw, this article is in continuation of my previous interview articles like:

If you haven’t read them already you may find them useful for your job interview or practice purpose

1) What is Event-Dispatcher-Thread (EDT) in Swing?
The Event Dispatcher Thread or EDT is a special thread in Swing and AWT.  The Event-Driven Thread is used to render graphics and listen for events in Swing. You will get a bonus point if you are able to highlight that time-consuming operations like connecting to the database, opening a file, or connecting to the network should not be done on EDT thread because it could lead to freezing GUI because of blocking and time-consuming nature of these operations instead they should be done on a separate thread and EDT can just be used to spawn those thread on a button click or mouse click.



2) Does Swing is a thread-safe? What do you mean by swing is not thread-safe?
This swing interview question is getting very popular nowadays. Though it’s pretty basic many developers don't understand thread-safety issues in Swing. Since Swing components are not thread-safe it means you can not update these components in any thread other than Event-Dispatcher-Thread.

If you do so you will see unexpected behavior e.g. freezing GUI due to the deadlock of incorrect values etc. Some time interviewer will also ask what are thread-safe methods in the Swing API? Those are the methods that can be safely called from any thread and there are only a couple of them e.g. repaint() and revalidate(). See this article for more discussion on Swing and thread-safety.


3) What are differences between Swing and AWT?
One of the classic Java swing interview questions and mostly asked on phone interviews. There are a couple of differences between Swing and AWT:
  1. The AWT component are considered to be heavyweight while Swing component are considered  lightweights
  2. The Swing GUI has pluggable look and feel.
  3. The AWT is platform dependent i.e. the same GUI will look different on different platforms because they use native components e.g. a Frame will look different on Windows and Linux if you pick native components. On the other hand, Swing components are developed in Java and are platform dependent. It also provide consistent GUIs across platform e.g. if you run NetBeans which is created in Java on Linux or Windows you will see consistency in GUI. 
You can further read Core Java, Volume II--Advanced Features by  Cay S. Horstmann.  to learn more about the difference between Swing and AWT. 


4) Why Swing components are called lightweight components?
Another popular Java swing interview question, I guess the oldest that generally comes as a follow-up to the earlier question based on your answer provided. Since the AWT components are associated with native screen resources they are called heavyweight components while Swing components use the screen resource of an ancestor instead of having their own and that's why called lightweight or lighter component. See here for more AWT base Interview Questions in Java. 


5) What is the difference between invokeAndWait and invokeLater?
This swing interview question is asked differently at different points. Some time interviewer asks how do you update swing component from a thread other than EDT instead of directly asking the difference between invokeAndWait() and invokeLater() method of Swing API.

For such kind of scenario, we use SwingUtilities.invokeAndWait(Runnable r) and SwingUtilities.invokeLetter(Runnable r) though there are quite a few differences between these two, the major one is that invokeAndWait() is a blocking call and wait until GUI update is finished while invokeLater is a non-blocking asynchronous call, so it won't wait for GUI to be updated. The update request will be picked by Event Dispatcher thread from the request queue.

In my opinion, these question has its own value and every swing developer should be familiar with these questions or concept not just for interview point of view but on application perspective. you can read more on my post  how InvokeAndWait and InvokeLater work in Swing?



6) Write code for JTable with custom cell editor and custom cell renderer?
Now comes the harder part of Swing interviews, questions asked in this part of Swing interviews is mostly about writing code and checking developer’s experience of API and GUI development and understanding of the key concept of swing code etc.

The JTable is one of the favorite topic of all Swing interviews and most popular questions on swing interviews are from JTable, why? Because here interviewer will directly ask you to write code. One more reason of immense popularity of JTable based questions is because JTable heavily used in all Electronic trading GUI.

The GUI used for online stock trading uses JTable to show data in tabular format so an in-depth knowledge of JTable is required to work on online trading GUI developed in Swing. While this question is just an example questions around JTable are mostly centered around updating table, how do you handle large volume of data in table, using customize cell renderer and editor, sorting table data based on any column etc. so just make sure you have done quite a few handsome exercise on JTable before appearing for any Java Swing interview in IB.

JTable  Swing Interview Questions Answers



7) How do you handle opening of the database, file, or network connection on a click of a button?
This one is one of the easy Java swing interview questions. The interviewer is interested in whether you know the basic principle of Java GUI development or not. The answer is, you should not do this operation in EDT thread instead spawn a new thread from ActionListener or button and disable the button until operation gets completed to avoid resubmitting the request.

The only condition is that your GUI should always be responsive no matter what happens on the network connection or database connection because these operations usually take time, particularly when you are running GUI on slow networks.





8) Write code to print the following layout (mainly focused on GridBag layout)?
After JTable, the second favorite topic of the swing interviewers is GridBagLayout. The GridBagLayout in the swing is most powerful but at the same time, most complex layout and a clear cut experience and expertise around GridBagLayout is desired for developing Swing GUI for trading systems.

No matter whether you are developing GUI for equities trading, futures or options trading or forex trading you always required GridBagLayout. Swing interview question on GridBagLayout will be mostly on writing code for a particular layout just like an example shown here. In which six buttons A, B, C, D, E and F are organized in a certain fashion.

Java Swing GUI Interview Questions answers



9) Prediction of the output of code?
This is another category of Swing interview questions asked in IB whether they will give you code and asked what would be the output, how will the GUI look like. This type of question is based upon how well you understand and visualize the code. Whether you are familiar with default layout manager of various component classes or not e.g. default layout of JFrame is BorderLayout. So do some practice of these kinds of java Swing interview questions as well?




10) The question around JList is like Creating a JList component such which should contain all the asset classes like Stocks, Futures, Options, and FX, etc in the form of String. But constraint is that the JList should always be sorted in ascending order except that all assets which begin with "Electronic trading” appear on top?

This is an excellent Java Swing interview question which is based on the real-world task and focuses on the JList component, Sorting, and customizing the JList model. For sorting, you can use comparable in Java, and by customizing the JList model you can display content as requested. Once you successfully answer this question there may be some follow-up on customizing the color of cell etc which can be done by customizing the renderer.

These are just a few swing interview questions that I find interesting. Swing is particularly important in the Front office area and there is a lot of demand for good Swing developers in Wall Street firms. If you have good skills in FIX Protocol + Core Java + Swing you would be most sought after in the electronic trading domain. Anyway, this list is no means complete so please share any interesting java swing interview questions you have faced.


My previous Java Interview articles

If you want to learn more about Java Swing, I suggest you pick a good Java book like Java: How to Program by Deitel and Deitel or  Core Java, Volume II--Advanced Features by  Cay S. Horstmann. Both books are good to learn the basic and advanced concept of Java GUI development using Swing. 

11 comments :

Anonymous said...

EDT != Event Driven Thread
EDT == Event Dispatch Thread

Tavi said...

I believe its called Event Dispatch Thread... (http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html)

Anyway... nice list of sweetpoints :)

Javin @ stringbuffer vs Stringbuilder said...

my bad Tavi and Anonymous , its indeed called Event Dispatcher Thread.

Prabhu said...

Indeed these interview questions on Swing is much better than the other source available on Internet. I just think number of questions are little less but quality is awesome. Few more JFC Swing interview questions from my collection:

1) Why can't you call paint() method directly instead of calling repaint() method?

2) What is clipping? How do you create animation using Clipping in Canvas?

3) How do you sort JTable based upon Colum?
4) When do you use JOptionPane in Swing?
5) What is difference between Container and Component in Swing?
6) How do you make a Swing application to run as Java web start?
7) What is JNLP file?
8) Is Swing Single Threaded?
9) Tell me 5 Swing components you have used?
10) Which Swing GUI Designer have you used?
11) How do you create layout for Swing components?
12) Difference between BorderLayout, FlowLayout, CardLayout and BoxLayout in Swing?
13) is Swing part of Java Development kit?
14) Which JAR contains Swing code ?
15) How do you write your own Swing component like Ticker?
16) Can you create 3D effect using Swing component

Let me know if you have answers of these Swing questions.

Virat said...

Can you please share some AWT Interview questions and some Java FX Interview question. I know little bit of Swing and AWT and learning Java FX. There is not many Java FX or AWT interview question available, Please share with answers. Thanks

Anonymous said...

Couple of Swing questions for experienced and Senior Java Developers, mostly 4 to 6 years experieced :

1) Is Swing is thread-safe? (No)
2) Can we modify TableModel in thread which loads data from database? (No)
3) What is SwingWorker? How does it work? It works on worker pattern and used to perform time consuming task in worker thread. It also manages to update EDT when task is done.
4) How do you find if current thread is Event Dispatcher Thread? (SwingUtilities.isEventDispatcherThread()) method

Unknown said...

Dangerous misinformation blog here!
1) As it have been pointed out, EDT = "Event Dispatch Thread". You read that and write "its indeed called Event Dispatcher Thread" (and did not correct your article, btw). Please, learn to read before writing (and after learn to write as well)! "Dispatch", not "Dispatcher"!
3) and 4) AWT: lightweight, Swing: heavyweight. Funnily, your justification is correct but leads to the opposite status.

Please pay attention to what you write, you've got people reading this blog you know...

javin paul said...

Hello Xavier, Sorry about EDT typo, I should have corrected it long back. I didn't get your comment regarding AWT being lightweight and Swing being heavyweight, I think I have wrote it correctly, AWT is heavyweight because each AWT component is a native platform component. Let me know if I am overlooking something.

Anyway, thanks for your comments and concern, completely agree. The article is also updated now.

Anonymous said...

Swing questions in 2017??? OMFG
This should have been buried deep like a decade ago. No sane developer use swing anyways...

Unknown said...

Thank you for your reply Javin. Nice to see that you're still sticking around 5 years later!
Well it seems that a lot of people get confused about which one is light of heavy. In my mind, Swing was heavy cause all components (UI & behaviour) are embedded in it, needing a lot of classes to be loaded, whilst AWT relies on system pre-made components, thus being smaller in memory and faster at runtime. But it seems that I was wrong after all, and a single look at javadoc is backing your position. My apologies.
But still, it's "Dispatch_" and not "Dispatcher", and it doesn't correct the many syntax, grammatical and spelling errors ("Does Swing is thread-safe"? Common!)... I hope you improved your abilities since then...
Oh and in response to Anonymous stating that "No sane developer use swing anyways": well, today, many firms are still using Swing for financial programs which are handling amounts of money the human brain can't realize every second.
But it's true that the world of finance is insane!

Unknown said...

I understand Swing components should only be updated in the EDT thread so my question is how do you disable a button then connect to a database in a separate thread then enable the button once the connection is complete? The button is not thread safe so it shouldn't be passed to the 'start connection' thread.

Post a Comment