Java All Impotant Question
Java All Impotant Question
b) Simple : Java is very easy to learn and follow. It’s syntax are very easy.
Any programmer who has some basic knowledge about any object oriented
languages like C++ can easily follow Java.
f) Portable : Java is portable because you can run Java bytecode on any
hardware which has compliant JVM which converts bytecode according to
that particular hardware.
j) Extensible : You can develop new classes using existing interfaces, you
can declare new methods to existing classes or you can develop new sub
classes to existing classes. That is all because of extensible nature of Java.
a) Inheritance
b) Abstraction
c) Polymorphism
d) Encapsulation
e) Multiple Inheritance : One class extends more than one classes. (Java
does not support multiple inheritance)
java.lang.Object class
9) You know that all classes in Java are inherited from java.lang.Object
class. Do interfaces also inherited from java.lang.Object class?
No, only classes in Java are inherited from java.lang.Object class. Interfaces
in Java are not inherited from java.lang.Object class. But, classes which
implement interfaces are inherited from java.lang.Object class.
10) How do you restrict a member of a class from inheriting to it’s sub
classes?
13) What happens if both, super class and sub class, have a field with same
name?
Super class field will be hidden in the sub class. You can access hidden super
class field in sub class using super keyword.
16) What are the differences between static initializers and instance
initializers?
Static initializers are executed when a class is loaded into the Instance initializers are executed each time
memory. class is created.
Static initializers are mainly used to initialize static members Instance initializers are used to initialize no
or class members of the class. instance members of a class.
Also Read : Java Inheritance Quiz
ClassName::new
18) Can you create an object without using new operator in Java?
Yes, We can create an object without using new operator. There are some
other ways to create objects other than using new operator. But, 95% of
object creation in Java is done through new operator only.
1Class c = Class.forName("packageName.MyClass");
2
3MyClass object = (MyClass) c.newInstance();
b) Using clone() method.
20) Can we call sub class constructor from a super class constructor?
No. There is no way in Java to call sub class constructor from a super class
constructor.
21) Do constructors have return type? If no, what happens if you keep return
type for a constructor?
No, constructors in Java don’t have return type. If you keep return type for a
constructor, it will be treated as a normal method and also compiler gives a
warning saying that method has a constructor name.
22) What is no-arg constructor?
25) What is the difference between class variables and instance variables?
Class variables are declared with keyword static. Instance variables are declared without static k
Class variables are common to all instances of a class. Instance variables are not shared between the
These variables are shared between the objects of a class. Each instance will have their own copy of inst
As class variables are common to all objects of a class, As each object will have its own copy of insta
changes made to these variables through one object will changes made to these variables through one o
reflect in another. in another object.
A class can have any number of constructors. These constructors will have
different list of arguments. It is called constructor overloading. Constructor
overloading provides different ways to instantiate a class.
28) What are the differences between static and non-static methods?
Yes, we can overload main() method. A Java class can have any number of
main() methods. But to run the Java class, class should have main() method
with signature as public static void main(String[] args) . If you do any
modification to this signature, compilation will be successful. But, you can’t
run the Java program. You will get run time error as main method not found.
No, main() method must be public. You can’t define main() method as private
or protected or with no access modifier. This is because to make the main()
method accessible to JVM.
No, main() method must be declared as static so that JVM can call main()
method without instantiating it’s class.
Access Modifiers
Non-access Modifiers
35) What are access modifiers in Java?
These are the modifiers which are used to restrict the visibility of a class or a
field or a method or a constructor. Java supports 4 access modifiers.
These are the modifiers which are used to achieve the functionalities other
than the accessibility. For example,
37) Can a method or a class be final and abstract at the same time?
No, it is not possible. A class or a method can not be final and abstract at the
same time. final and abstract are totally opposite in nature. final class or final
method must not be modified further where as abstract class or abstract
method must be modified further.
41) A class can not be declared with synchronized keyword. Then, why we
call classes like Vector, StringBuffer are synchronized classes?
Any classes which have only synchronized methods and blocks are treated as
synchronized classes. Classes like Vector, StringBuffer have only
synchronized methods. That’s why they are called as synchronized classes.
When the data is converted from one data type to another data type, then it
is called type casting. Type casting is nothing but changing the type of the
data. Using type casting, only type of the data is changed but not the data
itself.
a) Primitive Casting : When the data is casted from one primitive type ( like
int, float, double etc… ) to another primitive type, then it is called Primitive
Casting.
b) Derived Casting : When the data is casted from one derived type to another
derived type, then it is called derived casting.
The data is implicitly casted from small sized primitive type to big sized
primitive type. This is called auto-widening. i.e The data is automatically
casted from byte to short, short to int, int to long, long to float and float to
double..
You have to explicitly cast the data from big sized primitive type to small
sized primitive type. i.e you have to explicitly convert the data from double
to float, float to long, long to int, int to short and short to byte. This is called
explicit narrowing.
46) Can an int primitive type of data implicitly casted to Double derived type?
Auto-widening occurs when small sized primitive type is casted to big sized
primitive type. Auto-upcasting occurs when sub class type is casted to super
class type. Auto-boxing occurs when primitive type is casted to
corresponding wrapper class.
When a class has more than one method with same name but different
parameters, then we call those methods are overloaded. Overloaded
methods will have same name but different number of arguments or different
types of arguments.
52) What is the method signature? What are the things it consists of?
Method name
Number of arguments
Types of arguments
53) How do compiler differentiate overloaded methods from duplicate
methods?
54) Can we declare one overloaded method as static and another one as
non-static?
55) Is it possible to have two methods in a class with same method signature
but different return types?
No, compiler will give duplicate method error. Compiler checks only method
signature for duplication not the return types. If two methods have same
method signature, straight away it gives compile time error.
Yes. Compiler checks only method signature for overloading of methods not
the visibility of methods.
1public class A
2{
3 public A()
4 {
5 //-----> (1)
6 }
7
8 void A()
9 {
10 //-----> (2)
11 }
12}
None of them. It is neither constructor overloaded nor method overloaded.
First one is a constructor and second one is a method.
Modifying a super class method in the sub class is called method overriding.
Using method overriding, we can change super class method according to
the requirements of sub class.
There are 5 main rules you should kept in mind while overriding a method.
They are,
e) You can not increase the scope of exceptions while overriding a method
with throws clause.
No, static methods can not be overridden. If we try to override them they will
be hidden in the sub class.
66) Can we override protected method of super class as public method in the
sub class?
Yes. You can increase the visibility of overriding methods but can’t reduce it.
67) Can we change the return type of overriding method from Number type
to Integer type?
68) Can we override a super class method without throws clause as a method
with throws clause in the sub class?
Yes. Overridden method may throw SQLException or it’s sub class exception or
any unchecked type of exceptions.
(Click here to see more about method overriding with throws clause)
71) How do you refer super class version of overridden method in the sub
class?
Using super keyword, we can refer super class version of overridden method
in the sub class.
No question of overriding private methods. They are not at all inherited to sub
class.
Yes. You can remove throws clause of a method while overriding it.
Yes, we can change. But, exceptions must be compatible with throws clause
in the super class method.
Click here to see what is static binding and dynamic binding in Java.
79) Abstract class must have only abstract methods. True or false?
Not necessarily. Abstract class may or may not have abstract methods.
Because, final and abstract are totally opposite in nature. A final class or
method can not be modified further where as abstract class or method must
be modified further. final keyword is used to denote that a class or method
does not need further improvements. abstract keyword is used to denote that
a class or method needs further improvements.
83) Can we instantiate a class which does not have even a single abstract
method but declared as abstract?
85) We can’t instantiate an abstract class. Then why constructors are allowed
in abstract class?
True. Abstract classes can be nested i.e an abstract class can have another
abstract class as it’s member.
No. The fields of interfaces are static and final by default. They are just like
constants. You can’t change their value once they got.
Yes, we can declare an interface with abstract keyword. But, there is no need
to write like that. All interfaces in Java are abstract by default.
96) For every Interface in java, .class file will be generated after compilation.
True or false?
True. .class file will be generated for every interface after compilation.
97) Can we override an interface method with visibility other than public?
No. While overriding any interface methods, we should use public only.
Because, all interface methods are public by default and you should not
reduce the visibility while overriding them.
No. You can’t define interfaces as local members of methods like local inner
classes. They can be a part of top level class or interface.
No, an interface can’t extend a class. But it can extend another interface.
No. Interfaces don’t extend Object class. ( Click here for more )
103) What are marker interfaces? What is the use of marker interfaces?
107) Can we access non-static members of outer class inside a static nested
class?
No, we can’t access non-static members of outer class inside a static nested
class. We can access only static members of outer class inside a static
nested class.
Member inner classes are the classes which are declared as non-static
members of another class. Member inner classes can be accessed only by
instantiating the outer class.
No, member inner classes can’t have static members in them. They can have
only non-static members. But, exception being the static and final field. i.e
member inner class can have static and final field, but it must be initialized at
the time of declaration only.
110) Can we access all the members of outer class inside a member inner
class?
Yes, we can access all the members, both static and non-static, of outer
class inside a member inner class.
112) Can we use local inner classes outside the method or block in which
they are defined?
No. Local inner classes are local to method or block in which they are
defined. We can’t use them outside the method or block in which they are
defined.
No. Local inner classes can’t be declared with access modifiers.They can’t be
private or protected or public.
114) What is the condition to use local variables inside a local inner class?
The condition is that local variables must be final. We can’t use non-final local
variables inside a local inner class.
Anonymous inner classes are the inner classes without a name. You can
instantiate an anonymous inner class only once. Click here for more info on
anonymous inner classes.
116) What is the main difference between static and non-static nested
classes?
The main difference between static and non-static nested classes is that you
need not to instantiate the outer class to access static nested classes. But,
to access non-static nested classes, you have to instantiate the outer class.
119) Can we change the state of an object to which a final reference variable
is pointing?
Yes, we can change the state of an object to which a final reference variable
is pointing, but we can’t re-assign a new object to this final reference
variable.
120) What is the main difference between abstract methods and final
methods?
Abstract methods must be overridden in the sub classes and final methods
are not at all eligible for overriding.
A final class is very useful when you want a high level of security in your
application. If you don’t want inheritance of a particular class, due to security
reasons, then you can declare that class as a final.
No, we can’t change the value of an interface field. Because interface fields,
by default, are final and static. They remain constant for whole execution of a
program.
123) Where all we can initialize a final non-static global variable if it is not
initialized at the time of declaration?
124) What are final class, final method and final variable?
final variable —> can not change it’s value once it is initialized.
125) Where all we can initialize a final static global variable if it is not
initialized at the time of declaration?
127) What is ArrayStoreException in Java? When you will get this exception?
132) There are two array objects of int type. one is containing 100 elements
and another one is containing 10 elements. Can you assign array of 100
elements to an array of 10 elements?
133) “int a[] = new int[3]{1, 2, 3}” – is it a legal way of defining the arrays in
Java?
134) What are the differences between Array and ArrayList in Java?
135) What are the different ways of copying an array into another array?
137) How do you check the equality of two arrays in java? OR How do you
compare the two arrays in Java?
142) While creating the multidimensional arrays, can you specify an array
dimension after an empty dimension?
144) What value does array elements get, if they are not initialized?
146) What are the different ways to iterate over an array in Java?
148) How do you find all pairs of elements in an array whose sum is equal to a
given number?
154) In how many ways you can create string objects in Java?
158) Which is the final class in these three classes – String, StringBuffer and
StringBuilder?
160) Why StringBuffer and StringBuilder classes are introduced in Java when
there already exist String class to represent the set of characters?
161) How many objects will be created in the following code and where they
will be stored in the memory?
1String s1 = "abc";
2
3String s2 = "abc";
162) How do you create mutable string objects?
163) Which one will you prefer among “==” and equals() method to compare
two string objects?
166) How many objects will be created in the following code and where they
will be stored?
1String s1 = new String("abc");
2
3String s2 = "abc";
167) Where exactly string constant pool is located in the memory?
170) What is the main difference between Java strings and C, C++ strings?
171) How many objects will be created in the following code and where they
will be stored?
173) do you have any idea why strings have been made immutable in Java?
174) What do you think about string constant pool? Why they have provided
this pool as we can store string objects in the heap memory itself?
175) What is the similarity and difference between String and StringBuffer
class?
178) How do you remove all white spaces from a string in Java?
181) Write a Java program to check whether two strings are anagram or not?
182) Write a Java program to reverse a given string with preserving the
position of spaces?
183) How do you convert string to integer and integer to string in Java?
193) In how many ways, you can create threads in Java? What are those?
Explain with examples?
195) What is the default daemon status of a thread? How do you check it?
196) Can you convert user tread into daemon thread and vice-versa? Explain
with example?
198) Can we change the name of the main thread? If yes, How?
199) Do two threads can have same name? If yes then how do you identify the
threads having the same name?
201) What is the default priority of a thread? Can we change it? If yes, how?
205) Does the thread releases the lock it holds when it is going for sleep?
209) I want only some part of the method to be synchronized, not the whole
method? How do you achieve that?
214) As you know that synchronized static methods need class level lock and
synchronized non-static methods need object level lock. Is it possible to run
these two methods simultaneously?
219) What do you know about lock ordering and lock timeout?
222) What is the difference between wait() and sleep() methods in Java?
224) Though they are used for inter thread communication, why wait(), notify()
and notifyAll() methods are included in java.lang.Object class not in
java.lang.Thread class?
225) What do you know about interrupt() method? Why it is used?
234) What is the difference between calling start() method and calling run()
method directly as anyhow start() method internally calls run() method?
236) Suppose there are two threads T1 and T2 executing their task
concurrently. If an exception occurred in T1, will it effect execution of T2 or it
will execute normally?
237) Which one is the better way to implement threads in Java? Is it using
Thread class or using Runnable interface?
239) What are the differences between user threads and daemon threads?
243) After Java 8, what do you think about Java? Is it still an object oriented
language or it has turned into functional programming language?
244) What are the three main features of Java 8 which make Java as a
functional programming language?
245) What are lambda expressions? How this feature has changed the way
you write code in Java? Explain with some before Java 8 and after Java 8
examples?
247) How the compiler determines the return type of a lambda expression?
250) What are the functional interfaces? Do they exist before Java 8 or they
are the whole new features introduced in Java 8?
251) What are the new functional interfaces introduced in Java 8? In which
package they have kept in?
254) Which functional interface do you use if you want to perform some
operations on an object and returns nothing?
255) Which functional interface is the best suitable for an operation which
creates new objects?
257) Along with functional interfaces which support object types, Java 8 has
introduced functional interfaces which support primitive types. For example,
Consumer for object types and intConsumer, LongConsumer, DoubleConsumer
for primitive types. What do you think, is it necessary to introduce separate
interfaces for primitive types and object types?
258) How functional interfaces and lambda expressions are inter related?
259) What are the method references? What is the use of them?
261) What are the major changes made to interfaces from Java 8?
262) What are default methods of an interface? Why they are introduced?
263) As interfaces can also have concrete methods from Java 8, how do you
solve diamond problem i.e conflict of classes inhering multiple methods with
same signature?
264) Why static methods are introduced to interfaces from Java 8?
268) What do you mean by pipeline of operations? What is the use of it?
270) Which type of resource loading do Java 8 streams support? Lazy Loading
OR Eager Loading?
274) What are reducing operations? Name the reducing operations available
in Java 8 streams?
285) What is the difference between Java 8 Spliterator and the iterators
available before Java 8?
286) What is the difference between Java 8 StringJoiner, String.join() and
Collectors.joining()?
287) Name three important classes of Java 8 Date and Time API?
288) How do you get current date and time using Java 8 features?
289) Given a list of students, write a Java 8 code to partition the students
who got above 60% from those who didn’t?
290) Given a list of students, write a Java 8 code to get the names of top 3
performing students?
291) Given a list of students, how do you get the name and percentage of
each student?
292) Given a list of students, how do you get the subjects offered in the
college?
293) Given a list of students, write a Java 8 code to get highest, lowest and
average percentage of students?
294) How do you get total number of students from the given list of students?
295) How do you get the students grouped by subject from the given list of
students?
296) Given a list of employees, write a Java 8 code to count the number of
employees in each department?
297) Given a list of employees, find out the average salary of male and female
employees?
298) Write a Java 8 code to get the details of highest paid employee in the
organization from the given list of employees?
299) Write the Java 8 code to get the average age of each department in an
organization?
300) Given a list of employees, how do you find out who is the senior most
employee in the organization?
301) Given a list of employees, get the details of the most youngest employee
in the organization?
302) How do you get the number of employees in each department if you have
given a list of employees?
303) Given a list of employees, find out the number of male and female
employees in the organization?
(Answers for questions from 243 to 303 @ Java 8 Interview
Questions And Answers)
305) How the exceptions are handled in Java? OR Explain exception handling
mechanism in Java?
307) Can we keep other statements in between try, catch and finally blocks?
308) Can we write only try block without catch and finally blocks?
309) There are three statements in a try block – statement1, statement2 and
statement3. After that there is a catch block to catch the exceptions
occurred in the try block. Assume that exception has occurred in statement2.
Does statement3 get executed or not?
316) Can we keep the statements after finally block If the control is returning
from the finally block itself?
317) Does finally block get executed If either try or catch blocks are returning
the control?
321) Why it is always recommended that clean up operations like closing the
DB resources to keep inside a finally block?
322) What is the difference between final, finally and finalize in Java?
323) How do you create customized exceptions in Java?
325) What is the difference between throw, throws and throwable in Java?
329) Which class is the super class for all types of errors and exceptions in
Java?
330) What are the legal combinations of try, catch and finally blocks?
334) Do you know try-with-resources blocks? Why do we use them? When they
are introduced?
336) What are the changes made to exception handling from Java 7?
339) What is the root level interface of the Java collection framework?
340) What are the four main core interfaces of the Java collection framework?
342) Why Map is not inherited from Collection interface although it is a part of
Java collection framework?
357) Which popular collection type implements both List and Queue?
361) What are Deque and ArrayDeque? When they are introduced in Java?
373) How HashSet, LinkedHashSet and TreeSet differ from each other?
375) How Map interface is different from other three primary interfaces of
Java collection framework – List, Set and Queue?
384) How initial capacity and load factor affect the performance of an
HashMap?
389) What are the differences between Fail-Fast Iterators and Fail-Safe
Iterators?
404) What do you know about Java 9 immutable collections? How they are
different from unmodifiable collections returned by the Collections wrapper
methods?