筆試題收集

1、強制轉換 

public class Test1 {
	public static void main(String[] args) {
		((Test1)null).getInfo();
	}
	public static void getInfo(){
		System.out.println("張三");
	}
}
結果(輸出):張三

 2、return使用

public class Test1 {
	public static void main(String[] args) {
		try {
			int t = 1;
			return;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			System.out.println("111");
		}
	}
}
結果(輸出):111

3、Boolean 

public class Test1 {
	public static void main(String[] args) {
		int a = 1;
		Boolean[] b = new Boolean[3];
		Boolean c = b[a];
		System.out.println(c);
	}
}
結果(輸出):null

 3、if條件語句

public class Test1 {
	public static void main(String[] args) {
		int x = 3;
		if(x>0){
			System.out.println("A");
		}else if(x>-3){
			System.out.println("B");
		}else{
			System.out.println("C");
		}
	}
}
結果(輸出):A
當x>-3&&x<=0時,輸出:B

for (; ; ) {
    System.out.println(11);
}
結果(輸出):一直輸出11,死循環

4、類可以被final或abstract修飾,但是不能同時修飾

5、求1-100之間的素數

public static void main(String[] args) {

	System.out.println(2);
	for(int i=2;i<100;i++){
		if(i%2!=0){//去除能被2整除的數
			if(getNum(i)){
				System.out.println(i);
			}
		}
	}
}

//素數
private static boolean getNum(int i) {
	for(int j=2;j<i;j++){
		if(i%j==0){
			return false;
		}
	}
	return true;
}

6、統計一個字符串中字符出現的次數並打印出來

public static void main(String[] args) {

	String s = "abccbasdfwwdd";
	char[] c = s.toCharArray();
	Map<Character,Integer> map = new HashMap<Character,Integer>();
	for (char d : c) {
		if(map.containsKey(d)){
			map.put(d, map.get(d)+1);
		}else{
			map.put(d, 1);
		}
	}
	for (Entry<Character,Integer> en : map.entrySet()) {
		System.out.println(en.getKey()+":"+en.getValue());
	}
}

7、 刪除list集合中的重複元素

String[] arr = {"aa","bb","cc","aa","dd","cc"};
Arrays.sort(arr);
List<String> list = new ArrayList<String>();
for (String i : arr) {
    if(!list.contains(i))
	list.add(i);
}
System.out.println(list);
輸出結果:[dd, aa, bb, cc]

8、IO流

對象字節輸入流和輸出流

寫入文件對象信息
ObjectOutputStream output =  new ObjectOutputStream(new FileOutputStream(new File("文件路徑")));
output.writeObject(new User("張三",10));
output.close();


讀取文件對象信息
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("文件路徑")));
User user = (User) input.readObject();
System.out.println(user);
input.close();

緩衝字節輸入流和輸出流

遍歷文件夾,並將文件夾下的所有文件複製到指定的位置
public static void main(String[] args) throws IOException {
	recursion("文件夾路徑");
}

private static void recursion(String path) throws IOException {
	File file = new File(path);
	if(file.exists() && null != file && file.length()!=0){
		File[] files = file.listFiles();
		for (File file2 : files) {
			if(file2.isDirectory()){
				recursion(file2.getAbsolutePath());
			}else{
				try {
					BufferedInputStream in = new BufferedInputStream(new FileInputStream(file2.getAbsoluteFile()));
					File file3 = new File("文件夾路徑",file2.getName());
					File file4 = new File("文件夾路徑");
					if(!file4.exists())file4.mkdir();
					if(!file3.exists())file3.createNewFile();
					BufferedOutputStream out  = new BufferedOutputStream(new FileOutputStream(file3));
					int len;
					while((len=in.read())!=-1){
						System.out.println(len);
						out.write(len);
					}
					in.close();
						out.close();
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

9、Java統計一個字符串在另一個字符串中出現的次數

第一種方法:

public static void main(String[] args) {
    String a = "718679asdas718asasasc21133718";
	String b = "718";
    filter(a, b);
}
 
public static void filter(String a,String b){
    int count = 0;
    while(true){
        int i = a.indexOf(b);
        if(i==-1)break;
        count ++;
        a = a.substring(i+b.length());
    }
    System.out.println(count);
}

第二種方法:

public static void main(String[] args) {
    String a = "718679asdas718asasasc21133718";
	String b = "718";
    filter(a, b);
}
 
public static void filter(String a,String b){
    String replace = "";
    if(a.contains(b))
        replace = a.replace(b,"");
    int count = (a.length()-replace.length())/b.length();
    System.out.println(count);
}

10、float   f  =  3.4  正確嗎?

不正確,精度不準確,應該用強制類型轉換,如:float  f  =  (float )3.4 或  float  f  =  3.4f
在java裏,無小數點的默認是int,有小數點的默認是double

11、ArrayList的擴充問題?

ArrayList   list  =  new  ArrayList();這種是默認創建大小爲10的數組,每次擴容大小爲1.5倍ArrayList   list  =  new  ArrayList(20);使用的是ArrayList的有參構造函數,創建時直接分配其大小,沒有擴充

12、存在使  i + 1 < i  的數嗎?

存在,若 i 爲 int 整型,那麼當 i 爲 int 能表示的最大整數時, i + 1 就溢出變成負數了

13、對登錄表單的密碼如何進行加密?

RSA非對稱算法:使用RSA會產生公鑰和私鑰,基於產生的公鑰對明文加密,針對已經加密的明文,使用私鑰解密,得到明文

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章