Java Object Oriented

practise
One, single choice

1. Regarding classes and objects in Java, which of the following statements is wrong is B
A. All objects of the same class have the same characteristics and behavior
B. Classes are the same as objects, but they are said differently
C. An object is an entity with properties and behavior
D. Classes specify the characteristics and behaviors that objects possess
2. In java, the result of running the following program is A

A. Output: null
B. Works fine, but doesn't output anything
C. Compilation error, cannot run
D. Can run, but there will be exceptions when running
3. The correct result of running the following code is D

A. Compilation error, cannot run normally
B. Compiles correctly, but generates errors at runtime
C. hello
D. world
4. Which space is used to store the object A created using the new keyword
A. Heap
B. stack
C. Method area
D. Instance area
5. Analyze the following Java code, compile and run the result is C

A. The running result is: Student ID: 1 Name: Zhang San
B. The running result is: Student ID: null Name: Zhang San
C. The program has a compilation error
D. The program has a runtime exception
6. Which of the following are valid constructor overloading (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 running result is (A)

A. mainboard: ASUS, cpu: Intel
B. mainboard: s1,cpu: s2
C. mainboard: Intel, cpu: ASUS
D. ASUS, Intel
8. In Java, the output of the following program after compiling and running is (D)

A. 6
B. 3 4
C. 8
D. 7
In Java, which of the following statements about this is false is (multiple choice) BC
A. If you use this to call other constructors in the constructor, it can only be the first statement
B. You cannot call other constructors of the same class in a constructor
C. If you use this to call other constructors in the constructor, the statement can be placed anywhere
D. You can use "this.methodname()" or "this.property name" to refer to members of the current object
2. Programming

1. Create a Person class and a test class
Attributes: name, age, grade
method:
The student method with no parameters and no return value, described as: I am a student!
A method with a parameter (gender sex), described as: I'm a child! (where ** is the incoming parameter)
The mySelf method with no parameters and no return value, introducing your name, age, and grade (parameters refer to the renderings)

package com.dodoke.myobject.test;

public class Person {
	public	String name;
	public  int age;
	public  int grade;
	//student method with no parameters and no return value
	public void student(){
		System.out.println("I am a student!");
	}
	//method with parameters
	public void sex(String sex){
		System.out.println("I am a"+sex+"child");
	}
	//mySelf method with no parameters and no return value
	public void mySelf(){
		System.out.println("my name is"+name);
		System.out.println("my age is"+age);
		System.out.println("my grade is"+grade+"grade");
	}
}
package com.dodoke.myobject.test;

public class Test3 {
	public static void main(String[] args) {
		Person xiaohua = new Person();
		xiaohua.name = "little flower";
		xiaohua.age = 18;
		xiaohua.grade = 6;
		xiaohua.student();
		xiaohua.sex("male");
		xiaohua.mySelf();
	}
}

2. Write a custom monkey class

Effect picture:

package com.dodoke.myobject.test;

public class Monkey {
	public String name;
	public String feature;
	//No-argument constructor
	public Monkey(){
		this.name = "Vervet monkey";
		this.feature = "long tail";
	}
	//Constructor with parameters
	public Monkey(String name,String feature){
		this.name = name;
		this.feature = feature;
	}
}
package com.dodoke.myobject.test;

public class Test4 {
	public static void main(String[] args) {
		System.out.println("I'm a monkey spawned using no-argument constructs:");
		Monkey monkeyA = new Monkey();
		System.out.println("name:"+monkeyA.name);
		System.out.println("feature:"+monkeyA.feature);
		System.out.println("==========================");
		System.out.println("I am a monkey created using a parameterized construct:");
		Monkey monkeyB = new Monkey("white-headed langur","It has white hair on its head and likes to eat leaves");
		System.out.println("name:"+monkeyB.name);
		System.out.println("feature:"+monkeyB.feature);		
	}
}

Posted by bizshop on Tue, 31 May 2022 00:39:31 +0530