【Java】类和对象练习题

类和对象-练习题


这篇博客分享一些关于类和对象的基础题,便于快速了解一些关键点

01

下列代码的运行结果是?

public static void main(String[] args){
  String s;
  System.out.println("s="+s);
}

由于String s没有初始化,代码不能通过编译。

因为在Java当中局部变量必须先初始化,后使用。

public static void main(String[] args){
    Test test = new Test();  
    String s;
    System.out.println("s="+s);
    System.out.println(s);
}

02

请问,对语句行 test.hello(). 描述正确的有()

package NowCoder;
class Test {
	public static void hello() {
	    System.out.println("hello");
	}
}
public class MyApplication {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test=null;//代表当前不指向任何对象
		test.hello();//static的一个静态方法
	}
}
//静态方法不依赖于任何对象

A.能编译通过,并正确运行

B.因为使用了未初始化的变量,所以不能编译通过

C.以错误的方式访问了静态方法

D.能编译通过,但因变量为null,不能正常运行

/*hello方法是一个静态方法,调用静态方法不需要创建实例对象。

此时的		Test test=null; 表示test这个引用不指向任何对象。所以此处可以正常访问。

但是我们需要牢记,静态方法的正确访问方式应该是用过类型来访问。即:Test.hello();
*/

//若是将public static void hello()中的static去掉,那么这个程序运行就来就会报空指针错误

03

如下代码的输出结果是什么?

public class Test { 
    public int aMethod(){
        static int i = 0;//static只能修饰成员变量,不能修饰局部变量
        i++; 
        return i;
    } 
public static void main(String args[]){
    Test test = new Test(); 
    test.aMethod(); 
    int j = test.aMethod();
    System.out.println(j);
    } 
}

在方法当中定义的变量是局部变量,而静态的变量属于类变量。随着类的加载而被创建,而局部变量是调用该方法的时候,才创建的。

所以,此时两种变量的性质是冲突的。Java当中不允许定义局部的静态变量。

所以该题会编译失败

04

当你编译和运行下面的代码时,会出现下面选项中的什么情况?

public class Pvf{
    static boolean Paddy;//默认false
    public static void main(String args[]){
        System.out.println(Paddy);
    }
}

所以该题会输出false

05

已知如下类说明:

public class Test{

  private float f=1.0f;

  int m=12;

  static int n=1;

  public static void main(String args[]){

    Test t=new Test();

  }

}

如下哪些在main函数中使用是正确的()

A. t.f = 3.0 – 一定是错的 3.0默认是double类型

B. this.n static修饰的只能通过类名来访问,static修饰的成员不属于当前对象,它是属于当前类的

C. Test.m ----> this.m

D. Test.n

A:f是float类型,3.0默认是double,所以此时不能赋值

B:n是静态的,需要通过类名访问,不能通过this访问,this代表当前对象的引用,但是静态的成员变量不属于thisC:m是实例成员变量,需要通过对象来进行调用。

D:正确

06

关于以下程序代码的说明正确的是()

 public class HasStatic {// 1
	private static int x = 100;// 2
	public static void main(String args[]) {// 3
		HasStatic hsl = new HasStatic();// 4
		hsl.x++;// 5
		HasStatic hs2 = new HasStatic();// 6
		hs2.x++;// 7
		hsl = new HasStatic();// 8
		hsl.x++;// 9
		HasStatic.x--;// 10
		System.out.println(" x=" + x);// 11
	}
}

A. 程序通过编译,输出结果为:x=102

B. 程序通过编译,输出结果为:x=103

C. 10行不能通过编译.因为x星私有静态变量

D. 5行不能通过编译.因为引用了私有静态变量

本题中的静态成员变量x,属于类变量,只有一份。所有对x的操作针对的都是同一份。
静态成员变量的访问需要通过类名访问,这是正确的访问方式。本题中虽然使用了对象引用访问,但是不会报错,我们不建议这样访问,但不是错误,所以,不会编译报错。
综合以上2点,得出结论:本题可以正常通过编译和运行,输出结果是102

07

以下代码在编译和运行过程中会出现什么情况

public class TestDemo{

	private int count;

	public static void main(String[] args) {

		TestDemo test=new TestDemo(88);

		System.out.println(test.count);

	}

	 TestDemo(int a) {

		 count=a;

	}

}

A.编译运行通过,输出结果是88

B.编译时错误,count变量定义的是私有变量

C.编译时错误,System.out.println 方法被调用时test没有被初始化

D.编译和执行时没有输出结果

观察代码当执行:

TestDemo test = new TestDemo(88);
的时候,会先执行构造方法,将88的值,赋值给count,所以最终输出的值是88.

08代码执行先后顺序

public class Test{
    static int cnt = 6;
    static{
        cnt += 9;
    }
    public static void main(String[] args){
        System.out.println("cnt = " + cnt);
    }
    static{
        cnt /=3;
    };
}

cnt的值是( )

A. cnt=5

B. cnt=2

C. cnt=3

D. cnt=6

本题考察的是代码块的执行顺序。带代码中存在代码块和构造方法的时候。执行顺序为:

1.静态代码块

2.实例代码块

3.调用的对应的构造方法

第2种情况:当存在相同类型的代码块和成员变量的时候,需要看定义顺序执行。

本题中:

public class Test{
    static int cnt = 6;//1
    static{
        cnt += 9;//2
    }
    public static void main(String[] args){
        System.out.println("cnt = " + cnt);
    }
    static{
        cnt /=3;//3
    };
}

本题中先执行注释1处,再执行注释2处,此时结果变为了15,再执行注释3处,cnt = 5;

09

在JAVA中,假设A有构造方法A(int a),则在类A的其他构造方法中调用该构造方法和语句格式应该为()

A. this.A(x)

B. this(x)

C. super(x)

D. A(x)

this共有2种使用方式:

this.data 访问当前对象的实例成员变量

this.func() 访问当前对象的实例成员方法

this(参数列表) 访问当前对象的构造方法

此处在当前类当中,调用构造方法A(int a),使用this(x);的方式,所以选择B。



A:不能通过点号访问构造方法

C:super(x),是初始化父类继承过来的那部分成员

D:缺少this

同时,以下这张图是针对类和对象的总结
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值