java20 questions

To define class fields or class methods (static methods), which of the following modifiers should be used to modify () the
correct answer: B
public
static
protected
synchronized
analysis: A, public can modify classes, data members, construction methods, method members. Members modified by public can be called in any class, regardless of the same package or different packages, which is the most powerful modifier.
B. Static is used to modify member variables and member methods, and can also form static static code blocks, but there is no concept of global variables in the Java language.
C. Private can modify data members, construction methods, and method members, but cannot modify classes (here refers to external classes, and internal classes are not considered). Members modified by private can only be used in the class in which they are defined, and cannot be called in other classes.
D. Synchronized can be used to lock objects and methods or code blocks. When it locks a method or a code block, only one thread can execute this code at the same time.

There are two definitions of the following classes:
1
2
3
4
5
6
7
class Person {}
class Student extends Person { public int id; //student number public int score; //total score public String name; // name public int getScore(){return score;} } The relationship between class Person and class Student is (). Correct answer: B containment relationship inheritance relationship association relationship No relationship, the above class definition has grammatical errors. Analysis: Six relationships between classes in Java: the first type: inheritance relationship, for example: subclass inherits parent class, child interface inherits parent interface. The second type: the realization relationship, for example: the class realizes the interface. The third type: dependency relationship, for example: a class exists as a parameter of a method in another class, this relationship is accidental and temporary. The fourth type: association relationship, for example: a class exists as a member variable in another class, it is a strong dependency. The fifth type: aggregation relationship, for example: between the whole and the parts, they can be separated and have their own life cycles, and the parts are the same. Like flocks and birds.

















The sixth type: combination relationship, it is a kind of strong aggregation, and the whole and the part are inseparable, and have the same life cycle. The whole contains this part and also contains other parts. Other parts are different from this part, like CPU and computer. In addition to the cpu, the computer also has a keyboard and a monitor.

What keywords must be used to define an interface? () The
correct answer: C
public
class
interface
static
public public common
class
interface interface
static static,
so choose C.Interface interface

What is wrong in the list? ()

Correct answer: D
int is a basic type, which stores the value directly, and Integer is an object. Use a reference to point to this object.
Use super() in the subclass construction method to display the construction method of calling the parent class, super() must be written in the first line of the subclass construction method, otherwise
the main function of the compilation failure encapsulation is to hide the internal implementation details from the outside, which can be enhanced The security of the program
final is a modifier in java, which can modify classes, interfaces, abstract classes, methods and attributes.
Analysis: abstract class: subclass inheritance rewrite
final: rewrite is not allowed

Which of the following descriptions of init, service, and destroy methods in servlet are wrong?
Correct answer: D
init() method is the starting point of servlet life. Once a servlet is loaded, the server will immediately call its init() method
service() method to process all requests sent by the client. The
destroy() method marks the end of the
servlet life cycle. The servlet uses a synchronization mechanism under multithreading. Therefore, Under concurrent programming, servlet is a thread-safe
resolution: init method: It is a method called when the servlet instance is created. It is used to create or open any resources related to the servlet and initialize the state of the servlet. The Servlet specification guarantees that the init method will not be called before
Service method for processing any request : It is the method by which the servlet actually processes the request from the client. It is called by the web container. According to the HTTP request method (GET, POST, etc.), the request is distributed to doGet, doPost and other methods.
Destroy method: it is in the servlet Called by the web container when the instance is destroyed. The Servlet specification ensures that the processing of all requests is completed before the destroy method is called, and the situation of the destroy method needs to be overwritten: release any servlet-related resources opened in the init method to store the servlet state

The concept of multiple inheritance is implemented in Java through which of the following? ()
I. Extend two or more classes
II. Extend a class and implement one or more interfaces.
III. Implement two or more interfaces.

Correct answer: B
only I & II
only II & III
only III
are
analytical: Java only supports single inheritance, three ways to achieve multiple inheritance: (1) directly implement multiple interfaces (2) extend a class and then implement one Or multiple interfaces (3) inherit other classes through inner classes

In Java, what methods are used in HashMap to resolve hash conflicts?
Correct answer: C
open address method,
secondary hash method,
chain address method,
establish a public overflow area.
Resolution: use the chain address method to resolve conflicts

Given the following code
Copy code
1
2
3
4
5
6
7
8
9
10
public class TestObj{ public static void main(String[] args){ Object o=new Object(){ public boolean equals(Object obj){ return true; } }; System.out.println(o.equals("Fred")); } } Please give the result: () Correct answer: B throws an exception at runtime true Fred Compilation error analysis on the third line : object is all classes The parent class of, definitely contains the "fred" class, and all classes contain the equal method. The preceding content is equivalent to overriding the equal method so that it can only return true.















Check the program, whether there is a problem, if it does, point out the problem, if it does not exist, indicate the output result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class HelloB extends HelloA
{ public HelloB() { } { System.out.println(" I'm B class”); } static { System.out.println(“static B”); }










public static void main(String[] args)
{
new HelloB();
}
}
class HelloA
{
public HelloA()
{
}
{
System.out.println(“I’m A class”);
}
static
{
System.out.println(“static A”);
}
}

Correct answer: C Your answer: B (wrong)
static A
I'm A class
static B
I'm B class

I’m A class
I’m B class
static A
static B

static A
static B
I’m A class
I’m B class

I'm A class
static A
I'm B class
static B
analysis: 1. Static code block 2. Construction code block 3. The execution order of the construction method is 1>2>3; understand what they are doing.
1. Static code block: It is carried out during the initialization of the third step of the loading process of the class, and the main purpose is to give the initial value to the class variable.
2. Construction code block: It is independent and must be attached to the carrier to run. Java will put the construction code block in front of each construction method to instantiate some common instance variables and reduce the amount of code.
3. Construction method: used to instantiate variables.
1 is at the class level, 2 and 3 are at the instance level, naturally 1 is preferred 23.

What is the purpose of the command javac-d parameter? ()
Correct answer: A
Specify the root directory of the class hierarchy after
compilation. Specify the path of the dependent class
when compiling. Specify the encoding when compiling.
There is no such parameter.
Analysis: -d destination destination
-s source origin
javac -d specify to place the generated The location of the class file
javac -s specifies the location of the generated source file

The cross-platform nature of Java means that its source code can run on multiple platforms. () The
correct answer: B is
correct and
wrong.
Analysis: javac compiles, but java's own runtime environment is not the original code.

The correct statement about the following program Test.java is ().
1
2
3
4
5
6
7
8
public class Test { static String x=“1”; static int y=1; public static void main(String args[]) { static int z=2; System.out.println(x +y+z); } }






Correct answer: D
3
112
13 The
program has a compilation error.
Analysis: The variable modified by static belongs to the class and can only be defined outside the method in the class

  1. class Line {
  2. public class Point { public int x,y;}
  3. public Point getPoint() { return new Point(); }
  4. }
  5. class Triangle {
  6. public Triangle() {
  7. // insert code here
  8. }
  9. }
    Which piece of code is inserted in line 16 to get the coordinates of a Point object? ()
    Correct answer: D
    Point p = Line.getPoint();
    Line.Point p = Line.getPoint();
    Point p = (new Line( )).getPoint();
    Line.Point p = (new Line()).getPoint();
    Analysis: (1) If a class is defined inside another class, the class is called an inner class.
    Example: Define class B in class A, and class B is called an inner class.
    (2) Access rules for internal classes
    A: You can directly access members of external classes, including private
    B: For external classes to access internal class members, you must create objects
    (3) Classification
    of internal classes A: Member internal class
    B: Local internal Class
    C: Anonymous inner class
    (4) Member inner class access rules
    Member inner class is not static:
    outer class name. inner class name object name = new outer class name (). new inner class name ();
    member inner class is static :
    External class name. Internal class name Object name = new external class name. Internal class name ();
    (5) Local internal class
    A: Local internal class access to local variables must be finalized.
    B: Why?
    Because the local variables disappear after they are used, but the data in the heap memory does not disappear immediately.
    Therefore, the variable is still used in the heap memory, and the amount of change is gone.
    In order to make the value still exist, add final modification.
    Through the decompilation tool, we have seen that after adding final, the heap memory directly stores the value instead of the variable name.
    (6) Anonymous inner class (master)
    A: It is a simplified form of a local inner class
    B: The premise
    is that there is a class or interface
    C: Format:
    new class name or interface name () { Override method; } D: Essence: In fact Anonymous objects of subclasses that inherit the class or implement the interface



What is the index of the first column of the record row in the ResultSet?
Correct answer: C
-1
0
1
None of the above is
analytic: The method of reading data in the ResultSet result set is mainly getXXX(). Its parameter can be an integer to indicate the number of columns (starting from 1), or it can be a column name

The subsystem that implements transaction persistence in the DBMS is ()

Correct answer: D
Security Management Subsystem,
Integrity Management Subsystem,
Concurrency Control Subsystem,
Recovery Management Subsystem
Analysis: Atomicity: A transaction is a set of indivisible operating units, which either succeed or fail at the same time (by the DBMS Transaction management subsystem to achieve);
Consistency: the data integrity before and after the transaction must be consistent (test tasks performed by the integrity subsystem of the DBMS);
Isolation: the transactions of multiple users must not affect each other, and must be isolated from each other ( Implemented by the concurrency control subsystem of the DBMS);
Persistence: Once a transaction is committed, its impact on the database is permanent and irreversible. If it is rolled back later or an exception occurs, it will not affect the committed transaction (by DBMS's recovery management subsystem)

There are generally two methods for creating threads, one is () and the other is ().
Correct answer: BD
derives a new thread class from Java.lang.Thread and rewrites its runnable() method.
Derives a new thread class from Java.lang.Thread and rewrites its run() method to
implement Thread. Interface, rewrite the run() method in the Thread interface.
Implement the Runnable interface and rewrite the run() method in the Runnable interface.
Analysis: There are two ways to create a thread object:
1. Inherit the Thread class and overload the run method;
2. Implement Runnable Interface, implement the run method

Which of the following are valid keywords in java ()

Correct answer: AD
native
NULL
false
this
analysis: the keyword capitalized first letter
CMS garbage collector does not use user threads to participate in those stages.
Initial marking
Concurrent marking
Remarking
concurrent cleanup
analysis: User-level threads are not required The threads supported by the kernel and implemented in the user program do not depend on the core of the operating system. The application process uses the thread library to provide functions for creating, synchronizing, scheduling and managing threads to control the user threads.
The CMS GC process has 6 stages (4 concurrent, 2 suspend other applications)

  1. STW initial mark
  2. Concurrent marking
  3. Concurrent interruptible precleaning (Concurrent precleaning)
  4. STW remark
  5. Concurrent sweeping
  6. Concurrent reset (Concurrent reset)
    requires us to suspend other applications when it is first marked and re-marked, so user threads will not participate in these two phases.

Which of the following methods can be used to implement inter-thread notification and wake-up: ()
Correct answer: AC
Object.wait/notify/notifyAll
ReentrantLock.wait/notify/notifyAll
Condition.await/signal/signalAll
Thread.wait/notify/notifyAll
analysis: wait(), notify() and notifyAll() are methods in the Object class.
From the text description of these three methods, you can know the following information:

1) The wait(), notify() and notifyAll() methods are local methods, and are final methods, which cannot be overridden.

2) Calling the wait() method of an object can block the current thread, and the current thread must own the monitor (ie lock) of this object

3) Calling the notify() method of an object can wake up a thread that is waiting for the monitor of this object. If multiple threads are waiting for the monitor of this object, only one of them can be awakened;

4) Call the notifyAll() method to wake up all the monitor threads that are waiting for this object;

Some friends may have questions: why these three are not the methods in the Thread class declaration, but the methods declared in the Object class

(Of course, because the Thread class inherits the Object class, Thread can also call three methods)? Actually this question

The question is very simple, because each object has a monitor (ie lock), so let the current thread wait for the lock of an object, of course

It should be operated through this object. Instead of using the current thread to operate, because the current thread may wait for multiple threads

If the lock is operated by thread, it is very complicated.

As mentioned above, if the wait() method of an object is called, the current thread must own the monitor of this object (ie

Lock), so the call to the wait() method must be performed in a synchronized block or synchronized method (synchronized block or

synchronized方法)。

Calling the wait() method of an object is equivalent to letting the current thread surrender the monitor of this object, and then enter the waiting state,

Wait for the subsequent acquisition of the lock of this object again (the sleep method in the Thread class causes the current thread to suspend execution for a period of time, from

And let other threads have a chance to continue execution, but it does not release the object lock);

The notify() method can wake up a monitor thread that is waiting for the object, when multiple threads are waiting for the object

If the monitor is used, only one of the threads can be awakened, and it is not known which thread to awaken.

Similarly, to call the notify() method of an object, the current thread must also own the monitor of this object, so call

The notify() method must be performed in a synchronized block or synchronized method (synchronized block or synchronized method).

The nofityAll() method can wake up all the threads of the monitor waiting for the object, which is different from the notify() method.
Condition only appeared in java 1.5. It is used to replace the traditional Object's wait() and notify() to achieve inter-thread cooperation. Compared with the use of Object's wait() and notify(), use Condition1's await() , Signal() This way to achieve inter-thread collaboration is safer and more efficient. Therefore, it is generally recommended to use Condition, as described in the blog post on blocking queues, blocking queues actually use Condition to simulate inter-thread cooperation.
Condition is an interface, and the basic methods are the await() and signal() methods;
Condition depends on the Lock interface. The basic code for generating a Condition is lock.newCondition() to
call Condition’s await() and signal() methods, both must be in Within the lock protection, it means that await() in Conditon corresponds to wait() of Object; signal() in Condition corresponds to notify() of Object; Condition must be used between lock.lock() and lock.unlock. SignalAll() in corresponds to notifyAll() of Object

What are the synchronizers provided by the JDK for concurrent programming?
Correct answer: ABC
Semaphore
CyclicBarrier
CountDownLatch
Counter
Analysis: Synchronizers are objects that enable threads to wait for another thread, allowing them to coordinate actions. The most commonly used synchronizers are CountDownLatch and Semaphore, and the less commonly used ones are Barrier and Exchanger

Guess you like

Origin blog.csdn.net/weixin_56071709/article/details/115001884