2016年關(guān)于Java編程與程序運(yùn)行結(jié)果筆試題
1.Java編程,打印昨天的當(dāng)前時(shí)刻
public class YesterdayCurrent{
public void main(String[] args){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(cal.getTime());
}
}
2.文件讀寫,實(shí)現(xiàn)一個(gè)計(jì)數(shù)器
public int getNum(){
int i = -1;
try{
String stri="";
BufferedReader in = new BufferedReader(new FileReader(f));
while((stri=in.readLine())!=null){
i = Integer.parseInt(stri.trim());
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
return i;
}
public void setNum(){
int i = getNum();
i++;
try{
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(f,false)));
out.write(String.valueOf(i)); //可能是編碼的原因,如果直接寫入int的話,將出現(xiàn)java編碼和windows編碼的.混亂,因此此處寫入的是String
out.close() ;
}catch(Exception e){
e.printStackTrace();
}
}
3. 指出下面程序的運(yùn)行結(jié)果:
class A{
static{
System.out.print("1");
}
public A(){
System.out.print("2");
}
}
class B extends A{
static{
System.out.print("a");
}
public B(){
System.out.print("b");
}
}
public class Hello{
public static void main(String[] ars){
A ab = new B(); //執(zhí)行到此處,結(jié)果: 1a2b
ab = new B(); //執(zhí)行到此處,結(jié)果: 1a2bab
}
}
注:類的static 代碼段,可以看作是類首次加載(被虛擬機(jī)加載)執(zhí)行的代碼,而對(duì)于類的加載,首先要執(zhí)行其基類的構(gòu)造,再執(zhí)行其本身的構(gòu)造
4.寫一個(gè)Singleton模式的例子
public class Singleton{
private static Singleton single = new Singleton();
private Singleton(){}
public Singleton getInstance(){
return single;
}
}
【2016年關(guān)于Java編程與程序運(yùn)行結(jié)果筆試題】相關(guān)文章:
Java程序開發(fā)與運(yùn)行環(huán)境06-02
JAVA編程面試題及答案09-27
關(guān)于JAVA的XML編程10-08
關(guān)于Java程序員面試題集05-19
Java基本編程技巧11-16
Java編程開發(fā)簡介06-12
java程序員面試試題06-02