找工作笔试中的常见考点

1.Java程序初始化执行顺序:

父类静态变量、父类静态代码块、子类静态变量、子类静态代码块、父类非静态变量、父类非静态代码块、父类构造函数、子类非静态变量、子类非静态代码块、子类构造函数。

2.程序运行结果是多少?

public class People {
String name;
public People(){
System.out.print(1);
}
public People(String name){
System.out.print(2);
this.name = name;
}
public static void main(String[] args) {
new Child("mike");
}


}
class Child extends People{
People father;
public Child(String name){
System.out.print(3);
this.name = name;
father = new People(name + ":F");
}
public Child(){
System.out.print(4);
}
}

答:132

3..编程题一:给定一个数组判断是否有序?

public class Demo4 {

	public static void main(String[] args) {
		int[] arr = {6,5,4,3,2,1};
		boolean flag = method(arr);
		if(flag){
			System.out.println("已排序");
		}else{
			System.out.println("未排序");
		}
	}

	public static boolean method(int[] arr) {
		for(int i=0;i<arr.length-2;i++){
			if(arr[i]>arr[i+1]){
				if(arr[i+1]<=arr[i+2]) return false;
			}else if(arr[i]>arr[i+1]){
				if(arr[i+1]<=arr[i+2]) return false;
			}
		}
		return true;
	}

}
编程题二:使用FileOutputStream将100,101,102,103,104,105以数组的形式写入到d:\text.txt文件中?

public class Demo4 {

	public static void main(String[] args) {
		int[] data = {100,101,102,103,104,105};
		try {
			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/text.txt"));
			for(int i=0;i<data.length;i++){
				out.write(data[i]);
			}
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
编程题三:设有一个背包可以放入的物品重量为weight,现有n件物品,重量分别为w1,w2,...,wn,由大到小,
问能否从这n件物品中选择若干件放入背包中,使放入的重量之和正好为weight?

public class Demo4 {
	static int[] arr = {3,5,6,7,8,9};
	public static void main(String[] args) {
		
		int weight = 18;
		
		int n = arr.length;
		
			if(f(weight,n)==1)
				System.out.println("可以找到");
			else
				System.out.println("无法找到");
		
	}
	public static int f(int w,int s){
		if(w==0) return 1;
		if(w<0||w>0 && s==0) return 0;
		if(f(w-arr[s-1],s-1)==1) return 1;
		return f(w,s-1);
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值