Browsed by
Tag: java 10 interview questions and answers

Can You Make Use of a ServletOutputStream Object From within a JSP Page

Can You Make Use of a ServletOutputStream Object From within a JSP Page

No. You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients. A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational perspective, it is not. In general, it is not recommended to use a ServletOutputStream directly from within a JSP page. The JSP (JavaServer Pages) technology is designed to separate presentation logic from business…

Read More Read More

Can an anonymous class be declared as implementing an interface and extending a class

Can an anonymous class be declared as implementing an interface and extending a class

An anonymous class may implement an interface or extend a superclass, but may not be declared to do both. Yes, in Java, an anonymous class can be declared as implementing an interface and extending a class simultaneously. This allows you to create a class on the fly that provides a specific implementation for an interface while also inheriting from a class. Here is an example: java interface MyInterface { void interfaceMethod(); } class MyBaseClass { void baseClassMethod() { System.out.println(“Base class…

Read More Read More

How is JSP Include Directive Different From JSP Include Action.

How is JSP Include Directive Different From JSP Include Action.

When a JSP include directive is used, the included file’s code is added into the added JSP page at page translation time, this happens before the JSP page is translated into a servlet. While if any page is included using action tag, the page’s output is returned back to the added page. This happens at runtime.

How many times may an object’s finalize() method be invoked by the garbage collector?

How many times may an object’s finalize() method be invoked by the garbage collector?

An object’s finalize() method may only be invoked once by the garbage collector. In Java, there is no guarantee on when or how many times the finalize() method of an object will be invoked by the garbage collector. The finalize() method is called by the garbage collector before it reclaims the memory occupied by an object, but there is no certainty about the exact timing or number of invocations. It’s important to note that relying on the finalize() method for critical resource…

Read More Read More

How to Read a File from CLASSPATH in Java

How to Read a File from CLASSPATH in Java

This can be done in two simple ways   Using ClassLoader.getResourceAsStream This method can be used to load any file from CLASSPATH InputStream in =this.getClass().getClassLoader().getResourceAsStream(“MyFile.txt”);   Using Class.getResourceAsStream This method can be used to load files using relative path to the package of the class InputStream in =this.getClass().getResourceAsStream(“SomeTextFile.txt”);   This method can also be used to load any files from CLASSPATH by prefixing a “/”