Java language programming (04747) + answer

Posted by aeafisme23 on Tue, 08 Mar 2022 05:36:09 +0100

National unified naming test for higher education self-study examination in April 2021

Java language programming (I)

Course code (04747)

matters needing attention:

  1. This paper is divided into two parts. The first part is multiple-choice questions and the second part is non multiple-choice questions.

  2. Candidates must answer in the designated position of the answer sheet (paper) according to the order of the test questions, which is invalid on the test paper.

  3. 2B pencil must be used for the graffiti part and drawing part, and black handwriting signing pen must be used for the writing part.

Part I multiple choice questions

1, Single choice questions (there are 10 sub questions in this major question, 1 point for each sub question, 10 points in total. Among the alternatives listed in each sub question, only one item is the one that best meets the requirements of the question, please choose it).

  1. Of the following options, those that do not belong to Java language features or syntax components are (global variables)

    A. Object oriented B. global variables C. JVM D. multithreading

  2. Among the following options, the one that can be used as a document comment is (/ * * I'm javadoc comment * /)

    A. / * I'm javadoc comment * / B. / / I'm javadoc comment*/

    C. / * * I'm javadoc comment * / D. / I'm javadoc comment*/

  3. Among the following options, (k + +) cannot be used as a conditional expression in a for statement

    A. k++ B. k<=10 C. k== -1 D. true

  4. In the description of the class, the correct is (the definition constructor can not be displayed in the class)

    A. Each class must have a main method

    B. Class can not display the definition constructor

    C. When declaring a class, you must use the public modifier

    D. Class is encapsulated, so the data members of the class cannot be accessed

  5. Among the following options, which can be correctly added to the Test class of the following Java code segment and become the overloaded method of cal() (public int cal(int x,inty,floatz){return 0;})

    public class Test{
    public void cal(int x,int y,int z){}
    //Add the overloaded method of cal() here
    }

    A. public int cal(int x,int z){}

    B. public int cal(int x,int y,int z){}

    C. public int cal(int x,int y,int z){return 0;}

    D. public int cal(int x,int y,float z){return 0;}

  6. Among the following options, the one that can correctly initialize array m is (int [] m = New Int [5];)

    A. int [ ] m; B. m = {1,2,3,4,5};

    B. int [ ] m = new int [5]; D. int [ ] m = new int [5]{1,2,3,4,5};

  7. Among the following options, (void aMethod();) can be used as a member of the Java interface

    A. void aMethod(); B. private void aMethod();

    C. interface void aMethod(); D. protected void aMethod();

  8. If there is a statement FileOutputStream out = new FileOutputStream("myfile. Dat") in the program;, The correct option is (if the file myFile.dat exists, the existing contents in the file will be overwritten)

    A. If the file myfile If dat exists, IOException will be thrown

    B. If the file myfile If dat does not exist, an IOException exception will be thrown

    C. If the file myfile If dat exists, the existing contents in the file will be overwritten

    D. If the file myfile If dat exists, new content will be added at the end of the file

  9. If you want to set the position and size of components in the container by yourself, the operations you need to perform are (calling the methods setLayout(null) and setboundaries())

    A. Call method setSize()

    B. Call method getContentPane()

    C. Call method getPreferredSize()

    D. Call the methods setLayout(null) and setBounds()

  10. The priority automatically obtained by the child thread is (parent priority)
    A. Lowest priority B. highest priority C. random priority D. parent priority

Part II non multiple choice questions

2, Fill in the blanks (there are 10 small questions in this big question, 2 points for each small question, 20 points in total).

  1. The java compiler compiles Java programs into binary code that can be recognized by the virtual machine. This code is called bytecode.

  2. Statement system out. println((20+2*9)/4%7); The output of is______ 2______.

  3. The conditional expression in the if statement must get a______ Logic (Boolean)__ Value of type.

  4. The wrapper class corresponding to the basic type char is______ Character__.

  5. If defined: int myArray [] [] = {23}, {46,20}, {98,81,64}, {54,43,55,76};, Then the statement system out. println(myArray[1][1]); Will output___ 20________.

  6. The data member variables in the interface must be declared______ Assign initial value _.

  7. The stream that can input and output objects is called___ Object flow ____.

  8. The method to set the text of the text in the component is______ setFont()____.

  9. Statement jtextfield = new jtextfile (10); In, the function parameter 10 represents the value of the text input box___ Number of columns ____.

  10. The methods to realize the interaction between threads are notify()/notifyall() and_______ wait()___.

3, Short answer questions (there are five small questions in this major question, with 4 points for each small question, a total of 20 points).

  1. Explain the conditions that the number of if and else should meet in the nested if statement and the corresponding rules of else statement.

Answer: 1 The number of if is equal to or more than the number of else.

​ 2. The else clause belongs to the if statement that is logically closest to it, that is, there is no nearest if matching else in the same block.

  1. If the formal parameter of the method is a reference type, what is the content passed by the argument when calling the method? What is the effect on the arguments when the method returns?

Answer: 1 When the method is called, the first address of the argument is passed to the formal parameter.

​ 2. When the method returns, the formal parameter is modified, that is, the argument is modified.

  1. If array A contains three strings of "Java", "program" and "My", please write out two different statement forms to create the array and carry out static initialization.

Answer:

  1. String A[]={"Java","program","My"};

         2. String A[]=new String[3];
    

​ A[0]="Java"; A[1]="program";A[2]="My";

  1. What is the relationship between methods with the same name in the same class? What is the relationship between methods with the same name in different classes?

Answer: 1 In the same class, methods with the same name with different signatures are overloaded methods.

​ 2. Between classes with inheritance relationship, the method with the same name with the same parameter table is an override method, and the method in the descendant class overrides the method with the same name in the ancestor class. Different methods with the same name in the parameter table are overloaded methods. In other different classes, the method with the same name has no relationship.

  1. What are lightweight components? What are heavyweight components? What is the display effect when using both lightweight and heavyweight components in the program?

Answer: 1 Components that do not contain native code are called lightweight components.

        2. Components that contain native code are called heavyweight components.
        3. When a heavyweight component is used with a lightweight component, if the component area overlaps, the heavyweight component is always displayed on it.

4, Procedural blank filling questions (there are 3 sub questions in this major question, with 2 points for each blank, a total of 18 points).

  1. The following program inputs an integer a between 1 and 9 from the keyboard, calculates the sum s from a a to a and outputs it. For example, when a=5, s = 5 + 55 + 555 + 5555.
import java.util.Scanner;

public class Test26{
   public static void main(String[] args){
      int a,s=0,r=0;
      Scanner sc=new Scanner(System.in);
      System.out.println("Input 1~9 An integer between:" );
      a=____① sc.nextInt____;
      for(int i=1;i<=a;i++){
         r=___②_r*10+a_____;
         s=____③___s+r____;
      }
      System.out.println("And are:"+s);
   }
}
  1. The following procedure defines the parent class Person and the child class Student. The program will output: Liubei 25 Shanghai A.
class Person{
   private String name;
   private int age;
   public String getName(){return name;}
   public void setName(String name){this.name=name;}
   public int getAge(){return age;}
   public void setAge(int age){this.age=age;}
   Person(String name,int age){this.name=name;this.age=age;}
   public String toString(){
     String s=name+"\t"+age;
     return s;
   }
}
class Student extends Person{
   private String hometown;
   private char grade;
   Student(String name,int age,String hometown,char grade){
      ____①_super(name,age)__;
      this.hometown=hometown;
      this.grade=grade;
   }
   void print(){
   String s=__②_super.toString()__+" "+hometown+ ""+grade;
   System.out.println(s);
   }
}
public  class Test27{
   public static void main(String[] args){
   Student st = new Student("Liubei",25, "Shanghai", 'A');
   st.___print()____;
   }
}
  1. The following program creates a window, places a panel in the window, and draws lines and rectangles in the panel.
import javax.swing.*;import java.awt.*;
public class Test28{
   public static void main(String[] args){
      MyFrame = new MyFrame();
   }
}
class MyFrame extends JFrame{
   public MyFrame(){
   setTitle("Test28");
   setSize(300,200);
   getContentPane().add(___①_new Mypanel()___);
   set Visible(true);
   }
}
class Mypanel extends JPanel{
   Image myImg;
   public void paintComponent(__②__Graphics g_){
      g.setColor(Color.green);
      g.drawLine(100,50,30,50);
      g.setColor(Color.red);
      ___③_g.drawRect__(100,50,100,46);
   }
}

5, Program analysis questions (there are 3 sub questions in this major question, 6 points for each sub question, 18 points in total).

  1. Read the program and write the output of the program.
class  SuperClass{
   String name;
   public SuperClass(String s){
      name=s;
      System.out.println(s);  //
   }
   public void method(){
     System.out.println("superclass!");  //
   }
}
class SubClass extends SuperClass{
   String department;
   public SubClass(String s,String d){
      super(s);
      department=d;
      System.out.println(d);  //
   }
   public void method(){ }
}
public class Test29{
   public static void main(String[] args){
      SuperClass superc=new SuperClass("super");
      SubClass subc=new SubClass("sub1", "Java");   
      SuperClass ssc=new SubClass("sub2", "Java");
      superc.method();
      subc.method();
      ssc.method();
   }
}
                                  //super\sub1\Java\sub2\Java\superclass!\
  1. Read the program and describe the output of the program.
import java.io.*;
public class Test30{
   public static void main(String[] args) throws IOException{
      InputStreamReader isr=new InputStreamReader(System.in);
      BufferedReader br=new BufferedReader(isr);
      String str=br.readLine();
      int i,ditNo=0,upCharNo=0,IoCharNo=0,otherCharNo=0;
      for(i=0;i<str.length();i++){
         if(str.char At(i)<= '9'&& str.charAt(i)>='0')
            ditNo++;
         else if(str.charAt(i)<= 'Z'&& str.charAt(i)>= 'A')
            upCharNo++;
         else if(str.charAt(i)<= 'z'&& str.charAt(i)>= 'a')
            IoCharNo++;
         else otherCharNo++;
      }
      System.out.println("N1="+ditNo+ "\t"+ "N2="+upCharNo);
      System.out.println("N3="+IoCharNo+ "\t"+ "N4="+otherCharNo);
   }
}

A: the program reads a string of characters, counts the number of numbers, capital letters, small letters and its symbols, and outputs the statistical results.

  1. Read the program and write the output of the program.
class MyThread3 implements Runnable{
   private String[] s;
   public MyThread3(String[] s){
      this.s=s;
   }
   public void run(){
      int count=0;
      long startTime=System.currentTimeMillis();
      long endTime=System.currentTimeMillis();
      while (endTime-startTime<4500){
         try{
            if(s[count%s.length].equals("Yellow")){
               System.out.println("Yellow on");
               Thread.currentThrea().sleep(800);
            }
            if(s[count%s.length.equals("Red")]{
               System.out.println("Red on");
               Thread.currentThread().sleep(1000);
            }
            if(s[count%s.length].equals("Green")){
               System.out.println("Green on");
               Thread.currentThread().sleep(3000);
            }
            count++;
            endTime=System.currentTimeMillis();
         }catch(Exception e){
            e.printStackTrace();
         }
      }
   }
}
class Test31{
   public static void main(String[] args){
      String[] s={"Green", "Red", "Yellow"};
      Thread t=new Thread(new MyThread3(s));
      t.start();
   }
}
               //Green on\Red on\Yellow on

6, Program design questions (there are 2 small questions in this major question, 7 points for each small question, 14 points in total).

  1. Write the method double sum s (int n), calculate the value s of the following formula, and return.

double sumS(int n){
   double s=1;
   //Please fill in the code that should be written here on the answer sheet (paper)
}
double sumS(int n){
    double s = 1;
    if(n<=0)return-1;
    else{
        for(int i=1;i<=n;i++)
            s+=1.0/3*i);
        return s;
    }
}  //You can use other circular statements. If the result is correct, you will get the same score
  1. Class Test33 displays a window in response to mouse events. When the mouse enters the window, the current coordinates of the mouse are displayed in the bottom row of the window, as shown in Figure 33. Requirements: write code to realize the mouse event handler.

import java.awt.*;
import java.awt.event.*;
import.javax.swing.*;
public class Test33 implements MouseMotionListener{
   private JFrame frame;
   private JLable tf;
   public static void main(String) args){
      Test33 two=new Test33();
      two.go();
   }
   public void go(){
      frame=new JFrame("Mouse Control");
      Container contentPane=frame.getContentPane();
      contentPane.add(new JLabel("get mouse event"),BorderLayout.NORTH);
      tf=new JLabel();
      contentPane.add(tf,BorderLayout.SOUTH);
      frame.addMouseMotionListener(this);
      frame.setSize(300,300);
      frame.setVisible(true);
   }
   //Please fill in the code written here on the answer sheet (paper)
   public void mouseDragged(MouseEvent e){
       String s="Mouse dragging: X="+e.getX()+"Y="+e.getY();
       tf.setText(s);
   }
   public void mouseMoved(MouseEvent e){
       String s="Mouse dragging: X="+e.getX()+"Y="+e.getY();
       tf.setText(s);
   }
} 

Topics: Java