Sun考试认证题目解析(强力推荐,巩固基础)

转载请注明出处:http://www.ming-yue.cn/java-basic/java

1,给定如下代码,求j的值。app

public class Test {
	public static void main(String[] args) throws Exception {
		int i = 0xFFFFFFF1;
		int j = ~i;
	}
}


A. 0 B. 1 C. 14 D. –15 E. An error at line 3 causes compilation to fail. F. An error at line 4 causes compilation to fail.函数

答案:Cspa

解析:本题是考察进制,原码补码,非操做等知识。首先,0xFFFFFFF1是16进制,F用二进制表示为1111,因此整个i用二进制表示为11111111111111111111111111110001,因为j是~i(即0变1,1变0),表示j是00000000000000000000000000001110,因此变成了j的值是1110,便可求得j=14.此处若再加一个要求,求i的值。则因为计算机中存储的都是补码,第一位是符号位,转换成原码的方式是:其他位取反,最后加1,因此求得i的值是100000000000000000000000000001111,1表示负号,因此i=-15..net

2,给定如下代码,选出下面为true的选项:code

Integer i = new Integer (42);
Long 1 = new Long (42);
Double d = new Double (42.0);

A. (i ==1) B. (i == d) C. (d == 1) D. (i.equals (d)) E. (d.equals (i)) F. (i.equals (42))orm

答案:F对象

解析:i,l,d因为类型不一样,所以ABC没法经过编译。DE选项返回false,能经过编译,可是二者对象类型不一样,返回false。F选项在java5以后增长了自动装箱的功能,因此F为true。题目答案给的是DE,多是版本比较旧。再也不过多琢磨了。blog

3,求出下面程序的输出结果:内存

public class test {
	private static int j = 0;

	private static boolean methodB(int k) {
 		j += k;
		return true;
	}

 	public static void methodA(int i) {
		boolean b:
		b = i < 10 | methodB (4);
		b = i < 10 || methodB (8);
	}

	public static void main (String args[] ) {
		methodA (0);
		System.out.println(j);
	}
}


A. The program prints “0” B. The program prints “4” C. The program prints “8” D. The program prints “12” E. The code does not complete.

答案:B

解析:本题考察的是与或非以及逻辑运算符的使用。首先要明确,&和|以及~是位运算符,而||和&&是逻辑运算符。位运算符所有参与计算,而逻辑运算符存在短路的状况。下面引用一段解释:因为&& 要求它的参与操做的两个操做数都是布尔值真,才得真,因此只要得出其中一个为假,那么另外一部分的表达式就不会被求值。 同理因为||要求它的参与操做的两个操做数只要其中之一为真,就得真,因此只要得出其中一个为真,那么另外一部分也不会被求值(在上面的例子中是methodB (8)不会被调用)。这就是逻辑操做符所谓的“短路求值”。

位操做没有这一特性,因此无论那边的值是如何,任何参与运算的表达式都会被执行求值,因此methodB(4)执行。

4,求出下面的输出结果:

public class test {
	public static void main (String args[]) {
		System.out.println (6 ^ 3);
 	}
}


答案:5

解析:考察^操做符。^指的是按位异或操做。即两个二进制数按位进行异或,相同的值为0,不一样的值为1。因此6的二进制为0110,3的二进制为0011,因此异或以后为0101,值为5。

5,下面程序的输出结果是:

public class Foo {
	public static void main (String [] args) {
		StringBuffer a = new StringBuffer (“A”);
		StringBuffer b = new StringBuffer (“B”);
		operate (a,b);
		System.out.println{a + “,” +b};
	}
	static void operate (StringBuffer x, StringBuffer y) {
		x.append {y};
		y = x;
	}
}


A. The code compiles and prints “A,B”. B. The code compiles and prints “A,A”. C. The code compiles and prints “B,B”. D. The code compiles and prints “AB,B”. E. The code compiles and prints “AB,AB”. F. The code does not compile because “+” cannot be overloaded for StringBuffer

答案:D

解析:本题考察StringBuffer以及函数传递参数等问题,下面谈一下个人理解,不敢保证所有正确。首先,a,b分别指向A和B的地址,其对应的内容是A和B,调用operate函数时,将a,b的引用传递给x,y,也就是说x,y分别复制了a,b的引用,如今x,y分别指向A和B。当x执行append操做时,因为StringBuffer的特性,这里的内容变成了AB,也就是说这个时候x和a所指向内存的内容都是AB,而y==x表示的是让y一样指向x指向的内容,即如今y指向的也是AB,可是b并无改变,因此b的值仍是B。还有困惑的能够参考这里:http://blog.csdn.net/xia7139/article/details/8783066

6,下面的程序输出什么?

public class Test {
	public static void main(String[] args) {
		StringBuffer aBuffer = new StringBuffer("java");
		String bString = new String("java");
		stringReplace(bString);
		bufferReplace(aBuffer);
		System.out.println(bString+aBuffer);
		
	}
	public static void stringReplace(String string){
		string = string.replace("j", "i");
	}
	public static void bufferReplace(StringBuffer buffer){
		buffer = buffer.append("C");
	}
}

答案:javajavaC

解析:本题考察String和StringBuffer的用法。定义一个String对象的时候会有个“池”的概念,也就是说String a = "abc";String b = "abc";这里表示,a和b指向的是一块地址,定义b后发现string池中已有如今的对象,因此直接用,若是这时候用equal和==比较,两者都是true。可是若是是String c = new String("abc");则a,b和c用equal是true,用==是false。由于c指向了不一样的地址,可是内容相同。

而StringBuffer不一样。StringBuffer指向的始终不变,经过append操做改变内容,不会产生新的对象,因此StringBuffer比String快。

因此上题很好解释了,bString指向的是一块地址B,而stringReplace函数只是改变了新的引用string的内容,对bString没有改变。而aBuffer经过append改变了本身指向的内容,因此其内容也会跟着变化。

7,如下哪一个输出结果是正确的?

public class Test {
	public static void main(String[] args) {
		Integer n = new Integer(0);
		add3(n);
		System.out.println(n);
		
	}
	public static void add3(Integer i){
		int a = i.intValue();
		a = a+3;
		i = new Integer(a);
	}
}

A. Compilation will fail. B. The program prints “0”. C. The program prints “3”. D. Compilation will succeed but an exception will be thrown at line 3.

答案:B

解析:Integer是对象,int是实数,因此当add3函数改变i引用指向的内容时,原始的n并无改变,其内容依然是0。

8,选出正确的重载构造函数

public class ConstOver {
	public ConstOver (int x, int y, int z) {
	}
}

A. ConstOver ( ) { }

B. Protected int ConstOver ( ) { }

C. Private ConstOver (int z, int y, byte x) { }

D. Public Object ConstOver (int x, int y, int z) { }

E. Public void ConstOver (byte x, byte y, byte z) { }

答案:AC

解析:构造函数:能够用public,private,protected修饰,但不能有返回类型,或者void修饰。重载表示函数名相同,可是参数或类型必定不一样。

9,选出正确的重载函数:

public class MethodOver {
	public void setVar (int a, int b, float c) {
	}
}

A. Private void setVar (int a, float c, int b) { }

B. Protected void setVar (int a, int b, float c) { }

C. Public int setVar (int a, float c, int b) (return a;)

D. Public int setVar (int a, int b, float c) (return a;)

E. Protected float setVar (int a, int b, float c) (return c;)

答案:AC

解析:谨记重载的含义,函数名必定相同,参数或者类型不一样。

10,选出下面覆盖getVar()的方法:

class BaseClass {
	private float x = 1.0f ;
	protected float getVar ( ) { return x;}
}
class Subclass extends BaseClass {
	private float x = 2.0f;
	//insert code here
}

A. Float getVar ( ) { return x;}

B. Public float getVar ( ) { return x;}

C. Float double getVar ( ) { return x;}

D. Public float getVar ( ) { return x;}

E. Public float getVar (float f ) { return f;}

答案:BD

解析:覆盖(overriding)指的是函数名和参数要和原方法相同,只是具体的实现细节不一样。

——————————————————2015.5.13————————————————

11,待续。

相关文章
相关标签/搜索