`

基础测试题

阅读更多
1、环境变量path和classpath的作用是什么?
答:path环境变量。作用是指定命令搜索路径,在i命令行下面执行命令如javac编译程序时,它会到path变量所指定的路径中查找看是否能找到相应的命令程序。我们需要把jdk安装目录下的bin目录增加到现有的path变量中,bin目录中包含经常要用到的可执行文件如javac/java/javadoc等待,设置好path变量后,就可以在任何目录下执行javac/java等工具了
classpath环境变量。作用是指定类搜索路径,要使用已经编写好的类,前提当然是能够找到它们了,JVM就是通过classpath来寻找类的。我们需要把jdk安装目录下的lib子目录中的dt.jar和tools.jar设置到classpath中,当然,当前目录“.”也必须加入到该变量中。

2、编写程序计算1+2+3+....+100的和。
答:
public class Testsum {

 public static void main(String[] args) {

   int sum =0;

   for(int i=1;i <101;i++){

      sum+=i; 

   }

   System.out.println("1+2+3+....+100="+sum);

 }

}


3、已知一个int数组, 编程从数组中获取最大数。
答:
public class Testmax {

 public static void main(String[] args) {

  int[] intarray = {1,-300,10,205,6,40};

  int max=intarray[0];

  for(int i=0;i< intarray.length;i++){

   max =(max > intarray[i])? max : intarray[i];

  }

  System.out.println("数组中最大数为:" + max);

 }

}


4、编写程序获取已知文件的扩展名. 注意: abc.txt的扩展名是txt, abc.java.txt的扩展名也是txt。
答:
import java.io.File;

import java.io.IOException;

public class TestFileName {

  public static void main(String args[]){ 

   File f1 = new File("abc.txt");

   File f2 = new File("abc.java.txt");

   if(!f1.exists()){

    try {

  f1.createNewFile();

  } catch (IOException e) {

  e.printStackTrace();

  }

   }

   if(!f2.exists()){

     try {

   f2.createNewFile();

   } catch (IOException e) {

   e.printStackTrace();

   }

   }

   String fname1 = f1.getName();

   String fname2 = f2.getName();

   System.out.println(fname1+"扩展名是:"+fname1.substring(fname1.lastIndexOf(".")+1,fname1.length()));

   System.out.println(fname2+"扩展名是:"+fname2.substring(fname2.lastIndexOf(".")+1,fname2.length()));

 }

}


5、定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性. 属性需要私有并提供get, set方法, 可以通过构造函数进行初始化。
答:
public class Student implements java.io.Serializable{

  private static final long serialVersionUID = 1L;

  private String name;

  private int age;

  private int grade;

  public Student(String name,int age,int grade){

   this.name = name;

   this.age = age;

   this.grade = grade;

  }

  public String getName() {

   return name;

  }

  public void setName(String name) {

   this.name = name;

  }

  public int getAge() {

   return age;

  }

  public void setAge(int age) {

   this.age = age;

  }

  public int getGrade() {

  return grade;

  }

 public void setGrade(int grade) {

   this.grade = grade;

 } 

}


6、使用第5题定义的学生类创建5个对象, 属性可为任意值. 编程对这5个对象按成绩排序, 并将结果输出。
答:
public class GradeCompositor {

 public static void main(String[] args) {

  Student[] s = new Student[5];//定义Student数组对象

  s[0] = new Student("张三", 20, 89);

  s[1] = new Student("李四", 16, 99);

  s[2] = new Student("王五", 23, 83);

  s[3] = new Student("陈小东", 19, 88);

  s[4] = new Student("周小明", 20, 88);

  for(int i=0;i<s.length-1;i++){

   for(int j=i+1;j<s.length;j++){ 

    if(s[i].getGrade() < s[j].getGrade()){

     int grade=s[i].getGrade();    //交换位置

     s[i].setGrade(s[j].getGrade());

     s[j].setGrade(grade);

    }

   }

  }

  int i=1;

  for(Student st :  s){   

   System.out.println("姓名:"+st.getName()+" 年龄:"+ st.getAge() +" 成绩:"+st.getGrade()+" 序号:"+ i++);

  }

 }

}


7、编写程序拷贝一个文件. 尽量使用效率高的方式。
答:
import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class CopeFile {

 public static void main(String[] args) {

  FileInputStream fin = null;

  FileOutputStream fout = null;

  try {

    fin =new FileInputStream("F:\\2011年兔年春节素材大全.tif");

    fout = new FileOutputStream("D:\\2011年兔年春节素材大全.tif");

   byte[] buffer=new byte[1024*2];

   int temp=fin.read(buffer);

      while (temp!=-1)

       {  

       fout.write(buffer,0,temp);

       temp=fin.read(buffer);

       }

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }finally{

   if(fout != null){

    try {

     fout.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

    fout = null;

   }

   if(fin != null){

    try {

     fin.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

    fin = null;

   }

  }

 }

}


8、UDP协议与TCP协议有什么不同?
答:TCP协议提供了一种可靠的数据传输服务,它是一种面向连接的数据传输协议。在数据传输之前,通信节点之间必须建立起连接。为确保正确地接收数据,TCP协议要求在目标计算机成功收到数据时发回一个确认(即ACK)。如果在某个时限内未收到相应的ACK,将重新传送数据包。如果网络拥塞,这种重新传送将导致发送的数据包重复。但是,接收电脑可使用数据包的序号来确定它是否为重复数据包,并在必要时丢弃它。UDP协议是一种面向无连接的数据传输服务,它不能保证数据包以正确的顺序被接收。该协议不能保证数据准确无误地到达目的地。UDP在许多方面非常有效。当某个程序的目标是尽快地传输尽可能多的信息时(其中任意给定数据的重要性相对较低),可使用UDP协议。

9、编写HTML注册表单, 需要字段: 用户名, 密码, 确认密码, 性别(单选), 城市(下拉列表), 兴趣爱好(多选), 个人简介(文本域)。

答:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>HTML注册表单</title>

</head>

<body>

<form action="" method="post" onsubmit="return checkform(this);">

用户名:<input type="text" name="username" value="" /><br>

密码  <input type="password" name="password" value="" /><br>

确认密码:<input type="password" name="confirm_password" value="" /><br>

性别:<input type="radio" name="sex" value="0"  />男<input type="radio" name="sex" value="1" />女<br>

城市: <select name="city">

     <option value="0">北京</option>

     <option value="1">上海</option>

     <option value="2">广州</option>

     <option value="3">深圳</option>

   </select><br>

兴趣爱好:<input type="checkbox" name="read" value="0" />看书 

      <input type="checkbox" name="music" value="1" />听音乐

      <input type="checkbox" name="TV" value="2" />看电视

      <input type="checkbox" name="net" value="3" />上网

      <input type="checkbox" name="draw" value="4" />画画

      <input type="checkbox" name="pingpong" value="5" />乒乓球<br>

个人简介: <textarea rows="5" cols="40" name="resume"></textarea> <br>      

   <input type="submit" value="确定"/>       

</form>

</body>

</html> 


10、使用JavaScript验证第9题的HTML表单. 用户名: 必须是字母数字或下划线, 不能以数字开头. 密码: 6-16位字母数字下划线. 确认密码: 和密码一致. 其他项为必填。
答:
<script type="text/javascript">

function checkform(form){

 if(!(/^[a-zA-Z_]{1}[a-zA-Z_0-9]*$/.test(form.username.value))){

  alert("用户名必须是字母数字或下划线, 不能以数字开头!");

  return false;

 }else if(!(/[a-zA-Z_0-9]{6,16}/.test(form.password.value))){

  alert("密码 6-16位字母数字下划线!");

  return false;

 }else if(form.confirm_password.value != form.password.value){

  alert("确认密码和密码一致!");

  return false;

 }else if(form.sex[0].checked==false && form.sex[1].checked==false){

  alert("性别不能为空!");

  return false;

 }else if(form.city.value =="" || form.city.value == null){

  alert("城市不能为空!");

  return false;

 }

 else if(form.read.checked == false && form.music.checked == false && form.TV.checked == false && form.net.checked == false && form.draw.checked == false && form.pingpong.checked == false){

  alert("兴趣爱好不能为空!");

  return false;

 }

 else if(form.resume.value =="" || form.resume.value == null){

  alert("个人简介不能为空!");

  return false;

 }

 else{

  return true;

 }

} 

</script>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics