Niu Ke net java basic knowledge brush question record-01

  1. Assume Base b = new Derived(); What is the output result after calling and executing b.methodOne()?
public class Base
{
   public void methodOne()
   {
      System.out.print("A"); // 3. Output A
      methodTwo();  // 4. The method two method overridden by the subclass is called
   }
 
   public void methodTwo()
   {
      System.out.print("B"); // 6. Output B
   }
}
 
public class Derived extends Base
{
   public void methodOne() // 1. First step
   {
      super.methodOne();  //2. Call super to call the methodOne() method of the parent class
      System.out.print("C"); // 8. Output C
   }
 
   public void methodTwo()
   {
      super.methodTwo();  // 5. Call the methodTwo() method of the parent class
      System.out.print("D"); //7. Output D
   }
}
  • ABDC
  • AB
  • ABCD
  • ABC
Investigation contents:
	1. Rules for using polymorphic member methods: compile to the left and run to the right
	2. In polymorphism, subclass override method, when super Call is to call the parent class method (as long as it is a method overridden by a subclass, it will not be overridden) super All calls are subclass methods]
  1. Which description of the following Java program is correct: ()
public class ThreadTest extends Thread {
    public void run() {
        System.out.println("In run");
        yield();
        System.out.println("Leaving run");
    }
    public static void main(String []argv) {
        (new ThreadTest()).start();
    }
}
  • The output of program running is only In run
  • The output of program running is only Leaving run
  • The program run output has In run first and then Leaving run
  • The output of the program runs first with Leaving run and then with In run
  • The program exited without any output
  • The program will be suspended and can only be forced to exit
Investigation contents:
	1. Thread.yidel()The function of this method is to pause the currently executing thread object and execute other threads.
	2. The above code has two threads, a main thread and a ThreadTest Thread, main thread call ThreadTest It's over after, so yield()The comity thread has no effect because there is only one thread at this time.
  1. What's true about final? ( )
  • The method of final class cannot be accessed by the class of the same package
  • Whether the methods of the final class can be accessed by the classes of the same package is not determined by final
  • The final method is equivalent to the private method
  • The reference and value of the final object itself cannot be changed
Investigation contents:
	1. final Modifier variable: final Variables can be explicitly initialized and can only be initialized once. Declared as final A reference to an object cannot point to a different object. however final The data in the object can be changed. in other words final The reference of the object cannot be changed, but the value inside can be changed. final Modifiers are usually associated with static Modifier to create a class constant.
	2. final Decorating method: in class final Methods can be inherited by subclasses, but cannot be modified by subclasses. statement final The main purpose of the method is to prevent the content of the method from being modified.
	3. final Decoration class: final Class cannot be inherited. No class can inherit final Class.
  1. For the constructor description of subclasses, the error in the following description is ().
  • A subclass cannot inherit the parameterless constructor of a parent class.
  • A subclass can use the super keyword in its constructor to call the constructor with parameters of the parent class, but the call statement must be the first executable statement of the subclass constructor.
  • When creating an object of a subclass, if there is no parameter constructor, the parameterless constructor of the parent class will be executed first, and then its own parameterless constructor will be executed.
  • Subclasses can inherit not only the parameterless constructor of the parent class, but also the parameterless constructor of the parent class.
Investigation contents:
	1.  Constructors cannot be inherited, and constructors can only be called explicitly or implicitly.
  1. The following is true:
Integer i = 42;
Long l = 42l;
Double d = 42.0;
  • (i == l)
  • (i == d)
  • (l == d)
  • i.equals(d)
  • d.equals(l)
  • i.equals(l)
  • l.equals(42L)
Investigation contents:
	1. Basic and basic package“=="Operator, the basic encapsulated type will be unpacked automatically and changed into the basic type before comparison, so Integer(0)Will automatically unpack for int Types are compared and obviously returned true;
	2. Two Integer Type“=="Compare if its value is-128 To 127, then return true,Otherwise return false, This with Integer.valueOf()It is related to the buffer object of, and will not be repeated here.
	3. The packaging of two basic types is carried out equals()Comparison, first equals()The types will be compared. If the types are the same, continue to compare the values. If the values are the same, return true. 
	4. Basic encapsulation type call equals(),However, the parameter is a basic type. At this time, automatic packing will be carried out first, and the basic type will be converted to its packaging type, and then the comparison in 3 will be carried out. 
  1. What are the running results of the following JAVA programs ()
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);
}
  • 1 1
  • 1.0 1.0
  • 1 1.0
  • 1.0 1
Investigation contents:
	Conversion rules for ternary operator types:
		1.If the two operands are not convertible, they will not be converted, and the return value is Object type
		2.If two operands are explicitly typed expressions (such as variables), they are converted according to normal binary numbers, int Type conversion to long Type, long Type conversion to float Type, etc.
		3.If one of the two operands is a number S,The other is an expression, and its type is marked as T,So, if the number S stay T Within the range of, convert to T Type; if S Beyond T Type range, then T Convert to S Type.
		4.If both operands are direct quantity numbers, the return value type is the one with the larger range
  1. The following statement about ThreadLocal class is correct
  • ThreadLocal inherits from Thread
  • ThreadLocal implements the Runnable interface
  • ThreadLocal plays an important role in data sharing among multiple threads
  • ThreadLocal uses a hash table to provide a copy of a variable for each thread
  • ThreadLocal ensures the data security between threads, and the data of each thread will not be accessed and destroyed by other threads
Investigation contents:
	1. ThreadLocal Class declaration for: public class ThreadLocal<T>It can be seen that ThreadLocal Not inherited from Thread,Nor has it been achieved Runnable Interface.
	2. ThreadLocal Class maintains its own unique copy of variables for each thread. Each thread has its own independent variable. therefore ThreadLocal The important role is not the data sharing among multiple threads, but the independence of data.
	3. ThreadLocal A variable in the hash table is defined for each thread
  1. The access permissions of abstract class methods are public by default. ( )
  • correct
  • error
Investigation contents:
	1. About abstract classes
		JDK 1.8 Previously, the default access permissions for methods of abstract classes were protected
		JDK 1.8 The default access permission of the method of the abstract class becomes default
	2. About interface
		JDK 1.8 Previously, methods in interfaces had to be public of
		JDK 1.8 The method in the interface can be public Yes, it can also be default of
		JDK 1.9 The method in the interface can be private of
	3. The field modifiers in the interface are: public static final(Default (no write)
  1. For multithreading and multiprocessing, the following description is correct ():
  • In multi process, the child process can obtain all heap and stack data of the parent process; Threads share data with other threads in the same process and have their own stack space.
  • Because threads have their own independent stack space and share data, the overhead of all execution is relatively large, which is not conducive to resource management and protection.
  • Threads communicate faster and switch faster because they are in the same address space.
  • A thread can belong to multiple processes.
Investigation contents:
	1. A thread can only belong to one process, and a process can have multiple threads, but at least one thread (commonly known as the main thread).
	2. Resources are allocated to processes, and all threads of the same process share all resources of the process.
	3. During the execution of threads, cooperative synchronization is required. The threads of different processes should be synchronized by means of message communication.
	4. The processor is allocated to threads, that is, threads are really running on the processor.
	5. Thread refers to an execution unit in a process and a schedulable entity in the process.
  1. Subclass A inherits parent class B, A = new A(); Then the execution order of the constructor of parent class B, the static code block of parent class B, the non static code block of parent class B, the constructor of subclass A, the static code block of subclass A and the non static code block of subclass A is?
  • Static code block of parent class B - > constructor of parent class B - > static code block of subclass a - > non static code block of parent class B - > constructor of subclass a - > non static code block of subclass a
  • Static code block of parent class B - > constructor of parent class B - > non static code block of parent class B - > static code block of subclass a - > constructor of subclass a - > non static code block of subclass a
  • Static code block of parent class B - > static code block of child class A - > non static code block of parent class B - > constructor of parent class B - > non static code block of child class A - > constructor of child class A
  • Constructor of parent class B - > static code block of parent class B - > non static code block of parent class B - > static code block of subclass a - > constructor of subclass a - > non static code block of subclass a
Investigation contents:
	1. When instantiating a subclass object, first load the of the parent class class When the file enters the memory, the static code block is executed with the creation of the class, so the static code block of the parent class is executed first and the static code block of the child class is executed first class The file is loaded again, and similarly, the static code block is executed first; To instantiate a subclass object, you must first call the constructor of the parent class, and the non static code block of the parent class will be executed before calling the constructor of the parent class
	2. Parent classBStatic code block->SubclassAStatic code block->Parent classBNon static code block->Parent classBConstructor->SubclassANon static code block->SubclassAConstructor
  1. Define the following methods in the class Tester. If public double max(int x, int y) {/ / omit}, which of the following method headers is an overload of the above methods?
  • public int max(int a, int b) {}
  • public int max(double a, double b) {}
  • public double max(int x, int y) {}
  • private double max(int a, int b) {}
Investigation contents:
	Java Overloaded rules:
		1,Must have different parameter lists;
		2,You can have different return types as long as the parameter list is different;
		3,There can be different access modifiers;	
		4,Different exceptions can be thrown;	
		5,Methods can be overloaded in a class or in a subclass.
  1. There are the following code snippets:
String str1="hello";
String str2="he"+ new String("llo");
System.out.println(str1==str2);

The output result is:

  • true
  • None of it
  • null
  • false
Investigation contents:
	1)String Class is final Class, which means String Class cannot be inherited, and its member methods default to final method. stay Java In, by final The decorated class is not allowed to be inherited, and the member methods in the class are defaulted to final method.
    2)String Class bottom is char Array to hold the string. Also by final Modified
  1. The basic points of the single example are two
  • Constructor private
  • Static factory method
  • None of the above is true
  • Unique instance
Investigation contents:
	1,A singleton class can only have one instance.
	2,A singleton class must create its own unique instance.
	3,A singleton class must provide this instance to all other objects.
  1. The following types are Final ()
  • HashMap
  • StringBuffer
  • String
  • Hashtable
Investigation contents:
	1. string And stringbuffer Are implemented through character arrays.
	2. among string The character array is final Modified, so the character array cannot be modified.
	3. stringbuffer The character array of does not have final Modifier, so the character array can be modified.
	4. string And stringbuffer All final Modification is just to restrict the reference address they store from being modifiable. As for whether the content of the address can be modified, it depends on whether the character array can be modified.

Keywords: Java Back-end

Added by dyluck on Tue, 22 Feb 2022 18:10:18 +0200