Java Interview Questions -19

Java Interview Questions -19

51.What are JSP scripting elements?

- JSP scripting elements lets to insert Java code into the servlet that will be generated from the current JSP page. There are three forms: a) Expressions of the form <%= expression %> that are evaluated and inserted into the output, b) Scriptlets of the form<% code %>that are inserted into the servlet’s service method, and c) Declarations of the form <%! Code %>that are inserted into the body of the servlet class, outside of any existing methods.

52.How do you pass data (including JavaBeans) to a JSP from a servlet?
- (1) Request Lifetime: Using this technique to pass beans, a request dispatcher (using either “include” or forward”) can be called. This bean will disappear after processing this request has been completed. Servlet: request. setAttribute(”theBean”, myBean); RequestDispatcher rd = getServletContext(). getRequestDispatcher(”thepage. jsp”); rd. forward(request, response); JSP PAGE:<jsp: useBean id=”theBean” scope=”request” class=”. . . . . ” />(2) Session Lifetime: Using this technique to pass beans that are relevant to a particular session (such as in individual user login) over a number of requests. This bean will disappear when the session is invalidated or it times out, or when you remove it. Servlet: HttpSession session = request. getSession(true); session. putValue(”theBean”, myBean); /* You can do a request dispatcher here, or just let the bean be visible on the next request */ JSP Page:<jsp:useBean id=”theBean” scope=”session” class=”. . . ” /> 3) Application Lifetime: Using this technique to pass beans that are relevant to all servlets and JSP pages in a particular app, for all users. For example, I use this to make a JDBC connection pool object available to the various servlets and JSP pages in my apps. This bean will disappear when the servlet engine is shut down, or when you remove it. Servlet: GetServletContext(). setAttribute(”theBean”, myBean); JSP PAGE:<jsp:useBean id=”theBean” scope=”application” class=”. . . ” />

53.How can I set a cookie in JSP?
- response. setHeader(”Set-Cookie”, “cookie string”); To give the response-object to a bean, write a method setResponse (HttpServletResponse response) - to the bean, and in jsp-file:<% bean. setResponse (response); %>

54.How can I delete a cookie with JSP?
- Say that I have a cookie called “foo, ” that I set a while ago & I want it to go away. I simply: <% Cookie killCookie = new Cookie(”foo”, null); KillCookie. setPath(”/”); killCookie. setMaxAge(0); response. addCookie(killCookie); %>

55.How are Servlets and JSP Pages related?
- JSP pages are focused around HTML (or XML) with Java codes and JSP tags inside them. When a web server that has JSP support is asked for a JSP page, it checks to see if it has already compiled the page into a servlet. Thus, JSP pages become servlets and are transformed into pure Java and then compiled, loaded into the server and executed.

56.What is skeleton and stub? what is the purpose of those?
- Stub is a client side representation of the server, which takes care of communicating with the remote server. Skeleton is the server side representation. But that is no more in use… it is deprecated long before in JDK.

57.What is the final keyword denotes?
- final keyword denotes that it is the final implementation for that method or variable or class. You can’t override that method/variable/class any more.

58.What is nested class?
- If all the methods of a inner class is static then it is a nested class.

59.What is inner class?
- If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class.

60.What is composition?
- Holding the reference of the other class within some other class is known as composition.

61.What is the significance of ListIterator?
- You can iterate back and forth.

62.What is the major difference between LinkedList and ArrayList?
 - LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.

63.How will you invoke any external process in Java?
 - Runtime.getRuntime().exec(….)

64.What is the finalize method do?
- Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.

65.What is mutable object and immutable object?
- If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …)

66.What is aggregation?
- It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation.

67.What are the methods in Object?
- clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString

68.what is a daemon thread?
- These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.

69.What is the basic difference between string and stringbuffer object?
 - String is an immutable object. StringBuffer is a mutable object.

70.What is the purpose of Void class?
- The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.

71.What is reflection?
- Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.

72.. Can a source file contain more than one class declaration?
Yes a single source file can contain any number of Class declarations but only one of the class can be declared as public.

73. What is a package?
Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.

74. Are JVM's platform independent?
JVM's are not platform independent. JVM's are platform specific run time implementation provided by the vendor.

75. What is the difference between a JDK and a JVM?
JDK is Java Development Kit which is for development purpose and it
includes execution environment also. But JVM is purely a run time
environment and hence you will not be able to compile your source files
using a JVM.

76. Does Java support multiple inheritance?
Java doesn't support multiple inheritance.

77.what do you mean by platform independence?
Platform independence means that we can write and compile the java
code in one platform (eg Windows) and can execute the class in any other
supported platform eg (Linux,Solaris,etc).

78. How to define a constant variable in Java?
The variable should be declared as static and final. So only one copy of
the variable exists for all instances of the class and the value can't be
changed also.
static final int PI = 2.14; is an example for constant.

79. What is a pointer and does Java support pointers?
Pointer is a reference handle to a memory location. Improper handling of
pointers leads to memory leaks and reliability issues hence Java doesn't
support the usage of pointers.

80. What is the base class of all classes?

81. What is the arguement of main() method?
main() method accepts an array of String object as arguement.

82. Does the order of public and static declaration matter in main()
No. It doesn't matter but void should always come before main().

83.can a main()method be overloaded?
Yes. You can have any number of main() methods with different method

84. What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the
compiled java class files.

85. Is Java a pure object oriented language?
Java uses primitive data types and hence is not a pure object oriented

86. What are local variables?
Local varaiables are those which are declared within a block of code like
methods. Local variables should be initialised before accessing them.

87. Should a main() method be compulsorily declared in all java classes?
No not required. main() method should be defined only if the source class
is a java application.

88.can a class be declared as  protected?
A class can't be declared as protected. only methods can be declared as

89. What is the access scope of a protected method?
A protected method can be accessed by the classes within the same
package or by the subclasses of the class in any package.

90. Can you give few examples of final classes defined in Java API?
java.lang.String, java.lang.Math are final classes.

91. What is the return type of the main() method?
Main() method doesn't return anything hence declared void.

92. Why is the main() method declared static?
main() method is called by the JVM even before the instantiation of the
class hence it is declared as static.

93. What are instance variables?
Instance variables are those which are defined at the class level. Instance
variables need not be initialized before using them as they are
automatically initialized to their default values.

94.can a main()method be declared final?
Yes. Any inheriting class will not be able to have it's own default main()

95. Which package is imported by default?
java.lang package is imported by default even without a package

96. What is the purpose of declaring a variable as final?
A final variable's value can't be changed. final variables should be

97. I don't want my class to be inherited by any other class. What should
i do?
You should declared your class as final. But you can't define your class
as final, if it is an abstract class. A class declared as final can't be
extended by any other class.

98. Can a class declared as private be accessed outside it's package?
Not possible.

99. How is final different from finally and finalize()?
final is a modifier which can be applied to a class or a method or a variable. final class can't be inherited, final method can't be overridden  and final variable can't be changed.

100. What is the importance of static variable?
static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change
gets reflected in all the objects.

101. Can we declare a static variable inside a method?
Static varaibles are class level variables and they can't be declared
inside a method. If declared, the class will not compile.

102. What is an Abstract Class and what is it's purpose?
A Class which doesn't provide complete implementation is defined as an
abstract class. Abstract classes enforce abstraction.

Also Read:


Post a Comment