java笔试题汇总—编程题

来源:互联网 发布:冰川网络发行价 编辑:程序博客网 时间:2024/06/13 15:07
编程题1.写一个算法对1,8,5,2,4,9,7进行顺序排列。
public class OrderbyArray { 
//冒泡排序 Bubble Sort 最简单的排序方法是冒泡排序方法
    public int[] orderArray(int[] array){
    for(int i=0;i<array.length;i++){ 
    for(int j=i;j<array.length;j++){
            if(array[i]>array[j]){
            array[i] = array[j]; 
             array[j] = s; 
            }      
         }     
     }     
 } 
 public static void main(String[] args) { 
int [] array = {1,8,5,2,4,9,7};  
     array = order.orderArray(array); 
     }
 }


2.写一段Jdbc连接Oracle的程序,并实现数据查询。
创建一个连接数据库的工具类  
import java.sql.*;   
public class DbUtil {   
public static Connection getConnection(){    
String driver = "";    
String name = "scot";    
Connection conn = null;  
try {   
Class.forName(driver);   
conn = DriverManager.getConnection(url,name,psw);  
} catch (ClassNotFoundException e) {  
e.printStackTrace();  
e.printStackTrace();  
}  return conn;   
}
}


import java.sql.*;public class SearchInfo {   
public void searchInfo(int id){
Connection conn = null;
PreparedStatement pstat = null;
ResultSet res = null;
String sql = "select * from users where id=?";
conn = DbUtil.getConnection();
try {  pstat = conn.prepareStatement(sql);
pstat.setInt(1, id);  res = pstat.executeQuery();
while(res.next()){   String name = res.getString("name");
}
} catch (SQLException e) {
e.printStackTrace(); 
}   
}
}




3.写一个Singleton(单例模式)出来。
public class LazySingleton {
private static LazySingleton instance = null;
// 默认的私有的构造方法,保证外界无法直接实例化 
private LazySingleton() { } 
// 静态方法,返回此类的唯一实例 
public  static LazySingleton getInstance() {
if (instance == null) {
   instance = new LazySingleton();
}  
return instance; 
}  

public void pp(){
System.out.println("lazy ok"); 
}
}


4.用循环控制语句打印输出:1+3+5+…….+99=?结果。
int sum = 0;  
for(int i=1;i<100;i+=2){
  sum = sum+i;  
}  
System.out.println("1+3+5+...+99 = "+sum);






5.在try-catch-finally块中的退出语句。请输入文字
public class Test{ 
public static void main(String[] args) {
  int a=1;  
  try  {
a=a/0;  
  }catch(Exception e)  {
      System.out.println("catch");
 return;//当return时,finally中的语句会执行。
 //System.exit(0);
 //若用上这句,finally中的语句不会执行。直接返回,退出程序。
  }  finally //当没有System.exit(0);时,无论是否发生异常它都会执行。
 {   
 System.out.println("finally");  
 } 
}
}


注:try-catch-finally块的顺序不能调换。






6.设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
 注:因为这4个线程共享J,所以线程类要写到内部类中。加线程:每次对j加一。
 减线程:每次对j减一。
 public class TestThreads{ private int j=1; 
 //加线程
 private class Inc implements Runnable {  
public void run()  {
for(int i = 0;i < 10;i++){
inc();   





//减线程 
private class Dec implements Runnable {  
public void run()  {
for(int i = 0;i < 10;i++)   {
dec();   
}  




//加1 
private synchronized void inc() {
  j++;  
  System.out.println(Thread.currentThread().getName()+"-inc:"+j); 
 } 
 
//减1 
private synchronized void dec() {
  j--;  
  System.out.println(Thread.currentThread().getName()+"-dec:"+j); 



//测试程序 
public static void main(String[] args) {
  TestThreads test = new TestThreads();  
  //创建两个线程类  
  Thread thread = null;  
  Inc inc = test.new Inc();  
  Dec dec = test.new Dec();  
  //启动4个线程  
  for(int i = 0;i < 2;i++)  {   
      thread = new Thread(inc);   
 thread.start();   
 thread = new Thread(dec);   
 thread.start();  

}
}


7.用socket通讯写出客户端和服务器端的通讯,要求客户发送数据后能够回显相同的数据。
 Server.java:源代码 
 import java.net.*;import java.io.*;
 class  Server{  public Server()  {
BufferedReader br = null;
PrintWriter pw = null;  
try   {
ServerSocket server = new ServerSocket(8888);
//建立服务器端
Socket socket = server.accept();
//监听客户端   
//得到该连接的输入流    
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));   
//得到该连接的输出流    
pw = new PrintWriter(socket.getOutputStream(),true);    
//先读后写    
String data = br.readLine();    
System.out.println(data);
//输出到控制台    
pw.println(data);
//转发给客户端   
}catch(Exception e)   {
e.printStackTrace();   
}   
finally   {
try    {    
//关闭读写流    
br.close();     
pw.close();    
}catch(Exception e)    {} 
}  
}  

public static void main(String[] args)  {   
Server server = new Server();  
}
}


Client.java:源代码


import java.net.*;
import java.io.*;
class  Client{ public Client() {
BufferedReader br = null;  
PrintWriter pw = null;  
try  {    
Socket socket = new Socket("localhost",8888);
//与服务器建立连接,服务器要先启    
//得到Socket的输入与输出流    
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));    
pw = new PrintWriter(socket.getOutputStream(),true);    
//先写后读    pw.println("Client:你好!");    
String data = null;    
while(true){     
data = br.readLine();     
if(data!=null) break;    
}    
System.out.println(data);
}catch(Exception e)  {   
e.printStackTrace();  
}finally  {  
try {    
br.close();    
pw.close();   
}catch(Exception e)   {}  


public static void main(String[] args) {  
Client c = new Client(); 
}
}
0 0
原创粉丝点击