题目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
说明:I++就是先使用I,然后再加1,而++I就是先加1,然后再使用I
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。因为System.currentTimeMillis()是以毫秒为单位的,这很可能造成三次创建的System.currentTimeMillis())+"a" ,这个String对象是内容相同的,也就是说equals()判断结果为true,导致最后只有一个元素“3”。
修改,这样测试了一下:import java.util.HashMap;
import java.util.Map;/**
*
* @author ZangXT
*/
public class Test { public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put(String.valueOf(System.nanoTime()) + "a", "1");
map.put(String.valueOf(System.nanoTime()) + "a", "2");
map.put(String.valueOf(System.nanoTime()) + "a", "3");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.printf(entry.getValue());
}
}
}使用毫微秒为单位获取时间,结果是123的排列,每次运行结果不同。
因为作为Key的String不同,每一项方到HashMap里的位置是不确定的,具体可以去分析String的hashCode()方法与HashMap的put方法。
2:D
3:4 (不确定)
4:150 20
5:Y X Y Z
6:A
方法在编译期需要捕获的NumberFormatException 异常
@后面的字符串(hash码),由于每次对象所在存储空间都不相同,所以这题目选D,无法确定~
写好看点package Test;
public class qdbcool
{
static{int x =5;}//在第一次被载入JVM时运行,但由于是局部变量,所以豪无用处
static int x,y;//初始化 x=0;y=0;
public static void myMethod()
{
y = x++ + ++x;
System.out.println(y);//调用之后的y值
System.out.println(x);//调用之后的x值
}
public static void main(String[] args)
{
x--;
System.out.println(x);
myMethod();
System.out.println(x+ y++ +x);//这样可以看得很清晰了(就是这给看错了,哭!)
}
}
题目5:类的实例化顺序(父静--子静;父变量--父构造--子变量-子构造)。
题目6:多态问题
System.out.println(y);//调用之后的y值
System.out.println(x);//调用之后的x值我想问下执行这句话后x=1,y=0,
执行x--这句话之后x还是0,然后然后执行x++之后x还是0,然后执行++x之后x才变为1?
那么x--和x++什么时候开始的减操作和加操作什么时候真正执行呢?
y = x++ + ++x;
x++后x=0,x++值为-1;
++x后x=1,++x值为1;
所以y=-1+1=0,x=1;
4:15 0
20
5:YXYZ
6:A
对于第一题: 我还有疑惑
我尝试编程:
class MyException extends Exception {
MyException() {
}
}
class My1Exception extends Exception {
My1Exception() {
}
}class A {
public int format(String str) {
int i = Integer.valueOf(str);
// throw new MyException();
return i;
} public static void testTester() throws MyException, My1Exception {
new A().format("S");
}
}public class Test{
public static void main(String[] args) throws MyException, My1Exception {
A.testTester();
}}
但是跑出来的异常是
Exception in thread "main" java.lang.NumberFormatException: For input string: "S"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.valueOf(Integer.java:553)
at A.format(Test.java:21)
at A.testTester(Test.java:27)
at Test.main(Test.java:35)为什么MyException 就没有跑出来 我十分不解 请高人相助
因为方法声明抛出MyException,这个异常必须在代码中显式的使用try catch语句来捕获或者再次throw.
而NumberFormatException 是个RuntimeException,是不要求你在代码里处理的.
这个题的意思是考察你对这两类异常的理解.
我原来也认为选D, 但是测试一下居然是3.
TreeSet应该是有序的。
这样的测试结果是D。
HashSet HashMap这种是无序的, TreeSet TreeMap好像是有序的。 记的不是很清楚了。