This questions are mainly from internet.
What do you know about Java?
Java is nothing but a high-level programming language from the Sun Microsystem, and now from Oracle Inc. It is released in the year 1995 and runs on multiple platforms like Linux, Windows, Mac and other variations of UNIX. James Gosling is known as the father of Java programming language.
Can you differentiate between J2SDK 1.5 and J2SDK 5.0?
There is no difference between J2SDK 1.5 and J2SDK 5.0. Sun Microsystems has just re-branded the versions. Since java 1.6, Java 6 and Java 7 are called for the version of 1.6 and 1.7, respectively.
Can you name a few platforms that support Java?
Yes, Java is supported by Windows, Mac as well as UNIX/Linux like Ubuntu, Red Hat, Sun Solaris, etc..
Could you tell me why Java is called Architectural Neutral?
The main reason is because the compiler creates an architecture neutral object file format. This makes the compiled code to be executable on different other processors.
What is the difference between an Interface and an Abstract class?
An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (
private
,protected
, etc.), but has some abstract methods.What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
How do you implement a thread in Java?
In Java, there is
Thread
class that can be extended andRunnable
interface that can be implemented. Since Java 1.6, there is a new interfaceCallable
, which is also used as a thread interface but usually used as a concurrency task.Is the Java method called by reference or by value?
Java does manipulate objects by reference, and all object variables are references. However, Java doesn’t pass method arguments by reference; it passes them by value.
What is the difference between a constructor and a method?
A
final
class can’t be extended ie., final class may not be extended. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant).Difference between
Vector
andArrayList
?Vector
is synchronized whereasArrayList
is not.What is
static
in Java? Andfinal
?static
means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class. Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a super class can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can’t override a static method with a non-static method. In other words, you can’t change a static method into an instance method in a subclass.What are the different ways you can use
static
?static
can be used in four ways: static variables, static methods, static classes and it can be used across a block of code in any class in order to indicate code that runs when a virtual machine starts and before the instances are created.Do you think all property of Immutable Object needs to be final?
Not compulsory as we can easily achieve the same by making member as non-final but private and not changing them except in the constructor. Also, avoid providing setter methods for them. If it is a mutable object, then prevent leaking any reference for that member.
Can you brief me about Singleton Class?
It basically controls object creation, restricting the number to one while allowing the flexibility to create objects if the scenario changes. Singleton is one of the design methods. It guarantees that there is only one instance at runtime.
In Java, what is the default value of float and double?
Default value of Float is
0.0f
while0.0d
for Double.Can you explain type erasure?
It is nothing but a JVM phenomenon, which means that the runtime has no idea about the types of generic objects like
List<Integer>
.Explain Dot Operator.
It is used to access the instance variables and methods of class objects. Also, we can use it to access classes and sub-packages from a package.
What do you mean by Serialization in Java?
Well, serialization is nothing but a process of transforming objects into a stream of bytes. While deserialization is the opposite operation, which transforms the stream of bytes into an instance.
Can you tell me the reason why
String
class is considered immutable?It is because to avoid change in
String
object once it is created. As String is immutable, you can share it between different threads in a safe way. This is quite crucial in multithreaded programming.Can you quickly brief about
Map
,HashMap
,HashTable
, andTreeMap
?Map
is an interface, andHashMap
is a class that implements aMap
. It is not synchronized and supports null values and keysHashTable
is a synchronized version ofHashMap
.TreeMap
is similar toHashMap
but usesTree
to implementMap
.Do you think not overriding
hashcode()
method has any performance implication?A weak
hashcode
function will result into frequent collision inHashMap
, which will at the end increase the time to add an object withinHashMap
.Can you explain when and why Getters and Setters important?
We can put setters and getters within interfaces, which can hide implementation details. This allows us to make member variables public in Java. Getters and Setters are important to encapsulate Java class, which hide the direct accesses to member fields. This design is called Plain Old Java Object (POJO).
Is it possible to import same package or class twice? Will the JVM load the package twice at runtime?
It is possible to import the same package or class more than one time. Also, it won’t have any effect on compiler or JVM. JVM will load the class for one time only, irrespective of the number of times you import the same class.
What is difference between Throw and Throws?
While Throw is used to trigger an exception, Throws is used in the declaration of exception. It is not possible to handle checked exception without Throws.
What is the significance of the order in which catch statements for
FileNotFoundException
andIOException
are written?It is crucial to consider the order as the
FileNoFoundException
is inherited from theIOException
. Therefore, it is important that exception’s subclasses caught first.Can throw some light on Yielding and Sleeping?
When any task invokes its
yield()
method, it will return to the ready state. Whenever a task invokessleep()
method, it will return to the wait state.Why you should use Vector class?
It provides the capability to implement a growable array of objects. It is quite useful when we don’t know the exact size of the array.
Can you tell me the number of bits used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
For Unicode 16 bits and ASCII needs 7 bits. However, ASCII is usually represented as 8 bits. UTF-8 presents characters through 8, 16 and 18 bit pattern. UTF-16 will require 16-bit and larger bit patterns.
What is Applets?
A small program based on Java that can be transformed from one computer to another using the Applet Viewer or web browser.
What is the use of Locale?
Locale is an object containing geographical, cultural and political information which helps in using custom codes and conventions of specific country or region for writing applications in that language.
What is the use of Java Package?
Java Package is useful for organizing projects containing multiple modules and protecting them from unauthorized access.
While working in the JVM, do we need to import java.lang package?
No, by default it is loaded in the JVM.
Can Applets communicate with each other?
Yes, they can communicate via shared static variables even if they belong to same of different classes.
Can a .java file support more than one java classes?
Yes, it can support more than one Java classes in a condition where one of them is a public class.
MAIN, NEXT, DELETE & EXIT, which of these is a keyword in Java?
None of these is a keyword in Java
How to handle errors while writing or accessing Stored Procedures.
Store Procedure itself returns the error codes if any but, in case if it fails to do so, we can resort to catching SQL Exception.
From
ArrayList
andLinkedList
, which one helps to perform an indexed search in a list of objects?ArrayList
What is the use of
File
Class?It helps in accessing files and directories of a local system. Note that both files and directories are represented by
File
class.Does Java support Default arguments?
No, it does not support.
Describe life cycle of Applets?
Initialization, Starting, Stopping, Destroying & Painting. Applets are not so useful nowadays. As to the rich client applications in the web world, HTML5 is the future.
What is the method applied to load an image in Applet class?
It’s
getImage
.