1. 程式人生 > >Java面試集合(三)-30道面試題

Java面試集合(三)-30道面試題

前言

大家好,我是 Vic,今天給大家帶來Java面試集合(三)的概述,希望你們喜歡

1.在Java中是否可以含有多個類?
答:可以含有多個類,但只有一個是public類,public類的類名與檔名必須一致。

2.說說&和&&的區別?
答:&&短路與,當第一個表示式為false時,第二個表示式不會進行。&,當一個表示式為false時,第二個表示式會進行。

3.char變數型別,能否儲存一箇中文漢字?
答:可以儲存一個漢字,因為char是用Unicode編碼來儲存的,所以可以儲存。

4.final關鍵字修飾變數時,是引用不變,還是引用物件不變?
答:使用final關鍵字修飾變數時,是引用變數不能變,引用變數所指物件中的內容是可以改變的。

5.靜態變數和例項變數的區別?

答:靜態變數前要加static修飾,而例項變數不用。在靜態變數中不需要例項物件來呼叫輸出,而例項變數則需要進行例項化,才能使用。

public class Test{
   public int i = 1;
   public static int j = 0;
   public static void main(String[] args) {
       Test in = new Test();
       System.out.println(in.i);
       System.out.println(j);
   }   
}

6.如何理解Math類中的ceil,floor,round?
答:ceil為天花板,則向上取整為取大的值,補充到整數;floor為地板,則向下取整為取最近小的整數;round為周圍,四捨五入,原則在原來基礎上+0.5,超過0.5的進位,不超過0.5就取進小整數。

7.Overload和Override的區別?
答:Overload為過載,Override為覆蓋,重寫。

8.請說說分層設計的好處?
答:

  • 實現了軟體之間的解耦

  • 便於進行分工

  • 便於對軟體元件的重用

  • 便於進行維護

  • 便於對功能的擴充套件

9.Java中實現多型的機制是?
答:父類或介面定義的引用變數指向子類具體實現類的例項物件,引用變數指向具體的例項物件。

10.說說abstract,interface?
答:

  • abstract修飾class為抽象類,抽象類不能建立例項物件,抽象類中的方法不必要抽象abstract修飾,但是含有abstract修飾的方法的類則必須是抽象類。
    abstract class內可以沒有抽象方法,不可以被例項化,但是可以被宣告。

  • interface介面中的所有方法必須是抽象的,介面中方法預設為public abstract型別,介面中變數型別預設public static final型別。
    介面中的成員變數必須定義初始化,實現介面類必須在該類實現所有的方法。

在抽象類中有構造方法,介面中沒有;抽象類中有普通成員變數,介面中沒有;抽象類中可以有靜態方法,介面中不能有靜態方法。

11.什麼是內部類?
答:內部類是在一個類的內部定義的類,靜態內部類可以有靜態成員變數,而非靜態內部類不能有靜態成員;內部類可以在外部類中的方法中定義,也可以在外部類的方法外定義。

靜態內部類中對外部類進行引用,只有非靜態外部類才能對外部類進行引用。

在靜態內部類中不需要進行外部類的例項,就可以進行例項化,而非靜態內部類需要在外面建立內部類的例項物件,建立時,一定要先建立外部類的例項物件,然後用外部類的例項物件去建立內部類的例項物件。

12.String是否可以被繼承?
答:不可以被繼承,因為java.lang.String類是final型別的,不能繼承的類,被final關鍵字修飾的類,並且不能被修改,不能改變!

當一個final型別中,String s = "Vic";s = s + " love ";表示原有的物件並沒有被改變而是該引用轉向了新的物件,原有的s引用不在指向原有的物件了。

13.1到99累加的String和StringBuffer效率?

StringBuffer sb = new StringBuffer();
for(int i = 0; i<100; i++){
sb.append(i);
}
String str = new String();
for(int i = 0; i<100; i++){
str = str + i;
}

StringBuffer只建立一個物件,而String建立了多個物件。

14.說說final,finally,finalize?
答:final用於修飾屬性,方法,類,被修飾的屬性是不可以變的,被修飾的方法是不可被覆蓋的,被修飾的類是不可以被繼承的。

finally這個是在異常處理語句中的一部分,finally中的語句是總要執行的。

finalize是垃圾回收集機制的,記住這點就夠了。

15.在Java中有幾種方法來實現執行緒?
答:

//new Thread(){}.start();
new Thread(){
public void run(){
}
}.start();

new Thread(new Runnable(){
public void run(){
}
}).start();

16.說說迭代器模式?

答:

public static void print(String str){
Iterator it = str.iterator();
while(it.hasNext()){
 String str = it.next();
 System.out.println(str);
}
}
17.說說裝飾者模式?

答:

public interface Person{
void eat();
}
public class Student implements Person{
public void eat(){
 System.out.println("eating");
}
}
public class Me implements Person{
private Student student;
Me(Student student){
 this.student=student;
}
public void eat(){
System.out.println("study");
student.eat();
System.out.println("sleeping");
}
}
public class PersonDemo{
public static void main(String[] args){
 Student student = new Student();
 Me me = new Me(student);
 me.eat();
}
}

18.說說單例模式?

//私有構造方法;私有靜態引用,返回值為靜態的公有方法
public class Singleton{
private static Singleton singleton = new Singleton();
private Singleton(){
}
public static Singleton getInstance(){
 return singleton;
}
}

19.說說工廠模式?

public interface School{
void Study();
}
public class Studentone implements School{
public void Study(){
 System.out.println("studyone");
}
}
public class Studenttwo implements School{
public void Study(){
 System.out.println("studytwo");
}
}
public interface AllSchool{
School getSchool();
}

public class Studentone implements AllSchool{
public School getSchool(){
 return new Study();
}
}
public class Studenttwo implements AllSchool{
public School getSchool(){
 return new Study();
}
}

public class SchoolDemo{
public void test(){
 AllSchool allSchool = null
 //one
 allSchool = new Studentone();
 Studentone one = allSchool.getSchool();
 one.Study();
 //two
 allSchool = new Studenttwo();
 Studenttwo two = allSchool.getSchool();
 two.Study();
}
}

20.說說原型模式?
答:實現Cloneable介面,重寫Object類中的clone方法

21.說說生成器模式?
答:

class Ym{
public void ymRequest(){
 System.out.println("getYm");
}
}
interface Target{
public void request();
}
class Ym extends Ym implements Target{
public void request(){
 super.ymRequest();
}
}
public class Test{
public static void main(String[] args){
 Target adapter = new Adapter();
 adapter.request();
}
}

22.說說代理模式?

//靜態
public interface Info{
public void infoOne():
public void infoTwo();
}

public class TrueInfo implements Info{
public void infoOne(){
}
public void infoTwo(){
}
}

public class FlaseInfo implements Info{
private Info trueInfo;
public FlaseInfo(Info trueInfo){
 this.trueInfo = trueInfo;
}
public void infoOne(){
}
public void infoTwo(){
}
}

public class Test{
public static void main(String[] args){
 Info trueInfo = new TrueInfo();
 Info flaseInfo = new FlaseInfo(trueInfo);
 flaseInfo.infoOne();
 flaseInfo.infoTwo();
}
}

23.說說外觀模式?

public class Studentone{
public void One(){
 System.out.println("Studentone one");
}
public void Two(){
 System.out.println("Studentone two");
}
}

public class Studenttwo{
public void One(){
 System.out.println("Studenttwo one");
}
public void Two(){
 System.out.println("Studenttwo two");
}
}

public class School{
public void Study(){
 Studnetone one = new Studentone();
 one.One();
 one.Two();
 Studenttwo two = new Studnettwo();
 two.Ono();
 two.Two();
}
}

public class Test{
public static void main(String[] args){
 School school = new School();
 school.Study();
}
}

24.說說氣泡排序?

public class Demo{
public static void main(String[] args){
int[] nums = { 3,1,7,5,8,9,23,45};
for(int i = 0; i< nums.length-1;i++){
 for(int j = 0;j<nums.length-1-i;j++){
  if(nums[j]>nums[j+1]){
  int temp = nums[j];
  nums[j] = nums [j+1];
  nums[j+1] = temp;
}
}
for(int j = 0; j<nums.length;j++){
Systm.out.println(nums[j]);
}
}

25.說說選擇排序?

//這種就是排序演算法,比如有6個人,第一輪要進行5次比較
//第一輪
for(int index=1;index<arr.length;index++)
{
if(arr[0]>arr[index])
{
 int temp = arr[0];
 arr[0] = arr[index];
 arr[index] = temp;
}
}
print(arr);

//第二輪
for(int index=2;index<arr.length;index++)
{
if(arr[1]>arr[index])
{
 int temp = arr[1];
 arr[1] = arr[index];
 arr[index] = temp;
}
}
print(arr);

//第三輪
for(int index=3;index<arr.length;index++)
{
if(arr[2]>arr[index])
{
 int temp = arr[2];
 arr[2] = arr[index];
 arr[index] = temp;
}
}
print(arr);

//第四輪
for(int index=4;index<arr.length;index++)
{
if(arr[3]>arr[index])
{
 int temp = arr[3];
 arr[3] = arr[index];
 arr[index] = temp;
}
}
print(arr);

//第五輪
for(int index=5;index<arr.length;index++)
{
if(arr[4]>arr[index])
{
 int temp = arr[4];
 arr[3] = arr[index];
 arr[index] = temp;
}
}
print(arr);

//第六輪沒有,我們arr.length=6舉例
//int index = 6;index<arr.length; false

public static void selectionSort(int[] arr)
{
for(int count=1;count<arr.length;count++)
{
for(int index=count;index<arr.length;index++)
{
 if(arr[count-1]>arr[index])
 {
   int temp = arr[count-1];
   arr[count-1] = arr[index];
   arr[index] = temp;
 }
}
}

26.說說substring()?
答:substring(int beginIndex, int endIndex),返回字串從beginIndex到endIndex-1的內容。

27.static關鍵字的用途?
答:“ static方法就是沒有this的方法。在static方法內部不能呼叫非靜態方法,反過來是可以的。而且可以在沒有建立任何物件的前提下,僅僅通過類本身來呼叫static方法。這實際上正是static方法的主要用途。” ---《Java程式設計思想》

static程式碼塊,只會在類載入的時候執行一次。static變數不需要建立物件就可以引用。

靜態成員變數可以通過物件訪問,只要訪問許可權足夠就可以。

靜態程式碼塊,隨著類的載入而執行,只執行一次。

class StaticDemo{
static{
System.out.println("靜態程式碼塊");
}
void show(){
System.out.println("方法");
}
}

public class Test{
public static void main(String[] args){
 new StaticDemo().show();
 new StaticDemo().show();
 }
}
//result
靜態程式碼塊
方法
方法
public class Test{
static{
System.out.println("靜態");
}
public static void main(String[] args){
System.out.println("靜態main");
}
}
//result 靜態程式碼塊優先於main函式執行
靜態
靜態main
class StaticDemo{
static{
System.out.println("parent靜態程式碼塊");
}
{
System.out.println("parent非靜態程式碼塊");
}
StaticDemo(){
System.out.println("parent構造方法");
}

public class Test extends StaticDemo{
 static{
 System.out.println("child靜態");
}
{
System.out.println("child非靜態");
}
Test(){
System.out.println("child構造方法");
}
public static void main(String[] args){
System.out.println("main");
new Test();
}
}

//result
parent靜態程式碼塊
child靜態
main
parent非靜態程式碼塊
parent構造方法
child非靜態
child構造方法

28.說說IO?

//第一種:輸入流輸出流
//第二種:位元組流字元流
//第三種:節點流處理流
//FileInputStream
class Test{
public static void main(String args[]){
 FileInputStream fis = null;
 try{
 fis = new FileInputStream("e:/read.txt");
 byte[] buffer = new byte[100];
 fis.read(buffer,0,buffer.length);
 for(int i = 0;i<buffer.length;i++){
  System.out.println(buffer[i]);
 }
}
catch(Exception e){
System.out.println(e);
 }
}
}
class Test{
public static void main(String args[]){
 FileInputStream fis = null;
 FileOutputStream fos = null;
try{
 fis = new FileInputStream("e:/read.txt");
 fos = new FileOutputStream("e:/write.txt");
 byte[] buffer = new byte[100];
 int temp = fis.read(buffer,0,buffer.length);
 fos.write(buffer,0,temp);
 }
  catch(Exception e){
   System.out.println(e);
  }
 }
}
class Test{
public static void main(String args[]){
 FileInputStream fis = null;
 FileOutputStream fos = null;
 try{
  fis = new FileInputStream("e:/read.txt");
  fos = new FileOutputStream("e:/write.txt");
 byte[] buffer = new byte[1024];
 while(true){
  int temp = fis.read(buffer,o,buffer.length);
  if(temp = -1){
   break;
  }
  fos.write(buffer,0,temp);
 }
 }catch(Exception e){
  System.out.println(e);
}finally{
 try{
 fis.close();
 fos.close();
}catch(Excepiton e){
System.out.println(e);
 }
}
}
}
//字元流
public class TextChar
public static void main(String args[]){
 FileReader fr = null;
 FileWriter fw = null;
 try{
 fr = new FileReader("e:/read.txt");
 fw = new FileWriter("e:/write.txt");
  
  char[] buffer = new char[100];
  int temp = fr.read(buffer,0,buffer.length);
  fw.write(buffer,0,temp); 
  }
  catch(Exception e){
   System.out.println(e);
  }finally{
    try{
      fr.close();
      fw.close();
      }
  catch(Excepiton e){
   System.out.println(e); 
  }
 }
}
//FileReader和BufferedReader
class Test{
public static void main(String args[]){
 FileReader fileReader = null;
 BufferedReader bufferedReader = null;
try{
 fileReader = new FileReader("e:/read.txt");
 bufferedReader = new BufferedReader(fileReader);
 String line = null;
  while(true){
  line = bufferedReader.readLine();
  if(line == null){ 
    break;
  }
  System.out.println(line);
 }
 }catch(Exception e){
 System.out.println(e); 
}
 finally{
  try{
    bufferedReader.close(); 
    fileReader.close();
   }
   catch(Exception e){
    System.out.println(e);
   }
  }
 }
}
public class Test{
public static void main(String[] args) throws Exception{
 //位元組流
FileInputStream in = new FileInputStream("c:/read.txt");
FileOutStream out = new FileOutputStream("c:/write.txt");
byte[] buffer = new byte[1024];
 int len;
 while( (len = in.read(buffer)) != -1){
 out.write(buffer,0,len);
 }
 in.close();
 out.close();
 //字元流
 BufferedReader bf = new BufferedReader(new FileReader("c:/read.txt");
 BufferedWriter bw = new BufferedWriter(new FileWriter("c:/write.txt");
String str;
while( (str=bf.readLine()) != null ){
 bw.write(str);
 bw.newLine();
}
bf.close();
bw.close();
 }
}

29.同步和非同步什麼時候用?
答:如果資料將線上程間共享,進行同步存取;如果應用程式在物件上呼叫時間長,建議使用非同步。

30.說出一些常用的類,包,介面?
答:類:BufferedReader,BufferedWriter,String,Integer,System,Class,FileReader
包:java.util,java.io,java.lang,java.sql,javax.servlet
介面:List,Map,Set,Remote,Document

總結

  • 本文講了Java面試集合(三),如果您還有更好地理解,歡迎溝通

  • 定位:分享 Android&Java知識點,有興趣可以繼續關注