|
Question 1 What will happen if you compile/run this code? 1: public class Q1 extends Thread 2: { 3: public void run() 4: { 5: System.out.println("Before start method"); 6: this.stop(); 7: System.out.println("After stop method"); 8: } 9: 10: public static void main(String[] args) 11: { 12: Q1 a = new Q1(); 13: a.start(); 14: } 15: } A) Compilation error at line 7. B) Runtime exception at line 7. C) Prints "Before start method" and "After stop method". D) Prints "Before start method" only.
Question 2 What will happen if you compile/run the following code? 1: class Test { 3: static void show() { 5: System.out.println("Show method in Test class"); 6: } 7:} 8: public class Q2 extends Test { 11: static void show() { 13: System.out.println("Show method in Q2 class"); 14: } 15: public static void main(String[] args) { 17: Test t = new Test(); 18: t.show(); 19: Q2 q = new Q2(); 20: q.show(); 22: t = q; 23: t.show(); 25: q = t; //必须造型 26: q.show(); 27: } 28: } A) prints "Show method in Test class" "Show method in Q2 class" "Show method in Q2 class" "Show method in Q2 class" B) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Test class" C) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Q2 class" D) Compilation error.
Question 3 The following code will give 1: class Test 2: { 3: void show() 4: { 5: System.out.println("non-static method in Test"); 6: } } 8: public class Q3 extends Test { 10: static void show() { 12: System.out.println("Overridden non-static method in Q3"); 13: } 15: public static void main(String[] args) { 17: Q3 a = new Q3(); 18: } 19: } A) Compilation error at line 3. B) Compilation error at line 10. C) No compilation error, but runtime exception at line 3. D) No compilation error, but runtime exception at line 10.
Question No :4 The following code will give 1: class Test 2: { 3: static void show() //static方法不能被覆盖 4: { 5: System.out.println("Static method in Test"); 6: } 7: } 8: public class Q4 extends Test 9: { 10: void show() 11: { 12: System.out.println("Overridden static method in Q4"); 13: } 14: public static void main(String[] args) 15: { 16: } 17: } A) Compilation error at line 3. B) Compilation error at line 10. C) No compilation error, but runtime exception at line 3. D) No compilation error, but runtime exception at line 10.
Question No :5 The following code will print 1: int i = 1; 2: i <<= 31; 3: i >>= 31; 4: i >>= 1; 6: int j = 1; 7: j <<= 31; 8: j >>= 31; 10: System.out.println("i = " +i ); 11: System.out.println("j = " +j); A) i = 1 j = 1 B) i = -1 j = 1 C) i = 1 j = -1 D) i = -1 j = -1
Question No :6 The following code will print 1: Double a = new Double(Double.NaN); 2: Double b = new Double(Double.NaN); 4: if( Double.NaN == Double.NaN ) 5: System.out.println("True"); 6: else 7: System.out.println("False"); 9: if( a.equals(b) ) 10: System.out.println("True"); 11: else 12: System.out.println("False"); A) True True B) True False C) False True D) False False Question No :7 1: if( new Boolean("true") == new Boolean("true")) 2: System.out.println("True"); 3: else 4: System.out.println("False");
A) Compilation error. B) No compilation error, but runtime exception. C) Prints "True". D) Prints "False".
Question No :8 1: public class Q8 { 3: int i = 20; 4: static { 6: int i = 10; 7: } 9: public static void main(String[] args) { 11: Q8 a = new Q8(); 12: System.out.println(a.i); 13: } 14: } A) Compilation error, variable "i" declared twice. B) Compilation error, static initializers for initialization purpose only. C) Prints 10. D) Prints 20.
Question No :9 The following code will give 1: Byte b1 = new Byte("127"); 3: if(b1.toString() == b1.toString()) System.out.println("True"); 5: else System.out.println("False");//对象是对象,而String很特殊,不能完全将其视为对象 A) Compilation error, toString() is not avialable for Byte. B) Prints "True". C) Prints "False". Question No :10 What will happen if you compile/run this code? 1: public class Q10 2: { 3: public static void main(String[] args) 4: { 5: int i = 10; 6: int j = 10; 7: boolean b = false; 8: 9: if( b = i == j) 10: System.out.println("True"); 11: else 12: System.out.println("False"); 13: }//一是优先级,自增减运算、位运算、乘除取模加减、移位、逻辑、赋值; 其次是什么运算符,返回的就是什么值 14: } A) Compilation error at line 9 . B) Runtime error exception at line 9. C) Prints "True". D) Prints "Fasle".
Question 11 What will happen if you compile/run the following code? 1: public class Q11 2: { 3: static String str1 = "main method with String[] args"; 4: static String str2 = "main method with int[] args"; 5: 6: public static void main(String[] args) 7: { 8: System.out.println(str1); 9: } 10: 11: public static void main(int[] args) 12: { 13: System.out.println(str2); 14: } 15: } A) Duplicate method main(), compilation error at line 6. B) Duplicate method main(), compilation error at line 11. C) Prints "main method with main String[] args". D) Prints "main method with main int[] args".
Question 12 What is the output of the following code? 1: class Test { 3: Test(int i) { 5: System.out.println("Test(" +i +")"); 6: } 7: } 9: public class Q12 { 11: static Test t1 = new Test(1); 13: Test t2 = new Test(2); 15: static Test t3 = new Test(3); //静态成员先初始化,先静态后动态,先父类后子类 17: public static void main(String[] args) { 19: Q12 Q = new Q12(); 20: } 21: } A) Test(1) Test(2) Test(3) B) Test(3) Test(2) Test(1) C) Test(2) Test(1) Test(3) D) Test(1) Test(3) Test(2)
Question 13 //byte的范围是从+127到-128,因为在负数里面还多出了个符号位为1而其余各位均是0的数,它并非负0,而是-128 What is the output of the following code? 1: int i = 16; 2: int j = 17; 4: System.out.println("i >> 1 = " + (i >> 1)); 5: System.out.println("j >> 1 = " + (j >> 1));//结尾的1右移就会消失 A) Prints "i >> 1 = 8" "j >> 1 = 8" B) Prints "i >> 1 = 7" "j >> 1 = 7" C) Prints "i >> 1 = 8" "j >> 1 = 9" D) Prints "i >> 1 = 7" "j >> 1 = 8"
Question 14 //左移与右移带符号,而无符号右移则完全视作正数右移。前者只允许整型数移位。后者只对int和long型整数有效,若对byte和short操作,系统会将其转化为int型整数 What is the output of the following code? 1: int i = 45678;//正数减一再加个负号即可 2: int j = ~i; 4: System.out.println(j); A) Compilation error at line 2. ~ operator applicable to boolean values only. B) Prints 45677. C) Prints -45677. D) Prints -45679.
Question 15 What will happen when you invoke the following method? 1: void infiniteLoop() 2: { 3: byte b = 1; 5: while ( ++b > 0 ) ; 7: System.out.println("Welcome to Java"); 8: }
A) The loop never ends(infiniteLoop). B) Prints "Welcome to Java". C) Compilation error at line 5. ++ operator should not be used for byte type variables. D) Prints nothing.
Question 16 In the following applet, how many Buttons will be displayed? 1: import java.applet.*; 2: import java.awt.*; 4: public class Q16 extends Applet { 6: Button okButton = new Button("Ok"); 8: public void init() { 10: add(okButton); 11: add(okButton); 12: add(okButton); 13: add(okButton); 15: add(new Button("Cancel")); 16: add(new Button("Cancel")); 17: add(new Button("Cancel")); 18: add(new Button("Cancel"));//新的对象 20: setSize(300,300); 21: } 22: } A) 1 Button with label "Ok" and 1 Button with label "Cancel" . B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" . C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" . D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" .
Question 17 In the following, which is correct Container-Default layout combination? A) Applet – FlowLayout ok! B) Applet - BorderLayout C) Applet - CardLayout D) Frame – Flowlayout E) Frame – BorderLayout ok! F) Frame - CardLayout G) Panel – FlowLayout ok! H) Panel - BorderLayout. Question 18 What is the output of the following code? 1: String str = "Welcome"; 2: 3: str.concat(" to Java!");// 只是返回一个新的连接后的字符串,但原来字符串并未被替代,除非赋值 4: 5: System.out.println(str); A) Strings are immutable, compilation error at line 3. B) Strings are immutable, runtime exception at line 3. C) Prints "Welcome". D) Prints "Welcome to Java!".
Question 19 What is the output of the following code? 1: class MyClass { 3: static int maxElements; 5: MyClass(int maxElements) { 7: this.maxElements = maxElements; 8: } 10: } 12: public class Q19 { 14: public static void main(String[] args) { 16: MyClass a = new MyClass(100); 18: MyClass b = new MyClass(100); 20: if(a.equals(b)) System.out.println("Objects have the same values"); 22: else System.out.println("Objects have different values"); 24: } 25: } A) Compilation error at line 20. equals() method was not defined. B) Compiles fine, runtime exception at line 20. C) Prints "Objects have the same values". D) Prints "Objects have different values"; equals() method was available in base class Object. So it won't give any compilation error. Here MyClass is a user-defined class, so the user has to implement equals() method according to his requirments.
Question 20 1: import java.applet.*; 2: import java.awt.*; 4: public class Q20 extends Applet { 6: Button okButton = new Button("Ok"); 8: public void init() { 10: setLayout(new BorderLayout()); 12: add("South", okButton); 13: add("North", okButton); 14: add("East", okButton); 15: add("West", okButton); 16: add("Center", okButon); 18: setSize(300,300); 19: } 20: } The above Applet will display A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet. B) Only one Button with label "Ok" at the Top of the Applet. C) Only one Button with label "Ok" at the Bottom of the applet. D) Only one Button with label "Ok" at the Center of the Applet. 因为只有一个Button对象,所以只有最后一次添加的有效,它同时扩展至Applet周边
Question 21 What will happen if you compile/run the following code? 1:public class Q21 { 3: int maxElements; 5: void Q21() { 构造体不允许声明void
7: maxElements = 100; 8: System.out.println(maxElements); 9: } 11: Q21(int i) { 13: maxElements = i; 14: System.out.println(maxElements); 15: } 17: public static void main(String[] args) { 19: Q21 a = new Q21(); 20: Q21 b = new Q21(999); 21: } 22: } A) Prints 100 and 999. B) Prints 999 and 100. C) Compilation error at line 3, variable maxElements was not initialized. D) Compillation error at line 19.
Question 22 What will happen if run the following code? 1:Boolean[] b1 = new Boolean[10]; 3:boolean[] b2 = new boolean[10]; 6:System.out.println("The value of b1[1] = " +b1[1]); 7:System.out.println("The value of b2[1] = " +b2[1]); A) Prints "The value of b1[1] = false" "The value of b2[1] = false". B) Prints "The value of b1[1] = null" "The value of b2[1] = null". C) Prints "The value of b1[1] = null" "The value of b2[1] = false". D) Prints "The value of b1[1] = false" "The value of b2[1] = null". The value of b1[1] = null 对象 The value of b2[1] = false 逻辑值 The value of i[1] = 0 整型值 The value of c[0] = 字符 初值竟为空值,奇怪! The value of s[1] = null 字符串 The value of b[1] = 0 长型整型值 The value of f[1] = 0.0 float型值 对象的初值为null
Question 23 Which of the following are valid array declarations/definitions? 1: int iArray1[10]; 2: int iArray2[]; 3: int iArray3[] = new int[10]; 4: int iArray4[10] = new int[10]; 5: int []iArray5 = new int[10]; 6: int iArray6[] = new int[]; 7: int iArray7[] = null; A) 1. B) 2. ok C) 3. ok D) 4. E) 5. ok F) 6. G) 7. ok
Question 24 What is the output for the following lines of code? 1: System.out.println(" " +2 + 3); 先出现字符串,故+在此作为重载运算符 2: System.out.println(2 + 3); 无字符串,故+在此作为纯运算符 3: System.out.println(2 + 3 +"");先出现数字,故第一个+在此作为纯运算符,后一个+作为重载运算符 4: System.out.println(2 + "" +3); A) Compilation error at line 3 B) Prints 23, 5, 5 and 23. C) Prints 5, 5, 5 and 23. D) Prints 23, 5, 23 and 23.
Question 25 The following declaration(as a member variable) is legal. static final transient int maxElements = 100; A) True. B) False.
Question 26 What will happen if you compile/run the following lines of code? 1: int[] iArray = new int[10]; 3: iArray.length = 15; 5: System.out.println(iArray.length); A) Prints 10. B) Prints 15. C) Compilation error, you can't change the length of an array. D) Runtime exception at line 3. Question 27 Map接口:Map包含capacity 和load factor,后者默认值为0.75,当加入map的数量超过了load factor的限制后,map会自动扩大一倍。它包括两个构造器,一个是无参构造器,一个是以map作为参数的构造器,实际上它起到的作用相当于完全复制出一个新的map。Map本身不允许被用作key或value,因为在这种情况下equals, hashCode方法会出现问题。该接口无线程锁,所以当有多线程的需要时,可以给它加锁Map m = Collections.synchronizedMap(new HashMap(...));。
Collection接口:。包括两个构造器,一个是无参构造器,一个是以Collection对象作为参数的构造器,实际上它起到的作用相当于完全复制出一个新的Collection对象。
List接口:继承自Collection接口,允许相同的元素,也允许null元素。方法有iterator, add, remove, equals, hashCode等。从0开始计数。当其本身被用作元素时,equals, hashCode方法不再适用。一个长度为n的表包含n+1个元素,从0到n+1。
ListIterator:其所指向位置并无元素,它是通过previous() ,next()来返回所需元素。
Set:继承自Collection接口。不允许相同元素。至多允许有一个null元素。从Collection那里继承了add, equals , hashCode方法。不允许它本身作为元素。可能出现的错误有NullPointerException 或者 ClassCastException。
Integer:有两个构造器Integer(int value)和Integer(String s),典型方法有toString(),intValue(),parseInt(String s)。后两个方法等效。
Hashtable:实现map接口,是synchronized,与Map接口一样,自然也有两个默认构造器。典型方法有put(Object key, Object value),Object get(Object key),boolean equals(Object o),isEmpty()等。 例子This example creates a hashtable of numbers. It uses the names of the numbers as keys: Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3)); To retrieve a number, use the following code: Integer n = (Integer)numbers.get("two"); if (n != null) { System.out.println("two = " + n); }
Hashmap:实现map接口,非synchronized,典型方法为Object put(Object key, Object value), Object get(Object key)等。
TreeMap:实现Map, SortedMap接口,非synchronized。严格按照顺序排列,或为升序,或通过指定的排序方法。典型方法为Object put(Object key, Object value),Object get(Object key)等。
Vector:继承自List接口,是synchronized。通过capacity 和 capacityIncrement进行存储管理。典型方法有void add(int index, Object element) , boolean add(Object o),Object elementAt(int index),Object get(int index)等。
Stack:继承自Vector。典型方法有 Object pop() Removes the object at the top of this stack and returns that object as the value of this function. Object push(Object item) Pushes an item onto the top of this stack. int search(Object o) Returns the 1-based position where an object is on this stack.
ArrayList:实现List,非synchronized(若有需要,可以List list = Collections.synchronizedList(new ArrayList(...));)。允许null元素。典型方法为 void add(int index, Object element) Inserts the specified element at the specified position in this list. boolean add(Object o); Object get(int index) Returns the element at the specified position in this list. Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element.
上面所有的方法加入新元素都是加入对象,而并非单纯的基本类型量。可以加入”I am”这样的字符串,因为String常量本就是对象。但单纯的如int量想要加入上面的类,必须包装,如new Integer(10),或为”10”; What will happen if you compile/run the folowing lines of code? 1: Vector a = new Vector(); 3: a.addElement(10); //应该为”10” 5: System.out.println(a.elementAt(0)); A) Prints 10. B) Prints 11. C) Compilation error at line 3. D) Prints some garbage.
Question 28 What will happen if you invoke the following method? 1: public void check() { 3: System.out.println(Math.min(-0.0,+0.0)); 4: System.out.println(Math.max(-0.0,+0.0)); 5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0)); 6: } A) prints -0.0, +0.0 and false. B) prints -0.0, +0.0 and true. C) prints 0.0, 0.0 and false. D) prints 0.0, 0.0 and true. ok
Question 29 What will happen if you compile/run this code? 1: int i = 012; 12表示十进制数 2: int j = 034; 012表示八进制 3: int k = 056; 0xBAAC表示十六进制 4: int l = 078; //integer number too large: 078 超过了8进制的表示 6: System.out.println(i); 7: System.out.println(j); 8: System.out.println(k); A) Prints 12,34 and 56. B) Prints 24,68 and 112. C) Prints 10, 28 and 46. D) Compilation error.
Question 30 When executed the following line of code will print System.out.println(-1 * Double.NEGATIVE_INFINITY); A) -Infinity B) Infinity;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ C) NaN D) -NaN ANSWERS Question No: 1 D. After the execution of stop() method, thread won't execute any more statements. Question No: 2 D. Explicit casting is required at line 25. Question No: 3 B. You cann't override an non-static method with static method. Question No: 4 B. You cann't override a static method with non-static method. Question No: 5 D. Question No: 6 C. Question No: 7 D. Question No: 8 D. Here the variable '"i" defined in static initializer is local to that block only. The statements in the static initializers will be executed (only once) when the class is first created. Question No: 9 C. Question No: 10 C. Conditional operators have high precedence than assignment operators. Question No 11 C. Here the main method was overloaded, so it won't give compilation error. Question No 12 D. No matter where they declared, static variables will be intitialized before non-static variables. Question No 13 A. 16 >> 1 is 8 and 17 >> 1 also 8. Question No 14 D. Java allows you to use ~ operator for integer type variables.The simple way to calculate is ~i = (- i) - 1. Question No 15 B. Here the variable 'b' will go upto 127. After that overflow will occur, so 'b' will be set to -ve value, the loop ends and prints "Welcome to Java" Question No 16 B. Question No 17 A, E and G. For Applets and Panels FlowLayout is the default one, BorderLayout is default for Window and Frames. Question No 18 C. Strings are immutable. So str.concat("to Java!") will not append anything to str. Infact it will create another string "Welcome to Java!" and leaves it. Question No 19 D. equals() method was available in base class Object. So it won't give any compilation error. Here MyClass is a user-defined class, so the user has to implement equals() method according to his requirments. Question No 20 D. Question No 21 D. Constructors should not return any value. Java won't allow to indicate with void. In this case void Q21() is an ordinary method which has the same name of the Class. Question No 22 C. By default objects will be initialized to null and primitives to their corresponding default vaulues. The same rule applies to array of objects and primitves. Question No 23 B,C,E and G. You can't specify the array dimension in type specification(left hand side), so A and D are invalid. In line 6 the array dimension is missing(right hand side) so F is invalid. You can intialize an array with null. so G is valid. Question No 24 B. Question No 25 A. Question No 26 C. Once array is created then it is not possible to change the length of the array. Question No 27 C. You can't add primitives to Vector. Here 10 is int type primitive. Question No 28 B. The order of floating/double values is -Infinity --> Negative Numbers/Fractions --> -0.0 --> +0.0 --> Positive Numbers/Fractions --> Infinity. Question No 29 D. Here integers are assinged by octal values. Octal numbers will contain digits from 0 to 7. 8 is illegal digit for an octal value, so you get compilation error. Question No 30 B. Compile and see the result.
|