practice
1, Single choice
1. For classes and objects in Java, the following statement is wrong: B
A. All objects of the same class have the same characteristics and behavior
B. Classes are the same as objects, but they have different meanings
C. Objects are entities with attributes and behaviors
D. Class specifies the characteristics and behavior that an object has
2. In java, the running result of the following program is A
A. Output: null
B. Works normally but does not output anything
C. Compilation error, unable to run
D. Can run, but exceptions will occur when running
3. The correct result of running the following code is D
A. Compilation error, unable to run normally
B. Compile correctly, but run time error
C. hello
D. world
4. Which space is used to store the object A created with the new keyword
A. Heaps
B. Stack
C. Method area
D. Instance area
5. Analyze the following Java code, compile and run the result as C
A. The running result is: student number: 1 Name: Zhangsan
B. The running result is: Student ID: null Name: Zhangsan
C. Compilation error in program
D. Program has run-time exception
6. Which of the following are legal constructor overloads (multiple choice) AC
A. public Display(String s){}
B. public int display(int n1,int n2){}
C. Display (int ...a){}
D. public display(Strnig s,int a){}
7. The operation result is (A)
A. mainboard: ASUS, cpu:Intel
B. mainboard: s1,cpu: s2
C. mainboard:Intel, cpu: ASUS
D. Asustek, Intel
8. In Java, the output of the following programs after compilation and operation is (D)
A. 6
B. 3 4
C. 8
D. 7
9. In Java, the following incorrect statement about this is (multiple choice) CD
A. In a constructor, if this is used to call another constructor, it can only be the first statement
B. Cannot call another constructor of the same class in a constructor
C. In the constructor, if you use this to call another constructor, the statement can be placed anywhere
D. You can use "this. method name ()" or "this. attribute name" to reference members of the current object
2, Programming
1. Create Person class and test class
Attribute: name, age, grade
Method:
The student method with no parameter and no return value is described as: I am a student!
The method with parameters (sex) is described as: I am a * * child! (where, * * is the incoming parameter)
mySelf method with no parameters and no return value. Introduce your name, age and grade (refer to the renderings for parameters)
package com.myobject; public class Person { public String name; public int age; public String grade; public void student() { System.out.println("I am a student!"); } public void sex(String sex) { System.out.println("I am a" + sex + "Boy!"); } public void mySelf() { System.out.println("My name:" + name); System.out.println("this year" + age + "Years old"); System.out.println( grade + "Yes"); } }
package com.myobject; public class Test { public static void main(String[] args) { //Instantiate an object with the new keyword Person person = new Person(); //Pass in parameter values of name, age and grade person.name = "Xiao Ming"; person.age = 10; person.grade = "Class 4, grade 3"; //Call student, sex and mySelf methods respectively person.student(); person.sex("male"); person.mySelf(); } }
I am a boy!
My name: Xiao Ming
10 years old
Class 4, grade 3
}
2. Writing a custom monkey class
Rendering:
package com.myobject; public class Monkey { //Attribute: name, feature String name; String feature; //Parameterless construction method (initialize the attribute values of name and feature by default, and refer to the rendering for attribute values) public Monkey() { System.out.println("I am a monkey generated by nonparametric Construction:"); this.name = "Long tailed monkey"; this.feature = "long tail-sb. who never closes the door when walking in or out"; } //Construction method with parameters (receiving external parameters and assigning values to name and feature respectively) public Monkey(String name,String feature) { System.out.println("I am a monkey generated by using a structure with parameters:"); this.name = name; this.feature = feature; } }
package com.myobject; public class MonkeyTest { public static void main(String[] args) { Monkey one = new Monkey(); System.out.println("Name:" + one.name); System.out.println("Features:" + one.feature); System.out.println("------------------------"); Monkey two = new Monkey("White headed langur","He has white hair on his head and likes to eat leaves"); System.out.println("Name:" + two.name); System.out.println("Features:" + two.feature); } }
I am a monkey generated by nonparametric Construction: Name: long tailed monkey Features: long tail ------------------------ I am a monkey generated by using a structure with parameters: Name: white headed langur Features: white hair on the head, like to eat leaves