Hi Guys, In this post I'll share some interview questions and answers which i have faced or asked in interview panel.you can read my previous post related to JAVA QA JAVA QA PART 1 , JAVA QA PART 2 , JAVA QA PART 3 and JAVA QA PART 4 .
Q1. When a lot of changes are
required in data, which one should be a preference to be used? String or
StringBuffer?
Ans: Since
StringBuffers are dynamic in nature and we can change the values of
StringBuffer objects unlike String which is immutable, it’s always a good
choice to use StringBuffer when data is being changed too much. If we use
String in such a case, for every data change a new String object will be
created which will be an extra overhead.
Q2. What’s the purpose of using
Break in each case of Switch Statement?
Ans: Break is
used after each case (except the last one) in a switch so that code breaks
after the valid case and doesn’t flow in the proceeding cases too.
If break isn’t
used after each case, all cases after the valid case also get executed
resulting in wrong results.
Q3. How garbage collection
is done in Java?
Ans: In java,
when an object is not referenced any more, garbage collection takes place and
the object is destroyed automatically. For automatic garbage collection java
calls either System.gc() method or Runtime.gc() method.
Q4. How we can execute any code
even before main method?
Ans: If we
want to execute any statements before even creation of objects at load time of
class, we can use a static block of code in the class. Any statements inside
this static block of code will get executed once at the time of loading the
class even before creation of objects in the main method.
Q5. Can a class be a super class
and a sub-class at the same time? Give example.
Ans: If there
is a hierarchy of inheritance used, a class can be a super class for another
class and a sub-class for another one at the same time.
In the example
below, continent class is sub-class of world class and it’s super class of
country class.
public class world {
..........
}
public class continenet extends world {
............
}
public class country extends continent {
......................
}
|
Q6. How objects of a class
are created if no constructor is defined in the class?
Ans: Even if
no explicit constructor is defined in a java class, objects get created
successfully as a default constructor is implicitly used for object
creation. This constructor has no parameters.
Q7. In multi-threading how
can we ensure that a resource isn’t used by multiple threads simultaneously?
Ans: In
multi-threading, access to the resources which are shared among multiple
threads can be controlled by using the concept of synchronization. Using
synchronized keyword, we can ensure that only one thread can use shared
resource at a time and others can get control of the resource only once it has
become free from the other one using it.
Q8. Can we call the constructor
of a class more than once for an object?
Ans:
Constructor is called automatically when we create an object using new keyword.
It’s called only once for an object at the time of object creation and hence,
we can’t invoke the constructor again for an object after its creation.
Q9. There are two classes named
classA and classB. Both classes are in the same package. Can a private member
of classA can be accessed by an object of classB?
Ans: Private
members of a class aren’t accessible outside the scope of that class and any
other class even in the same package can’t access them.
Q10. Can we have two methods in a
class with the same name?
Ans: We can
define two methods in a class with the same name but with different number/type
of parameters. Which method is to get invoked will depend upon the parameters
passed.
For example in
the class below we have two print methods with same name but different
parameters. Depending upon the parameters, appropriate one will be called:
public class methodExample {
public void print() {
system.out.println("Print
method without parameters.");
}
public void print(String name) {
system.out.println("Print
method with parameter");
}
public static void main(String args[]) {
methodExample obj1= new methodExample();
obj1.print();
obj1.print("xx");
}
}
Q11. How can we make copy of a
java object?
Ans: We can
use the concept of cloning to create copy of an object. Using clone, we create
copies with the actual state of an object.
Clone() is a
method of Cloneable interface and hence, Cloneable interface needs to be
implemented for making object copies.
Q12. What’s the benefit of using
inheritance?
Ans: Key
benefit of using inheritance is reusability of code as inheritance enables
sub-classes to reuse the code of its super class.
Polymorphism (Extensibility ) is another great benefit which allow
new functionality to be introduced without effecting existing derived
classes.
Q13. What’s the default
access specifier for variables and methods of a class?
Ans: Default
access specifier for variables and method is package protected i.e variables
and class is available to any other class but in the same package,not outside
the package.
Q14. Give an example of use of
Pointers in Java class.
Ans: There are
no pointers in Java. So we can’t use concept of pointers in Java.
Q15. How can we restrict
inheritance for a class so that no class can be inherited from it?
Ans: If we
want a class not to be extended further by any class, we can use the
keyword Final with
the class name.
In the
following example, Stone class is Final and can’t be extend
public Final Class Stone {
// Class methods and Variables
}
Q16. What’s the access scope of
Protected Access specifier?
Ans: When a
method or a variable is declared with Protected access specifier, it becomes
accessible in the same class,any other class of the same package as well as a
sub-class.
Access Levels
|
||||
MODIFIER
|
CLASS
|
PACKAGE
|
SUBCLASS
|
WORLD
|
public |
Y
|
Y
|
Y
|
Y
|
protected |
Y
|
Y
|
Y
|
N
|
no modifier
|
Y
|
Y
|
N
|
N
|
private |
Y
|
N
|
N
|
N
|
Q17. What’s difference between
Stack and Queue?
Ans: Stack and
Queue both are used as placeholder for a collection of data. The primary
difference between a stack and a queue is that stack is based on Last in First
out (LIFO) principle while a queue is based on FIFO (First In First Out)
principle.
Q18. In java, how we can disallow
serialization of variables?
Ans: If we
want certain variables of a class not to be serialized, we can use the
keyword transient while
declaring them. For example, the variable trans_var below is a transient
variable and can’t be serialized:
public class transientExample {
private transient trans_var;
// rest of the code
}
Q19. How can we use
primitive data types as objects?
Ans: Primitive
data types like int can be handled as objects by the use of their respective
wrapper classes. For example, Integer is a wrapper class for primitive data
type int. We can apply different methods to a wrapper class, just like any
other object.
Q20. Which types of exceptions
are caught at compile time?
Ans: Checked
exceptions can be caught at the time of program compilation. Checked exceptions
must be handled by using try catch block in the code in order to successfully
compile the code.
Q21. Describe different states of
a thread.
Ans: A thread
in Java can be in either of the following states:
·
Ready: When a
thread is created, it’s in Ready state.
·
Running: A
thread currently being executed is in running state.
·
Waiting: A
thread waiting for another thread to free certain resources is in waiting
state.
·
Dead: A thread
which has gone dead after execution is in dead state.
Q22. Can we use a default
constructor of a class even if an explicit constructor is defined?
Ans: Java
provides a default no argument constructor if no explicit constructor is
defined in a Java class. But if an explicit constructor has been defined,
default constructor can’t be invoked and developer can use only those
constructors which are defined in the class.
Q23. Can we override a method by
using same method name and arguments but different return types?
Ans: The basic
condition of method overriding is that method name, arguments as well as return
type must be exactly same as is that of the method being overridden.
Hence using a different return type doesn’t override a method.
Q24.What will be the output of
following piece of code?
public class operatorExample {
public static void main(String args[]) {
int x=4;
system.out.println(x++);
}
}
Ans: In this
case postfix ++ operator is used which first returns the value and then
increments. Hence it’s output will be 4.
Q25. A person says that he
compiled a java class successfully without even having a main method in it? Is
it possible?
Ans: main
method is an entry point of Java class and is required for execution of the
program however; a class gets compiled successfully even if it doesn’t have a
main method. It can’t be run though.
Cheers :)
No comments:
Post a Comment