1、测试无答案Questions 1. What will happen when you attempt to compile and run the following code? public class Staticstaticint 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; Choices:a. Compiletime error b.
2、 prints : 1 c. prints : 2 d. prints : 3 e. prints : 7 f. prints : 8 2. Given the following code, what will be the output? class Valuepublic int i = 15; public class Testpublic static void main(String argv) Test t = new Test();t.first(); public void first() int i = 5; Value v = new Value();v.i = 25;s
3、econd(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); Choices:a. 15 0 20b. 15 0 15 c. 20 0 20 d. 0 15 20 3. What will happen when you attempt to compile and run the following code? class MyParent int x
4、, y;MyParent(int x, int y)this.x = x;this.y = y;public int addMe(int x, int y)return this.x + x + y + this.y;public int addMe(MyParent myPar)return addMe(myPar.x, myPar.y); class MyChild extends MyParentint z;MyChild (int x, int y, int z)super(x,y);this.z = z;public int addMe(int x, int y, int z)ret
5、urn this.x + x + this.y + y + this.z + z;public int addMe(MyChild myChi)return addMe(myChi.x, myChi.y, myChi.z);public int addMe(int x, int y)return this.x + x + this.y + y; public class MySomeOnepublic static void main(String args)MyChild myChi = new MyChild(10, 20, 30);MyParent myPar = new MyParen
6、t(10, 20);int x = myChi.addMe(10, 20, 30);int y = myChi.addMe(myChi);int z = myPar.addMe(myPar);System.out.println(x + y + z); Choices:a. 300 b. 240 c. 120 d. 180 e. Compilation error f. None of the above 4. What will be the result of executing the following code? 1. boolean a = true;2. boolean b =
7、false;3. boolean c = true;4. if (a = true)5. if (b = true)6. if (c = true) System.out.println(Some things are true in this world);7. else System.out.println(Nothing is true in this world!);8. else if (a & (b = c) System.out.println(Its too confusing to tell what is true and what is false);9. else Sy
8、stem.out.println(Hey this wont compile); Choices:a. The code wont compileb. Some things are true in this world will be printedc. Hey this wont compile will be printedd. None of these 5. What will happen when you attempt to compile and run the following code? interface MyInterface public class MyInst
9、anceTest implements MyInterfacestatic String s; public static void main(String args)MyInstanceTest t = new MyInstanceTest(); if(t instanceof MyInterface)System.out.println(I am true interface);else System.out.println(I am false interface); if(s instanceof String)System.out.println(I am true String);
10、else System.out.println(I am false String); Choices:a. Compiletime error b. Runtime error c. Prints : I am true interface followed by I am true String d. Prints : I am false interface followed by I am false String e. Prints : I am true interface followed by I am false String f. Prints : I am false i
11、nterface followed by I am true String 6. What results from attempting to compile and run the following code? public class Ternarypublic static void main(String args)int a = 5;System.out.println(Value is - + (a 5) ? 9.9 : 9); Choices:a. prints: Value is - 9 b. prints: Value is - 5 c. Compilation erro
12、r d. None of these 7. In the following pieces of code, A and D will compile without any error. True/False? A: StringBuffer sb1 = abcd; B: Boolean b = new Boolean(abcd); C: byte b = 255; D: int x = 0x1234; E: float fl = 1.2; Choices:a. Trueb. False 8. Considering the following code, Which variables m
13、ay be referenced correctly at line 12? 1.public class Outer2.3.public int a = 1;4.private int b = 2;5.public void method(final int c)6.7.int d = 3;8.class Inner9.10.private void iMethod(int e)11. 12. 13.14.15.16. Choices:a. a b. bc. cd. de. e 9. What will be the result of executing the following cod
14、e? public static void main(String args) char digit = a; for (int i = 0; i 10; i+) switch (digit) case x : int j = 0;System.out.println(j); default : int j = 100;System.out.println(j); int i = j; System.out.println(i); Choices:a. 100 will be printed 11 times. b. 100 will be printed 10 times and then
15、there will be a runtime exception. c. The code will not compile because the variable i cannot be declared twice within the main() method. d. The code will not compile because the variable j cannot be declared twice within the switch statement.e. None of these. 10. What will happen when you attempt t
16、o compile and run the following code? class MyThread extends Threadpublic void run()System.out.println(MyThread: run(); public void start()System.out.println(MyThread: start(); class MyRunnable implements Runnablepublic void run()System.out.println(MyRunnable: run(); public void start()System.out.pr
17、intln(MyRunnable: start(); public class MyTest public static void main(String args)MyThread myThread = new MyThread();MyRunnable myRunnable = new MyRunnable();Thread thread = new Thread(myRunnable);myThread.start();thread.start(); Choices:a. Prints : MyThread: start() followed by MyRunnable:run()b.
18、Prints : MyThread: run() followed by MyRunnable:start()c. Prints : MyThread: start() followed by MyRunnable:start()d. Prints : MyThread: run() followed by MyRunnable:run()e. Compile time error f. None of the above 11. Given the code below, and making no other changes, which access modifiers (public,
19、 protected or private) can legally be placed before myMethod() on line 3? If line 3 is left as it is, which keywords can legally be placed before myMethod on line 8? 1.class HumptyDumpty2.3.void myMethod() 4.5.6.class HankyPanky extends HumptyDumpty7.8.void myMethod() 9. Choices:a. private or nothin
20、g(i.e. leaving it as it is) on line 3. Nothing(i.e. leaving it as it is) or protected or public on line 8.b. public or protected on line 3. private or nothing(i.e. leaving itas it is) on line 8. c. nothing(i.e. leaving it as it is) or protected or public on line 3. private or nothing(i.e. leaving it
21、 as it is) on line 8.d. None of the above. 12. What will be the result of executing the following code? / Filename; SuperclassX.javapackage packageX; public class SuperclassXprotected void superclassMethodX()int superclassVarX; / Filename SubclassY.java1.package packageX.packageY;2.3.public class Su
22、bclassY extends SuperclassX4.5.SuperclassX objX = new SubclassY();6.SubclassY objY = new SubclassY();7.void subclassMethodY()8.9.objY.superclassMethodX();10.int i;11.i = objY.superclassVarX;12.13. Choices:a.Compilation error at line 5b. Compilation error at line 9c. Runtime exception at line 11d. No
23、ne of these 13. Consider the class hierarchy shown below: - class FourWheeler implements DrivingUtilitiesclass Car extends FourWheelerclass Truck extends FourWheelerclass Bus extends FourWheelerclass Crane extends FourWheeler- Consider the following code below:1.DrivingUtilities du;2.FourWheeler fw;
24、3.Truck myTruck = new Truck();4.du = (DrivingUtilities)myTruck;5.fw = new Crane();6.fw = du; Which of the statements below are true? Choices:a. Line 4 will not compile because an interface cannot refer to an object. b. The code will compile and run. c. The code will not compile without an explicit c
25、ast at line 6, because goingdown the hierarchy without casting is not allowed.d.The code at line 4 will compile even without the explicit cast. e.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime. 14. What results from the following code? 1.class MyCla
26、ss2.3.void myMethod(int i) System.out.println(int version);4.void myMethod(String s) System.out.println(String version);5.public static void main(String args)6.7.MyClass obj = new MyClass();8.char ch = c;9.obj.myMethod(ch);10.11. Choices:a. Line 4 will not compile as void methods cant be overridden.
27、b. An exception at line 9.c. Line 9 will not compile as there is no version of myMethod which takes a char as argument.d. The code compiles and produces output: int version.e. The code compiles and produces output: String version. 15. What is the result when you compile and run the following code? public class ThrowsDemo static void throwMethod() System.out.println(Inside throwMethod.); throw new IllegalAccessException(demo); public static void main(String args) try throwMethod(); catch (IllegalAccessException e) System.out.println(Caught + e); Choices:a. Compilation errorb. Runt
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1