0% found this document useful (0 votes)
24 views

Java All Impotant Question

Uploaded by

[L]Jinesh pande
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Java All Impotant Question

Uploaded by

[L]Jinesh pande
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

1) What are the main features of Java?

a) Object Oriented : Java is an object oriented language where everything is


done keeping objects (data) in mind.

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.

c) Platform Independent : Java is a write once, run everywhere language.


That means Java program written on one platform can be run on any other
platforms without much difficulties.

d) Secured : Java is a highly secured language through which you can


develop virus-free and highly secured applications.

e) Robust : Java is a robust because of automatic garbage collection, better


exception and error handling mechanism, no explicit use of pointers and
better memory management system.

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.

g) Multithreaded : Java supports multithreaded programming where multiple


threads execute their task simultaneously.

h) Distributed : Java is distributed because you can develop distributed large


applications using Java concepts like RMI and EJB.

i) Dynamic : Java is a dynamic language because it supports loading of


classes on demand.

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.

k) Functional Style Programming : With the introduction of lambda


expressions, functional interfaces and Stream API in Java 8, you can also
write functional style of programming in Java.

2) What is the latest version of Java?

Java 17 or JDK 17 is the latest version of Java which is released on


September 14, 2021. (Keep checking Oracle website for latest Java
releases).

3) What are the fundamental principles of object oriented programming?

a) Inheritance
b) Abstraction

c) Polymorphism

d) Encapsulation

4) What do you mean by inheritance in Java?

Inheritance is one of the key principle of object oriented programming.


Through inheritance, one class can inherit the properties of another class.
The class from which properties are inherited is called super class and the
class to which properties are inherited is called sub class.

(Click here to see more info on Inheritance in Java)

5) What are the different types of inheritance?

There are 5 types of inheritance.

a) Single Inheritance : One class is extended by only one class.

b) Multilevel Inheritance : One class is extended by a class and that class in


turn is extended by another class thus forming a chain of inheritance.

c) Hierarchical Inheritance : One class is extended by many classes.

d) Hybrid Inheritance : It is a combination of above types of inheritance.

e) Multiple Inheritance : One class extends more than one classes. (Java
does not support multiple inheritance)

6) does Java supports multiple inheritance? If not, why?

To avoid ambiguity, complexity and confusion, Java does not supports


multiple inheritance. i.e. a class in Java can not extend more than one
classes. For example, if Class C extends Class A and Class B which have a
method with same name, then Class C will have two methods with same
name. This causes ambiguity and confusion for which method to use. To avoid
this, Java does not supports multiple inheritance.

7) If Java doesn’t supports multiple inheritance, then how do you implement


multiple inheritance in Java?

Through interfaces, we can implement multiple inheritance in Java. A class in


Java can not extend more than one classes, but a class can implement more
than one interfaces.

8) What is the parent class of all classes in Java?

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?

By declaring that member as a private. Because, private members are not


inherited to sub classes.

11) Can a class extend itself?

No, a class can not extend itself.

12) Do constructors and initializers also inherited to sub classes?

No, constructors and initializers (Static initializers and instance initializers)


are not inherited to sub classes. But, they are executed while instantiating a
sub class.

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.

14) Do static members also inherited to sub classes?

Yes, static members of a class are also inherited to sub classes.

15) What is the difference between super() and this()?

super() : It is a calling statement to super class constructor.

this() : It is a calling statement to same class constructor.

16) What are the differences between static initializers and instance
initializers?

Static initializers 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

17) How do you instantiate a class using Java 8 method references?

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.

a) Using newInstance() Method

1Class c = Class.forName("packageName.MyClass");
2
3MyClass object = (MyClass) c.newInstance();
b) Using clone() method.

1MyClass object1 = new MyClass();


2
3MyClass object2 = object1.clone();
c) Using object deserialization

1ObjectInputStream inStream = new ObjectInputStream(anInputStream );


2
3MyClass object = (MyClass) inStream.readObject();
d) Creating string and array objects

1String s = "string object";


2
3int[] a = {1, 2, 3, 4};
19) What is constructor chaining?

Constructor Chaining is a technique of calling another constructor from one


constructor. this() is used to call same class constructor where
as super() is used to call super class constructor.

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?

Constructor without arguments is called no-arg constructor. Default


constructor in Java is always a no-arg constructor.

23) What is the use of private constructors?

Private constructors are used to restrict the instantiation of a class. When a


class needs to prevent other classes from creating it’s objects then private
constructors are suitable for that. Objects to the class which has only private
constructors can be created within the class. A very good use of private
constructor is in singleton pattern. This ensures only one instance of a class
exist at any point of time.

(Click here to see more about Java Singleton Design Pattern)

24) Can we use this() and super() in a method?

No, we can’t use this() and super() in a method.

25) What is the difference between class variables and instance variables?

Class Variables 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.

Class variables can be accessed using either class name or


Instance variables can be accessed only throug
object reference.
(Click here to see more about Class Variables Vs Instance Variables)

26) What is the constructor overloading? What is the use of constructor


overloading?

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.

27) What is the difference between constructor and method?

Constructor is a special member of a class which is used to create the


objects to the class. It is special because it will have same name as class. It
will have no return type.
Method is ordinary member of a class which is used to implement some
behavior of a class. It will have it’s own name and return type.

28) What are the differences between static and non-static methods?

Static method is common to all instances of a class. Static methods are


stored in the class memory. Where as non-static methods are stored in the
object memory. Each instance of a class will have their own copy of non-
static methods.

Also Read : Classes And Objects Quiz

29) Can we overload main() method?

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.

30) Can we declare main() method as private?

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.

31) Can we declare main() method as non-static?

No, main() method must be declared as static so that JVM can call main()
method without instantiating it’s class.

32) Why main() method must be static?

Suppose, if main() is allowed to be non-static, then while calling the main


method JVM has to instantiate it’s class. While instantiating it has to call
constructor of that class. There will be an ambiguity if constructor of that
class takes an argument that what argument JVM has to pass while
instantiating class containing main() method.

33) Can we change the return type of a main() method?

No, the return type of main() method must be void only.

34) How many types of modifiers are there in Java?

Two types of modifiers are there in Java. They are,

 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.

a) private : private fields or methods or constructors are visible within the


class in which they are defined.

b) protected : Protected members of a class are visible within the package


but they can be inherited to sub classes outside the package.

c) public : public members are visible everywhere.

d) default or No-access modifiers : Members of a class which are defined with


no access modifiers are visible within the package in which they are defined.

(For more info on access modifiers, click here.)

36) What are non-access modifiers in Java?

These are the modifiers which are used to achieve the functionalities other
than the accessibility. For example,

a) static : This modifier is used to specify whether a member is a class


member or an instance member.

b) final : It is used to restrict the further modification of a class or a method


or a field. (for more on final, click here).

c) abstract : abstract class or abstract method must be enhanced or modified


further. (For more on abstract, click here).

d) synchronized : It is used to achieve thread safeness. Only one thread can


execute a method or a block which is declared as synchronized at any given
time. (for more on synchronized, click here.)

(For more info on access Vs non-access modifiers, click here)

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.

38) Can we declare a class as private?

We can’t declare an outer class as private. But, we can declare an inner


class (class as a member of another class) as private.

39) Can we declare an abstract method as private?


No, abstract methods can not be private. They must be public or protected or
default so that they can be modified further.

40) Can we use synchronized keyword with class?

No. synchronized keyword can be used either with a method or block.

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.

Also Read : Java Modifiers Quiz

42) What is type casting?

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.

(Click here for more info on type casting in Java)

43) How many types of casting are there in Java?

There are two types of casting.

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.

44) What is auto widening and explicit narrowing?

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.

45) What is auto-up casting and explicit down casting?


An object of sub class type can be automatically casted to super class type.
This is called auto-up casting. An object of super class type should be
explicitly casted to sub class type, It is called explicit down casting.

46) Can an int primitive type of data implicitly casted to Double derived type?

Yes, first int is auto-widened to double and then double is auto-boxed


to Double.

1double d = 10; //auto-widening from int to double


2
3Double D = d; //auto-boxing from double to Double
47) What is ClassCastException?

ClassCastException is an exception which occurs at run time when an object


of one type can not be casted to another type. (Click here to see more on
ClassCastException)

48) What is boxing and unboxing?

Wrapping of primitive content into corresponding wrapper class object is


called boxing. Unwrapping the wrapper class object into corresponding
primitive content is called unboxing.

49) What is the difference between auto-widening, auto-upcasting and auto-


boxing?

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.

(Click here to see more detailed article on auto-widening Vs auto-upcasting


Vs auto-boxing)
50) What is polymorphism in Java?

Polymorphism refers to any entity whether it is a method or a constructor or


an operator which takes many forms or can be used for multiple tasks.

(Click here to see more info on polymorphism in Java)

51) What is method overloading in Java?

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.

(Click here to see more on method overloading in Java)

52) What is the method signature? What are the things it consists of?

Method signature is used by the compiler to differentiate the methods.


Method signature consist of three things.

 Method name
 Number of arguments
 Types of arguments
53) How do compiler differentiate overloaded methods from duplicate
methods?

Compiler uses method signature to check whether the method is overloaded


or duplicated. Duplicate methods will have same method signatures i.e same
name, same number of arguments and same types of arguments. Overloaded
methods will also have same name but differ in number of arguments or else
in types of arguments.

54) Can we declare one overloaded method as static and another one as
non-static?

Yes. Overloaded methods can be either static or 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.

56) In MyClass , there is a method called myMethod with four different


overloaded forms. All four different forms have different visibility – private,
protected, public and default. Is myMethod properly overloaded?

Yes. Compiler checks only method signature for overloading of methods not
the visibility of methods.

57) Can overloaded methods be synchronized?

Yes. Overloaded methods can be synchronized.

58) Can we declare overloaded methods as final?

Yes, overloaded methods can be final.

59) In the below class, is constructor overloaded or is method overloaded?

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.

60) Overloading is the best example of dynamic binding. True or false?


False. Overloading is the best example for static binding. (Click here to see
what is static binding and what is dynamic binding)

61) Can overloaded method be overrided?

Yes, we can override a method which is overloaded in super class.

62) What is method overriding in Java?

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.

(Click here to see more info on method overriding in Java)

63) What are the rules to be followed while overriding a method?

There are 5 main rules you should kept in mind while overriding a method.
They are,

a) Name of the method must be same as that of super class method.

b) Return type of overridden method must be compatible with the method


being overridden. i.e if a method has primitive type as it’s return type then it
must be overridden with primitive type only and if a method has derived type
as it’s return type then it must be overridden with same type or it’s sub class
types.

c) You must not reduce the visibility of a method while overriding.

d) You must not change parameter list of a method while overriding.

e) You can not increase the scope of exceptions while overriding a method
with throws clause.

64) Can we override static methods?

No, static methods can not be overridden. If we try to override them they will
be hidden in the sub class.

65) What happens if we change the arguments of overriding method?

If we change the arguments of overriding method, then that method will be


treated as overloaded not overridden.

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?

Yes. You can change as Integer is a sub class of Number type.

68) Can we override a super class method without throws clause as a method
with throws clause in the sub class?

Yes, but only with unchecked type of exceptions.

69) Can we change an exception of a method with throws clause


from SQLException to NumberFormatException while overriding it?

Yes. Overridden method may throw SQLException or it’s sub class exception or
any unchecked type of exceptions.

70) Can we change an exception of a method with throws clause from


unchecked to checked while overriding it?

No. We can’t change an exception of a method with throws clause from


unchecked to checked.

(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.

72) Can we override private methods?

No question of overriding private methods. They are not at all inherited to sub
class.

73) Can we remove throws clause of a method while overriding it?

Yes. You can remove throws clause of a method while overriding it.

74) Is it possible to override non-static methods as static?

No. You can’t override non-static methods as static.

75) Can we change an exception of a method with throws clause from


checked to unchecked while overriding it?

Yes. We can change an exception from checked to unchecked but reverse is


not possible.
76) Can we change the number of exceptions thrown by a method with throws
clause while overriding it?

Yes, we can change. But, exceptions must be compatible with throws clause
in the super class method.

77) What is the difference between method overloading and method


overriding?

Click here to see the differences between method overloading and


overriding.

78) What is static binding and dynamic binding in Java?

Click here to see what is static binding and dynamic binding in Java.

Also Read : Java Polymorphism Quiz

79) Abstract class must have only abstract methods. True or false?

False. Abstract methods can also have concrete methods.

80) Is it compulsory for a class which is declared as abstract to have at least


one abstract method?

Not necessarily. Abstract class may or may not have abstract methods.

81) Can we use abstract keyword with constructors?

No. Constructor, Static Initialization Block, Instance Initialization Block and


variables can not be abstract.

82) Why final and abstract can not be used at a time?

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?

No, we can’t instantiate a class once it is declared as abstract even though it


does not have abstract methods.

84) Can we declare abstract methods as private? Justify your answer?


No. Abstract methods can not be private. If abstract methods are allowed to
be private, then they will not be inherited to sub class and will not get
enhanced.

85) We can’t instantiate an abstract class. Then why constructors are allowed
in abstract class?

It is because, we can’t create objects to abstract classes but we can create


objects to their sub classes. From sub class constructor, there will be an
implicit call to super class constructor. That’s why abstract classes should
have constructors. Even if you don’t write constructor for your abstract class,
compiler will keep default constructor.

86) Can we declare abstract methods as static?

No, abstract methods can not be static.

87) Can a class contain an abstract class as a member?

Yes, a class can have abstract class as it’s member.

88) Abstract classes can be nested. True or false?

True. Abstract classes can be nested i.e an abstract class can have another
abstract class as it’s member.

89) Can we declare abstract methods as synchronized?

No, abstract methods can not be declared as synchronized. But methods


which override abstract methods can be declared as synchronized.

90) Can we declare local inner class as abstract?

Yes. Local inner class can be abstract.

91) Can abstract method declaration include throws clause?

Yes. Abstract methods can be declared with throws clause.

92) Can abstract classes have interfaces in it?

Yes, abstract classes can have interfaces as their member.

Also Read : Java Abstract Classes Quiz

93) Can interfaces have constructors, static initializers and instance


initializers?

No. Interfaces can’t have constructors, static initializers and instance


initializers.
94) Can we re-assign a value to a field of interfaces?

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.

95) Can we declare an Interface with abstract keyword?

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.

98) Can interfaces become local members of the methods?

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.

99) Can an interface extend a class?

No, an interface can’t extend a class. But it can extend another interface.

100) Like classes, do interfaces also extend java.lang.Object class by default?

No. Interfaces don’t extend Object class. ( Click here for more )

101) Can interfaces have static methods?

Yes, from Java 8, interfaces can also have static methods.

102) Can an interface have a class or another interface as it’s members?

Yes. Interfaces can have classes or interfaces as their members.

103) What are marker interfaces? What is the use of marker interfaces?

Click here to see about marker interfaces in Java.

104) What are the changes made to interfaces from Java 8?

Click here to see the changes made interfaces from Java 8.


105) What are the changes made to interfaces from Java 9?

Click here to see the changes made interfaces from Java 9.

Also Read : Java Interfaces Quiz

106) How many types of nested classes are there in Java?

Java supports 2 types of nested classes. They are,

a) Static Nested Classes

b) Non-static Nested Classes OR Inner Classes

Non-static nested classes can be of 3 type,

a) Member Inner Classes

b) Local Inner Classes

c) Anonymous Inner Classes

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.

108) What are member inner classes in Java?

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.

109) Can member inner classes have static members in them?

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.

111) Can we declare local inner classes as static?


No. Local inner classes can’t be static.

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.

113) Can we declare local inner classes as private or protected or public?

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.

115) What are anonymous inner classes in Java?

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.

Also Read : Java Nested Classes Quiz

117) What is the use of final keyword in Java?

final keyword in Java is used to make any class or a method or a field as


unchangeable. You can’t extend a final class, you can’t override a final
method and you can’t change the value of a final field. final keyword is used
to achieve high level of security while coding.

(Click here for more info on final keyword)

118) What is the blank final field?

Uninitialized final field is called blank final field.

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.

121) What is the use of final class?

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.

122) Can we change the value of an interface field? If not, why?

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?

In all constructors or in any one of instance initialization blocks.

124) What are final class, final method and final variable?

final class —> can not be extended.

final method —> can not be overridden in the sub class.

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?

In any one of static initialization blocks.

126) Can we declare constructors as final?

No, constructors can not be final.

Also Read : Java Increment And Decrement Operators Quiz

127) What is ArrayStoreException in Java? When you will get this exception?

128) Can you pass the negative number as an array size?


129) Can you change the size of the array once you define it? OR Can you
insert or delete the elements after creating an array?

130) What is an anonymous array? Give example?

131) What is the difference between int[] a and int a[]?

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?

136) What are jagged arrays in Java? Give example?

137) How do you check the equality of two arrays in java? OR How do you
compare the two arrays in Java?

138) What is ArrayIndexOutOfBoundsException in Java? When it occurs?

139) How do you sort the array elements?

140) How do you find the intersection of two arrays in Java?

141) What are the different ways of declaring multidimensional arrays in


Java?

142) While creating the multidimensional arrays, can you specify an array
dimension after an empty dimension?

143) How do you search an array for a specific element?

144) What value does array elements get, if they are not initialized?

145) How do you find duplicate elements in an array?

146) What are the different ways to iterate over an array in Java?

147) How do you find second largest element in an array of integers?

148) How do you find all pairs of elements in an array whose sum is equal to a
given number?

149) How do you separate zeros from non-zeros in an integer array?


150) How do you find continuous sub array whose sum is equal to a given
number?

151) What are the drawbacks of the arrays in Java?

(Answers for questions from 127 to 151 @ Array Interview Questions


And Answers)

Also Read : Java Arrays Quiz

152) Is String a keyword in Java?

153) Is String a primitive type or derived type?

154) In how many ways you can create string objects in Java?

155) What is string constant pool?

156) What is special about string objects as compared to objects of other


derived types?

157) What do you mean by mutable and immutable objects?

158) Which is the final class in these three classes – String, StringBuffer and
StringBuilder?

159) What is the difference between 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?

164) Which class do you recommend among String, StringBuffer and


StringBuilder classes if I want mutable and thread safe objects?

165) How do you convert given string to char array?

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?

168) I am performing lots of string concatenation and string modification in


my code. which class among string, StringBuffer and StringBuilder improves
the performance of my code. Remember I also want thread safe code?

169) What is string intern?

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?

1String s1 = new String("abc");


2
3String s2 = new String("abc");
172) Can we call String class methods using string literals?

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?

176) What is the similarity and difference between StringBuffer and


StringBuilder class?

177) How do you count the number of occurrences of each character in a


string?

178) How do you remove all white spaces from a string in Java?

179) How do you find duplicate characters in a string?

180) Write a Java program to reverse a string?

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?

184) Write a code to prove that strings are immutable in Java?


185) Write a code to check whether one string is a rotation of another?

186) Write a Java program to reverse each word of a given string?

187) Print all substrings of a string in Java?

188) Print common characters between two strings in alphabetical order in


Java?

189) How find maximum occurring character in a string in Java?

190) What is difference between Java 8 StringJoiner, String.join() and


Collectors.joining()?

191) How to reverse a sentence word by word in Java?

(Answers for questions from 152 to 191 @ Java Strings Interview


Questions And Answers)

Also Read : Java Strings Quiz

192) What is multithreaded programming? Does Java supports multithreaded


programming? Explain with an example?

193) In how many ways, you can create threads in Java? What are those?
Explain with examples?

194) How many types of threads are there in Java? Explain?

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?

197) Is it possible to give a name to a thread? If yes, how do you do that?


What will be the default name of a thread if you don’t name a thread?

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?

200) What are MIN_PRIORITY, NORM_PRIORITY and MAX_PRIORITY?

201) What is the default priority of a thread? Can we change it? If yes, how?

202) What is the priority of main thread? Can we change it?

203) What is the purpose of Thread.sleep() method?


204) Can you tell which thread is going to sleep after calling
myThread.sleep(5000) in the below program? is it main thread or myThread?

205) Does the thread releases the lock it holds when it is going for sleep?

206) What is the purpose of join() method? Explain with an example?

207) What do you mean by synchronization? Explain with an example?

208) What is object lock or monitor?

209) I want only some part of the method to be synchronized, not the whole
method? How do you achieve that?

210) What is the use of synchronized blocks?

211) What is mutex?

212) Is it possible to make constructors synchronized?

213) Can we use synchronized keyword with variables?

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?

215) If a particular thread caught with exceptions while executing a


synchronized method, does executing thread releases lock or not?

216) Synchronized methods or synchronized blocks – which one do you


prefer?

217) What is deadlock in Java?

218) How do you programatically detect the deadlocked threads in Java?

219) What do you know about lock ordering and lock timeout?

220) How do you avoid the deadlock? Tell some tips?

221) How threads communicate with each other in Java?

222) What is the difference between wait() and sleep() methods in Java?

223) What is the difference between notify() and notifyAll() 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?

226) How do you check whether a thread is interrupted or not?

227) What is the difference between isInterrupted() and interrupted()


methods?

228) Can a thread interrupt itself? Is it allowed in Java?

229) Explain thread life cycle? OR Explain thread states in Java?

230) In what state deadlocked threads will be?

231) What is the difference between BLOCKED and WAITING states?

232) What is the difference between WAITING and TIMED_WAITING states?

233) Can we call start() method twice?

234) What is the difference between calling start() method and calling run()
method directly as anyhow start() method internally calls run() method?

235) How do you stop a thread?

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?

238) What is the difference between program, process and thread?

239) What are the differences between user threads and daemon threads?

240) What is the use of thread groups in Java?

241) What is the thread group of a main thread?

242) What activeCount() and activeGroupCount() methods do?

(Answers for questions from 192 to 242 @ Java Threads Interview


Questions And Answers)

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?

246) How the signature of lambda expressions are determined?

247) How the compiler determines the return type of a lambda expression?

248) Can we use non-final local variables inside a lambda expression?

249) What are the advantages of lambda expressions?

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?

252) What is the difference between Predicate and BiPredicate?

253) What is the difference between Function and BiFunction?

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?

256) When you use UnaryOperator and BinaryOperator interfaces?

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?

260) What are the different syntax of Java 8 method references?

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?

265) What are streams? Why they are introduced?

266) Can we consider streams as another type of data structure in Java?


Justify your answer?

267) What are intermediate and terminal operations?

268) What do you mean by pipeline of operations? What is the use of it?

269) “Stream operations do the iteration implicitly” what does it mean?

270) Which type of resource loading do Java 8 streams support? Lazy Loading
OR Eager Loading?

271) What are short circuiting operations?

272) What are selection operations available in Java 8 Stream API?

273) What are sorting operations available in Java 8 streams?

274) What are reducing operations? Name the reducing operations available
in Java 8 streams?

275) What are the matching operations available in Java 8 streams?

276) What are searching / finding operations available in Java 8 streams?

277) Name the mapping operations available in Java 8 streams?

278) What is the difference between map() and flatMap()?

279) What is the difference between limit() and skip()?

280) What is the difference between findFirst() and findAny()?

281) Do you know Stream.collect() method, Collector interface and Collectors


class? What is the relation between them?

282) Name any 5 methods of Collectors class and their usage?

283) What are the differences between collections and streams?

284) What is the purpose of Java 8 Optional class?

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)

304) What is an exception?

305) How the exceptions are handled in Java? OR Explain exception handling
mechanism in Java?

306) What is the difference between error and exception 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?

310) What is unreachable catch block error?

311) Explain the hierarchy of exceptions in Java?

312) What are run time exceptions in Java. Give example?

313) What is OutOfMemoryError in Java?

314) what are checked and unchecked exceptions in Java?

315) What is the difference between ClassNotFoundException and


NoClassDefFoundError in Java?

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?

318) Can we throw an exception manually? If yes, how?

319) What is Re-throwing an exception in Java?

320) What is the use of throws keyword in Java?

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?

324) What is ClassCastException in Java?

325) What is the difference between throw, throws and throwable in Java?

326) What is StackOverflowError in Java?

327) Can we override a super class method which is throwing an unchecked


exception with checked exception in the sub class?

328) What are chained exceptions 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?

331) What is the use of printStackTrace() method?

332) Give some examples to checked exceptions?

333) Give some examples to unchecked exceptions?

334) Do you know try-with-resources blocks? Why do we use them? When they
are introduced?

335) What are the benefits of try-with-resources?

336) What are the changes made to exception handling from Java 7?

337) What are the improvements made to try-with-resources in Java 9?

(Answers for questions from 304 to 337 @ Java Exception Handling


Interview Questions

338) What is the Java Collection Framework? Why it is introduced?

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?

341) Explain the class hierarchy of Java collection framework?

342) Why Map is not inherited from Collection interface although it is a part of
Java collection framework?

343) What is Iterable interface?


344) What are the characteristics of List?

345) What are the major implementations of List interface?

346) What are the characteristics of ArrayList?

347) What are the three marker interfaces implemented by ArrayList?

348) What is the default initial capacity of ArrayList?

349) What is the main drawback of ArrayList?

350) What are the differences between array and ArrayList?

351) How Vector is different from ArrayList?

352) Why it is recommended not to use Vector class in your code?

353) What are the differences between ArrayList and Vector?

354) What are the characteristics of Queue?

355) Mention the important methods of Queue?

356) How Queue differs from List?

357) Which popular collection type implements both List and Queue?

358) What are the Characteristics of LinkedList?

359) What are the differences between ArrayList and LinkedList?

360) What is the PriorityQueue?

361) What are Deque and ArrayDeque? When they are introduced in Java?

362) What are the characteristics of sets?

363) What are the major implementations of Set interface?

364) What are the differences between List and Set?

365) What are the characteristics of HashSet?

366) How HashSet works internally in Java?

367) What are the characteristics of LinkedHashSet?

368) When you prefer LinkedHashSet over HashSet?


369) How LinkedHashSet works internally in Java?

370) What is SortedSet? Give one Example?

371) What is NavigableSet? Give one example?

372) What are the characteristics of TreeSet?

373) How HashSet, LinkedHashSet and TreeSet differ from each other?

374) What are the differences between Iterator and ListIterator?

375) How Map interface is different from other three primary interfaces of
Java collection framework – List, Set and Queue?

376) What are the popular implementations of Map interface?

377) What are the characteristics of HashMap?

378) How HashMap works internally in Java?

379) What is hashing?

380) What is the initial capacity of HashMap?

381) What is the load factor of HashMap?

382) What is the threshold of an HashMap? How it is calculated?

383) What is rehashing?

384) How initial capacity and load factor affect the performance of an
HashMap?

385) What are the differences between HashSet and HashMap?

386) What are the differences between HashMap and HashTable?

387) How do you remove duplicate elements from an ArrayList in Java?

388) Which Collection type do you suggest me If I want a sorted collection of


objects with no duplicates?

389) What are the differences between Fail-Fast Iterators and Fail-Safe
Iterators?

390) How do you convert an Array to ArrayList and an ArrayList to Array?

391) What is the difference between Collection and Collections?


392) How collections are different from Java 8 streams?

393) How do you convert HashMap to ArrayList in Java?

394) What keySet(), values() and entrySet() methods do?

395) What is the difference between Iterator and Java 8 Spliterator?

396) How do you sort an ArrayList?

397) What are the differences between HashMap and ConcurrentHashMap?

398) How do you make collections read-only or unmodifiable?

399) How do you reverse an ArrayList in Java?

400) What are the differences between synchronized HashMap, HashTable


and ConcurrentHashMap?

401) How do you sort HashMap by keys?

402) How do you sort HashMap by values?

403) How do you merge two maps with same keys?

404) What do you know about Java 9 immutable collections? How they are
different from unmodifiable collections returned by the Collections wrapper
methods?

405) What do you know about Java 10 List.copyOf(), Set.copyOf() and


Map.copyOf() methods? Why they are introduced?

406) What are the differences between Enumeration And Iterator?

407) Which is of type RandomAccess – ArrayList, LinkedList, HashSet and


HashMap?

You might also like