Niu Ke's Diary (November 3, 2021)
Title:
Which of the following exceptions is a check type exception that needs to be declared when writing a program ( ) A.NullPointerException B.ClassCastException C.FileNotFoundException D.IndexOutOfBoundsException
Resolution:
Correct answer: C
Here is the reference
- The pink ones are checked exceptions, which must be caught by the try{}catch statement block or declared by the throws clause in the method signature. Checked exceptions must be caught and handled at compile time. They are named checked exceptions because the java compiler and the Java virtual machine need to check to ensure that this rule is observed
- Green exceptions are runtime exceptions. Programmers need to analyze the code to decide whether to catch and handle them, such as null pointers, which are divided by 0
- If it is declared as error, it is a serious error, such as system crash, virtual machine error, dynamic link failure, etc. these errors cannot be recovered or cannot be captured, which will lead to application interruption. Error does not need to be captured.
Title:
The following integer constants i The correct definition is( ) A.final i; B.static int i; C.static final int i=234; D.final float i=3.14f;
Resolution:
C
There are three variables modified by final in java: static variable, instance variable and local variable, which represent three types of constants respectively. int keyword modifies an integer type. Static modifies a static variable, that is, when using this keyword to modify a variable, a storage space will be created in memory for the variable before creating an object. If you need to use this static variable to create a pair of objects in the future, you will share the storage space of this variable.
A: Missing required integer type declaration
B: Missing constant keyword final
D: Define a floating point constant
Title:
Which of the following statements is correct? () A.java Collection classes in (e.g Vector)It can be used to store any type of object, and the size can be adjusted automatically. However, it needs to know the type of stored object in advance before it can be used normally. B.stay java In, we can use violations( Exception)To throw some messages that are not errors, but this is more overhead than returning a result directly from the function. C.java Interface contains function declarations and variable declarations. D.java In, subclasses cannot access private and protected members of the parent class.
Resolution:
Correct answer: B
The situation mentioned in option B is our custom Exception. Please read it carefully: we can use Exception To throw some non error messages, yes, non error messages. For example, I define an Exception. If a variable is greater than 10, an Exception will be thrown, which corresponds to the situation of option B. I use throwing an Exception to indicate that the variable is greater than 10, rather than a function body (determine whether it is greater than 10 in the function body, and then return true or false) Judge, because the function call is in and out of the stack, the stack is the fastest under the register and takes up less space, and the user-defined Exception exists in the heap, the memory overhead of the Exception must be large! So B pair.
The C option means that the interface contains method declarations and variable declarations. Because the method in the interface is abstract public by default, it is in accordance with the syntax rules to write only function declarations in the interface. However, the variable is decorated with public final static by default, which means that it is a static constant. The constant must be initialized at the time of declaration, regardless of whether it is in the interface or in the class! Therefore, the second half of the C sentence is wrong and must be When declaring and giving initialization!
Title:
following JAVA What is the result of the program( ) public static void main(String[] args) { Object o1 = true ? new Integer(1) : new Double(2.0); Object o2; if (true) { o2 = new Integer(1); } else { o2 = new Double(2.0); } System.out.print(o1); System.out.print(" "); System.out.print(o2); } A.1 1 B.1.0 1.0 C.1 1.0 D.1.0 1
Resolution:
Conversion rules for ternary operator types:
1. If the two operands are not convertible, the conversion will not be performed, and the return value is of Object type
2. If two operands are explicitly typed expressions (such as variables), they are converted according to normal binary numbers, int type is converted to long type, long type is converted to float type, etc.
3. If one of the two operands is the number S and the other is an expression, and its type is marked as t, then the number S is converted to type t if it is within the range of T; if S is beyond the range of type T, t is converted to type S.
4. If both operands are direct quantity numbers, the return value type is the one with the larger range
Meet 4, so choose D
Title:
Which of the following regular expressions cannot be associated with a string“ https://www.tensorflow.org/ (without quotation marks) matches? () A.[a-z]+://[a-z.]+/ B.https[://]www[.]tensorflow[.]org[/] C.[htps]+://www.tensorflow.org/ D.[a-zA-Z.:/]+
Resolution:
Correct answer: B
- Any character means matching any corresponding character, such as a matching a, 7 matching 7, - matching -.
- [] stands for matching any one of the characters in brackets, such as [abc] matching a or b or c.
- -Inside and outside the brackets represent different meanings. For example, when outside, it matches - - if [a-b] in the brackets means to match any of the 26 lowercase letters; [a-zA-Z] matches any of the 52 uppercase and lowercase letters; [0-9] matches any of the ten numbers.
- The meaning inside the brackets is different from that outside. If it is outside, it means the beginning. For example, 7 [0-9] means a string with a matching beginning of 7 and the second digit is any number. If it is inside the brackets, it means any character other than this character (including numbers and special characters), for example, [^ abc] means any character other than abc.
- . means to match any character.
- \d represents a number
- \D means non numeric.
- \s indicates that it is composed of null characters, [\ t\n\r\x\f].
- \s indicates that it is composed of non null characters, [^ \ s].
- \w stands for letters, numbers, underscores, [a-zA-Z0-9_].
- \W indicates that it is not composed of letters, numbers and underscores.
- .?: indicates 0 or 1 occurrences.
- . + indicates one or more occurrences.
- *Indicates 0, 1, or more occurrences.
- {n} Indicates n occurrences.
- {n,m} indicates n~m times.
- {n,} indicates n or more occurrences.
- XY means X followed by Y, where X and y are part of a regular expression, respectively.
- X|Y means X or Y. for example, "food|f" matches foo (d or F), while "(food)|f" matches food or F.
- (10) Sub expression that treats X as a whole
Title:
What is the output of the following code? public class Base { private String baseName = "base"; public Base() { callName(); } public void callName() { System. out. println(baseName); } static class Sub extends Base { private String baseName = "sub"; public void callName() { System. out. println (baseName) ; } } public static void main(String[] args) { Base b = new Sub(); } }
Resolution:
Answer: A
new Sub(); in the process of creating a derived class, first create a base class object, and then create a derived class.
The creation of the base class is called the Base() method by default. The callName() method is invoked in the method. Since the method exists in the derived class, the called callName() method is derived from the derived class, and the derived class is not yet constructed, so the value of the variable baseName is null.
Title:
Which of the following behaviors is interrupted without causing InterruptedException: ( )? A.Thread.join B.Thread.sleep C.Object.wait D.CyclicBarrier.await E.Thread.suspend
Resolution:
Correct answer: E
The representative methods of throwing InterruptedException are:
wait method of java.lang.Object class
sleep method of java.lang.Thread class
join method of java.lang.Thread class
Title:
frequently-used servlet What is the name of the package? A.java.servlet B.javax.servlet C.servlet.http D.javax.servlet.http
Resolution:
Using Java technology to develop WEB applications and deeply understand the mechanism of Servlet will play an important role in promoting application development. If you want to deeply understand the mechanism of Servlet, you have to understand javax.servlet package
The javax.servlet package contains 7 interfaces, 3 classes and 2 exception classes, which are:
Interfaces: requestdispatcher, servlet, ServletConfig, ServletContext, ServletRequest, servlethresponse and SingleThreadModel classes: GenericServlet,ServletInputStream and ServletOutputStream
Exception classes: ServletException and UnavailableException
Servlet life cycle a servlet life cycle method is defined in the servlet interface, which are Init,Service and Destroy respectively. The simple servlet of servlet life cycle method is demonstrated:
import javax.servlet.*; import java.io.IOException; public class PrimitiveServlet implements Servlet { public void init(ServletConfig config) throws ServletException { System.out.println("init"); } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { System.out.println("service"); } public void destroy() { System.out.println("destroy"); } public String getServletInfo() { return null; } public ServletConfig getServletConfig() { return null; } }
How to get the ServletConfig object in the Servlet?
In the Servlet Init method, the Servlet Container will pass in a ServletConfig object through which developers can obtain the Servlet initialization parameters defined in the web.xml file
The following is an example of getting Servlet initial parameters:
import javax.servlet.*; import java.util.Enumeration; import java.io.IOException; public class ConfigDemoServlet implements Servlet { public void init(ServletConfig config) throws ServletException { Enumeration parameters = config.getInitParameterNames(); while (parameters.hasMoreElements()) { String parameter = (String) parameters.nextElement(); System.out.println("Parameter name : " + parameter); System.out.println("Parameter value : " + config.getInitParameter(parameter)); } } public void destroy() { } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException } public String getServletInfo() { return null; } public ServletConfig getServletConfig() { return null; } }
How do I get the ServletContext object?
The ServletContext object can be obtained through the getServletContext method of the ServletConfig object
import javax.servlet.*; import java.util.Enumeration; import java.io.IOException; public class ContextDemoServlet implements Servlet { ServletConfig servletConfig; public void init(ServletConfig config) throws ServletException { servletConfig = config; } public void destroy() { } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { ServletContext servletContext = servletConfig.getServletContext(); Enumeration attributes = servletContext.getAttributeNames(); while (attributes.hasMoreElements()) { String attribute = (String) attributes.nextElement(); System.out.println("Attribute name : " + attribute); System.out.println("Attribute value : " + servletContext.getAttribute(attribute)); } System.out.println("Major version : " + servletContext.getMajorVersion()); System.out.println("Minor version : " + servletContext.getMinorVersion()); System.out.println("Server info : " + servletContext.getServerInfo()); } public String getServletInfo() { return null; } public ServletConfig getServletConfig() { return null; } }
How to share information between servlets?
We can maintain the information shared between different servlets through Servlet Context
How to solve the multi Thread problem of Servlet?
If the Servlet needs to read and write external resources, we need to consider the problem of threads. We can use the declarative interface SingleThreadModel to avoid resource conflicts between multiple threads. However, it should be noted that if the Servlet only reads external resources, we should not implement this interface. If we implement this interface, Servlets can only serve one user request at a time, and subsequent user requests must wait in the queue
Title:
Which of the following technologies can be used in WEB Implementation of session tracking in development? A.session B.Cookie C.URL Rewrite D.Hidden domain
Resolution:
ABCD
Session tracing is a flexible and lightweight mechanism that makes state programming on the Web possible.
HTTP is a stateless protocol. Whenever a user sends a request, the server will respond. The relationship between the client and the server is discrete and discontinuous. When a user switches between multiple pages of the same website, it is impossible to determine whether it is the same customer. Session tracking technology can solve this problem. When a customer switches between multiple pages, the server will save the user's information.
There are four ways to implement Session tracking technology: URL rewriting, hiding form fields, cookies and Session.
1) . hidden form field:, very suitable for session applications that need a lot of data storage.
2) . URL Rewriting: the URL can be appended with parameters and sent together with the server's request. These parameters are name / value pairs.
3) . Cookie: a cookie is a small, named data element. The server uses the set cookie header as HTTP
Part of the response is transmitted to the client, and the client is requested to save the Cookie value and use one in subsequent requests to the same server
The Cookie header returns it to the server. Compared with other technologies, one advantage of cookies is that after the browser session ends, even
It can still retain its value after the client computer restarts
4) . Session: bind objects to a Session using setAttribute(String str,Object obj) method
Title:
true,false,null,sizeof,goto,synchronized Which are Java keyword? A.true B.false C.null D.sizeof E.goto F.synchronized
Resolution:
Title:
The following can get the result set correctly: A. Statement sta=con.createStatement(); ResultSet rst=sta.executeQuery("select * from book"); B. Statement sta=con.createStatement("select * from book"); ResultSet rst=sta.executeQuery(); C. PreparedStatement pst=con.prepareStatement(); ResultSet rst=pst.executeQuery("select * from book"); D. PreparedStatement pst=con.prepareStatement("select * from book"); ResultSet rst=pst.executeQuery();
Resolution:
A. D is correct; Creating a Statement does not pass parameters. PreparedStatement needs to pass in sql statements