Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Ques 1. How could Java classes direct program messages to the system console, but error messages, say to a file?

Ans. By default, both System console and err point at the system console.

The class System has a variable out that represents the standard output.
The variable err that represents the standard error device. 
Stream st = new Stream (new  FileOutputStream ("withoutbook_com.txt"));

Is it helpful? Add Comment View Comments
Ques 2. What are the differences between an interface and an abstract class?
  • An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.
  • Abstract class are used only when there is a "IS-A" type of relationship between the classes. Interfaces can be implemented by classes that are not related to one another and there is "HAS-A" relationship. 
  • You cannot extend more than one abstract class. You can implement more than one interface. 
  • Abstract class can implement some methods also. Interfaces can not implement methods. 
  • With abstract classes, you are grabbing away each class’s individuality. With Interfaces, you are merely extending each class’s functionality.
  • As per Java 8, interface can have method body as well.
Is it helpful? Add Comment View Comments
Ques 3. Why would you use a synchronized block vs. synchronized method?
Ans. 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) {
  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 4. Explain the usage of the keyword transient?
Ans. This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).
For example:
class T { transient int a; // will not persist int b; // will persist }
import java.util.Date;

public class Logon implements Serializable {
	private Date date = new Date();
	private String username;
	private transient String password;
	public Logon(String name, String pwd) {
		username = name;
		password = pwd;
	public String toString() {
		String pwd = (password == null) ? "(n/a)" : password;
		return "logon info: \n username: " + username + "\n date: " + date
		+ "\n password: " + pwd;

	public static void main(String[] args) throws Exception {
		Logon a = new Logon("Hulk", "myLittlePony");
		System.out.println("logon a = " + a);
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
		Thread.sleep(1000); // Delay for 1 second
		// Now get them back:
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
		System.out.println("Recovering object at " + new Date());
		a = (Logon) in.readObject();
		System.out.println("logon a = " + a);
Is it helpful? Add Comment View Comments
Ques 5. How can you force garbage collection?
Ans. You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

The following code of program will help you in forcing the garbage collection. First of all we have created an object for the garbage collector to perform some operation. Then we have used the System.gc(); method to force the garbage collection on that object. Then we have used the System.currentTimeMillis(); method to show the time take by the garbage collector.

import java.util.Vector;

public class GarbageCollector{
	public static void main(String[] args) {
		int SIZE = 200;
		StringBuffer s;
		for (int i = 0; i < SIZE; i++) {
		System.out.println("Garbage Collection started explicitly.");
		long time = System.currentTimeMillis();
		System.out.println("It took " +(System.currentTimeMillis()-time) + " ms");
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2018 WithoutBook