300+ (UPDATED) JAVA Interview Questions and Answers 2023
300+ (UPDATED) JAVA Interview Questions and Answers 2023
#1 Softwar
my.newtonschool.co
developme
OPEN
my.newtonschool.co
Java is a object-oriented programming language originally developed by Sun
Micro systems and released in 1995. Java runs on a variety of platforms, such
as Windows, Mac OS, and the various versions of UNIX.
Java runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX/Linux like HP-Unix, Sun Solaris, Red hat
Linux, Ubuntu, Cent OS, etc.
A singleton class in java can have only one instance and hence all its
methods and variables belong to just one instance. Singleton class
concept is useful for the situations when there is a need to limit the
number of objects for a class.
The best example of singleton usage scenario is when there is a limit of
having only one connection to a database due to some driver limitations
or because of any licensing issues.
There are 3 access modifiers. Public, protected and private, and the default
one if no identifier is specified is called friendly, but programmer cannot
specify the friendly identifier explicitly.
7. What is are packages?
know more
Inheritance is the process of inheriting all the features from a class. The
advantages of inheritance are reusability of code and accessibility of variables
and methods of the super class by subclasses.
A super class is a class that is inherited whereas sub class is a class that does
the inheriting.
JAVA Interview
Questions
When two threads are waiting each other and can’t precede the program is
said to be deadlock.
Applet is a dynamic and interactive program that runs inside a web page
displayed by a java capable browser
JDBC is a set of Java API for executing SQL statements. This API consists of a
set of classes and interfaces to enable programs to write pure Java Database
applications.
Stored procedure is a group of SQL statements that forms a logical unit and
performs a particular task. Stored Procedures are used to encapsulate a set of
operations or queries to execute on database. Stored procedures can be
compiled and executed with different parameters and results and may have
any combination of input/output parameters.
Global variables are globally accessible. Java does not support globally
accessible variables due to following reasons:
Bin contains all tools such as javac, appletviewer, awt tool, etc., whereas lib
contains API and all packages.
Method overloading: When a method in a class having the same method name
with different arguments is said to be method overloading. Method overriding
: When a method in a class having the same method name with same
arguments is said to be method overriding.
this() can be used to invoke a constructor of the same class whereas super()
can be used to invoke a super class constructor.
URL stands for Uniform Resource Locator and it points to resource files on
the Internet. URL has four components: http://www. address.
com:80/index.html, where http – protocol name, address – IP address or host
name, 80 – port number and index.html – file path.
RMI architecture consists of four layers and each layer performs specific
functions: a) Application layer – contains the actual object definition. b)
Proxy layer – consists of stub and skeleton. c) Remote Reference layer – gets
the stream of bytes from the transport layer and sends it to the proxy layer. d)
Transportation layer – responsible for handling the actual machine-to-
machine communication.
Checked exception are those which the Java compiler forces you to catch. e.g.
IOException are checked Exceptions.
Runtime exceptions are those exceptions that are thrown at runtime because
of either wrong input data or because of wrong business logic etc. These are
not checked by the compiler at compile time.
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.
Under preemptive scheduling, the highest priority task executes until it enters
the waiting or dead states or a higher priority task comes into existence.
Under time slicing, a task executes for a predefined slice of time and then
reenters the pool of ready tasks. The scheduler then determines which task
should execute next, based on priority and other factors.
Really, just a very fast compiler… In this incarnation, pretty much a one-pass
compiler — no offline computations. So you can’t look at the whole method,
rank the expressions according to which ones are re-used the most, and then
generate code. In theory terms, it’s an on-line problem.
41. What is nested class?
public: Any thing declared as public can be accessed from anywhere. private:
Any thing declared as private can’t be seen outside of its class. protected: Any
thing declared as protected can be accessed by classes in the same package
and subclasses in the other packages. default modifier : Can be accessed only
to classes in the same package.
A memory leak is where an unreferenced object that will never be used again
still hangs around in memory and doesnt get garbage collected.
50. What is the difference between the prefix and postfix forms of
the ++ operator?
The prefix form performs the increment operation and returns the value of
the increment operation. The postfix form returns the current value all of the
expression and then performs the increment operation on that value.
Static class loading: The process of loading a class using new operator is called
static class loading. Dynamic class loading: The process of loading a class at
runtime is called dynamic class loading.
Dynamic class loading can be done by using
Class.forName(….).newInstance().
JSP is a technology that returns dynamic content to the Web client using
HTML, XML and JAVA elements. JSP page looks like a HTML page but is a
servlet. It contains Presentation logic and business logic of a web application.
Apache server is a standalone server that is used to test servlets and create
JSP pages. It is free and open source that is integrated in the Apache web
server. It is fast, reliable server to configure the applications but it is hard to
install. It is a servlet container that includes tools to configure and manage the
server to run the applications. It can also be configured by editing XML
configuration files.
Pragma is used inside the servlets in the header with a certain value. The
value is of no-cache that tells that a servlets is acting as a proxy and it has to
forward request. Pragma directives allow the compiler to use machine and
operating system features while keeping the overall functionality with the
Java language. These are different for different compilers.
58. Briefly explain daemon thread.
It is a daemon thread.
64. Define How many objects are created in the following piece of
code?
65.What is UNICODE?
Constructor in Java must have same name as the class name and if the name
is different, it doesn’t act as a constructor and compiler thinks of it as a
normal method.
In Java, there is not goto keyword and java doesn’t support this feature of
going to a particular labeled line.
In java, a thread which is in dead state can’t be started again. There is no way
to restart a dead thread.
JDK is development Kit of Java and is required for development only and to
run a Java program on a machine, JDK isn’t required. Only JRE is required.
Polymorphism
Yes, we can do this by use of native methods. In case of native method based
development, we define public static methods in our Java class without its
implementation and then implementation is done in another language like C
separately.
No a variable can’t be static as well as local at the same time. Defining a local
variable as static gives compilation error.
No, we can’t change the value of any variable of an interface in the
implementing class as all variables defined in the interface are by default
public, static and Final and final variables are like constants which can’t be
changed later.
So, garbage collection helps in reducing the chances of a program going out of
memory but it doesn’t ensure that.
79. Can we have any other return type than void for main method?
No, Java class main method can have only void return type for the program to
get successfully executed.
80. I want to re-reach and use an object once it has been garbage
collected. Define How it’s possible?
Yes an Interface can inherit another Interface, for that matter an Interface can
extend more than one Interface.
In java, each object when created gets a memory space from a heap. When an
object is destroyed by a garbage collector, the space allocated to it from the
heap is re-allocated to the heap and becomes available for any new objects.
86. Define How can we find the actual size of an object on the
heap?
In java, there is no way to find out the exact size of an object on the heap.
Memory isn’t allocated before creation of objects. Since for both classes, there
are no objects created so no memory is allocated on heap for any class.
If a class has multiple constructors, it’s possible to call one constructor from
the body of another one using this().
For example, in below code we have defined an anonymous class in one line of
code:
public java.util.Enumeration testMethod()
@Override
return false;
@Override
return null;
Arrays are static and once we have specified its size, we can’t change it. If we
want to use such collections where we may require a change of size ( no of
items), we should prefer vector over array.
If there is main method in more than one classes in a java application, it won’t
cause any issue as entry point for any application will be a specific class and
code will start from the main method of that particular class only.
93. I want to persist data of objects for later use. What’s the best
approach to do so?
The best way to persist data for future use is to use the concept of
serialization.
In Java, if we define a new class inside a particular block, it’s called a local
class. Such a class has local scope and isn’t usable outside the block where its
defined.
Java provides a Collection API which provides many useful methods which
can be applied on a set of objects. Some of the important classes provided by
Collection API include ArrayList, HashMap, TreeSet and TreeMap.
97. Can we cast any other type to Boolean Type with type casting?
No, we can neither cast any other primitive type to Boolean data type nor can
cast Boolean data type to any other primitive data type.
99. Define How does a try statement determine which catch clause
should be used to handle an exception?
When an exception is thrown within the body of a try statement, the catch
clauses of the try statement are examined in the order in which they appear.
The first catch clause that is capable of handling the exception is executed.
The remaining catch clauses are ignored.
100. What will be the default values of all the elements of an array
defined as an instance variable?
If the array is an array of primitive types, then all the elements of the array
will be initialized to the default value corresponding to that primitive type.
106. Can you write a Java class that could be used both as an
applet as well as an application?
Yes, just add a main() method to the applet.
109. When is the ArrayStoreException thrown?
When copying elements between different arrays, if the source or destination
arguments are not arrays or their types are not compatible, an
ArrayStoreException will be thrown.
110. Can you call one constructor from another if a class has
multiple constructors?
Yes, use this() syntax.
111. What’s the difference between the methods sleep() and wait()?
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.
116. 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.
139. When a thread is created and started, what is its initial state?
A thread is in the ready state as initial state after it has been created and
started.
152. What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills
bits that have been shifted out.
Initialization
Starting
Stopping
Destroying
Painting
175. Which class represents the socket that both the client and
server use to communicate with each other?
java.net.Socket class represents the socket that both the client and server use
to communicate with each other.
181. If System.exit (0); is written at the end of the try block, will
the finally block still execute?
No in this case the finally block will not execute because when you say
System.exit (0); the control immediately goes out of the program, and thus
finally never executes.
197. What are order of precedence and associativity and how are
they used?
Order of precedence determines the order in which operators are evaluated in
expressions. Associatity determines whether an expression is evaluated left-
to-right or right-to-left.
199. What is the difference between inner class and nested class?
When a class is defined within a scope of another class, then it becomes inner
class. If the access modifier of the inner class is static, then it becomes nested
class.
Runnable state
Running state
Blocked state
Dead state
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Post Comment
Engineering 2023 , FAQs Interview Questions , Theme by Engineering|| Privacy Policy|| Terms and Conditions||
ABOUT US|| Contact US||
Engineering interview questions,Mcqs,Objective Questions,Class Lecture Notes,Seminor topics,Lab Viva Pdf PPT
Doc Book free download. Most Asked Technical Basic CIVIL | Mechanical | CSE | EEE | ECE | IT | Chemical | Medical
MBBS Jobs Online Quiz Tests for Freshers Experienced .