Top 100 Java Interview Questions and Answers 1705840383
Top 100 Java Interview Questions and Answers 1705840383
Created By
KAMESH TURKAR
Top 100 Java Interview Questions & Answers
1. What is a JVM?
Answer: JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
7. What restrictions are placed on the location of a package statement within a source code file?
Answer: A package statement must appear as the first line in a source code file (eliminating blank lines
and comments).
8. What method is used to specify a container’s layout?
Answer: The setLayout() method is used to specify a container’s layout.
WWW.LINKEDIN.COM/IN/KAMESH21
10. What are the access modifiers in Java?
Answer: 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.
14. what value is a variable of the String type automatically initialized? Answer: The default value of a
String type is null.
WWW.LINKEDIN.COM/IN/KAMESH21
object while another thread is in the process of using or updating that object’s value. This often leads to
major errors.
Program
The execution part is handled by JVM to provide machine independence.
25. What modifiers may be used with an inner class that is a member of an outer class?
Answer: A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.
26. Which java. util classes and interfaces support event handling?
Answer: The Event Object class and the Event Listener interface support event processing.
WWW.LINKEDIN.COM/IN/KAMESH21
28. Is null a keyword?
Answer: No, the null is not a keyword.
31. What’s new with the stop(), suspend() and resume() methods in JDK 1.2 ?
Answer:
These methods have been deprecated in JDK 1.2.
33. What is the difference between the >> and >>> operators?
Answer: The >> operator carries the sign bit when shifting right while the >>> zero-fills bits that have
been shifted out.
Answer: this() can be used to invoke a constructor of the same class whereas super() can be used to
invoke a super class constructor.
36. What value does read Line() return when it has reached the end of a file?
Answer:The readLine() method returns null when it has reached the end of a file.
WWW.LINKEDIN.COM/IN/KAMESH21
37. What is the Java API?
Answer: The Java API is a large collection of ready-made software components that provide many
usefulcapabilities, such as graphical user interface (GUI) widgets.
Answer: Global variables are globally accessible. Java does not support globally accessible variables due
to following reasons:
The global variables breaks the referential transparency Global
variables creates collisions in namespace.
Answer: public: Any thing declared as public can be accessed from anywhere. private: Any thing declared
asprivate can’t be seen outside of its class. protected: Any thing declared as protected can be accessedby
classes in the same package and subclasses in the other packages. default modifier : Can beaccessed
only to classes in the same package.
Answer: Servlets are modules that extend request/response-oriented servers, such as java-enabled
webservers. For example, a servlet might be responsible for taking data in an HTML order-entry form and
applying the business logic used to update a company’s order database.
WWW.LINKEDIN.COM/IN/KAMESH21
44. What is clipping?
Answer: Clipping is the process of confining paint operations to a limited area or shape.
WWW.LINKEDIN.COM/IN/KAMESH21
ex: public float add(int a, int b, int c) methods can have multiple arguments. Separate with
commas when we have multiple arguments. thrown in the method are instances of their subclass.
53. What is the difference between the prefix and postfix forms of the ++ operator?
Answer: 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.
54. In how many ways we can do exception handling in java? Answer:
We can handle exceptions in either of the two ways :
1) By specifying a try-catch block where we can catch the exception.
2) Declaring a method with throws clause.
56. Can we define a package statement after the import statement in java?
Answer: We can’t define a package statement after the import statement in java. a package statement
must be the first statement in the source file. We can have commented before the package statement.
57. Define How many objects are created in the following piece of code? MyClass c1, c2, c3; c1 = new
MyClass (); c3 = new MyClass ();
Answer: Only 2 objects are created, c1 and c3. The reference c2 is only declared and not initialized.
WWW.LINKEDIN.COM/IN/KAMESH21
58. What is JSP?
Answer: JSP is a technology that returns dynamic content to the Web client using HTML, XML and
JAVAelements. JSP page looks like a HTML page but is a servlet. It contains Presentation logic
andbusiness logic of a web application.
Answer: 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.
63.What is UNICODE?
Answer: Unicode is used for internal representation of characters and strings and it uses 16 bits to
represent each other.
Answer: In Java, there are no destructors defined in the class as there is no need to do so. Java has its
owngarbage collection mechanism which does the job automatically by destroying the objects when no
longer referenced
WWW.LINKEDIN.COM/IN/KAMESH21
66. What will be the output of Round(3.7) and Ceil(3.7)?
Answer: Static methods can’t be overridden in any class while any methods in an interface are by default
abstract and are supposed to be implemented in the classes being implementing the interface. So it
makes no sense to have static methods in an interface in Java.
72. When do we use synchronized blocks and advantages of using synchronized blocks?
Answer: If very few lines of code require synchronization then it is recommended to use synchronized
blocks. The main advantage of synchronized blocks over synchronized methods is it reduces the waiting
time of threads and improves performance of the system.
73. What is the difference between access specifiers and access modifiers in java?
Answer: In C++ we have access specifiers as public, private, protected and default and access modifiers
as static, final. But there is no such division of access specifiers and access modifiers in java. In Java, we
WWW.LINKEDIN.COM/IN/KAMESH21
have access to modifiers and nonaccess modifiers. Access Modifiers: public, private, protected, default
Non Access Modifiers: abstract, final, strip.
WWW.LINKEDIN.COM/IN/KAMESH21
77. Can we create a constructor in abstract class?
Answer: We can create a constructor in the abstract class, it doesn’t give any compilation error. But
when we cannot instantiate class there is no use in creating a constructor for abstract class.
78. String and StringBuffer both represent String objects. Can we compare String andStringBuffer in
Java?
Answer: Although String and StringBuffer both represent String objects, we can’t compare them with
each other and if we try to compare them, we get an error.
82. Can we cast any other type to Boolean Type with type casting?
Answer: No, we can neither cast any other primitive type to Boolean data type nor can cast Boolean data
typeto any other primitive data type.
WWW.LINKEDIN.COM/IN/KAMESH21
84. Explain the importance of finally block in java?
Answer: Finally block is used for cleaning up of resources such as closing connections, sockets, etc. if try
block executes with no exceptions then finally is called after try block without executing catch block. If
there is an exception thrown in try block finally block executes immediately after the catch block. If an
exception is thrown, finally block will be executed even if the no catch block handles the exception.
85. Can we catch more than one exception in a single catch block?
Answer: From Java 7, we can catch more than one exception with a single catch block. This type of
handling reduces the code duplication.
Note: When we catch more than one exception in a single catch block, catch parameter is implicitly final.
We cannot assign any value to catch parameter.
Ex : catch(ArrayIndexOutOfBoundsException || ArithmeticException e)
In the above example, e is final we cannot assign any value or modify e in the catch statement.
87. State some situations where exceptions may arise in java? Answer:
1) Accessing an element that does not exist in the array.
2) Invalid conversion of number to string and string to a number.
(NumberFormatException)
3) The invalid casting of class
(Class cast Exception)
4) Trying to create an object for interface or abstract class
(Instantiation Exception)
WWW.LINKEDIN.COM/IN/KAMESH21
Answer: Error is the subclass of Throwable class in java. When errors are caused by our program we call
that as Exception, but some times exceptions are caused due to some environmental issues such as
running out of memory. In such cases, we can’t handle the exceptions. Exceptions which cannot be
recovered are called as errors in java. Ex: Out of memory issues.
Answer: Constructors must have the same name as the class and can not return a value. They are only
called once while regular methods could be called many times.
Answer: 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.
Answer: Private constructor is used if you do not want other classes to instantiate the object and to
prevent subclassing.
Answer: Type casting means treating a variable of one type as though it is another type.
95. What is the difference between the >> and >>> operators?
Answer: The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been
shifted out.
WWW.LINKEDIN.COM/IN/KAMESH21
96. What is the difference between inner class and nested class?
Answer: 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.
97. Can you call one constructor from another if a class has multiple constructors?
Answer: We can pass them around as method parameters where a method expects an object. It also
provides utility methods.
WWW.LINKEDIN.COM/IN/KAMESH21