Daily Java practice questions (a little progress every day series)

prompt: Many friends reported that the answer is not very good when you see directly, then I willThe color of the answer is set to transparent, The answer is still behind the question, you need to select it with the mouse to see it (as shown in the figure below). At the same time, for the small partners on the mobile phone (the mobile phone can also be selected and viewed by long press), I will also put all the answers at the bottom of the article, I hope to give Friends who make a little progress every day have a better experience.I'm late, but I'm not absent. I will try to ensure that I have more Java practice questions every day. At the end of the article, there is a limited-time benefit discount for everyone to win the official CSDN [Java Engineer Knowledge Graph], which will help you grow! Come on! Make a little progress every day!

Insert picture description here

1、Which is not Java primitive types? (Which is not Java primitive types?)
Correct answer: B Your answer: B (correct)

Boolean is a packaged class, not a primitive class.
Java requires that the first letter of the class name must be capitalized. It can be seen intuitively that Boolean is a reference type, not a basic data type.
The basic data types in java correspond to a reference type. For example, Float is a reference type of float, and Integer is a reference type of int.
Java data types are divided into two categories: basic data types and reference data types (abstract data types)
basic data types (8 types):
Integer type:
byte 1 byte 8 bits (0000 0000)
short 2 bytes
int 4 bytes
long 8 bytes
Floating point type:
float 4 bytes
double 8 bytes
Character type:
char 2 bytes
Boolean Type:
boolean length is disputed (true, false) The
first character of the package type corresponding to the basic data type is capitalized, except for int and char
reference data type: (in memory Address)
class, interface, array

21. Which of the following class definitions are legal abstract class definitions? ()
Correct answer: C Your answer: C (correct)
abstract Animal{abstract void growl();}
class abstract Animal{abstract void growl();}
abstract class Animal{abstract void growl();}
abstract class Animal{ abstract void growl(){System.out.println( “growl”);};}

1. First of all, the modifier of the class should be before the class keyword, AB is wrong;
2. Abstract method cannot have method body, D is wrong.

3In the following variable definition statements, the legal one is () the
correct answer: C Your answer: C (correct)
long a=123L

Insert picture description here

4, What type of result is the expression (short)10/10.2*2 after operation?
Correct answer: C Your answer: C (correct)

First of all, pay attention to (short)10/10.2*2, not (short) (10/10.2 * 2). The former just converts the strong 10 to short, and because there are floating-point numbers in the formula, it will The result value undergoes an automatic type promotion. The floating-point number defaults to double, so the answer is double; the latter is to force the value to short after the calculation is completed.

5, Under the respective optimal conditions, to sort N numbers, which algorithm has the lowest complexity? ()
Correct answer: A Your answer: A (correct)
Insertion sort
Quick sort
Heap sort
Merge sort

Problem solution:
For insertion sort, the optimal condition is that it is ordered by itself, so it is enough to loop it again.
Insert picture description here

6, Which of the following codes (A, B, C, D) to replace the [code] in the following program will not cause a compilation error?
interface Com{ int M=200; int f(); } class ImpCom implements Com{ [Code] } Correct answer: A Your answer: A (correct) public int f(){return 100+M;} int f( ){return 100;} public double f(){return 2.6;} public abstract int f();

Solution: There are
two test points for this question:
1. The implementation class can get the attributes in the interface.
2. The realization needs to be a public method

7, Which of the following functions are overloaded functions of public void example(){…}? ()
Correct answer: AD Your answer: AD (correct)
public void example(int m){…}
public int example(){…}
public void example2(){…}
public int example(int m,float f) {...}

Overloading only requires that the parameter list is different, and the return value is irrelevant.
When java is overloaded, it is distinguished by the number of parameters and the type. The method name is the same, and the return type can be the same or different, but the return type is not used as the distinction, so b is also wrong, because the parameter list of b is the same as the original

8, The difference between abstract class and interface, the following statement is wrong is the
correct answer: ACD Your answer: ACD (correct)
A: The interface is public, there can be no private methods or variables, it is for others to use, and Abstract classes can have private methods or private variables.
B: Abstract class represents an inheritance relationship in the Java language. A class can only use the inheritance relationship once. However, a class can implement multiple interfaces to achieve multiple inheritance. The interface also has the functions of identification (there are no methods in it, such as the Remote interface) and data sharing (the variables in it are all constants).
C: In the abstract class, you can have your own data members or non-abstarct member methods. In the interface, you can only have static data members that cannot be modified (that is, they must be static final, but in the interface Data members are generally not defined in ), and all member methods are of public abstract type by default.
D: Abstract class and interface reflect different design concepts. In fact, the abstract class represents the "has-a" relationship, and the interface represents the "is-a" relationship.

Abstract class: In Java, a class modified by the abstract keyword is called an abstract class, and a method modified by the abstract keyword is called an abstract method. Abstract methods have only method declarations and no method bodies. The characteristics of abstract classes:
a. Abstract classes cannot be instantiated and can only be inherited;
b. Contains abstract methods must be abstract classes, but abstract classes do not necessarily contain abstract methods;
c. The modifiers of abstract methods in abstract classes are only It can be public or protected, and the default is public;
d. If a subclass inherits an abstract class, the subclass must implement the abstract method of the parent class, otherwise the subclass must also be defined as an abstract class;
e. The abstract class can contain attributes, methods, Construction method, but the construction method can not be used for instantiation, the main purpose is to be called by subclasses.
Interface: The interface in Java is modified with the interface keyword. The characteristics are:
a. The interface can contain variables and methods; variables are designated as public static final by the hermit, and methods are designated as public abstract by the hermit (before JDK1.8);
b. Interface support Multiple inheritance, that is, one interface can extend multiple interfaces, which indirectly solves the single inheritance problem of classes in Java;
c. One class can implement multiple interfaces;
d. New features are added to the interface in JDK1.8:
(1 ), the default method (default method): JDK 1.8 allows adding non-abstract method implementation to the interface, but it must be modified with the default keyword; the method that defines the default can not be implemented by the implementation subclass, but can only be implemented by the implementation subclass Object call; if the subclass implements multiple interfaces, and these interfaces contain the same default method, the subclass must override the default method;
(2) Static method: In JDK 1.8, the static keyword is allowed to modify a method and provide an implementation, which is called an interface static method. The interface static method can only be called through the interface (interface name. static method name).
Note: jdk1.9 is the default method and static method that allows private modification in the interface.
A: jdk1.9 is the default method and static method that allows private modification in the interface, A is wrong; abstract classes can have private variables and methods.
B: Correct
C: Abstract classes can have abstract and non-abstract methods; jdk1.8 interface can have default methods and static methods, C is wrong.
D: Emphasize the inheritance relationship, is-a, if A is-a B, then B is the parent of A;
represents the composition relationship, like-a, interface, if A like a B, then B is the interface of A. ;
Emphasize the subordination relationship, has-a, if A has a B, then B is a component of A.
Item D is wrong.

9, The following narrative about java constructor is correct ()
correct answer: CD Your answer: CD (correct)
The return value of the constructor is void.
If there are multiple classes in a source file, then the constructor must have the same name as the public class
The constructor can have 0, 1, or more than one parameter.
Each class can have more than one constructor.

(1) The constructor of A has no return value and is wrong;
(2) When there are multiple classes in B, the constructor may not have the same name as the public class, because each class can have a constructor, and the function name belongs to the same class, which is wrong;
(3) The C constructor can have any number of parameters, which is correct;
(4) Each class of D has a constructor by default. The description of "more than one" in the options makes people misunderstand that there are at least two parameters , so it is wrong.

10, The following statement about the final keyword is wrong is the
correct answer: AC Your answer: AC (correct)
final is a modifier in java. Classes, interfaces, abstract classes, methods, and properties
that can be modified by final modification must not be inherited.
Final modified methods cannot be overloaded
final modified variables are not allowed to be assigned again

Problem solution:
final modification of classes, methods, attributes! Abstract classes cannot be modified, because abstract classes generally need to be inherited, and cannot be inherited after final modification.
The final modified method cannot be overridden and not overloaded!
The final modification attribute, this attribute is a constant and cannot be assigned again!

Answer summary:
1. Correct answer: B
2. Correct answer: C
3. Correct answer: C
4. Correct answer: C
5. Correct answer: A
6. Correct answer: A
7. Correct answer: AD
8. Correct answer: ACD
9, correct answer: CD
10, correct answer: AC

Some of the above questions are from the Niuke comment area, thank you for the explanation of the comment area.

CSDN official exclusive production of [Java Engineer Knowledge Graph] limited-time benefits, details use WeChat scan code to view, it is good for someone to lead the way on the way to growth, learn the system, come on, make a little progress every day!

Insert picture description here
Make small but daily progress!
If you don't advance, you will retreat!

After reading, if it is helpful to you, thank you for your support!
If you are on a computer, you will see "One key triple connection"Are you done, right click on it [haha]

Insert picture description here

Come on!

Work together!