NoClassDefFoundError vs ClassNotFoundException
Question: How do u create a thread.
Question: Which one is preferred?
Question: Explain transient variable?
NoClassDefFoundError
|
ClassNotFoundException
|
source was successfully compiled, but
at runtime, the required class files were not found.This may be something
that can happen in the distribution or production of JAR files, where not all
the required class files were included.
|
trying to make reflective
calls to classes at runtime, but the classes the program is trying to call is
does not exist.
|
It is an error
|
It is an exception
|
Question: How many types of exceptions are there in java?
Answer: Types of exceptions are:
Answer: Types of exceptions are:
1) Checked exception.
-This is also called compile time exception
-Compiler forces to catch the exception.
2) Unchecked exception:
- This is also called run time exception.
-Ex. NullPointerException
What is annotation in Java?
Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program.
Annotations have following number of uses:
*Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
*Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
*Runtime processing — Some annotations are available to be examined at runtime.
Annotations Used by the Compiler
@Deprecated
@Override
@SuppressWarnings
What are performance issue with serialization?
1.Serialization is a recursive algorithm. Starting from a single object, all the objects that can be reached from that object by following instance variables, are also serialized.
2.Both serializing and deserializing require the serialization mechanism to discover information about the instance it is serializing using reflection to discover all the field values.
3.If you don't explicitelly set a "serialVersionUID“ class attribute, the serialization mechanism has to compute it. This involves going through all the fields and methods to generate a hash. This procedure can be quite slow.
4.Using the default serialization mechanism, all the serializing class description information is included in the stream, such as :
The description of all the serializable superclasses
The description of the class itself
The instance data associated with the specific instance of the class
NOTE: serialversionUID is static but even it gets serialized.
What is annotation in Java?
Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program.
Annotations have following number of uses:
*Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
*Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
*Runtime processing — Some annotations are available to be examined at runtime.
Annotations Used by the Compiler
@Deprecated
@Override
@SuppressWarnings
What are performance issue with serialization?
1.Serialization is a recursive algorithm. Starting from a single object, all the objects that can be reached from that object by following instance variables, are also serialized.
2.Both serializing and deserializing require the serialization mechanism to discover information about the instance it is serializing using reflection to discover all the field values.
3.If you don't explicitelly set a "serialVersionUID“ class attribute, the serialization mechanism has to compute it. This involves going through all the fields and methods to generate a hash. This procedure can be quite slow.
4.Using the default serialization mechanism, all the serializing class description information is included in the stream, such as :
The description of all the serializable superclasses
The description of the class itself
The instance data associated with the specific instance of the class
NOTE: serialversionUID is static but even it gets serialized.
What are Comparable and Comparator interface?
Comparable example:
public class Emp implements Comparable<Emp>{
private String name;
private int age;
public String toString() {
return name+age;
}
Emp(String name,int age){
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Emp e1 = new Emp("a",10);
Emp e2 = new Emp("b",20);
Emp e3 = new Emp("c", 12);
List<Emp> list = new ArrayList<Emp>();
list.add(e2);
list.add(e1);
list.add(e3);
Collections.sort(list);
System.out.println(list);
}
@Override
public int compareTo(Emp emp) {
if(emp.age>this.age)
return 1;
else if(emp.age<this.age)
return -1;
else return 0;
}
}
private String name;
private int age;
public String toString() {
return name+age;
}
Emp(String name,int age){
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Emp e1 = new Emp("a",10);
Emp e2 = new Emp("b",20);
Emp e3 = new Emp("c", 12);
List<Emp> list = new ArrayList<Emp>();
list.add(e2);
list.add(e1);
list.add(e3);
Collections.sort(list);
System.out.println(list);
}
@Override
public int compareTo(Emp emp) {
if(emp.age>this.age)
return 1;
else if(emp.age<this.age)
return -1;
else return 0;
}
}
output:
--------------
[b20, c12, a10]
Comparator example:
Comparator interface is useful if we have a legacy code of Person class which does not implement comparable interface. So, if we want to sort a collection containing instances of Person class, we can not. In this scenario we can use Comparator.
public class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
int p1Age = ((Person) p1).getAge();
int p2Age = ((Person) p2).getAge();
if (p1Age > p2Age)
return 1;
else if (p1Age < p2Age)
return -1;
else
return 0;
}
}
@Override
public int compare(Person p1, Person p2) {
int p1Age = ((Person) p1).getAge();
int p2Age = ((Person) p2).getAge();
if (p1Age > p2Age)
return 1;
else if (p1Age < p2Age)
return -1;
else
return 0;
}
}
public class Person {
private int age;
private String name;
public String toString() {
return name + age;
}
Person(int age, String name) {
this.setAge(age);
this.setName(name);
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Person p1 = new Person(10, "a");
Person p2 = new Person(30, "b");
Person p3 = new Person(20, "c");
List<Person> list = new ArrayList<Person>();
list.add(p3);
list.add(p2);
list.add(p1);
Collections.sort(list, new PersonComparator());
System.out.println(list);
}
}
private int age;
private String name;
public String toString() {
return name + age;
}
Person(int age, String name) {
this.setAge(age);
this.setName(name);
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Person p1 = new Person(10, "a");
Person p2 = new Person(30, "b");
Person p3 = new Person(20, "c");
List<Person> list = new ArrayList<Person>();
list.add(p3);
list.add(p2);
list.add(p1);
Collections.sort(list, new PersonComparator());
System.out.println(list);
}
}
output:
----------
[a10, c20, b30]
Difference between Comparable and Comparator:
Comparable
|
Comparator
|
Defined in java.lang package
|
Defined in java.util package
|
Has method:
public int compareTo(Object o)
|
Has method:
int compare (Object o1, Object o2)
|
compares "this" reference with the object specified.
|
compare two objects provided
|
Natural ordering
|
Total ordering
|
What are different types of inner classes?
member inner class:
public class OuterClass {
int outer = 10;
void outerMethod() {
System.out.println(new innerClass().inner);
}
class innerClass {
int inner = 20;
void innerMethod() {
System.out.println(outer);
System.out.println(inner);
}
}
public static void main(String[] args) {
OuterClass.innerClass x = new OuterClass().new innerClass();
x.innerMethod();
OuterClass o = new OuterClass();
o.outerMethod();
}
}
void outerMethod() {
System.out.println(new innerClass().inner);
}
class innerClass {
int inner = 20;
void innerMethod() {
System.out.println(outer);
System.out.println(inner);
}
}
public static void main(String[] args) {
OuterClass.innerClass x = new OuterClass().new innerClass();
x.innerMethod();
OuterClass o = new OuterClass();
o.outerMethod();
}
}
static inner class:
public class Outer {
int o1 = 10;
static int o2 = 60;
void outerMethod(){
System.out.println(inner.i1);
System.out.println(new inner().i2);
}
static class inner{
static int i1 = 20;
int i2 = 30;
void innerMethod(){
System.out.println(i1);
System.out.println(i2);
System.out.println(o2);
}
}
public static void main(String[] args){
Outer outer = new Outer();
outer.outerMethod();
Outer.inner name = new Outer.inner();
name.innerMethod();
}
}
int o1 = 10;
static int o2 = 60;
void outerMethod(){
System.out.println(inner.i1);
System.out.println(new inner().i2);
}
static class inner{
static int i1 = 20;
int i2 = 30;
void innerMethod(){
System.out.println(i1);
System.out.println(i2);
System.out.println(o2);
}
}
public static void main(String[] args){
Outer outer = new Outer();
outer.outerMethod();
Outer.inner name = new Outer.inner();
name.innerMethod();
}
}
output:
-------------
20
30
20
30
60
30
20
30
60
Question : What is a thread?
Answer: Thread is a lightweight process. It is a part of a process.
Question: How do u create a thread.
Answer: a) by implementing Runnable Interface
b) by extending Thread class.
In both way we have to override run() method.
Question: Which one is preferred?
Answer: Runnable interface is preferred as class implementing this interface can also extend other class.
Question: What is synchronization? Why do u need it?
Answer: By synchronization we can ensure that a shared resource is locked by one thread at one point of time so that shared resource does not get changed by two threads at one time. It eliminates the possibility of dirty reading.
public class Resource implements Runnable {
@Override
public synchronized void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName());
}
}
}
public class Process {
public static void main(String[] args){
Resource sharedResource = new Resource();
Thread t1 = new Thread(sharedResource);
Thread t2 = new Thread(sharedResource );
t1.start();
t2.start();
}
}
@Override
public synchronized void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName());
}
}
}
public class Process {
public static void main(String[] args){
Resource sharedResource = new Resource();
Thread t1 = new Thread(sharedResource);
Thread t2 = new Thread(sharedResource );
t1.start();
t2.start();
}
}
Question. How will you achieve synchronization in java
Ans: by Synchronized blocks and synchronized methods..
Question: Explain transient variable?
Answer: the value of this member variable does not get serialized with the object.
Question: What's the difference between the methods sleep() and wait()
Answer: sleep is a method inside thread class and wait is a method inside object class. if a thread goes for sleep state,then it will be in that state till the time specified. If a thread goes to wait state then by notify() and notifyAll() method, thread state gets changes from wait state to ready state.
Question:What are the states of a thread?
Answer: Following are the states of a thread:
a)New : A thread which has not yet started.
b)Runnable:Thread starts executing in JVM but wait for Processor
c)Blocked:Thread waiting for a monitor lock
d)waiting:Waiting for another thread to complete a particular action.
e)Timed_waiting:A thread waits for a specified time.
f)Terminated: Thread has completed the execution.
Sample program:
Thread t = new Thread();
Thread.State e = t.getState();
Thread.State[] ts = e.values();
for (int i = 0; i < ts.length; i++) {
System.out.println(ts[i]);
}
Thread.State e = t.getState();
Thread.State[] ts = e.values();
for (int i = 0; i < ts.length; i++) {
System.out.println(ts[i]);
}
Question: What is difference between wait and sleep?
Answer:
sleep(t) : where t is in millisecond, put thread in sleep state exactly for t(ms).
sleep(t) : where t is in millisecond, put thread in sleep state exactly for t(ms).
wait(t) :cause a thread to wait for t millisecond,but if it receives notify()/notifyAll() call, it changes its state from wait to ready.
Question: When state of thread changes to Timed_wait?
Answer: A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
Thread.sleep(time in ms)Object.wait(with timeout )Thread.join( with timeout)
Question: When state of a thread changes to Waiting state?
Answer:
A thread is in the waiting state due to calling one of the following methods:
Object.waitwith no timeoutThread.joinwith no timeout
Answer: Vector class is synchronized and ArrayList is not synchronized. That's why arrayList is faster than Vector.
Question: What's the difference between a queue and a stack?
Answer: Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule i.e first-in-first-out
Question: For an indexed search in a list of objects which one will you use: ArrayList or LinkedList?
Answer: ArrayList
Question: Under what condition u will prefer ArrayList/LinkedList
Answer:
When we have to go for indexed search and data does not changes ,use
ArrayList. If the data changes frequently then we will go for LinkedList
as insertion and deletion is easy in LinkedList compared to ArrayList
as in ArrayList removing an element Removes
the element at the specified position in this list and shifts any
subsequent elements to the left (subtracts one from their indices).
No comments:
Post a Comment