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 271. What kind of thread is the Garbage collector thread?

It is a daemon thread.

Is it helpful? Add Comment View Comments
 

Ques 272. Explain Garbage collection mechanism in Java?

Garbage collection is one of the most important features of Java. The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use.
In Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. Garbage collection is an automatic process and can't be forced. There is no guarantee that Garbage collection will start immediately upon request of System.gc().

Is it helpful? Add Comment View Comments
 

Ques 273. What is FileOutputStream in java?

Java.io.FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. Following are the important points about FileOutputStream:

  • This class is meant for writing streams of raw bytes such as image data.

  • For writing streams of characters, use FileWriter

public class FileOutputStream extends OutputStream
Constructors:
1FileOutputStream(File file) 
This creates a file output stream to write to the file represented by the specified File object.
2FileOutputStream(File file, boolean append) 
This creates a file output stream to write to the file represented by the specified File object.
3FileOutputStream(FileDescriptor fdObj) 
This creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
4FileOutputStream(String name) 
This creates an output file stream to write to the file with the specified name.
5FileOutputStream(String name, boolean append) 
This creates an output file stream to write to the file with the specified name.
Example:
file = new File("c:/newfile.txt");
fop = new FileOutputStream(file);
if (!file.exists()) {
	file.createNewFile();
} 
// get the content in bytes
byte[] contentInBytes = content.getBytes(); 
fop.write(contentInBytes);
fop.flush();
fop.close();

Is it helpful? Add Comment View Comments
 

Ques 274. Can a method be static and synchronized?

A static method can be synchronized. If you do so, the JVM will obtain a lock on the java.lang. Class instance associated with the object. It is similar to saying:

synchronized(XYZ.class) { 
}

Is it helpful? Add Comment View Comments
 

Ques 275. Does JVM maintain a cache by itself? Does the JVM allocate objects in heap? Is this the OS heap or the heap maintained by the JVM? Why?

Yes, the JVM maintains a cache by itself. It creates the Objects on the HEAP, but references to those objects are on the STACK.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook