Yesterday, I had a test.The result is not well。So I will write the result and reasons at weekends.
Because of some reason , the last about the test ,nearly
38 questions has been lost .
1.哪个不会有编译错误?
package firstTest;
public class Blip {
protected int blipver(int x){
return 0;
}
}
class Vert extends Blip{
A.public int blipver(int x){ return 0;}
B.private int blipver(int x){ return 0;}
C.private void blipver(int x){ return 0;}
D.protected long blipver(int x){ return 0;}
}
答案:A
原因:
http://baike.baidu.com/subview/1267921/16783798.htm?fr=aladdin
重写需要注意:方法名和参数类型,个数必须一致,但是JDK1.5以后要求返回类型可以不一样,但是是其父类的子类,而且权限不能低于父类权限
2.写出结果:
package firstTest;
public class TestCar {
public static void main(String[] args) {
RaceCar racer = new RaceCar();
Car car = new RaceCar();
Vehicle vehicle = new RaceCar();
System.out.println("racer:"+racer.speed()
+"car:" +car.speed()
+"vehicle:"+vehicle.speed());
}
}
abstract class Vehicle{
public int speed(){
return 0;
}
}
class Car extends Vehicle{
public int speed(){
return 100;
}
}
class RaceCar extends Car{
public int speed(){
return 150;
}
}
答案:racer:150car:150vehicle:150
原因:这一类型反复在这次的测试中出现。例如:
package firstTest;
class Foo {
int value;
public void f() {
System.out.println("Foo");
}
}
class Goo extends Foo {
int num;
public void f() {
System.out.println("Goo");
}
}
class Test{
public static void main(String[] args){
Foo obj = new Goo();
obj.value=200;
obj.f();
// obj.num = 5; //出现错误
// obj.g(); //出现错误
}
}
说明了,对象的方法到底访问哪个,最终还是取决于创建的对象的类型,而非引用的但是。
3.添加代码对arr数组实现冒泡排序:
package firstTest;
import java.util.*;
public class BubbleSort {
public static void main(String[] args) {
int arr[] = new int [10];
Random rand = new Random();
for (int i =0 ;i<arr.length;i++){
arr[i] = rand.nextInt(100);
}
for (int i =0 ;i<arr.length;i++){
System.out.print(arr[i]+",");
}
for(int i =0;i<arr.length-1;i++){
boolean isSwap = false;
**for(int j=0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
isSwap = true;
}
}**
if (!isSwap)
break;
}
System.out.println();
for (int i =0 ;i<arr.length;i++){
System.out.print(arr[i]+",");
}
}
}
注:双层循环时,break只跳出所在的一层
4.下面错误的是?
A. int[] arr = new int[]{1,2,3}
B. int[] arr = null;
arr = {1,2,3,4,5}
C. int[][] arr = new [][]{{1,2,3},{4,5},{6}}
D. int[][] arr = new int[2][]
答案:B
原因:没有初始化。
注:C中有三个数组,一个长度为3,一个为2,一个为1.
5.下面创建对象的总个数为__:
String s = “a”+”b”+”c”+”d”+”e”;
答案:1
原因: “a”,”b”,”c”,”d”,”e”都是常量,编译期间就已经运算在一起了。就创建了一个 。对于常量,编译时就直接存储它们的字面值而不是它们的引用。 在编译时就直接讲它们连接的结果提取出来变成了”abcde” 该语句在class文件中就相当于String s = “abcde” 然后当JVM执行到这一句的时候, 就在String pool里找 如果没有这个字符串,就会产生一个
6.代码运行结果:
package firstTest;
public class Test_6 {
int x;
public static void main(String[] args) {
Test_6 t = new Test_6();
t.x = 5;
change(t);
System.out.println(t.x);
}
public static void change(Test_6 t){
t.x = 3;
}
}
结果:3
原因:静态函数仅仅与输入的数有关,没有实际的对象,所以没有this指针,也不可以对非静态成员访问。
7.
package FirstTest;
public class Test_11 {
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("abc");
if(str1==str2){
System.out.println("true");
}else{
System.out.println("false");
String s3 = "hello"
String s4 = "hello";
System.out.println(s3==s4);
}
}
}
答案:false
用new String()创建的字符串不是常量,不能在编译期就确定,所以new String()创建的字符串不放入常量池中,他们有自己的地址空间。
http://www.cnblogs.com/dapeng111/p/3530542.html
http://blog.csdn.net/olanlanxiari/article/details/8104505
8.
package FirstTest;
public class A {
private int counter = 0;
public static int getInstanceCount() {
return counter;
}
public A() {
counter++;
}
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
A a3 = new A();
System.out.println(A.getInstanceCount());
}
}
A. 该类编译失败
B. 输出:1
C. 输出:3
D. 输出:0
答案:A
静态函数只能调用静态变量
9.
continue不能出现在switch语句中
原因:continue 是终止循环,switch是顺序执行
10.
在Java语言中,字符串“Java程序员”在内存中所占用的字节数是:_.
答案:14
11.
下列说法正确的是:
A.在Tree类中添加代码:public Tree() { Plant(); },编译将通过
B.在Plant类中添加代码:public Plant() { Tree(); },编译将通过
C.在Plant类中添加代码:public Plant() { this(”fern”); },编译将通过
D.在Plant类中添加代码:public Plant() { Plant(”fern”); },编译将通过
package FirstTest;
class Plant {
private String name;
public Plant(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Tree extends Plant {
public void growFruit() {
}
public void dropLeaves() {
}
public static void main(String[] args) {
}
}
答案:C
原因:java规定,子类在构造之前必须先构造父类,如果子类的构造方法中没有调用父类的构造方法,则java编译器会自动的加入对父类无参构造方法的调用
12.请看下列代码编译和运行的结果是()
package FirstTest;
interface DeclareStuff {
public static final int EASY = 3;
void doStuff(int t);
}
public class TestDeclare implements DeclareStuff {
public static void main(String[] args) {
int x = 5;
new TestDeclare().doStuff(++x);
}
public void doStuff(int s) {
s += EASY + ++s;
System.out.println("s=" + s);
}
}
答案:16
原因:
http://www.cnblogs.com/gw811/archive/2012/10/13/2722752.html