Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Test your skills through the online practice test: Core Java Quiz Online Practice Test

Ques 156. What are the approaches that you will follow for making a program very efficient?

By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more.

Is it helpful? Add Comment View Comments
 

Ques 157. What is an object's lock and which object's have locks?

An object's lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock. All objects and classes have locks. A class's lock is acquired on the class's Class object.

Is it helpful? Add Comment View Comments
 

Ques 158. Why would you use a synchronized block vs. synchronized method?

Synchronized blocks place locks for shorter periods than synchronized methods.

if you go for synchronized block it will lock a specific object.

if you go for synchronized method it will lock all the objects.

in other way Both the synchronized method and block are used to acquires the lock for an object. But the context may vary. Suppose if we want to invoke a critical method which is in a class whose access is not available then synchronized block is used. Otherwise synchronized method can be used.

Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc

For a detail clarification see the below code

for example:

//Synchronized block
class A { 
 public void method1() {...} 
}


class B
{
 public static void main(String s[])
 { 
  A objecta=new A();
  A objectb=new A();
  synchronized(objecta) {
   objecta.method1();
  }
  objectb.method1(); //not synchronized
 }
}
//synchronized method
class A { 
 public synchronized void method1() { ...}
}

class B {
 public static void main(String s[]) {
  A objecta=new A();
  A objectb =new A();
  objecta.method1(); objectb.method2();
 }
}

Is it helpful? Add Comment View Comments
 

Ques 159. What are the differences between the methods sleep() and wait()?

  • The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. 
  • The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Is it helpful? Add Comment View Comments
 

Ques 160. There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?

If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook