JSP question set

Source: Internet
Author: User
Tags set cookie
1. How to execute a thread-safe JSP?
A: You only need to add the following command:
<% @ Page isthreadsafe = "false" %>
2. What is the difference between collection and collections?
A: collection is an interface under java. util. It is the parent interface of various collection structures.
Collections is a class under java. util. It contains various static methods related to set operations.
3. Short I = 1; I = I + 1; and short I = 1; I + = 1; what are the errors?
Answer: There is a mistake in short S1 = 1; S1 = S1 + 1; S1 is short type, S1 + 1 is int type, and cannot be explicitly converted to short type. It can be changed to S1 = (short) (S1 + 1 ).
Short S1 = 1; S1 + = 1 is correct.
4. What is the servlet lifecycle? What is the difference between Servlet and CGI?
A: When a Web container loads servlet, the lifecycle begins. Call the servlet Init () method to initialize the servlet. By calling the Service () method,
Call different do *** () methods based on different requests. End the service. The Web Container calls the Servlet's destroy () method.
The difference with CGI is that the servlet is in a server process and runs its service method in multiple threads. An instance can serve multiple requests,
In addition, the instance will not be destroyed, and CGI will generate a new process for each request, and the service will be destroyed after completion, so the efficiency is lower than servlet.
5. What are JSP commands?
A: JSP commands are divided into two types: Page commands and action commands.
Action Commands include usebean, setproperty, getproperty, forward, plugin, and include.
6. How does JSP use annotations?
Answer: 1) <% -- %> 2); //; 3)/*****/; 4) <! -->;
7. Set cookie
A: cookie = new cookie ("mycookie", null );
Cookie. setmaxage (0 );
Cookie. setpath ("/");
Response. addcookie (cookie );
8. & Differences
A: & is a bitwise operator that represents bitwise and operation. & is a logical operator that represents
9. Differences between hashtable and hashmap
A: hashmap is a lightweight Implementation of hashtable (non-thread-safe implementation). They have completed the map interface. The main difference is that hashmap allows null key values ),
Because of non-thread security, the efficiency may be higher than that of hashtable.
Hashmap allows null as the key or value of an entry, whereas hashtable does not.
Hashmap removes the contains method of hashtable and changes it to containsvalue and containskey. The contains method is easy to misunderstand.
Hashtable inherits from the dictionary class, while hashmap is an implementation of the map interface introduced by java1.2.
The biggest difference is that the hashtable method is synchronize, but hashmap is not. When multiple threads access hashtable,
You do not need to implement synchronization for its own method, but hashmap must provide external synchronization for it.
The hash/rehash algorithms used by hashtable and hashmap are roughly the same, so there is no big difference in performance.
10. embedded objects in JSP
A: JSP has nine built-in objects.
1) Request client request, which contains parameters from post/get
2) Response sent from the response webpage to the client.
3) Manage pagecontext webpage attributes here
4) session related to the request
5) appliation servlet content being executed
6) Out is used to send response parameters.
7) page indicates the servlet instance generated on the page.
8) exeption error pages generated for JSP, exceptions not captured
9) configuration servlet architecture Components
11. Describe the differences between JSP and Servlet, their commonalities, and their respective application scopes.
A: Although JSP is essentially a servlet, the two are created in different ways. servlet is completely composed of Java program code and is good at Process Control and transaction processing.
It is not intuitive to generate dynamic web pages. jsp consists of HTML code and JSP tags, which can be used to conveniently compile dynamic web pages. Therefore, servlet is used in actual applications to control business processes.
JSP is used to generate dynamic web pages. In the Struts framework, JSP is located at the view layer of the MVC design mode, while servlet is located at the control layer.
12. The elements in the set cannot be repeated. How can we identify whether the elements are repeated or not? Is = or equals () used ()? What are their differences?
A: The elements in the set cannot be repeated. Use the iterator () method to identify whether the elements are repeated or not. Equals () is used to determine whether two sets are equal.
The equals () and = Methods Determine whether the reference value points to the same object equals () is overwritten in the class, so that when the content and type of the two separated objects match,
Returns the true value.
13. Give me the most common runtime exception
A: arithmeticexception, arraystoreexception, bufferoverflowexception, bufferunderflowexception, cannotredoexception,
Cannotundoexception, classcastexception, cmcmexception, concurrentmodificationexception, domexception,
Emptystackexception, illegalargumentexception, illegalmonitorstateexception, illegalpathstateexception,
Illegalstateexception, imagingopexception, indexoutofboundsexception, missingresourceexception,
Negativearraysizeexception, nosuchelementexception, nullpointerexception,
Profiledataexception, providerexception, rasterformatexception, securityexception, systemexception,
Undeclaredthrowableexception, unmodifiablesetexception, unsupportedoperationexception
14. can abstract methods be both static, native, and synchronized?
A: neither of them is possible.
15. Start a thread with run () or start ()
A: To start a thread is to call the START () method so that the virtual processor represented by the thread is runable, which means it can be scheduled and executed by JVM. This does not mean that the thread will run immediately.
The run () method can generate the exit sign to stop a thread.
16. There is a return statement in try {}, so will the code in finally {} following this try be executed? When will it be executed, before or after return?
A: Yes. It will be executed before return.
17. In the most efficient way, how many equals are calculated by multiplying 2 by 8?
Answer: 2 <3
18. What is the difference between abstract class and interface?
A: The class that declares the existence of a method without implementing it is called abstract class. It is used to create a class that reflects some basic behaviors and declare a method for this class,
However, this class cannot be implemented in this class. You cannot create an abstract instance. However, you can create a variable whose type is an abstract class and point it to a specific subclass.
. Abstract constructors or abstract static methods are not allowed. The subclasses of abstract classes provide implementation for all abstract methods in their parent classes. Otherwise, they are also abstract classes.
Yes. Instead, implement this method in the subclass. Other classes that know their behavior can implement these methods in the class.
An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. All methods in the interface are
Is abstract, and there is no program body. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot be inherited from the interface definition.
Action. When a class implements a special interface, it defines (to be given by the program body) all the methods of this interface. Then, it can call
Port method. Because there is an abstract class, it allows the interface name as the type of the referenced variable. Normally, dynamic Association editing will take effect. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.
19. String S = new string ("XYZ"); how many string objects are created?
A: There are two objects, one being "xyx" and the other being the reference object s pointing to "xyx.
20. What is the difference between sleep () and wait?
A: The sleep () method is used to stop a thread for a period of time. After the sleep interval expires, the thread may not resume execution immediately. This is because at that time, other threads can
Can be running and not scheduled to give up execution, unless (a) the "wake up" thread has a higher priority
(B) The Running thread is blocked for other reasons. When wait () is a thread interaction, if the thread sends a wait () call to a synchronization object X, the thread will pause the execution,
The called object enters the waiting state until it is awakened or the waiting time is reached.
21. Does the array have the length () method? Does string have the length () method?
A: The array does not have the length () method. It has the Length attribute.
String has the length () method.
22. Differences between overload and override. Can the overloaded method change the type of the returned value?
A: overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class,
Overload Overloading is a manifestation of polymorphism in a class. If a method in a subclass has the same name and parameters as its parent class,
We say this method is overwritten ). When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked.
If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called overloading ).
The overloaded method can change the type of the returned value.
23. Can an interface inherit an interface? Can an abstract class implement the (implements) interface? Whether the abstract class can inherit the concrete class)
A: The interface can inherit the interface. Abstract classes can be implemented (implements) interfaces, and whether abstract classes can inherit object classes, provided that object classes must have clear constructors.
24. Can I inherit the string class?
A: string is a final class, so it cannot be inherited.
25. Does swtich work on byte, long, or string?
A: In switch (expr1), expr1 is an integer expression. Therefore, the parameters passed to the switch and case statements should be int, short, Char, or byte.
Long and string cannot apply to swtich.
26. The two objects have the same value (X. Equals (y) = true), but different hash codes are available, right?
A: No. The same hash code exists.
27. after an object is passed as a parameter to a method, this method can change the properties of this object and return the changed result. Is it a value transfer or a reference transfer?
A: value transfer. The Java programming language only transmits parameters by values. When an object instance is passed as a parameter to a method, the parameter value is a reference to the object.
The object content can be changed in the called method, but the object reference will never change.
28. After a thread enters a synchronized method of an object, can other threads access other methods of this object?
A: No. One Synchronized Method of an object can only be accessed by one thread.
29. Whether constructor can be override
A: No. The constructor cannot be inherited, so it cannot be overwritten, but it can be reloaded.
30. Reverse Functions
String reverse (string Arg)
{
If (Arg. Length () = 0)
Return ARG;
Else
Return reverse (Arg. substring (1, Arg. Length) + Arg. substring (0, 1 );
}
31. Scope: public, private, and protected, and what is the difference when no data is written?
Package of the current class other package Child class other package non-Child class
Public √
Protected √ ×
Friendly √ ××
Private √ ×××
32. Differences between arraylist and vector
A: arraylist and vector are mainly used in two aspects.
1. Synchronization: the vector is thread-safe, that is, synchronous, while the arraylist is not secure or synchronous
2. Data growth: When we need to increase, the vector grows to the original training by default, while the arraylist is half of the original growth.
33. Why can't one Chinese character be stored in char variables?
A: It can be defined as a Chinese character. Because Java uses unicode encoding and a char occupies 16 bytes, it is okay to put a Chinese character.
34. multithreading has several implementation methods. What is synchronization? What are the implementation methods?
A: multithreading can be implemented in two ways: Inheriting the Thread class and implementing the runnable interface.
There are two types of synchronization implementation: synchronized, wait, and Y.
35. Is Float Type Float F = 3.4 correct?
A: It is incorrect, and the precision is not accurate. Forced conversion should be used. f = (float) 3.4;
36. Why should I use class. forname?
A: call this operation to return an object of the class with the specified class name as a string.
37. are two int-type integers not replaced by the 3rd-square variable?
For example, a = 10 B = 5
After conversion, A = 5 B = 10
Answer: 1) A = a + B; B = A-B; A = A-B;
2) A ^ = B; B ^ = A; A ^ = B;
38. Compile a program, create four threads, increase two threads, and decrease two threads.
Public class threadtest1 {
Private Int J;
Public static void main (string ARGs []) {
Threadtest1 TT = new threadtest1 ();
INC Inc = TT. New Inc ();
Dec dec = TT. New Dec ();
For (INT I = 0; I <2; I ++ ){
Thread t = new thread (INC );
T. Start ();
T = new thread (DEC );
T. Start ();
}
}
Private synchronized void Inc (){
J ++;
System. Out. println (thread. currentthread (). getname () + "-Inc:" + J );
}
Private synchronized void Dec (){
J --;
System. Out. println (thread. currentthread (). getname () + "-Dec:" + J );
}
Class Inc implements runnable {
Public void run (){
For (INT I = 0; I <10; I ++ ){
INC ();
}
}
}
Class dec implements runnable {
Public void run (){
For (INT I = 0; I <10; I ++ ){
Dec ();
}
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.