• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/48

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

48 Cards in this Set

  • Front
  • Back

Differences between Abstraction and Encapsulation

Abstraction and encapsulation are complementary concepts. On the one hand, abstraction focuses on the behavior of an object.



On the other hand, encapsulation focuses on the implementation of an object’s behavior. Encapsulation is usually achieved by
hiding information about the internal state of an object and thus, can be seen as a strategy used in order to provide abstraction

What is JVM ?

A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled
into a bytecode file, which is executed by the JVM.

What does the “static” keyword mean ?

The static keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to
which it belongs

Can you access non static variable in static context ?

A static variable is initialized
when the class is loaded by the JVM. If your code tries to access a non-static variable, without any instance, the compiler will
complain, because those variables are not created yet

What are the Data Types supported by Java ?

The eight primitive data types supported by the Java programming language are:
• byte
• short
• int
• long
• float
• double
• boolean
• char

What is autoboxing and unboxing?

Autoboxing is the automatic conversion made by the Java compiler between the primitive types and their corresponding object
wrapper classes. For example, the compiler converts an int to an Integer, a double to a Double, and so on. If the conversion goes
the other way, this operation is called unboxing

What is Function Overriding and Overloading in Java ?

Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters.



On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent
class.

What are pass by reference and pass by value ?

When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn’t affect the original value.



When an object is passed by reference, this means that the actual object is not passed, rather
a reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.

What is the difference between processes and threads ?

A process is an execution of a program, while a Thread is a single execution sequence within a process. A process can contain
multiple threads. A Thread is sometimes called a lightweight process

Explain different ways of creating a thread. Which one would you prefer and
why ?

• A class may extend the Thread class.
• A class may implement the Runnable interface.
• An application can use the Executor framework, in order to create a thread pool.


What is the difference between a synchronized method and a synchronized
block ?


In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword.



The synchronized keyword can be applied in a method level (coarse grained lock) or block level of code (fine grained lock)

What’s a deadlock ?

A condition that occurs when two processes are waiting for each other to complete, before proceeding.

How do you ensure that N threads can access N resources without deadlock ?

A very simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to follow
that ordering

What are the basic interfaces of Java Collections Framework ?

Java Collections Framework provides a well designed set of interfaces and classes that support operations on a collections of
objects. The most basic interfaces that reside in the Java Collections Framework are:



Collection, which represents a group of objects known as its elements.
Set, which is a collection that cannot contain duplicate elements.
List, which is an ordered collection and can contain duplicate elements.
Map, which is an object that maps keys to values and cannot contain duplicate keys

Why Collection doesn’t extend Cloneable and Serializable interfaces ?

The Collection interface specifies groups of objects known as elements. Each concrete implementation of a Collection can choose
its own way of how to maintain and order its elements.



Some collections allow duplicate keys, while some other collections don’t.
The semantics and the implications of either cloning or serialization come into play when dealing with actual implementations.
Thus, the concrete implementations of collections should decide how they can be cloned or serialized

What is an Iterator ?

The Iterator interface provides a number of methods that are able to iterate over any Collection.



Each Java Collection contains the iterator method that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration.


What is difference between fail-fast and fail-safe?

The Iterator’s fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification
in the collection. All the collection classes in java.util package are fail-fast, while the collection classes in java.util.concurrent
are fail-safe. Fail-fast iterators throw a ConcurrentModificationException, while fail-safe iterator never throws such
an exception.

How HashMap works in Java ?

The HashMap requires a hash function and uses hashCode and equals methods,
in order to put and retrieve elements to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection.



If the key exists, its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.

What differences exist between HashMap and Hashtable ?

• Both the HashMap and Hashtable classes implement the Map interface


• A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.


• A Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a
• Hashtable is suitable for multi-threaded environments.
• A HashMap provides its set of keys and a Java application can iterate over them. Thus, a HashMap is fail-fast. On the other
hand, a Hashtable provides an Enumeration of its keys.
• The Hashtable class is considered to be a legacy class.

What is difference between Array and ArrayList ?

• Arrays can contain primitive or objects, while an ArrayList can contain only objects.
• Arrays have fixed size, while an ArrayList is dynamic.



• An ArrayList provides more methods and features, such as addAll, removeAll, iterator, etc.
• For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this approach makes
them slower when working on fixed size primitive data types.

What is the tradeoff between using an unordered array versus an ordered array ?


The major advantage of an ordered array is that the search times have time complexity of O(log n), compared to that of an unordered array, which is O (n).



The disadvantage of an ordered array is that the insertion operation has a time complexity of O(n), because the elements with higher values must be moved to make room for the new element. Instead, the insertion operation
for an unordered array takes constant time of O(1).





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

• 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.

What’s the difference between Enumeration and Iterator interfaces ?

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, Iterators allow the caller to remove elements from the underlying collection, something which is not possible with Enumerations.


What is the difference between HashSet and TreeSet ?


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).

What is the purpose of garbage collection in Java, and when is it used ?

The purpose of garbage collection is to identify and discard those objects that are no longer needed by the application, in order for the resources to be reclaimed and reused.


What does System.gc() and Runtime.gc() methods do ?


These methods can be used as a hint to the JVM, in order to start a garbage collection. However, this it is up to the Java Virtual Machine (JVM) to start the garbage collection immediately or later in time.


When is the finalize() called ? What is the purpose of finalization ?

The finalize method is called by the garbage collector, just before releasing the object’s memory. It is normally advised to release
resources held by the object inside the finalize method.

If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object ?

No, the object will be available for garbage collection in the next cycle of the garbage collector.


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

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.

What is the difference between Serial and Throughput Garbage collector ?

The throughput gc uses a parallel version of the young generation collector

When does an Object becomes eligible for Garbage collection in Java ?

A Java object is subject to garbage collection when it becomes unreachable to the program in which it is currently used

Does Garbage collection occur in permanent generation space in JVM ?

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

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


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

What is the difference between Exception and Error in java ?


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.


What is the difference between throw and throws ?

The throw keyword is used to explicitly raise a exception within the program. On the contrary, the throws clause is used to
indicate those exceptions that are not handled by a method.



Each method must explicitly specify which exceptions does not handle, so the callers of that method can guard against possible exceptions. Finally, multiple exceptions are separated by a comma

What is the importance of finally block in exception handling ?


A finally block will always be executed, whether or not an exception is actually thrown. Even in the case where the catch statement is missing and an exception is thrown, the finally block will still be executed.



Last thing to mention is that the finally block is used to release resources like I/O buffers, database connections, etc.

How does finally block differ from finalize() method ?

A finally block will be executed whether or not an exception is thrown and is used to release those resources held by the application.



Finalize is a protected method of the Object class, which is called by the Java Virtual Machine (JVM) just before an object is garbage collected.

What is the advantage of PreparedStatement over Statement ?

PreparedStatements are precompiled and thus, their performance is much better. Also, PreparedStatement objects can be reused
with different input values to their queries.

What is a Servlet ?

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.

Explain the architecture of a Servlet.

The core abstraction that must be implemented by all servlets is the javax.servlet.Servlet interface.



Each servlet must implement it either directly or indirectly, either by extending javax.servlet.GenericServlet or javax.servlet.http.HTTPServlet.



Finally, each servlet is able to serve multiple requests in parallel using multithreading.

What is the difference between an Applet and a Servlet ?

An Applet is a client side java program that runs within a Web browser on the client machine. On the other hand, a servlet is a server side component that runs on the web server.



An applet can use the user interface classes, while a servlet does not have a user interface. Instead, a servlet waits for client’s HTTP requests and generates a response in every request.

How are Observer and Observable used?

Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed


state. The Observer interface is implemented by objects that observe Observable objects.

What is the Vector class?

The Vector class provides the capability to implement a growable array of objects

What is an Iterator interface?

The Iterator interface is used to step through the elements of a Collection

What is the difference between yielding and sleeping?

When a task invokes its yield() method, it returns to the ready state.



When a task invokes its sleep() method, it returns to the waiting state.

Which java.util classes and interfaces support event handling?

The EventObject class and the EventListener interface support event processing.

What are wrapped classes?

Wrapped classes are classes that allow primitive types to be accessed as objects.

Does garbage collection guarantee that a program will not run out of memory?

Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection