Category : Java | Sub Category : Java Interview questions | By Prasad Bonam Last updated: 2020-10-01 10:25:19 Viewed : 511
A: Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
A: Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.
A: Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded
A: When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.
A: Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods.
6. Q: List two Java IDE’s?
A: Netbeans, Eclipse, etc
A:
A
class is a blue print from which individual objects are created. A class can
contain fields and methods to describe the behavior of an object.
A: A class consist of
Local variable, instance variables and class variables.
A: Variables defined
inside methods, constructors or blocks are called local variables. The variable
will be declared and initialized within the method and it will be destroyed
when the method has completed.
A: Instance variables are variables within a class but outside any method.
These variables are instantiated when the class is loaded.
A: These are
variables declared with in a class, outside any method, with the static
keyword.
A:
Constructor
gets invoked when a new object is created. Every class has a constructor. If we
do not explicitly write a constructor for a class the java compiler builds a
default constructor for that class.
A: An Object is first
declared, then instantiated and then it is initialized.
14.
Q: What is the default
value of byte datatype in Java? A: Default value of byte
datatype is 0.
A: Default value of
float and double datatype in different as compared to C/C++. For float its 0.0f
and for double it’s 0.0d
A: This data type is
used to save space in large arrays, mainly in place of integers, since a byte
is four times smaller than an int.
A: Class variables
also known as static variables are declared with the static keyword in a class,
but outside a method, constructor or a block.
A: Java provides
access modifiers to set access levels for classes, variables, methods and
constructors. A member has package or default accessibility when no
accessibility modifier is specified.
A: This method is used to get the primitive data type of a certain String.
A:
The
String class is immutable, so that once it is created a String object cannot be
changed. Since String is immutable it can safely be shared between many threads
,which is considered very important for multithreaded programming.
A: The
String class is considered as immutable, so that once it is created a String
object cannot be changed. If there is a necessity to make alot of modifications
to Strings of characters then StringBuffer should be used.
A: Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.
A: java.util.regex consists of three classes: Pattern class, Matcher class and PatternSyntaxException class.
A: It is possible to define
a method that will be
A: An
exception is a problem that arises during the execution of a program.
Exceptions are caught by handlers positioned along the threads method invocation
stack.
A: It is an exception
that is typically a user error or a problem that cannot be foreseen by the
programmer. For example, if a file is to be opened, but the file cannot be
found, an exception occurs. These exceptions cannot simply be ignored at the
time of compilation.
A:
It
is an exception that occurs that probably could have been avoided by the
programmer. As opposed to checked exceptions, runtime exceptions are ignored at
the time of compliation.
A: The Exception class has two main subclasses : IOException class and RuntimeException Class.
A: If
a method does not handle a checked exception, the method must declare it using
the throwskeyword. The throws keyword appears at the end of a methods signature.
A: An exception can
be thrown, either a newly instantiated one or an exception that you just
caught, by using throw keyword.
A: The
finally keyword is used to create a block of code that follows a try block. A
finally block of code always executes, whether or not an exception has
occurred.
A: It
is the process where one object acquires the properties of another. With the
use of inheritance the information is made manageable in a hierarchical order.
A: If
the method overrides one of its superclass methods, overridden method can be
invoked through the use of the keyword super. It can be also used to refer to a
hidden field
A: Polymorphism
is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a
child class object.
A: It
refers to the ability to make a class abstract in OOP. It helps to reduce the
complexity and also improves the maintainability of the system.
A: These
classes cannot be instantiated and are either partially implemented or not at
all implemented. This class contains one or more abstract methods which are
simply method declarations without a body.
A: If
you want a class to contain a particular method but you want the actual
implementation of that method to be determined by child classes, you can
declare the method in the parent class as abstract.
A: It is the techn
A: The
main benefit of encapsulation is the ability to modify our implemented code
without breaking the code of others who use our code. With this Encapsulation
gives maintainability, flexibility and extensibility to our code.
A: An interface is a
collection of abstract methods. A class implements an interface, thereby
inheriting the abstract methods of the interface.
A: A Package can be
defined as a grouping of related types(classes, interfaces, enumerations and annotations
) providing access protection and name space management.
A:
Packages
are used in Java in-order to prevent naming conflicts, to control access, to
make searching/locating and usage of classes, interfaces, enumerations and
annotations, etc., easier.
A: A multithreaded
program contains two or more parts that can run concurrently. Each part of such
a program is called a thread, and each thread defines a separate path of
execution.
A: Thread can be
created by: implementing Runnable interface, extending the Thread class.
A:
An
applet is a Java program that runs in a Web browser. An applet can be a fully
functional Java application because it has the entire Java API at its disposal.
A: An applet extends
java.applet.Applet class.
A: It uses garbage collection to free the memory. By cleaning those objects that is no longer reference by any of the program.ique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.
A: It uses garbage collection to free the memory. By cleaning those objects that is no longer reference by any of the program.
A: It is used
with variables or methods and used to call constructer of same class.
A: It is a collection of element which cannot contain duplicate
elements. The Set interface contains only methods inherited from Collection and
adds the restriction that duplicate elements are prohibited.
A: It is a Set
implemented when we want elements in a sorted order.
A: It is used to sort collections and arrays of objects using the
collections.sort() and java.utils. The objects of the class implementing the
Comparable interface can be ordered.
Throw is used to trigger an
exception where as throws is used in declaration of exception.
A: The following shows
the explanation individually:
public: it is the access
specifier.
static: it allows main() to be
called without instantiating a particular instance of a class.
void: it affirns
the compiler that no value is returned by main(). main(): this method is called
at the beginning of a Java program. String args[ ]: args parameter is an
instance array of class String
A: Java Runtime Environment is an implementation of the Java
Virtual Machine which executes Java programs. It provides the minimum
requirements for executing a Java application;
A: JAR files is Java Archive fles and
it aggregates many files into one. It holds Java classes in a library. JAR
files are built on ZIP file format and have .jar file extension.
A: This is Web Archive File and used to store XML, java classes, and
JavaServer pages. which is used to
distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML
files, static Web pages etc.
A: It improves
the runtime performance of computer programs based on bytecode.
A: Object based programming languages follow all the features of
OOPs except Inheritance. JavaScript is an example of object based programming
languages
A: The java
compiler creates a default constructor only if there is no constructor in the
class.
4.
Q: Can a
constructor be made final? A: No, this is not possible.
A: It is used to initialize the static data member, It is excuted before main method at the time of classloading.
A: Holding the
reference of the other class within some other class is known as composition.
A: If a class
has multiple functions by same name but different parameters, it is known as
Method Overloading.
A: If a subclass provides a
specific implementation of a method that is already provided by its parent
class, it is known as Method Overriding.
A:
Method overloading increases the readability of the program. Method
overriding provides the specific implementation of the method that is already
provided by its super class parameter must be different in case of overloading,
parameter must be same in case of overriding.
A: Final
classes are created so the methods implemented by that class cannot be
overridden. It can’t be inherited.
A: A NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object etc.
A: A thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an objects lock, or by invoking an objects wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.
77. Q: How does multi-threading take place on a computer with a single CPU?
A: The operating systems task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.
A: After a thread is started, via its start() method of the Thread class, the JVM invokes the threads run() method when the thread is initially executed.
A: Yes, it does. The FileNoFoundException is inherited from the IOException. Exceptions subclasses have to be caught first.
A: 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.
A: The
Vector class provides the capability to implement a growable array of objects.
Vector proves to be very useful if you dont know the size of the array in advance, or you just
need one that can change sizes over the lifetime of a program.
A: Unicode requires 16 bits and ASCII
require 7 bits. Although the ASCII character set uses only 7 bits, it is
usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18
bit patterns. UTF-16 uses 16-bit and larger bit patterns.
A: These are
classes that allow primitive types to be accessed as objects. Example: Integer,
Character, Double, Boolean etc.
A: The Frame
class extends Window to define a main application window that can have a menu
bar.
A: javax.Swing
package. All components in Swing, except JApplet, JDialog, JFrame and JWindow
are lightweight components.
A: The paint() method supports painting via a Graphics object. The
repaint() method is used to cause paint() to be invoked by the AWT painting
thread.
A: It is used
to create objects that provide access to the files and directories of a local
file system.
9.
Q: What is the
difference between the Reader/Writer class hierarchy and the
InputStream/OutputStream class hierarchy? A: The
Reader/Writer class hierarchy is character-oriented, and the
InputStream/OutputStream class hierarchy is
byte-oriented.
A: The Class class is used to obtain
information about an objects design and java.lang.Class class instance
represent classes, interfaces in a running Java application.
A: A static variable is associated with
the class as a whole rather than with specific instances of a class. Non-static
variables take on unique values with each object instance.
A: Serialization is the process of writing the state of an object
to a byte stream. Deserialization is the process of restoring these objects.
13.
Q: Can you write
a Java class that could be used both as an applet as well as an application? A: Yes, just
add a main() method to the applet.
A: AWT components are
heavy-weight, whereas Swing components are lightweight. Heavy weight components
depend on the local windowing toolkit. For example, java.awt.Button is a heavy
weight component, when it is running on the Java platform for Unix platform, it
maps to a real Motif button.
A: Yes, since inheritance
inherits everything from the super class and interface, it may make the
subclass too clustering and sometimes error-prone when dynamic overriding or
dynamic overloading in some situation.
A:
The code sleep(2000); puts thread aside for exactly two seconds. The
code wait(2000), causes a wait of up to two second. A thread could stop waiting
earlier if it receives the notify() or notifyAll() call. The method wait() is
defined in the class Object and the method sleep() is defined in the class Thread.
A: The ArithmeticException is
thrown when integer is divided by zero or taking the remainder of a number by
zero. It is never thrown in floating-point operations.
A: The Collections API is a set of classes and interfaces that support
operations on collections of objects.
A: 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.
A: A break statement results
in the termination of the statement to which it applies (switch, for, do, or
while). A continue statement is used to end the current loop iteration and
return control to the loop statement.
97. Q: If a variable is declared as private, where may the variable be accessed?
A: A private variable may only be accessed within the class in which it is declared.
98. What is JVM ? Why is Java called the
“Platform Independent Programming Language” ? 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.
Java was designed to allow application programs
to be built that could be run on any platform, without having to be rewritten
or recompiled by the programmer for each separate platform. A Java virtual
machine makes this possible, because it is aware of the specific instruction
lengths and other particularities of the underlying hardware platform.
99. What is the Difference between JDK and JRE ? The Java Runtime Environment (JRE) is basically the Java Virtual Machine (JVM) where your Java programs are being executed. It also includes browser plugins for applet execution. The Java Development Kit (JDK) is the full featured Software Development Kit for Java, including the JRE, the compilers and tools (like JavaDoc, and Java Debugger), in order for a user to develop, compile and execute Java applications.
100. Explain the available thread states in a high-level. During its execution, a thread can reside in one of the following states:
Runnable: A thread becomes ready to run, but does not necessarily start running immediately.
Running: The processor is actively executing the thread code.
Waiting: A thread is in a blocked state waiting for some external processing to finish.
Sleeping: The thread is forced to sleep.
Blocked on I/O: Waiting for an I/O operation to complete. Blocked on Synchronization: Waiting to acquire a lock. Dead: The thread has finished its execution.