练习(二)(Java)

1、下面代码将输出什么内容:(B false

public class SystemUtil{
	public static boolean isAdmin(String userId){
		return userId.toLowerCase()=="admin";
	} 
	public static void main(String[] args){
		System.out.println(isAdmin("Admin"));
	}
}

2、阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有(A 能编译通过,并正确运行

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();
	}
}
A 能编译通过,并正确运行
B 因为使用了未初始化的变量,所以不能编译通过
C 以错误的方式访问了静态方法
D 能编译通过,但因变量为null,不能正常运行

3、. 在使用super和this关键字时,以下描述正确的是(A 在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一行,否则编译不通过

A 在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一行,否则编译不通过
B super()和this()不一定要放在构造方法内第一行
C this()和super()可以同时出现在一个构造函数中
D this()和super()可以在static环境中使用,包括static方法和static语句块

4、如下代码的输出结果是什么?( D 编译失败

public class Test {
	public int aMethod(){
	static int i = 0;
		i++;
		return i;
	}
	public static void main(String args[]){
		Test test = new Test();
		test.aMethod();
		int j = test.aMethod();
		System.out.println(j);
	}
}
A 0      B 1      C 2       D 编译失败

5、下列哪一种叙述是正确的(D 声明抽象方法不可写出大括号

A abstract修饰符可修饰字段、方法和类
B 抽象方法的body部分必须用一对大括号{ }包住
C 声明抽象方法,大括号可有可无
D 声明抽象方法不可写出大括号

6、下列说法正确的是(C constructor在一个对象被new 时执行

A class中的constructor不可省略
B constructor必须与class同名,但方法不能与class同名
C constructor在一个对象被new 时执行
D 一个class只能定义一个constructor

7、选项中哪一行代码可以替换 //add code here 而不产生编译错误(A public abstract void method(int a);

public abstract class MyClass {
	public int constInt = 5;
	//add code here
	public void method() {
	}
}
A public abstract void method(int a);
B consInt=constInt+5;
C public int method();
D public abstract void anotherMethod(){}

8、java 中哪个关键字可以对对象加互斥锁?(B synchronized

A transient
B synchronized
C serialize
D static

9、. 标题:汽水瓶 | 时间限制:1秒 | 内存限制:32768K
有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板,如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?

/*
输入描述:
输入文件最多包含10组测试数据,每个数据占一行,仅包含一个正整数n(1<=n<=100),表示小张手上的空汽水瓶数。n=0表示输入结束,你的程序不应当处理这一行。
输出描述:
对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。
注意:
在这里,最后输出的是可以兑换的饮料瓶数,不包括,之前拥有的瓶子数量,所以,可兑换的饮料瓶数的初始值是0
*/
import java.util.Scanner;

public class Drink {
    public static int drink(int n){
        int total = 0;//兑换饮料的瓶数
        while(n > 2){ //当空瓶数量大于2时,可以进行兑换
            total += n/3;//更新兑换饮料的数量
            n = n/3+n%3; //更新空瓶数量
        }
        if(n == 2){ //还剩两个空瓶时,可以借老板一瓶,再换一瓶
            total+= 1;
        }
        return total;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int total = drink(n);
            System.out.println(total);
        }
    }
}

10、 标题:数组中的逆序对 | 时间限制:3秒 | 内存限制:32768K
有一组数,对于其中任意两个数组,若前面一个大于后面一个数字,则这两个数字组成一个逆序对。请设计一个高效的算法,计算给定数组中的逆序对个数。给定一个int数组A和它的大小n,请返回A中的逆序对个数。保证n小于等于5000。

/*方法一:
顺序扫描整个数组,将每个数字与后面的数字进行比较,输出逆序对数量,时间复杂度O(n^2)
*/

import java.util.*;

public class AntiOrder {
    public int count(int[] A, int n) {
        int count = 0;
       for(int i  = 0;i < A.length;i++){
           for(int j = i+1;j<A.length;j++){
               if(A[i]>A[j]){
                   count++;
               }
           }
       }
       return count;
    }
}
/*
方法二:
分治思想,采用归并排序的思路来处理,先分后治:先把数组分隔成子数组,先统计出子数组内部的逆序对的数目,然后再统计出两个相邻子数组之间的逆序对的数目。

在统计逆序对的过程中,还需要对数组进行排序,排序过程就是归并排序的思路

逆序对的总数=左边数组中的逆序对的数量+右边数组中逆序对的数量+左右结合成新的顺序数组时中出现的逆序对的数量
*/
import java.util.*;
public class AntiOrder {
	public int count(int[] A, int n) {
		if (A == null || n == 0) {
			return 0;
		} 
		return mergeSortRecursion(A, 0, n - 1);
		} 
	public static int mergeSortRecursion(int[] arr, int l, int r) {
		if (l == r) {
			return 0;
		} 
		int mid = (l + r) / 2;
		return mergeSortRecursion(arr, l, mid) + mergeSortRecursion(arr, mid + 1, r) +merge(arr, l, mid, r);
	} 
	public static int merge(int[] arr, int left, int mid, int right) {
		int[] temp = new int[right - left + 1];
		int index = 0;
		int i = left;
		int j = mid + 1;
		int inverseNum = 0;// 新增,用来累加数组逆序对
		while (i <= mid && j <= right) {
			if (arr[i] <= arr[j]) {
			temp[index++] = arr[i++];
		} else {
			// 当前一个数组元素大于后一个数组元素时,累加逆序对
			// s[i] > s[j] 推导出 s[i]...s[mid] > s[j]
			inverseNum += (mid - i + 1);
			temp[index++] = arr[j++];
			}
		} 
		while (i <= mid) {
			temp[index++] = arr[i++];
		} 
		while (j <= right) {
			temp[index++] = arr[j++];
		} 
		for (int k = 0; k < temp.length; k++) {
			arr[left++] = temp[k];
		} 
		return inverseNum;
	}
}

你可能感兴趣的:(Java)