Java daily practice (3)
Radio part:
1. The output of the following code operation is
public class Person{
private String name = "Person";
int age=0;
}
class Child extends Person{
public String grade;
public static void main(String[] args){
Person p = new Child();
System.out.println(p.name);
}
}
A: Output: Person B: No output C: Compile error D: Run error
Answer C:
Here we pass Person p = new Child(), which is equivalent to referencing the subclass object by the parent class reference, but our name field is modified by private and can only be used in Person, so an error will be reported directly here, that is Compilation errors are very obvious through the compiler.
2. The output of the following program is
class Base{
public Base(String s){
System.out.print("B");
}
}
public class Derived extends Base{
public Derived (String s) {
System.out.print("D");
}
public static void main(String[] args){
new Derived("C");
}
}
A : BD
B : DB
C : C
D : compile error
The test here is that when creating a subclass object in inheritance, it will first help the parent class to construct through the construction method of the subclass, but the parent class in the title has a construction method with one parameter, so it will not provide no parameters at this time. The construction method, but because our subclass does not use super(s) to help the parent class to construct, it will report an error compilation error at this time.
It can also be clearly seen on the compiler:
3. The following statement about the construction method is incorrect ()
A construction method also belongs to the method of the class, and can assign values to member variables when creating objects
B constructor cannot be overloaded
C constructor has no return value
The D constructor must be the same as the class name
Here B is wrong at first glance. Here we can have multiple construction methods, without parameters, with one parameter, etc. At this time, doesn’t it satisfy our definition of overloading? Overloading: the method name is the same, and the return
type No requirement, the parameter type and number of parameters are different.
4. In exception handling, the following descriptions are incorrect
A try block cannot be omitted
B can use multiple catch blocks
C finally block can be omitted
D catch block and finally block can be omitted at the same time
Answer: The catch here cannot be omitted. It is to catch our exception through catch. If it is omitted, how to deal with the exception? So it is obvious to choose D directly
finally: It can be omitted. If finally is added, the code in finally will be executed.
5. In the following description, the wrong one is
A SQL language is also known as Structured Query Language
B The "static" keyword in java indicates that a member variable or member method can be accessed without an instance variable of the class to which it belongs
In C object-oriented development, passing by reference means that what is passed is not the actual object, but the reference of the object. Therefore, external changes to the referenced object will not be reflected on the referenced object
D java is a strongly typed language, javascript is a weakly typed language
E The three major characteristics of object-oriented include: encapsulation, inheritance, polymorphism
Answer: C, where external changes to the referenced object will be reflected in our object.
6. Which of the following statements is correct?
A instance method can directly call the instance method of the superclass
B Instance methods can directly call superclass class methods
C instance method can directly call the class method of this class
D Instance methods can directly call instance methods of other classes
Answer: A: Here our instance method wants to call the instance method of the superclass (parent class) through super.
to call, so this is wrong
B: The instance method cannot directly call the class method of the superclass (that is, the method modified by static), here it needs to be called by the class name, such as:Test.sum
C : Correct, we can call directly in the same class
D: Our instance method calls instance methods of other classes need objects of other classes to call, so D is also wrong
7. What is the output result:
String str1="hello";
String str2="he"+ new String("llo");
System.out.println(str1==str2);
A true
Neither B is right
C null
D false
Here we mainly examine whether the comparison between the string constant pool and == is an object, and str1 will put hello into the string constant pool, but he in str2 will splice an llo to generate a new object, so the two sides of == are
not The same object, so the printed result is false
8. The program reads in a value input by the user and requires the creation of a custom exception. If the input value is greater than 10, use the throw statement to explicitly throw an exception. The exception
output
is "something'swrong!", and the statement is () A if(i>10)throw new Exception("something'swrong!");
B if(i>10)throw Exception e(“something’swrong!”);
C if(i>10) throw new Exception e(“something’swrong!”);
D if(i>10)throw Exception( “something’swrong!”);
The main test here is how to throw an exception, obviously the answer is A
9. The following descriptions about the collection classes ArrayList, LinkedList, and HashMap are wrong ()
A HashMap implements the Map interface, which allows any type of key and value objects, and allows null to be used as a key or value
Both ArrayList and LinkedList implement the List interface
C ArrayList performs better when adding and removing elements
D ArrayList access speed is faster than LinkedList
Answer: C: The bottom layer of ArrayList is an array. Every time an element is added or deleted, the element needs to be moved, and the time complexity can reach O(N)
A: As mentioned in the chapter of Map, HashMap can use null as key and value
B: ArrayList and LinkedList implement the List interface, here is the article on the initial data structure, you can see it in a picture
D: In most cases, ArrayList is faster than LinkedList, because our array performs random access.
10. The class name in the Java program must be the same as the file name in which the class is stored.
A is right, B is wrong,
B here is different from the file name where we store this class, so here is B
programming questions
Topic 1: Find the longest continuous string of numbers in a string
This question is relatively simple and directly looks at the code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int left = 0;
int right = 0;
int count = 0, max = 0;
String str2 = new String();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
// 此时是字母部分 :
continue;
}
// 此时是数字部分
left = i;
while (i < str.length() && !(str.charAt(i) < '0' || str.charAt(i) > '9')) {
i++;
count++;
}
// 此时 走到了 i 为字母的地方
count = i - left;
right = i;
if (count > max) {
// 更新 数字字符串
str2 = str.substring(left, right);
max = count;
count = 0;
}
i = i - 1;
}
System.out.println(str2);
}
Topic 2: Numbers that appear more than half of the time in the array
This question is also very simple, we only need to take the middle number after sorting, (the tree exceeds half the length of the array, so it must be the middle number), but because of this, we need to make the sorting achieve the optimal time complexity Use quick sorting: Note that the title here says that there is a number in the array that appears more than half of the length of the number, so here we omit it and traverse it again to judge whether the number of occurrences of the intermediate number is greater thannumbers.length / 2
It is best to implement our quick sort by yourself:
public static int MoreThanHalfNum_Solution(int[] numbers) {
// write code here
if (numbers == null || numbers.length == 0) {
return -1;
}
quickSort(numbers, 0, numbers.length - 1);
return numbers[numbers.length / 2];
}
public static void swap(int[] arr, int start, int end) {
int tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
}
public static void quickSort(int[] arr, int start, int end) {
if (start >= end) {
return;
}
// 三数取中
int mid = fidMid(arr, start, end);
swap(arr, start, mid);
int p = fid(arr, start, end);
quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
}
public static int fidMid(int[] arr, int start, int end) {
int mid = start + ((end - start) >>> 1);
if (arr[start] < arr[end]) {
if (arr[mid] < arr[start]) {
return start;
} else if (arr[end] < arr[mid]) {
return end;
} else {
return mid;
}
} else {
// arr[start] > arr[end]
if (arr[mid] < arr[end]) {
return end;
} else if (arr[start] < arr[mid]) {
return start;
} else {
return mid;
}
}
}
// 找基准
public static int fid(int[] arr, int start, int end) {
int tmp = arr[start];
while (start < end) {
while (start < end && arr[end] >= tmp) {
end--;
}
arr[start] = arr[end];
while (start < end && arr[start] <= tmp) {
start++;
}
arr[end] = arr[start];
}
// 此时 end == start
arr[end] = tmp;
return end;
}
Another way of thinking:
Mode number: It is the number that appears more than half the length of the array.
If the two numbers are not equal, the two numbers are eliminated. In the worst case, one mode number and one non-mode number are eliminated each time. If there is a mode number,
The last number left must be the mode number.
public class Solution {
public int MoreThanHalfNum_Solution(int[] array) {
if (array == null || array.length == 0) {
return -1;
}
int result = array[0];
int times = 1;
for (int i = 1; i < array.length; i++) {
if (times != 0) {
// 向后判断 result 与 array[i] 是否相同
if (result == array[i]) {
times++;
} else {
// 此时不相等 ,如果 times 为 0 下次进入 if 语句就会直接走到
// else 更新 result
times--;
}
} else {
// 此时 times == 0 就需要更新 我们的 result
result = array[i];
times = 1;
}
}
// 此时 遍历完数组 result 就是我们的中间数 另外需要判断一下
times = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == result) {
times++;
}
}
return times > array.length / 2 ? result : 0;
}
}