Java Week2 Practice

必须要写点什么才能折叠起来。

Question1(好)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Question1{
public static void main (String[] args){
// double numbers = {2.5, 6, 2.6, 8.0};
double[] numbers = {2.5, 6, 2.6, 8.0};

// int[] marks = int[60];
int[] marks = new int[60];

// char letters[] = {a, b, c, d, e, f};
char[] letters = {'a', 'b', 'c', 'd', 'e', 'f'};

// String[] books = {Java, SQL, PHP};
String[] books = {"Java", "SQL", "PHP"};


char letter = 'a';

}
}

Question2

1
2
3
4
5
6
7
8
9
10
11
12
13
//Out of the range

public class Test_Q2{
public static void main (String[] args){
int[] intArray = new int[5];
for (int i=0; i<=intArray.length; i++){
intArray[i] = i;
}
System.out.println(intArray);


}
}

  会超界导致无法输出,只需要把=去掉就可以了。

Question3

  Assume we have a Flowerclass (*), and two of its methods are setColour(String colour)and setHeight(double height). What is wrong with the following code? Explain your answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Flower[] f = new Flower[2];
f[0] = new Flower();
f[0].setColour("Red");
f[0].setHeight(4.0);
f[1].setColour("Blue");
f[1].setHeight(3.5);
````
&emsp;&emsp;缺少一个 f[1] = new Flower();
# Question4
&emsp;&emsp;Assume we have a Flowerclass (\*), and two of its methods are setColour(String colour)and setHeight(double height). What is wrong with the following code? Explain your answer.
```java
Flower[] f = new Flower[2];
f[0] = new Flower();
f[0].setColour("Red");
f[0].setHeight(4.0);
f[1] = new Flower();
f[1].setColour("Blue");
f[1].setHeight(3.5); f[2] = new Flower();
f[2].setColour("Pink");
f[2].setHeight(2.5);

  同上,也是超界,把2改成3就对了。

Question5(好)

Assume we have a Flowerclass (*), and two of its methods are setColour(String colour)and setHeight(double height). What is wrong with the following code? Explain your answer.

1
2
3
4
5
6
7
8
9
10

//No initialize i

public class Test_Q5{
public static void main (String[] args){
int i;
i = i + 5;
System.out.println("i = " + i);
}
}

  Local Variables are not given a defined value! Instance variables are.
  可以 int i = 0;

Question7(好)

1
2
3
4
public class Square_Q7a{
public int square(int x) { return x*x; }
public double square(int y) { return y*y; }
}

  问题:square(int)已经被定义过一次了,所以不能被重复定义,可以改成public double square(double y) { return y*y; }

1
2
3
4
public class Square_Q7b{
public double square(int x) { return x*x; }
public double square(double y) { return y*y; }
}

  莫得问题!

1
2
3
4
public class Square_Q7c{
public double square(int x) { return x*x; }
public int square(double y) { return y*y; }
}

  转化会有损失精度问题,直接下下面的int改成double就可以了。

Question8

1
2
3
4
5
6
7
8
public class Car { 
public void m1() { System.out.println("car 1"); }
public void m2() { System.out.println("car 2"); }
public String toString() { return "vroom"; } }

public class Truck extends Car {
public void m1() { System.out.println("truck 1"); }
}
1
2
3
Truck mycar= new Truck(); 
System.out.println(mycar);
mycar.m1(); mycar.m2();

输出内容:
vroom
truck 1
car 2

Question9

1
2
3
4
5
6
7
8
9
public class Car { 
public void m1(){ System.out.println("car 1"); }
public void m2(){ System.out.println("car 2"); }
public String toString() { return "vroom"; } }

public class Truck extends Car {
public void m1() { System.out.println("truck 1"); }
public void m2() { super.m1(); }
public String toString() { return super.toString()+ "T"; }
1
2
3
4
Truck mycar= new Truck(); 
System.out.println(mycar);
mycar.m1();
mycar.m2();

输出内容:
vroomT
truck 1
car 1

Question10

1
2
3
4
5
6
7
8
9
10
public class Test_Q10{
public static void main (String[] args){
String s = "6";
int n = 3;
double d = 4.5;
System.out.println(s + n + d);

}

}

输出内容:634.5

Question11

Identify which statements are TRUE and which are FALSE. Justify your answers.
1.subclass has direct access to its superclass’ private data and methods.
2.A class can extend more than one superclass.
3.An abstract class must contain at least one abstract method.
4.An abstract class must not contain any instance variables.

1:错 不能继承private,只能继承protected和public
2:错 一个类不能继承多个父类,但是多个类可以继承一个类,这个Java继承里面有写
3:错 可以不包含任何抽象的方法以及变量,只是因为它是一个抽象,必须要事例
4:错 可以包含常量

Question12

A class Animalhas a subclass Dog. Which of the following is TRUE?
a) Dog cannot have subclasses.
b) Dog has no other parent than Animal.
c) Animal can have only one subclass.
d) Dog cannot have siblings.
答案: b
解析:a)狗可以有子类,比如母狗,公狗等。
c)动物可以分子类,比如猫,兔子等
d)狗也可以有兄弟姐妹,比如上面说的猫和兔子

Question13

1
2
3
4
5
6
7
public class Test_Q13a{
public static void main (String[] args){
String s1 = new String("aaa");
String s2 = new String("aaa");
System.out.println(s1==s2);
}
}

false

1
2
3
4
5
6
7
public class Test_Q13b{
public static void main (String[] args){
String s1 = new String("aaa");
String s2 = new String("aaa");
System.out.println(s1.equals(s2));
}
}

true

1
2
3
4
5
6
7
public class Test_Q13c{
public static void main (String[] args){
String s1 = "aaa";
String s2 = "aaa";
System.out.println(s1==s2);
}
}

true


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!