Java Exam Easy Recall Questions (Part I)

2020年3月28日 68点热度 0人点赞 0条评论
内容目录

The author is proficient in C#, and is learning JAVA solely for an exam in April.
How to learn the basic syntax of JAVA and pass the exam in the shortest time possible?
To cope with the exam, the sections on conditions and loops are similar in C, C++, C#, and JAVA, so there is no need to relearn them.
In terms of basic syntax, C++ and C# are similar, with constructs like namespace and inheritance (:).
In terms of advanced syntax, JAVA is also close to C#, and many APIs even have the same names.
When studying and practicing exam questions, I extract representative JAVA questions to focus on revising and understanding the types of problems, which allows me to pass the exam in the shortest time possible and achieve high scores.
La la la la la~
Note: The following questions are sourced from past exam papers or the internet, and will be deleted upon request!

1. The initial application target of the Java language is consumer electronic products.
It was initially designed for use in electronic products such as video game consoles and set-top boxes.

2. In the core of the Java SDK, the executable file corresponding to the interpreter is java.exe.
javac.exe is used to compile .java source files, while java.exe is used to interpret and execute the compiled program.


3. Binding connects the message sent to an object with the object that executes that message method.
Binding is divided into static binding and dynamic binding. Static binding is handled at compile time (the usual code); dynamic binding occurs when the type of the instantiated object is not known beforehand and is determined after compilation.
Dynamic binding is used when the required instance is determined at runtime.
Class A, subclasses B and C.

public void Test( string str )
{
    A a;
    if ( str == "B" )
        a = new B();
}else if ( str == "C" )
{
    a = new C();
}else
    return;
}

4. The default order of a java Date time string is day of the week, month, day, hour, minute, second, year.
The format is like Sat Mar 21 22:15:24 CST 2020.

5. What does ((k-1)^k)&k represent?
Answer: It retrieves the rightmost bit that is 1 of the non-zero integer variable k.

6. super
In Java, super is similar to base in C#.
In Java, it is called superclass or subclass; in C#, it is called base class/parent class and subclass.

super has multiple purposes:

  • To specify which constructor of the superclass to call;
  • To access methods overridden by the subclass;
  • To access methods hidden by the subclass.

7. && Assessment
Read the following program code and write the output result of the program.

    public static void main(String[] args) {
        int a, b, c;
        a = b = c = 1;
        boolean w;
        w = a++ > 1 && ++b > c++;
        System.out.println(a + "," + b + "," + c + "," + w);
    }

The answer is 2,1,1,false.
This question has a misleading component; at first glance, it appears to involve ++i and i++, but in practice, only a++>1 is needed. The reason is that && operations evaluate left to right, and when the left-side condition is false, the program does not execute the right-side expression.

8. String equality
Check the output of the following program code:

    public static void main(String[] args) {
        String a = new String("1");
        String b = new String("1");
        String aa = "1";
        String bb = "1";
    System.out.println(a == b);
    System.out.println(a == aa);
    System.out.println(aa == b);
    System.out.println(aa == bb);
}</code></pre>

The result is:

false
false
false
true

In Java, the == operator for String types compares the references of the objects.
Different new String() objects, even with the same value, are not the same object.
In C#, the == operator for string types is overridden to compare the values of the strings;
while String aa = "1"; and String bb = "1"; are both constants 1 and are not new, so the result of == is the same.

9. Program design question
Use a loop to store the 26 uppercase English letters in a one-dimensional array in dictionary order, then reverse the order (without using another array), and finally create a string based on the processed character array and output it. Complete the above requirements in code.

Analysis: This is one of the basic sorting algorithms. At first, I wanted to use the bubble sort algorithm to solve it. However, upon seeing the answer, it used a reversal method, which is simpler.
The reason is that the 26 letters are already sorted (if they were jumbled, then a bubble sort would be used), so just reversing them works.

Requirements:
Loop to store 26 uppercase letters in an array;
Reverse (flip);
Output the string;

The code for the first step is as follows:

        char a[] = new char[26];
        char c = 'A';
        for (int i = 0; i < 26; i++) {
            a[i] = (char) (c + i);
        }

The second step is as follows:

        for (int i = 0; i < 13; i++) {
            char tmp = a[i];
            a[i] = a[25 - i];
            a[25 - i] = tmp;
        }

The final step uses the String constructor:

        String str = new String(a);
        System.out.println(str);

10. Program design question
Write a class BigInteger that implements large integers of no more than 200 digits using arrays, and requires an addition operation for large integers.

Analysis: In Java, there is a BigInteger class that wraps an int[] and can represent integers of arbitrary length.
The question requires us to implement a class similar to BigInteger. Then, we need to implement decimal addition.
There are many methods, and how to write is not restricted, as long as it can compute.
One reference can be found at
https://wenku.baidu.com/view/6b050b976aec0975f46527d3240c844769eaa01b.html

痴者工良

高级程序员劝退师

文章评论