题目1:定义了如下类和测试方法,请问测试时要捕获下面哪个选项的异常______
class MyException extends Exception{
MyException(){}
}class A{
public int format(String str)throws MyException{
int i = Integer.valueOf(str);
return i;
}
}public void testTester(){
new A().format("1");
}
A Exception B MyException
C MyException 和 NumberFormatException
D RuntimeException
题目2:下列代码的输出结果是多少
public class Test {public static void main(String[] args)
{
Map <String,String> map = new HashMap <String,String>();
map.put(String.valueOf(System.currentTimeMillis())+"a","1");
map.put(String.valueOf(System.currentTimeMillis())+"a","2");
map.put(String.valueOf(System.currentTimeMillis())+"a","3");
for (Map.Entry <String, String> entry: map.entrySet()){
System.out.printf(entry.getValue());
}
}
}
A 123 B 213 C 321 D 123顺序无法确定题目3:下列代码的输出结果是多少,为什么?
public class Test {
static{int x =5;}
static int x,y;
public static void main(String[] args)
{
x--;
myMethod();
System.out.println(x+y+++x);
}
public static void myMethod(){
y = x++ + ++x;
}}题目4:下列代码的输出结果是多少,为什么?
class Value {
public int i = 15;
}public class Test {
public static void main(String[] args)
{
Test t = new Test();
t.first();
}
public void first(){
int i=5;
Value v = new Value();
v.i=25;
second(v,i);
System.out.println(v.i);
}
public void second(Value v, int i){
i=0;
v.i=20;
Value val = new Value();
v=val;
System.out.println(v.i+" "+i);
}
}
题目5:下列代码的输出结果是多少,为什么?
class X {
Y b = new Y();
X(){
System.out.print("X");
}
}class Y {
Y(){
System.out.print("Y");
}
}
public class Z extends X{
Y y = new Y();
Z(){
System.out.print("Z");
}
public static void main(String[] args)
{
new Z();
}
}
题目6:下列代码的输出结果是多少?
class classA
{
public void printValue()
{
System.out.print("classA ");
}
}
class classB extends classA
{
public void printValue()
{
System.out.print("classB ");
}
}
public class Test {public static void main(String[] args) throws IOException
{
classB objectB = new classB();
objectB.printValue();
classA as = (classA)objectB;
as.printValue();
as = new classA();
as.printValue();
}
}
A classB classB classA
B classB classB classB
C classB classA classA
D classB classA classB
class MyException extends Exception{
MyException(){}
}class A{
public int format(String str)throws MyException{
int i = Integer.valueOf(str);
return i;
}
}public void testTester(){
new A().format("1");
}
A Exception B MyException
C MyException 和 NumberFormatException
D RuntimeException
题目2:下列代码的输出结果是多少
public class Test {public static void main(String[] args)
{
Map <String,String> map = new HashMap <String,String>();
map.put(String.valueOf(System.currentTimeMillis())+"a","1");
map.put(String.valueOf(System.currentTimeMillis())+"a","2");
map.put(String.valueOf(System.currentTimeMillis())+"a","3");
for (Map.Entry <String, String> entry: map.entrySet()){
System.out.printf(entry.getValue());
}
}
}
A 123 B 213 C 321 D 123顺序无法确定题目3:下列代码的输出结果是多少,为什么?
public class Test {
static{int x =5;}
static int x,y;
public static void main(String[] args)
{
x--;
myMethod();
System.out.println(x+y+++x);
}
public static void myMethod(){
y = x++ + ++x;
}}题目4:下列代码的输出结果是多少,为什么?
class Value {
public int i = 15;
}public class Test {
public static void main(String[] args)
{
Test t = new Test();
t.first();
}
public void first(){
int i=5;
Value v = new Value();
v.i=25;
second(v,i);
System.out.println(v.i);
}
public void second(Value v, int i){
i=0;
v.i=20;
Value val = new Value();
v=val;
System.out.println(v.i+" "+i);
}
}
题目5:下列代码的输出结果是多少,为什么?
class X {
Y b = new Y();
X(){
System.out.print("X");
}
}class Y {
Y(){
System.out.print("Y");
}
}
public class Z extends X{
Y y = new Y();
Z(){
System.out.print("Z");
}
public static void main(String[] args)
{
new Z();
}
}
题目6:下列代码的输出结果是多少?
class classA
{
public void printValue()
{
System.out.print("classA ");
}
}
class classB extends classA
{
public void printValue()
{
System.out.print("classB ");
}
}
public class Test {public static void main(String[] args) throws IOException
{
classB objectB = new classB();
objectB.printValue();
classA as = (classA)objectB;
as.printValue();
as = new classA();
as.printValue();
}
}
A classB classB classA
B classB classB classB
C classB classA classA
D classB classA classB
c
d
第三题运行myMethod()这个之前,x=-1,
第四题注意:新new的对象Value val = new Value(); i值等于15第五题:首先初始化基类变量,然后调用基类构造器,然后是子类的。
第六题:
classA as = (classA)objectB;
这个虽然进行了类型转换,但是实际类型还是classB,
HashMap的API里有写"特别是它不保证该顺序恒久不变"
如果要保证顺序,应该用LinkedHashMap
c,d,2,15,xyz,A
class A{
public int format(String str)throws MyException{ //这里抛出的是MyException
int i = Integer.valueOf(str); //当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出NumberFormatException
return i;
第二题不知道
第三题
public class Test {
static{int x =5;}
static int x,y;
public static void main(String[] args)
{
x--;
//myMethod();
System.out.println(x/*+y+++x*/);
}
//public static void myMethod(){
//y = x++ + ++x;
//}
}
结果输出是X=-1,在运行myMethod(); 之前x是-1,进入myMethod()运行y = (x++) + (++x)后y=0;此时x=1然后运行x+(y++)+x=1+0+1=2。
第四题
public void second(Value v, int i){
i=0;
v.i=20;
Value val = new Value();
v=val;
System.out.println(v.i+" "+i); //v是Value中的,v.i为15,i在此方法内定义i=0;所以先输出15 0
}
} public void first(){
int i=5;
Value v = new Value();
v.i=25;
second(v,i);
System.out.println(v.i); //v.i本来为25但是调用了second方法,此时v.i为20,所以输出20
} 最后结果输出 15 0 20
class X {
Y b = new Y(); //从main函数开始Z继承X,构造Z的话先构造X,进入X类运行Y b = new Y(); 输出"Y"
X(){
System.out.print("X"); //接着输出"X"
}
} class Y {
Y(){
System.out.print("Y");
}
}
public class Z extends X{
Y y = new Y(); //然后再输出"Y"
Z(){
System.out.print("Z"); //最后输出"Z"
}
public static void main(String[] args)
{
new Z();
}
}
所以此题答案为"YXYZ"
class classA
{
public void printValue()
{
System.out.print("classA ");
}
}
class classB extends classA
{
public void printValue()
{
System.out.print("classB ");
}
}
public class Test { public static void main(String[] args) throws IOException
{
classB objectB = new classB(); //隐式转换,new出来的是classB,输出是"classB "
objectB.printValue();
classA as = (classA)objectB; //objectB本来定义为classB可以向其父类转换,输出是"classB "
as.printValue();
as = new classA(); //new出来的是classA(),所以输出是"classA "
as.printValue();
}
}
A classB classB classA
B classB classB classB
C classB classA classA
D classB classA classB
答案应该是A
请问如果这样的话
public void second(Value v, int i) {
i = 0;
v.i = 20;
Value val = new Value();
v = val;
v.i = 15; ////再次赋值15
System.out.println(v.i + " " + i);
}
再次赋值15 输出结果就该是 15 0 15 了
可是我验证了一下 咋还是15 0 20阿
望赐教~