题目来牛客网
1.类中的数据域使用private修饰为私有变量,所以任何方法均不能访问它。
A.正确
B.错误
答案:B,本类可以使用,反射也可以。
2.在类Tester中定义方法如下
public double max(int x, int y){//省略 }
则在该类中定义如下哪个方法头是对上述方法的重载(Overload)?
A public int max(int a, int b) {}
B.public int max(double a, double b) {}
C.public double max(int x, int y) {}
D.private double max(int a, int b) {}
解析:
方法的重载是指在同一个类中,方法名相同但是参数列表不同的情况。在这个问题中,已经定义了一个方法
public double max(int x, int y),现在需要找到其中哪个方法头是对该方法的重载。B
3.4.下面程序的输出是什么?
public class TestDemo
{
public static String output = "";
public static void foo(inti)
{
try
{
if (i == 1)
{
throw new Exception();
}
}
catch (Exception e)
{
output += "2";
return ;
} finally
{
output += "3";
}
output += "4";
}
public static void main(String[] args)
{
foo(0);
foo(1);
System.out.println(output);
}
}
A.342
B.3423
C.34234
D.323
解析:
- 首先,在
main方法中调用foo(0)。- 在
foo(0)中,参数不是 1,所以不会抛出异常。然后执行finally块,output被更新为 "3",然后执行output += "4",output变为 "34"。- 接着,在
main方法中调用foo(1)。- 在
foo(1)中,参数是 1,所以会抛出异常。然后进入catch块,output被更新为 "3423",然后执行return语句,结束该方法的执行。- 由于
foo(1)方法已经结束,程序继续执行,打印output的值,即 "3423"。B
4.
给出以下代码,请给出结果.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
A. null null 42
B.null 42 42
C.0042
D.042 42
E.An exception is thrown at runtime
F.Compilation
解析:
- 在
main方法中创建了一个PassO类的对象p。- 调用
start方法。- 在
start方法中,创建了一个Two类的对象t,其成员变量x的默认值为null,所以打印出来的是null。- 调用
fix方法,将t作为参数传递给fix方法。- 在
fix方法中,将tt的成员变量x的值设置为42。- 返回
tt。- 回到
start方法中,打印t.x和t2.x的值。因为t2和t引用的是同一个对象,所以它们的x值都是被修改过的,都为42。因此,程序输出为
null 42 42,选项 B 是正确的。