Java quiz madness first bullet

In order to achieve good results, I finally bowed my head to the tactics of the sea of ​​questions. This column will be updated about three or four times a week in the future, and I will share with you the good questions and the easy-to-mistake questions. Please support me a lot~
If there are related disputes, I hope the big guys will comment. area bombing


1. Definition of the interface

image.png
The interface method is public abstract by default, and the visibility of the corresponding method in the class that implements the interface cannot be less than the visibility of the interface method, so it can only be public.

2. Polymorphism

The output of the following code is

class Animal{
    
    
    public void move(){
    
    
        System.out.println("动物可以移动");
    }
}
class Dog extends Animal{
    
    
    public void move(){
    
    
        System.out.println("狗可以跑和走");
    }
    public void bark(){
    
    
        System.out.println("狗可以吠叫");
    }
}
public class TestDog{
    
    
    public static void main(String args[]){
    
    
        Animal a = new Animal();
        Animal b = new Dog(); 
        a.move();
        b.move();
        b.bark();
    }
}

Compile to the left, run to the right. That is, it will be regarded as the type on the left when compiling, and the method body of the type on the right will be seen when running. In this question, the animal class does not have another method, and b is called, so the compiler will think that b is an animal class, so an error is reported.
For non-static properties or non-static methods in polymorphism, compile to the left, and run to the right. That is, to see if the method/property exists in the class on the left during compilation, but the method/property of the class on the right is actually executed at runtime.
If you want to compile correctly, b should be cast

public class TestDog{
    
    
    public static void main(String args[]){
    
    
        Animal a = new Animal();
        Animal b = new Dog();
        a.move();
        b.move();
        if (b instanceof Dog){
    
    
            ((Dog) b).bark();
        }

    }
}

3. Stacks and queues

Stack usually refers to a "first in, first out" container. ( )
Answer: The error
stack spit out, the queue spit out

4. Comparison of streams

image.png
First of all, B and D are excluded, and the question is to ask for input.
Between A and C, inputStream is a byte stream input stream; InputStreamReader converts bytes into characters, and inputStreamReader processes character streams into byte streams. The title requirement is to process character input, so C is selected.

5. Properties of the interface

The output of the following program is

public class A implements B{
    
    
    public static void main(String args[]){
    
    
        int i;
        A a1=new  A();
        i =a1.k;
        System.out.println("i="+i);
    }
}
interface B{
    
    
    int k=10}

image.png
The variables in the interface are public static final by default. They are public, static, and final constants. They are equivalent to global constants, and the modifier can be omitted directly.
Implementing classes can directly access variables in the interface

6. Inheritance implementation and extension of classes and interfaces

image.png
1. The relationship between classes is inheritance, only single inheritance, but multi-layer inheritance. 2. The relationship between a class and an interface is implementation, which can be implemented alone or in multiple implementations. 3. The relationship between the interface and the interface is inheritance, either single inheritance or multiple inheritance. So it is A

7. Mutual call of parent class and child class

image.png
A: The instance method of a class can directly call the non-private instance method of the parent class. Conceptually, each subclass creates an implicitly corresponding parent class object when it is created. The super keyword is a reference to the parent class object in the subclass object. The subclass cannot call the parent class private method
B error, when the parent class class method is defined as private, it is invisible to the subclass, so the subclass cannot call
C error, the subclass specific instance method is invisible to the parent class , so it cannot be called directly, only by creating an instance object of the subclass, and then calling
D is correct, the instance method can call the instance method in its own class

8. Static code blocks

 class StaticStuff{
    
    
     static int x = 10;

     static {
    
     x += 5; }

 public static void main(String args[])
 {
    
    
     System.out.println(“x =+ StaticStuff .x);
 }
    static
     {
    
    x /= 3; }
}

image.png
The order of execution is:

  • Parent class static code block, static variable ps: execute in the order of declaration
  • Subclass static code block, static variable ps: execute in the order of declaration
  • Parent class local code block, member variable ps: execute in the order of declaration
  • parent class constructor
  • Subclass local code block, member variable ps: execute in the order of declaration
  • subclass constructor

9. Thread destruction

image.png

So the start method just puts the thread into the ready state instead of destroying it

10. Class Access

image.png
The difference between default and protected is that
as long as the former is an external package, access is not allowed.
The latter allows access as long as it is a subclass, even if the subclass resides in an external package.

Summary: default denies all access outside the package; protected accepts access from subclasses outside the package

11 toString and valueOf in String

public class CharToString {
    
    
 public static void main(String[] args)
 {
    
    
  char myChar = 'g';
  String myStr = Character.toString(myChar);
  System.out.println("String is: "+myStr);
  myStr = String.valueOf(myChar);
  System.out.println("String is: "+myStr);
 }
}
此代码片段输出正确的值是()
正确答案: A   你的答案: A (正确)
A
String is: g
String is: g

B
String is: 103
String is: g

C
String is: g
String is: 103
    
D    
String is: 103
String is: 103

image.png
All strings are returned, and only when char becomes int will it become the corresponding assic code

12. The use of two-dimensional arrays

image.png
The two-dimensional array in C language, the second control column, must have a value, and the row can not be! Don't mix up the
definition of two-dimensional array in Java, the value of the box in front of the one-dimensional length must be given

Have you mastered these little knowledge points?

Guess you like

Origin blog.csdn.net/qq_63511424/article/details/124315767