Java popular written test questions sorting
Xiaobian has sorted out the very popular written interview questions in 2017 for the friends interviewing Java programmers. If you are a reader preparing to interview Java programmers, learn it quickly.
1. The following statement is correct ()
A. Two public modified classes can exist in a file at the same time
B. Constructors can be overridden
C. Subclasses cannot access non public and protected modified properties of the parent class
D. Finally decorated classes can be inherited
Answer: C
There can only be one public class in a java source file at most. When there is a public class, the source file name must be consistent with it, otherwise it cannot be compiled. If there is no public class in the source file, there is no consistency requirement between the file name and the class. As for main (), you don't have to put it in the public class to run the program.
Override is to modify the method of the parent class after the child class inherits the parent class. Method name, parameter and return value must be the same. A method marked final cannot be overridden. If you cannot inherit a method, you cannot override it.
Extension: the difference between overriding and overloading
Java method overloading
That is, you can create multiple methods in a class, which have the same name, but different parameters and different definitions. When calling methods, the specific method to be used is determined by the number of different parameters and parameter types passed to them, and the return value types can be the same or different, which is also object-oriented polymorphism.
Java method rewriting
The polymorphism between parent and child classes redefines the functions of the parent class. If a method defined in a subclass has the same name and parameters as its parent class, we say that the method is overridden. In Java, subclasses can inherit the methods in the parent class without rewriting the same methods. However, sometimes subclasses do not want to inherit the methods of the parent class intact, but want to make certain modifications, which requires method rewriting. Method overrides are also called method overrides.
If a method in the subclass has the same method name, return type and parameter table as a method in the parent class, the new method will overwrite the original method. If you want the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.
The access rights of subclass functions cannot be less than those of the parent class;
Rewriting methods can only exist in inheritance relationships, and rewriting methods can only override non private methods of the parent class.
2,for(int x=0,y=0;(y!=0)&&(x<4); The number of execution times of X + +) loop is ()
A. Infinite times
B. 4 times
C. 3 times
D. Not once
Answer: D
The initial value of Y is 0. In the whole for loop, the value of Y remains unchanged, so (Y! = 0) in the judgment statement is not tenable, so it will not be executed once.
3. The following statement about Java heap is wrong ()
A. All instances and arrays of classes allocate memory on the heap
B. The heap memory occupied by the object is reclaimed by the automatic memory management system
C. Heap memory consists of living and dead objects and free fragment areas
D. Arrays are allocated on the stack
Answer: D
First, the array is allocated in the heap, so the statement of D is incorrect.
Java heap structure: the JVM heap is a runtime data area, and all class instances and arrays allocate memory on the heap. It is created when the JVM starts. The heap memory occupied by objects is collected by the automatic memory management system, that is, the garbage collector. Heap memory consists of living and dead objects. The surviving objects are accessible to the application and will not be garbage collected. Dead objects are objects that are inaccessible to the application and have not been recycled by the garbage collector. Until the garbage collector recycles these objects, they will occupy heap memory space.
4. When using the super and this keywords, the following description is correct ()
A. Use super () in the subclass constructor to display the constructor that calls the parent class;
Super () must be written in the first line of the subclass constructor, otherwise the compilation will not pass
B. Super () and this () do not have to be placed in the first line of the constructor
C. This () and super () can appear in a constructor at the same time
D. This () and super () can be used in the static environment, including static methods and static statement blocks
Answer: a
The Java keyword this can only be used in a method body. After an object is created, the Java virtual machine (JVM) will assign a pointer to the object that references itself. The name of the pointer is this. Therefore, this can only be used in non static methods in the class, and this must not appear in static methods and static code blocks.
The super key is similar to this in that the masked member variables or member methods become visible, or are used to reference the masked member variables and member methods.
However, super is used in subclasses to access the masked members in the direct parent class. Note that it is the direct parent class (that is, the nearest superclass above the class)
5. Which of the following statements is correct ()
A. Java programs will generate machine code after compilation
B. The compiled Java program will generate byte code
C. Java programs will generate DLLs after compilation
D. None of the above is correct
Answer: B
Java bytecode is an intermediate file generated by compiling java source files
Java virtual machine is an imaginary computer that can run Java bytecode. Java's cross platform is also relative to other programming languages.
First introduce the compilation process of C language: the C file is compiled by the C compiler to generate the windows executable file EXE file, and then executed in windows.
Then introduce the compilation process of Java: the java file is executed by the java compiler. The Java bytecode file is the class file in the Java virtual machine. The machine code is executed by the CPU; Java compiled bytecode.
The computer can only run machine code. Java turns bytecode into machine code when running. C / C + + is directly compiled into machine code when compiling
6. Which of the following statements is correct ()
A. The abstract modifier modifies fields, methods, and classes
B. The body part of the abstract method must be enclosed by a pair of braces {}
C. Declare abstract methods, braces are optional
D. Declaring abstract methods cannot write out braces
Answer: D
The abstract modifier is used to decorate classes and member methods
Abstract modified classes represent abstract classes. Abstract classes are located in the abstract layer of the inheritance tree, and abstract classes cannot be instantiated.
Abstract modified methods are used to represent abstract methods. Abstract methods have no method body. Abstract methods are used to describe the functions of the system, but do not provide specific implementation.
Abstract is an important keyword in Java, which can be used to modify a class or a method.
When decorating a method, it means that the method has only signature and no specific implementation, but leaves the specific implementation to the subclass inheriting the class, so there can be no curly braces.
7. The following statements are correct ()
A. The constructor in class cannot be omitted
B. The constructor must have the same name as class, but the method cannot have the same name as class
C. Constructor executes when an object is new
D. A class can only define one constructor
Answer: C
There may be misunderstandings here. In fact, ordinary class methods can have the same name as the class name. The only difference from construction methods is that construction methods do not return values.
8. Whether the GC thread is a daemon ()
Answer: Yes
Threads are divided into guard threads and non guard threads (i.e. user threads).
As long as any non daemon thread in the current JVM instance has not ended, the daemon thread will work completely; Only when the last non daemon thread ends, the daemon thread ends working with the JVM.
The most typical application of daemon thread is GC (garbage collector)
9. About sleep () and wait (), one of the following errors is ()
A. Sleep is the method of thread class and wait is the method of object class;
B. Sleep does not release the object lock, and wait discards the object lock;
C. Sleep pauses the thread, but the monitoring state is still maintained. It will automatically resume after the end;
D. After waiting, it enters the waiting lock pool. Only after issuing the notify method for this object, the object lock is obtained and enters the running state.
Answer: D
Sleep is a method of thread class, which causes this thread to suspend execution for a specified time and give execution opportunities to other threads, but the monitoring status remains and will be automatically restored after that time. Calling sleep will not release the object lock.
Wait is a method of object class. Calling the wait method on this object causes this thread to give up the object lock and enter the waiting lock pool waiting for this object. This thread enters the object lock pool only after issuing the notify method (or notifyAll) for this object, ready to obtain the object lock and enter the running state.
10. The method resume () is responsible for resuming the execution of which threads ()
A. A thread that is stopped by calling the stop () method.
B. A thread that is stopped by calling the sleep () method.
C. A thread that is stopped by calling the wait () method.
D. A thread that is stopped by calling the suspend () method.
Answer: D
Suspend can suspend a thread, that is, suspend the thread. It occupies resources but does not run. Use resume to resume the suspended thread and let the thread continue to execute.