Java Test One

本文包含Java Web编程的基础知识点,如是非题和填空题考察Java基础知识,程序题涉及Java语法和特性,还包括JSP和Servlet的使用等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、 是非题5题(每题3分)
1、 对象的特征是对象有状态、行为、标识ID ( )

2、 GenericServlet是一个与协议相关的Servlet类。( )
3、 Java中的this指的是当前类。 ( )
4、Java数据库连接用的是JDBC。 ( )
5、接口是一个纯的抽象类,可以包含私有方法。 ( )
二、 填空题(每空2分)
6、从对象到类是一个( )的过程。

7、J2EE Web层组件指( )、( )和可选的( )
8、在使用Statement类的对象向数据库发送SQL语句时,如果发送的时select语句应该调用( )方法,如果发送的是insert/update/delete语句一个调用()方法。
9、在J2EE Web应用中使用MVC设计模式, JSP充当( ),Servlet充当( ),可选的JavaBean充当( )。
10、使用JDBC访问数据库中的存储过程,应该使用( )
11、ServletSocket所做的工作是:( )
三、 程序题(每题2分,有单选和多选)
1.下面的程序中哪些可以输出Hello World
a. public class Hello{
static{
System.out.println(“Hello World”);
}
}
b.pulic class Hello{
public static void main(String[] arg){
System.out.println(“Hello World”);
}
}
c.pulic class Hello{
public void static main(String[] arg){
System.out.println(“Hello World”);
}
}
d.pulic class Hello{
static public void main(String[] arg){
System.out.println(“Hello World”);
}
}
答案: 

2. public class Test{
public static void main (String args[]){
int x=1,sum=0;
while(x<=10){
sum+=x;
x++;
}
System.out.println(“sum=”+sum);
}
}
输出是:

3、给出下列声明:
String s1=new String(“Hello”);
String s2=new String(“there”);
String s3=new String();
Which of the following are legal operations?
A、s3=s1+s2;
B、s3=s1-s2;
C、s3=s1&s2;
D、s3=s1&&s2;


4.A byte can be of what size
1)-128 to 127
2)(-2 power 8)-1 to 2 power 8
3)-255 to 256
4)depends on the particular implementation of the java virtual machine


5.
哪些是Java关键字?
1)if
2)THEN
3)const
4)try

6.哪些是合法的变量名?
1)2variable
2)variable2
3)_whatavariable
4)_3_
5)$another


7.
编译和执行下例代码会出现什么情况?
public class MyClass{
   static int i;
      public static void main(String argv[]){
        System.out.println(i);
      }
}

1) Error varable i may not have been initialized
2) null
3) 1
4) 0


8. 
编译和执行下例代码会出现什么情况?
public class Q{
    public static void main(String argv[]){
      int anar[]=new int[]{1,2,3};
      System.out.println(anar[1]);
   }
}

1)1
2)Error: anar is referenced before it is initialized
3)2
4)Error : size of array must be defined


9. 
编译和执行下例代码会输出什么?
int i=1;
switch (i) {
case 0:
System.out.println(“zero”);
break;
case 1:
System.out.println(“one”);
case 2:
System.out.println(“two”);
default:
System.out.println(“default”);
1) one
2) one,default
3) one,two,default
4) default


10. class Student{
    private String name;
    private byte age;
      public Student(String name, byte age){
         this.age = age;
         this.name = name;
      }
//
完成代码
}
public class Test{
     public static void main(String[] arg){
     Student stu1 = new Student(“Alice”, 23); //此句有错,请将正确的语句写在下面
     Student stu1 = 
     System.out.println(stu1);
     }
}
要求:完成Student类的代码,在控制台输出:Student Alice is 23 years old.

11根据JavaBean规范,完成下面的JavaBean?
public class Student{
      private String name;
      private byte age;
      private boolean married;


}

12、哪二种声明防止方法覆盖?
A、final void methoda() {}
B、void final methoda() {}
C、static void methoda() {}
D、static final void methoda() {}
E、final abstract void methoda() {}


13
、修改show方法使得该JSP可以正确运行

14、下面哪些关于JSP的陈述是正确的:
1) 获得客户端提交的数据使用request对象
2) 向客户端输出可以使用表达式,也可以使用out对象
3) JSP指令用于设置容器的状态,同时产生输出
4) 在使用include指令处理页面模块化时,当被包含的页面发生变化时,会自动表现出来。


15
、在web应用中处理会话有以下四种方式
1)( )
2)( )
3)( ) 
4)( )


16.
编译下例代码哪些没有错误?
1)
import java.awt.*;
package Mypackage;
class Myclass{}

2)
package Mypackage;
import java.awt.*;
class Myclass{}

3)
/* This is a comment */
package Mypackage;
import java.awt.*;
class Mycalss{}


17. 
编译和执行下例代码会输出什么?
public class Q 
public static void main(String argv[]){
int anar[]=new int[5];
System.out.println(anar[0]);
}
}

1) Eror:anar is referenced before it is initialized
2) null
3) 0
4) 5


18. 
编译和执行下例代码会输出什么?
abstract class MineBase {
  abstract void amethod();
  static int i;
}
public class Mine extends MineBase {
    public static void main(String argv[]){
      int[] ar=new int[5]
      for(i=0;i<AR.LENGTH;I++)
        system.out.println(ar[i]);
      }
}
1) a sequence of 5 0’s will be printed
2) Error:ar is used before it is initialized
3) Error Mine must be declared abstract
4) IndexOutOfBoundes Error

19、编译和执行下例代码会输出什么?
public class Borley extends Thread{
    public static void main(String argv[]){
    Borley b = new Borley();
    b.start();
   }
   public void run(){ 
      System.out.println("Running");
   }
}

1) Compilation and run but no output 
2) Compilation and run with the output "Running" 
3) Compile time error with complaint of no Thread target 
4) Compile time error with complaint of no access to Thread package


20
、哪些能停止一个线程运行?
1) The program exits via a call to System.exit(0);
2) Another thread is given a higher priority
3) A call to the thread’s stop method
4)A call to the halt method of the Thread class


21
、在Servlet生命周期中的哪些方法只被执行一次
1) init
2) service
3) destroy
4) doGet/doPost

四、简答题
22、说明一个Web应用的部署结构,各种组件如何部署?(5)

23、说说JSP、servlet的生命周期以及二者之间的对应关系(8)


24
、如何使用JDBC访问数据库,编程的步骤是什么、给出相应的案例代码?(8)

### 自动生成 Java 测试类 为了简化开发流程并提高效率,在Java中存在多种方法和工具可以用于自动生成测试类。这些工具不仅能够节省时间,还能确保遵循最佳实践编写单元测试。 #### 使用 IntelliJ IDEA 自动生成功能 IntelliJ IDEA 提供了一个便捷的功能来快速生成测试类。只需右键单击目标源文件夹内的类名,选择 `Go To` -> `Test` 或者通过快捷方式直接跳转到对应的测试类位置。如果不存在相应的测试类,则IDE会提供选项允许用户立即创建它[^2]。 #### 利用 JUnit 和 TestNG 的注解支持 JUnit 和 TestNG 是两个流行的框架,它们都提供了强大的注解机制以便于定义测试案例。例如: ```java package cn.juwatech.test; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; public class CalculatorTest { @Test public void testAddition() { Calculator calculator = new Calculator(); int result = calculator.add(2, 3); assertEquals(result, 5); // 断言预期结果等于实际计算的结果 } } ``` 这段代码展示了如何利用 TestNG 注解标记一个简单的加法运算测试函数。 #### 借助插件如 All-in-One Test Generation Plugin 对于更复杂的场景,开发者可以选择安装专门针对 Eclipse 或其他 IDE 设计的插件来进行批量化的测试类生成工作。这类插件通常具备智能析能力,可以根据现有业务逻辑推测可能存在的边界条件以及异常情况,并据此构建全面覆盖的应用程序接口(API)级别或集成层面的自动化测试套件[^4]。 #### 应用 Mockito 进行模拟对象处理 Mockito 是一种广泛使用的 mocking 框架,可以帮助隔离被测组件与其他依赖项之间的交互影响。这使得即使是在缺乏真实环境的情况下也能有效地验证特定功能的行为是否符合预期。下面是一个简单例子说明怎样运用 Mockito 来伪造外部服务调用返回固定数据给待检验的方法使用: ```java // 导入必要的库 import static org.mockito.Mockito.*; ... // 创建 mock 对象实例 CalculatorService service = mock(CalculatorService.class); // 配置期望行为 when(service.calculate(anyInt(), anyInt())).thenReturn(7); // 执行操作后验证其正确性 assertEquals(calculator.performCalculationUsingService(service), 7); verify(service).calculate(eq(2), eq(3)); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值