Java holiday assignment (Day04)

Posted by Jamez on Sat, 12 Feb 2022 06:37:50 +0100

I choice question

1. [C language] read the program and select the result (D)

int main()
   int a = 5;
   if( a = 0 ){
      printf("%d", a - 10);
   printf("%d", a++);
   return 0;

A. -5      B. 6      C. 1      D. 0

Parsing: note that the if statement if (a = 0) is an equal sign, which is an assignment at this time. Then a is changed to 0, and the if condition is false; When printing, it is post + +, so print a first, and the result is 0. Finally, perform self increment operation on a, and finally a becomes 1. When printing, it is still 0.

2. [C language] about the wild pointer, the following statement is wrong (c)

A. A wild pointer may point to an illegal memory address

B. Wild pointers may sometimes point to legitimate memory addresses

C. Wild pointers are caused by pointer uninitialization

D. Access to wild pointers may cause program exceptions

Parsing: a wild pointer is a pointer whose position is unknowable (random, incorrect, and without explicit restrictions). If a pointer variable is not initialized during definition, its value is random, and the value of the pointer variable is the address of another variable, which means that the pointer points to a variable whose address is uncertain. At this time, dereferencing is to access an uncertain address, So the result is unknowable. Therefore, in option C: not necessarily caused by uninitialization. It may also be that, for example, after the dynamic memory is opened up, free is used to release, but it is not empty in the end. Therefore: A, B and D are correct.

3. The output of the following java program is (B).

public class Example{
   String str=new String("hello");
   public static void main(String args[]){
      Example ex=new Example();
      System.out.print(ex.str+" and ");
   public void change(String str,char ch[]){
      str="test ok";

A: hello and ab       B: hello and cb 

C: hello and a         D: test ok and ab

Parsing: mainly examines whether the original value will be modified during the transfer of reference. Debugging and suggestion in IDEA. In the change function, all references are accepted, but str modifies the point of the formal parameter itself. ch modifies the contents of the original array object through this reference. Therefore, students need to pay attention to when the content of the original reference can be changed? It depends on whether the content of the original object is modified through the current reference.

4. The description of the following Test class is known. Which of the following statements is correct in the main method of this class? ( A )

public class Test {
   private float f = 1.0f;
   int m = 12;
   static int n = 1;
   public static void main (String args[]) {
      Test t = new Test();

A: t.f;           B: this.n;

C: Test.m    D: Test.f

Resolution: A: private members can be accessed in the same class

B: n is static and cannot be accessed through this

C: m is an ordinary member variable, which needs to be accessed through the reference of the object

D: f is an ordinary member variable, which needs to be accessed through the reference of the object

5. The following type conversions that cause information loss are (B)

A: float a=10;

B: int a =(int)8846.0;

C: byte a=10; int b=-a

D: double d=100

Analysis: A: the integer part can be stored in floating point type

B: Information will be lost, and the decimal part cannot be saved, even 0 cannot be saved

C: The literal value 10 is assigned to a, which does not exceed the range of representation and can be stored. int b = -a; There is no problem assigning one byte to four bytes

D: The same question as A.

II Code question

1. Five people sat together and asked how old the fifth person was? He said he was two years older than the fourth man. Asked the age of the fourth person, he said that he was 2 years older than the third person. Ask the third person and say that he is two years older than the second. Ask the second person and say that he is two years older than the first person. Finally, the first person was asked. He said he was 10 years old. How old is the fifth person?

public class getAge{
   public class static main(String[] args){
      int n = 2, age = 10;
      for(int i = 2 ;i <= 5;i++){
          age += n;
      System.out.println("The age of the fifth person is:" + age);
private static int getAge(int age){
       return 10;
       return getAge(age-1)+2;
public static void main(String[] args) {

2. Given a two-dimensional array of 3 times 3, the data in it is randomly generated. Find the sum of the elements of the two diagonals respectively.

public static void main(String[] args) {
   Random random = new Random();
   int[][] array = new int[3][3];
   for (int i = 0; i < 3; i++) {
       for (int j = 0; j < 3; j++) {
           array[i][j] = random.nextInt(10);
   int sum1 = 0;
   int sum2 = 0;
   for (int i = 0; i < array.length; i++) {
       for (int j = 0; j < array[i].length; j++) {
           //1. Determine the left to right diagonal
           if(i == j) {
             sum1 += array[i][j];
           //2. Determine the right to left diagonal
           if(i+j == array.length-1) {
             sum2 += array[i][j];

Topics: Java