招聘试题3
1.选择题(4′×10):
例题1.(d)
class Parent{
public void method1(){ //change here
System.out.println("Parent's method1()");
}
public void method2()
{
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent{
public void method1(){
System.out.println("Child's method1()");
}
public static void main(String args[]){
Child p = new Child();//change here
p.method2();
}
}
Choices:
a. Compile time error
b. Run time error
c. prints : Parent's method2()
Parent's method1()
d. prints : Parent's method2()
Child's method1()
例题2:
How can you force garbage collection of an object?
A. Garbage collection cannot be forced
B. Call System.gc().
C. Call System.gc(), passing in a reference to the object to be garbage collected.
D. Call Runtime.gc().
E. Set all references to the object to new values(null, for example).
例题3:
Consider the following class:
1. class Test(int i) {
2. void test(int i) {
3. System.out.println(“I am an int.”);
4. }
5. void test(String s) {
6. System.out.println(“I am a string.”);
7. }
8.
9. public static void main(String args[]) {
10. Test t=new Test();
11. char ch='y';
12. t.test(ch);
13. }
14. }
Which of the statements below is true?(Choose one.)
A. Line 5 will not compile, because void methods cannot be overridden.
B. Line 12 will not compile, because there is no version of test() that rakes a char argument.
C. The code will compile but will throw an exception at line 12.
D. The code will compile and produce the following output: I am an int.
E. The code will compile and produce the following output: I am a String.
例题4:
Which of the following lines of code will compile without error?
A.
int i=0;
if (i) {
System.out.println(“Hi”);
}
B.
boolean b=true;
boolean b2=true;
if(b==b2) {
System.out.println(“So true”);
}
C.
int i=1;
int j=2;
if(i==1|| j==2)
System.out.println(“OK”);
D.
int i=1;
int j=2;
if (i==1 &| j==2)
System.out.println(“OK”);
例题5:
Given the following declaration
String s = "hello";
Which are legal code?
A. s >> = 2;
B. char c = s[3];
C. s += "there";
D. int i = s.length();
E. s = s + 3;
例题6 (CDE)
Consider the following code:
Integer s = new Integer(9);
Integer t = new Integer(9);
Long u = new Long(9);
Which test would return true?
A. (s==u)
B. (s==t)
C. (s.equals(t))
D. (s.equals(9))
E. (s.equals(new Integer(9))
例题7(B)
What would be the result of attempting to compile and run the
following piece of code?
public class Test {
public int x;
public static void main(String args[]){
System.out.println("Value is " + x);
}
}
A. The output "Value is 0" is printed.
B. Non-static variable x cannot be referenced from a static context..
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.
例题8(AD)
Given the following method body:
{
if (sometest()) {
unsafe();
}
else {
safe();
}
}
The method "unsafe" might throw an IOException (which is not a subclass
of RunTimeException). Which correctly completes the method of
declaration when added at line one?
A. public void methodName() throws Exception
B. public void methodname()
C. public void methodName() throw IOException
D. public void methodName() throws IOException
E. public IOException methodName()
例题9(D)
The following is a program
1) class Exsuper{
2) String name;
3) String nick_name;
4)
5) public ExSuper(String s,String t){
6) name = s;
7) nick_name = t;
8) }
9)
10) public string toString(){
11) return name;
12) }
13) }
14)
15) public class Example extends ExSuper{
16)
17) public Example(String s,String t){
18) super(s,t);
19) }
20)
21) public String toString(){
22) return name +”a.k.a”+nick_name;
23) }
24)
25) public static void main(String args[]){
26) ExSuper a = new ExSuper(“First”,”1st”);
27) ExSuper b = new Example(“Second”,”2nd”);
28)
29) System.out.println(“a is”+a.toString());
30) System.out.println(“b is”+b.toString());
31) }
32) }
What happens when the user attempts to compile and run this program?
` A.
A Compiler error occurs at line 21
B.
An object of type ClassCastException is thrown at line 27
C.
The following output:
a is First
b is second
D.
The following output:
a is First
b is Second a.k.a 2nd
F.
The following output:
a is First a.k.a 1st
b is Second a.k.a 2nd
例题10
(D)
What is written to the standard output given the following statement:
System.out.println(4|7);
Select the right answer:
A.4
B.5
C.6
D.7
E.0
2.填空题(6′×5):
例题1:
Given:
1. public class SwitchTest {
2. public static void main(String[] args) {
3. System.out.println(“value = “ + switchIt(4));
4. }
5. public static int switchIt(int x) {
6. int j = 1;
7. switch (x) {
8. case 1: j++;
9. case 2: j++;
10. case 3: j++;
11. case 4: j++;
12. case 5: j++;
13. default: j++;
14. }
15. return j + x;
16. }
17. }
What is the result?
value = ____
例题2:
Given:
10. int i = 0;
11. for (; i <4; i += 2) {
12. System.out.print(i + “”);
13. }
14. System.out.println(i);
What is the result?
________
例题3:
1. public class test {
2. public static void add3 (Integer i) }
3. int val = i.intValue ( );
4. val += 3;
5. i = new Integer (val);
6. }
7.
8. public static void main (String args [ ] ) {
9. Integer i = new Integer (0);
10. add3 (i);
11. system.out.printIn (i.intValue ( ) );
12. }
13. )
What is the result?
______
例题4:
int index = 1;
boolean[] test = new Boolean[3];
boolean foo= test [index];
What is the result?
______
例题5:
1. public class test {
2. public static string output = “”;
3.
4. public static void foo(int i) {
5. try {
6. if(i==1) {
7. throw new Exception ();
8. }
9. output += “1”;
10. }
11. catch(Exception e) {
12. output += “2”;
13. return;
14. }
15. finally {
16. output += “3”;
17. }
18. output += “4”;
19. )
20.
21. public static void main (string args[]) (
22. foo(0);
23. foo(1);
24.
25. )
26. )
第24行时,output的值为______
3.程序题(15′×2):
1. 36块砖,36人搬。男搬4,女搬3,两个小儿抬一砖。要求一次全搬完。需男、女、小儿各若干?
2. 自定义一个字符串函数,其功能是随机产生一个由字母和数字组成的任意位数的字符串。
答案:
选择题:
1. D
2. A
点评:在Java中垃圾收集是不能被强迫立即执行的。调用System.gc()或Runtime.gc()静态方法不能保证垃圾收集器的立即执行,因为,也许存在着更高优先级的线程。所以选项B、D不正确。选项C的错误在于,System.gc()方法是不接受参数的。选项E中的方法可以使对象在下次垃圾收集器运行时被收集。
3. D
点评:在第12行,16位长的char型变量ch在编译时会自动转化为一个32位长的int型,并在运行时传给void test(int i)方法。
4. BC
点评:选项A错,因为if语句后需要一个boolean类型的表达式。逻辑操作有^、&、| 和 &&、||,但是“&|”是非法的,所以选项D不正确。
5. CDE
6. CE
7. B
8. AD
9. D
10.D
填空题:
1. 8
2. 0 2 4
3. 0
4. false
5. 13423
程序题:
1.
public class test {
public static void main(String args[]) {
int man = 0;
int woman = 0;
int kids = 0;
for (man = 0; man < 36; man++) {
for (woman = 0; woman < 36; woman++) {
for (kids = 0; kids < 36; kids++) {
if (man * 4 + woman * 3 + kids / 2 == 36 && man + woman + kids == 36 &&
kids % 2 == 0) {
System.out.println("man=" + man + ",woman=" + woman + ",kids=" +
kids);
}
}
}
}
}
}
2.
import java.util.*;
public class test {
public static void main(String args[]) {
Random random = new Random();
int n = random.nextInt(100);
StringBuffer sb = new StringBuffer();
for (int i = 0; i <= n; i++) {
int c = random.nextInt(255);
if ( (c <= '9' && c >= '0') || (c <= 'Z' && c >= 'A') ||
(c <= 'z' && c >= 'a')) {
sb.append( (char) c);
i++;
}
}
System.out.println(sb.toString());
}
}