2020-7-11 Java Object Oriented Exercise

Java Object Oriented Exercises

One, single choice

  1. Which of the following statements is false about classes and objects in Java 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 code below 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 object B 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 is a 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 result of the operation 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 the Person class and the 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)
class Person {
	String name;
	int age;
	String grade;
	public void student(){
		System.out.println("I am a student");
	}
	public void sex(String sex){
		System.out.println("I am a" + sex + "child");
	}
	public void meSelf() {
		System.out.println("My name is:" + name + ",this year, I:" + age + "Years old, I'm on:" + grade);
	}
}

public class Test {
	
    public static void main(String[] args) {
      //Instantiate an object using the new keyword
		Person per = new Person();
      //Pass in the parameter values ​​of name, age, grade
		per.name = "Fluwa";
		per.age = 7;
		per.grade = "second grade";
      //Call the student, sex, mySelf methods respectively
		per.student();
		per.sex("male");
		per.meSelf();
	}
    
}
  1. Writing custom monkey classes

    Effect picture:
class Monkey {
    //Attributes: name, feature
    String name;
    String feature;
    //No-parameter construction method (default initializes the attribute values ​​of name and feature, refer to the renderings for attribute values)
    public Monkey(){
    	this.name = "Vervet monkey";
    	this.feature = "long tail";
    }
    //Constructor with parameters (receiving externally passed parameters, assigning values ​​to name and feature respectively)
    public Monkey(String name, String feature){
    	this.name = name;
    	this.feature = feature;
    }
}

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

Tags: Java

Posted by kanch33 on Mon, 30 May 2022 06:34:18 +0530