测试题

来源:互联网 发布:李安知乎 编辑:程序博客网 时间:2024/05/03 14:03
 1、环境变量path和classpath的作用是什么?(10分)

1. path环境变量。作用是指定命令搜索路径,在i命令行下面执行命令如javac编译java程序时,它会到path变量所指定的路径中查找看是否能找到相应的命令程序。我们需要把jdk安装目录下的bin目录增加到现有的path变量中,bin目录中包含经常要用到的可执行文件如javac/java/javadoc等待,设置好path变量后,就可以在任何目录下执行javac/java等工具了。

2. classpath环境变量。作用是指定类搜索路径,要使用已经编写好的类,前提当然是能够找到它们了,JVM就是通过classpath来寻找类的。我们需要把jdk安装目录下的lib子目录中的dt.jar和tools.jar设置到classspath中,当然,当前目录“.”也必须加入到该变量中。


2、编写程序计算1+2+3+....+100的和。(10分)

package cha2;


public class Sum {public static void main(String args[])

{

 int i=1;

 int sum=0;

 while(i<=100)

 {

 sum=sum+i;

 i++;

 }

 System.out.println("sum="+sum);

 }

}

3、已知一个int数组, 编程从数组中获取最大数。(10分)

package cha2;

public class Getmax {

 public static void main(String [] args)  

 {   

     int[] a={8,2,3,5,4 };  

     int max=a[0];  

     for (int i=1;i<=4;i++)  

     {  

          if (a[i]>max)  

          {  

             max=a[i];  

          }  

     }  

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

 }  

}

4、编写程序获取已知文件的扩展名. 注意: abc.txt的扩展名是txt, abc.java.txt的扩展名也是txt。(10分)

package cha2;

public class name {

     public static void main(String  args[] )   {  

     String s="abc.java.txt";  

     String s1="abc.txt";

       System.out.println(s.substring(9,12));  

       System.out.println(s1.substring(4,7));  

   }  

}

5、定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性. 属性(成员变量)需要私有并提供get, set方法, 可以通过构造函数进行初始化。(10分)

package cha2;


public class student  

 {  

  private String name;  

  private int age;  

  private int scores;  

    public String getName()  {    return name;  };

    public int getAge()    {   return age;   };

    public int getScores()   {   return scores;   }  ;

  public void setName(String newName)   {name= newName;   } ;  

  public void setAge(int newAge)    {age= newAge;   } ;

  public void setScores(int newScores)   { scores= newScores;  }  ;

  public student(String newName,int newAge,int newScores){

    name= newName;

    age= newAge;

    scores= newScores;}


  public static void main(String [] args)  

  {  

    student s=new student("石雪",18,90);  

    System.out.println(s.getName()+"年龄为:"+ s.getAge()+"   成绩为:"+s.getScores());  

   

}}

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

解:在第五题的基础上只改变主函数:

public static void main(String [] args)  

  {  

    student [] s=  

    {  

           new student("张三",19,65),  

           new student("李四",21,43),  

           new student("王五",21,87),  

           new student("柳儿",17,96),  

           new student("石大",18,76)  

    };  

    student temp;  

    for(int i=0;i<=4;i++)  

    {  

       for(int j=i+1;j<=4;j++)  

       {  

           if (s[i].getScores()<s[j].getScores())  

           {  

              temp=s[i];  

              s[i]=s[j];  

              s[j]=temp;  

           }   

       }  

    }  

    for(int i=0;i<=4;i++)  {

    System.out.println(s[i].getName());  }

  }  

7、编写程序拷贝一个文件. 尽量使用效率高的方式。(15分)

答:使用该程序时,在命令行中输入源文件和目的文件。例如,源文件名为first.txt,复制后的文件名为second.txt。则需要输入:

     java  CopyFile  first.txt  second.txt


import java.io.*;

class CopyFile

{  public  static  void  main( String args[ ] ) throws  IOException

  {  int i;    

 FileInputStream  fin;    

FileOutputStream  fout;

   try                    //打开输入文件                                                          

   {    try   

{   fin=new  FileInputStream( args[0] );    }

        catch( FileNotFoundException  e )

        { System.out.println("Input File Not Found");  return;  }

       try  

{   fout=new  FileOutputStream( args[1] );  }//打开输出文件

       catch( FileNotFoundException  e )

       {   System.out.println("Error Opening Output File");   return;  }

   }  

catch( ArrayIndexOutOfBoundsException  e )

      {  System.out.println("Usage:CopyFile From To");  return;}//复制文件

      try

      {  do {  i=fin.read( );  if(i != -1)  fout.write(i); }   while(i != -1);  }

     catch(IOException e)         

{ System.out.println("File Error"); }

       fin.close();  

     fout.close();

}  

}



8、UDP协议与TCP协议有什么不同?(10分)

答:运输层的两个主要协议:用户数据报协议UDP和传输控制协议TCP

UDP:(1):它是无连接的,及发送数据之前不需要建立连接,因此减少了开销和发送数据之前的时延;(2)它使用尽最大努力支付,即不保证可靠交付,因此主机不需要维持复杂的状态表;(3)它是面向报文的,发送方的UDP对应用程序交下来的报文,在添加首部后就向下交付给IP层;(4)它没有拥塞控制;(5)它支持一对一、一对多、多对一和多对多的交互通信;(6)它的首部开销小。

TCP:(1)它是面向连接的再使用前必须先建立连接;(2)每一条TCP连接只能有两个端点,即它是点对点的;(3)它提供可靠交付的服务,即无猜错、不丢失、不重复、并且按序到达;(4)它提供全双工的通信;(5)它是面向字节流的。

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

<html>

   <head>

       <title>表单举例</title>

   </head>


   <body>

       <from name=from1


action=http://localhost/show.htm method=post>

<table width=400 border=2 align=center>

<tr>

<td width=80>用户名:</td>

<td width=320><input type=text name=linetext


value=" " size=20 maxlength=30></td>

</tr>

<tr>

<td width=80>密码:</td>

<td width=320><input type=password name=password


value=" " size=9 maxlength=9></td>

</tr>

<tr>

<td width=80>确认密码:</td>

<td width=320><input type=password name=password


value=" " size=8 maxlength=8></td>

</tr> 

<tr>

       <td width=80  height=40>性别:<td>

       <td >男<input type=radio name=sinput


value=男>

       女<input type=radio name=sinput value=女


></td>

      

       <tr>

       <td>城市:</td>

       <td><select name=selinput2>

          <option value=1>北京</option>

          <option value=2>天津</option>

          <option value=3>厦门</option>

       </select></td>

<tr>

       <td>兴趣爱好:</td>

       <td>看书<input type=checkbox name=minput


value=看书>

       讨论<input type=checkbox name=minput


value=讨论>

       上网<input type=checkbox name=minput


value=上网></td>


       <tr>

       <td>个人简历:</td>

       <td><textarea name=mtinput cols=20


rows=5></textarea></td>

       ]

       </from>

   </body>


</html>

原创粉丝点击