Consider the following code. Please list which lines below will not compile. (choose all that apply)
<o:p></o:p>
public class acd {
public static void main(String arg[])
{
4. Object obj1 = new Object();
5. char c = 3;
6. short s = 41;
7. byte b = 41;
8. s = b*b;
9. Double d = new Double(4.12f);
10. Float f = new Double(2.33);
11. d = obj1;
12. Float []ff = new Float[5];
13. float f1 = ff[0];
14. obj1 = ff;
15. obj1 = ff[5];
16. }
17. }
<o:p></o:p>
8,10,11
另一类似题:
Which of the following statements are legal? A. long l = 4990; B. int i = <st1:chmetcnv w:st="on" tcsc="0" unitname="l" sourcevalue="4" negative="False" numbertype="1" hasspace="False">4L</st1:chmetcnv>; C. float f = 1.1; D. double d = 34.4; E. double t = <st1:chmetcnv w:st="on" tcsc="0" unitname="F" sourcevalue=".9" negative="False" numbertype="1" hasspace="False">0.9F</st1:chmetcnv>.
True or False: In the code fragment below, line 4 is executed.
String s1 = “UID”
String s2 = “UID”
if(s1 == s2)
System.out.println(“ UID is printed”);
<o:p></o:p>
A.True
B.False
B
答案应该是A。参考一类似题:
String s= "hello"; String t = "hello"; char c[] = {'h','e','l','l','o'} ; Which return true? A. s.equals(t); B. t.equals(c); C. s==t; D. t.equals(new String("hello")); E. t==c.
public class Parent { public int addValue( int a, int b) { int s; s = a+b; return s; } } class Child extends Parent { } Which methods can be added into class Child? A. int addValue( int a, int b ){// do something...} B. public void addValue (){// do something...} C. public int addValue( int a ){// do something...} D. public int addValue( int a, int b )throws MyException {//do something...} 题目:哪些方法可以加入类Child中。 答案: (bc) 此题涉及方法重载(overload),方法重写(override)以及类派生时方法重写的规则。方法重载的规则是:一、参数列表必须不同,个数的不同完全可以,如果个数相同则参数类型的不同不能引起歧意,例如int 和long,float和double就不能作为唯一的类型不同;二、返回值可以不同,但是不能是重载时唯一的不同点(这点和c++中不同,c++中返回类型必须一致)。方法重写发生在类继承时,子类可以重写一个父类中已有的方法,必须在返回类型和参数列表一样时才能说是重写,否则就是重载,java中方法重写的一个重要而且容易被忽略的规则是重写的方法的访问权限不能比被重写的方法的访问权限低!重写的另一个规则是重写的方法不能比被重写的方法抛弃(throws)更多种类的异常,其抛弃的异常只能少,或者是其子类,不能以抛弃异常的个数来判断种类,而应该是异常类层次结果上的种类。