Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Ques 16. Can an inner class declared inside of a method access local variables of this method?

Ans. It is possible if these variables are final.

Is it helpful? Add Comment View Comments
Ques 17. What can go wrong if you replace && with & in the following code:
String a=null;
if (a!=null && a.length()>10) {
A single ampersand here would lead to a NullPointerException.
Is it helpful? Add Comment View Comments
Ques 18. What is the main difference between a Vector and an ArrayList?
Ans. Sometimes Vector is better; sometimes ArrayList is better; sometimes you don‘t want to use either. I hope you weren‘t looking for an easy answer because the answer depends upon what you are doing. There are four factors to consider:

Data growth 
Usage patterns
API: In The Java Programming Language Ken Arnold, James Gosling, and David Holmes describe the Vector as an analog to the ArrayList. So, from an API perspective, the two classes are very similar. However, there are still some major differences between the two classes.
Synchronization: Vectors are synchronized. Any method that touches the Vector‘s contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don‘t need a thread-safe collection, use the ArrayList. Why pay the price of synchronization unnecessarily?
Data growth: Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It‘s always best to set the object‘s initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don‘t know how much data you‘ll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.
Usage patterns: Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. These operations are more expensive because you have to shift all elements at index i and higher over by one element. So what does this all mean? It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1). However, indexing an element is a bit slower -- O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created.
Is it helpful? Add Comment View Comments
Ques 19. How can a subclass call a method or a constructor defined in a superclass?
Ans. Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass‘s constructor.
Is it helpful? Add Comment View Comments
Ques 20. What's the difference between a queue and a stack?
Ans. Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.

You can think of a queue like a line at the bank. The first person to get there will get to the teller first. If a bunch of people come while all the tellers are busy, they stand in line in the order in which they arrived. That is to say, new people (items) are added to the end of the line and the first person in line is the only one who is called to a teller. In real life this is known as "first come, first served." In programming terms it's known as first-in-first-out, or FIFO.

You can think of a stack like a deck of cards. You can put down cards into a pile on your table one at a time, but if you want to draw cards, you can only draw them from the top of the deck one at a time. Unlike a queue, the first card to be put down is the last card to be used. This is known as first-in-last-out, or FILO (also called LIFO for last-in-first-out).

A queue is a first-in-first-out data structure. When you add an element to the queue you are adding it to the end, and when you remove an element you are removing it from the beginning.

A stack is a first-in-last-out data structure. When you add an element to a stack you are adding it to the end, and when you remove an element you are removing it from the end.
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2020 WithoutBook