Java习题

目录

一、选择题

二、编程

1、奇数放到数组后面

2、交换两个数组

一、选择题

1.阅读如下代码,请问对语句行 test.hello(). 描述正确的有(A

package NowCoder;
class Test {
	public static void hello() {
	    System.out.println("hello");
	}
}
public class MyApplication {
	public static void main(String[] args) {
		Test test=null;
		test.hello();
	}
}

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

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

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

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

解析:静态的不依赖对象

   类的静态成员不依赖实例对象,静态成员在类编译的时候就已经被创建好了,经测试后发现,无论是是使用类名,还是使用我们定义的类变量都是可以访问的,哪怕它已经被实例化。例:如下代码

class Test{
    public static int num=0;
    public static void say(){
        System.out.println("say~");
    }
}
public class Main{
    public static void main(String[] args){
        Test test=new Test();
        test.say();
        Test.say();
        int num1=test.num;
        int num2=Test.num;
        System.out.println(num1);
        System.out.println(num2);
    }
}
//运行结果:
say~
say~
0
0

2.下面代码的运行结果是(由于String s没有初始化,代码不能编译通过。

public static void main(String[] args){

  String s;

  System.out.println("s="+s);

}

注释:s是局部变量,局部变量在使用前必须初始化

局部变量和成员变量及他们的区别:https://blog.csdn.net/Aug_IK/article/details/116135096


3.如下代码的输出结果是什么?(编译失败

public class Test { 
    public int aMethod(){
        static int i = 0;//错误写法!!! 静态变量只能在类主体中定义,不能在方法中定义!!!
//此时的i是类变量放在方法区,不依赖于对象,而aMethod()不是类方法,他依赖于对象
        i++; 
        return i;
    } 
public static void main(String args[]){
    Test test = new Test(); 
    test.aMethod(); 
    int j = test.aMethod();
    System.out.println(j);
    } 
}

 注:考察静态关键字,此时的i是类变量放在方法区,不依赖于对象,而aMethod()不是类方法,他依赖于对象。(但是即使是给aMethod()方法加上static关键字,也不正确)

  • 同样是错误的写法2:变量定义在方法内部是局部变量,放在栈上,static定义的变量放在方法区矛盾
public  class Main{
    public static int f(){
        static int i=0;//错误写法,定义在方法内部是局部变量,放在栈上,static定义的变量放在方法区
        i++;
        return i;
    }
    public static void main(String[] args){
        Main main =new Main();
        int num1=main.f();
        int num2=main.f();
        System.out.println(num2);
        System.out.println(num1);
    }
}

静态变量是先于类的其他而加载的,所以static代码块只能是类成员变量,而不能是局部变量,因为在static加载时,方法还没有分配空间。

静态变量只能在类主体中定义,不能在方法中定义!!!

静态变量只能在类主体中定义,不能在方法中定义!!!

静态变量只能在类主体中定义,不能在方法中定义!!!

  • 正确写法:静态变量只能在类主体中定义,不能在方法中定义!!!
public  class Main{
    static int i=0;//static修饰的是类变量,初始化一次
    public int f(){
        i++;
        return i;
    }
    public static void main(String[] args){
        Main main =new Main();
        int num1=main.f();
        int num2=main.f();
        System.out.println(num1);//运行结果:1
        System.out.println(num2);//运行结果:2
        
    }
}

 4.当你编译和运行下面的代码时,会出现下面选项中的哪种情况?(编译通过并输出结果false

public class Pvf{
    static boolean Paddy;//静态类变量(成员变量)boolean默认值是false
    public static void main(String args[]){
        System.out.println(Paddy);
    }
}

5.运行结果:102

public class HasStatic {
	private static int x = 100;   x是类变量 初始化一次 存在整个程序运行期
	public static void main(String args[]) {
		HasStatic hsl = new HasStatic();
		hsl.x++;   x访问不依赖对象 报警告 但是编译可以  结果没问题
		HasStatic hs2 = new HasStatic();
		hs2.x++;   x访问不依赖对象 报警告 但是编译可以  结果没问题
		hsl = new HasStatic();
		hsl.x++;// 9    x访问不依赖对象 报警告 但是编译可以  结果没问题
		HasStatic.x--;//10   正确访问x
		System.out.println(" x=" + x);
	}
}
//输出结果:102

6.已知如下类说明:

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();
  }
}

如下哪些使用是正确的(D)

A.t.f = 1.0  // 错误  1.0是double类型的数据   f是float类型的数据

B.this.n      //错误  this是当前对象的引用,n是静态的类变量,不依赖对象

C.Test.m    //错误  除非m是静态的

D.Test.n    // 正确   通过类名访问类变量

注:局部变量和成员变量及她们的区别:https://blog.csdn.net/Aug_IK/article/details/116135096


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

A.this.A(x)      

B.this(x)   //题目有一个参数

C.super(x)

D. A(x)

this的三种使用:this表示当前对象的引用

  this.data      //访问数据

  this.func()   //访问方法

 this()     //访问构造方法  ,有参写参


8.以下代码在编译和运行过程中会出现什么情况(编译运行通过,输出结果是88)

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;
	}
}

9.给定以下代码:程序输出结果为:(5)

public class Test{//初始化顺序 类加载过程 静态的先运行(按顺序运行)
    static int cnt = 6;  //静态成员变量   第一个运行
    static{              //静态代码块     第二个运行
        cnt += 9;       
    }
    public static void main(String[] args){    //最后运行
        System.out.println(“cnt =” + cnt);
    }
    static{             //静态代码块     第三个运行
        cnt /=3; 
    };
}

//运行结果:5

10、给定以下代码:程序输出结果为:(aaabbb)

class Test{	
	public String toString() {   //重写了toString方法
		System.out.print("aaa");  //先打印aaa
		return "bbb";             //再返回bbb
	}
}
public static void main(String[] args) {
	System.out.println(new Test());//匿名对象  先调用toString方法 在toString中接收到bbb打印出来,打印在toString方法输出的aaa后面
}
//运行结果:aaabbb

二、编程

1、奇数放到数组后面

题目描述:给定整型数组, 把所有的偶数放到数组前面, 把所有奇数放到数组后面.

思路:左边奇数,右边找偶数,交换

import java.util.Arrays;

public class Test{
    public static void main(String[] args) {
        int[] a= {1,2,3,4,5,6};
        func(a);
        System.out.println(Arrays.toString(a));
    }
    public static void func(int[] a){
        int left=0;
        int right=a.length-1;
        while(left<right){
            while(left<right&&a[left]%2==0){
                left++;
            }
            while (left<right&&a[right]%2!=0){
                right--;
            }
            int t=a[left];
            a[left]=a[right];
            a[right]=t;
        }
    }
}

2、交换两个数组

题目描述:给定两个整型数组, 交换两个数组的内容.

import java.util.Arrays;
public class Test{
    public static void main(String[] args) {
        int[] a= {1,2,3,4,5,6};
        int[] b= {4,5,6};
        System.out.println("a:"+Arrays.toString(a));
        System.out.println("b:"+Arrays.toString(b));
        swap(a,b);
        System.out.println("交换后:");
        System.out.println("a:"+Arrays.toString(a));
        System.out.println("b:"+Arrays.toString(b));
    }
    public static void swap(int[] a,int[] b) {
        int len = a.length < b.length ? a.length : b.length;
        for (int i = 0; i < len; i++) {
            int t = a[i];
            a[i] = b[i];
            b[i] = t;
        }
         if (a.length > b.length) {
            System.out.println("b数组长度不足");
            for (int i = len; i <a.length; i++) {
                a[i]=0;
            }
        }else{
            System.out.println("a数组长度不足");
            for (int i = len; i <b.length; i++) {
                b[i] = 0;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值