Keyword

Result: 252 questions

What are some of the best practices relating to the Java Collection framework ?

Answer:

  • Choosing the right type of the collection to use, based on the application’s needs, is very crucial for its performance. For example if the size of the elements is fixed and know a priori, we shall use an Array, instead of an ArrayList.
  • Some collection classes allow us to specify their initial capacity. Thus, if we have an estimation on the number of elements that will be stored, we can use it to avoid rehashing or resizing.
  • Always use Generics for type-safety, readability, and robustness. Also, by using Generics you avoid the ClassCastException during runtime.
  • Use immutable classes provided by the Java Development Kit (JDK) as a key in a Map, in order to avoid the implementation of the hashCode and equals methods for our custom class.
  • Program in terms of interface not implementation.
  • Return zero-length collections or arrays as opposed to returning a null in case the underlying collection is actually empty.
View

What’s the difference between Enumeration and Iterator interfaces ?

Answer:

Enumeration is twice as fast as compared to an Iterator and uses very less memory. However, the Iterator is much safer compared to Enumeration, because other threads are not able to modify the collection object that is currently traversed by the iterator. Also, Iteratorsallow the caller to remove elements from the underlying collection, something which is not possible with Enumerations.

View

What is the difference between HashSet and TreeSet ?

Answer:

The HashSet is Implemented using a hash table and thus, its elements are not ordered. The add, remove, and contains methods of a HashSet have constant time complexity O(1). On the other hand, a TreeSet is implemented using a tree structure. The elements in a TreeSet are sorted, and thus, the add, remove, and contains methods have time complexity of O(logn).

View

What is structure of Java Heap ? What is Perm Gen space in Heap ?

Answer:

The JVM has a heap that is the runtime data area from which memory for all class instances and arrays is allocated. It is created at the JVM start-up. Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. Heap memory consists of live and dead objects. Live objects are accessible by the application and will not be a subject of garbage collection. Dead objects are those which will never be accessible by the application, but have not been collected by the garbage collector yet. Such objects occupy the heap memory space until they are eventually collected by the garbage collector.

View

Does Garbage collection occur in permanent generation space in JVM ?

Answer:

Garbage Collection does occur in PermGen space and if PermGen space is full or cross a threshold, it can trigger a full garbage collection. If you look carefully at the output of the garbage collector, you will find that PermGen space is also garbage collected. This is the reason why correct sizing of PermGen space is important to avoid frequent full garbage collections. Also check our article Java 8: PermGen to Metaspace.

View

What are the two types of Exceptions in Java ? Which are the differences between them ?

Answer:

Java has two types of exceptions: checked exceptions and unchecked exceptions. Unchecked exceptions do not need to be declared in a method or a constructor’s throws clause, if they can be thrown by the execution of the method or the constructor, and propagate outside the method or constructor boundary. On the other hand, checked exceptions must be declared in a method or a constructor’s throws clause. See here for tips on Java exception handling.

View

What is the difference between Exception and Error in java ?

Answer:

Exception and Error classes are both subclasses of the Throwable class. The Exception class is used for exceptional conditions that a user’s program should catch. The Error class defines exceptions that are not excepted to be caught by the user program.

View

What will happen to the Exception object after exception handling ?

Answer:

The Exception object will be garbage collected in the next garbage collection.

View

What is an Applet ?

Answer:

A java applet is program that can be included in a HTML page and be executed in a java enabled client browser. Applets are used for creating dynamic and interactive web applications.

View

What is the difference between a Scrollbar and a JScrollPane ?

Answer:

Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPanehandles its own events and performs its own scrolling.

View

Name three Component subclasses that support painting.

Answer:

The CanvasFramePanel, and Applet classes support painting.

View

What is the difference between a MenuItem and a CheckboxMenuItem ?

Answer:

The CheckboxMenuItem class extends the MenuItem class and supports a menu item that may be either checked or unchecked.

View

How are the elements of a BorderLayout organized ?

Answer:

The elements of a BorderLayout are organized at the borders (North, South, East, and West) and the center of a container.

View

How are the elements of a GridBagLayout organized ?

Answer:

The elements of a GridBagLayout are organized according to a grid. The elements are of different sizes and may occupy more than one row or column of the grid. Thus, the rows and columns may have different sizes.

View

What is the difference between a Window and a Frame ?

Answer:

The Frame class extends the Window class and defines a main application window that can have a menu bar.

View

What is JDBC ?

Answer:

JDBC is an abstraction layer that allows users to choose between databases. JDBC enables developers to write database applications in Java, without having to concern themselves with the underlying details of a particular database.

View

Explain the role of Driver in JDBC.

Answer:

The JDBC Driver provides vendor-specific implementations of the abstract classes provided by the JDBC API. Each driver must provide implementations for the following classes of the java.sql package:ConnectionStatementPreparedStatementCallableStatementResultSet and Driver.

View

What is the use of CallableStatement ? Name the method, which is used to prepare a CallableStatement.

Answer:

CallableStatement is used to execute stored procedures. Stored procedures are stored and offered by a database. Stored procedures may take input values from the user and may return a result. The usage of stored procedures is highly encouraged, because it offers security and modularity.The method that prepares a CallableStatement is the following:

CallableStament.prepareCall();

 

View

What is RMI ?

Answer:

The Java Remote Method Invocation (Java RMI) is a Java API that performs the object-oriented equivalent of remote procedure calls (RPC), with support for direct transfer of serialized Java classes and distributed garbage collection. Remote Method Invocation (RMI) can also be seen as the process of activating a method on a remotely running object. RMI offers location transparency because a user feels that a method is executed on a locally running object. Check some RMI Tips here.

View

What is a Servlet ?

Answer:

The servlet is a Java programming language class used to process client requests and generate dynamic web content. Servlets are mostly used to process or store data submitted by an HTML form, provide dynamic content and manage state information that does not exist in the stateless HTTP protocol.

View

© 2017 QuizBucket.org