为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > Java选择判断题库

Java选择判断题库

2018-03-24 33页 doc 74KB 38阅读

用户头像

is_833902

暂无简介

举报
Java选择判断题库Java选择判断题库 Java选择题库 1 Which declarations of identifiers are legal? A. $persons B. TwoUsers C. *point D. this E. _endline Answer: ABE 答案 A,B,E 解析 Java的标识符可以以一个Unicode字符,下滑线(_),美元符($)开始,后续字符可以是前面的符号和数字,没有长度限制,大小写敏感,不能是保留字。 2 Which of the following answer is...
Java选择判断题库
Java选择判断库 Java选择题库 1 Which declarations of identifiers are legal? A. $persons B. TwoUsers C. *point D. this E. _endline Answer: ABE A,B,E 解析 Java的标识符可以以一个Unicode字符,下滑线(_),美元符($)开始,后续字符可以是前面的符号和数字,没有长度限制,大小写敏感,不能是保留字。 2 Which of the following answer is correct to express the value 8 in octal number? A. 010 B. 0x10 C. 08 D. 0x8 Answer: A 解析 八进制值以0开头,以0x开头的为十六进制值,八进制中不能出现数字8,最大只有7。 3 Which are not Java keys? A. TRUE B. sizeof C. const D. super E. void Answer: AB 解析 A:不是,Java中有true,但是这也不是关键字而是字面量(literal)。 B:不是,Java中不需要这个操作符,所有的类型(原始类型)的大小都是固定的。 C、D、E都是,需要说明的是const是java中未被使用的关键字。 4 What will be the result of executing the following code? 1. boolean a = true; 2. boolean b = false; 3. boolean c = true; 4. 5. 6. 7. if (a == true) if (b == true) if (c == true) System.out.println("Some things are true in this world"); else System.out.println("Nothing is true in this world!"); 8. else if (a && (b = c)) System.out.println("It?s too confusing to tell what is true and what is false"); 9. else System.out.println("Hey this won?t compile"); Choices: a. The code won?t compile b. "Some things are true in this world" will be printed c. "Hey this won?t compile" will be printed d. None of these ―――――――――――――― Answer: D D is correct. This is a very good question to test the concepts of execution flow in case of if conditions. The rule for attaching else statements with if conditions is the same as attaching close brackets with open brackets. A close bracket attaches with the closest open bracket, which is not already closed. Similarly an else statement attaches with the closest if statement, which doesn?t have an else statement already, attached to it. So the else statement at line 7 attaches to the if statement at line 6. The else statement at line 8 attaches to the if statement at line 5. The else statement at line 9 attaches to the if statement at line 8. Now let?s look at the execution. At line 4 since a is equal to true the execution falls to line 5. At line 5 since b is not true the execution goes to the corresponding else statement at line 8. Now it evaluates the condition inside the if statement. Please note here that an assignment statement also has a value equal to the value being assigned, hence (b = c) evaluates to true and subsequently a && (b = c) evaluates to true and "It?s too confusing to tell what is true and what is false" will be printed. Hence the correct answer is choice D. 5 What results from attempting to compile and run the following code? public class Ternary { public 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 error d. None of these ―――――――――― Answer: D D is correct. The code compiles successfully. In this code the optional value for the ternary operator, 9.0(a double) and 9(an int) are of different types. The result of a ternary operator must be determined at the compile time, and here the type chosen using the rules of promotion for binary operands, is double. Since the result is a double, the output value is printed in a floating point format. The choice of which value to be printed is made on the basis of the result of the comparison "a < 5" which results in false, hence the variable "a" takes the second of the two possible values, which is 9, but because the result type is promoted to double, the output value is actually written as 9.0, rather than the more obvious 9, hence D is correct. 6 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 error b. Runtime error c. Compile successfully, nothing is printed. d. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo ――――――――― Answer: A A is correct. Exception :java.lang.IllegalAccessExcption must be caught or placed in the throws clause of the throwMethod(), i.e. the declaration of throwMethod() be changed to "static void throwMethod() throws IllegalAccessExcption". Thus compilation error will occur. 8 Which statements about the garbage collection are true? A. The program developer must create a thread to be responsible for free the memory. B. The garbage collection will check for and free memory no longer needed. C. The garbage collection allow the program developer to explicity and immediately free the memory. D. The garbage collection can free the memory used java object at expect time. 翻译 关于垃圾收集的哪些叙述是对的。 A. 程序开发者必须自己创建一个线程进行内存释放的工作。 B. 垃圾收集将检查并释放不再使用的内存。 C. 垃圾收集允许程序开发者明确指定并立即释放该内存。 D. 垃圾收集能够在期望的时间释放被java对象使用的内存。 Answer: B 解析 Java语言将内存分配和释放的工组交给了自己,程序员不必做这些工作,它提 供一个系统级 的线程跟踪每个内存的分配,在JVM的空闲处理中,垃圾收集线 程将检查和释放不再使用的内 存(即可以被释放的内存)。垃圾收集的过程在java程序的生存期中是自动的, 不需要分配和 释放内存,也避免了内存泄漏。可以调用System.gc()方法建议(suggest)JVM 执行垃圾收集 以使得可被释放的内存能立即被使用,当此方法返回的时候,JVM已经做了最大的努力从被丢 弃的对象上回收内存空间。程序员不能指定收集哪些内存,一般而言也不用关心这个问题,除 非是程序的内存消耗很大,特别是有很多临时对象时可以“建议“进行垃圾收集以提高可用内 存。需要指出的是调用System.gc()方法不能保证JVM立即进行垃圾收集,而只能是建议,因为 垃圾收集线程的优先级很低(通常是最低的)。 9 1. public class X { 2. public object m () { 3. object o = new float (3.14F); 4. object [] oa = new object [1]; 5. oa[0]= o; 6. o = null; 7. oa[0] = null; 8.return o; 9. } 10. } When is the float object created in line 3, eligible for garbage collection? A. Just after line 5 B. Just after line 6 C. Just after line 7 D. Just after line 8(that is, as the method returns) ANSWER: C 选C是正确的,请看程序的执行过程: 第三行:创建Float对象,并用0变量进行引用 第四行:创建数组 第五行:数组的第0个位置对Float对象进行引用 第六行:取消变量O对Float对象的引用 第七行:取消数组第0个位置对Float对象进行引用。(此时,已无任何变量对Float对象进行引用,所以Float对象将eligible for garbage collection) 第八行:返回O的引用,即null 10 What will be printed when you execute the following code? class X { Y b = new Y(); X() { System.out.print("X"); } } class Y { Y() { System.out.print("Y"); } } public class Z extends X { Y y = new Y(); Z() { System.out.print("Z"); } public static void main(String[] args) { new Z(); } } Choices: a. Z b. YZ c. XYZ d. YXYZ ――――――――― Answer: D D is correct. A difficult but a fundamental question, please observe carefully. Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent?s constructor from the constructor of the child class via the super() statement). Also note that when an object is constructed the variables are initialized first and then the constructor is executed. So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result. After that constructor of X will be called which implies "X" will be printed. Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed. Thus YXYZ will be printed. 11 What will happen when you attempt to compile and run the following code? class Base { int i = 99; public void amethod() { System.out.println("Base.amethod()"); } Base() { amethod(); } } public class Derived extends Base { int i = -1; public static void main(String argv[]) { { System.out.println("Derived.amethod()"); } } Choices: a. Derived.amethod() -1 Derived.amethod() b. Derived.amethod() 99 c.Derived.amethod() 99 d. Derived.amethod() e.Compile time error ―――――――― Base b = new Derived(); System.out.println(b.i); b.amethod(); } public void amethod() Answer: B B is correct. The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class then compilation error would be reported as at compile time, when compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class. 12 Given: 1. public class Test { 2. public static void main (String [] args) { 3. string foo = “blue”; 4. string bar = foo; 5. foo = “green”; 6. System.out.printIn(bar); 7. } 8. } What is the result? A. An exception is thrown. B. The code will not compile. C. The program prints “null” D. The program prints “blue” E. The program prints “green” Answer: D 13 public class Parent { int change() {,} } class Child extends Parent { } Which methods can be added into class Child? A. public int change(){} B. int chang(int i){} C. private int change(){} D. abstract int chang(){} Answer: AB 需要注意的是答案D的内容,子类可以重写父类的方法并将之声明为抽象方法,但是这引发的问题是类必须声明为抽象类,否则编译不能通过,而且抽象方法不能有方法体,也就是方法声明后面不能带上那两个大括号({}),这些D都不能满足。 14 Given the code below, and making no other changes, which access modifiers (public, 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 HumptyDumpty 2.{ 3.void myMethod() {} 4.} 5. 6.class HankyPanky extends HumptyDumpty 7.{ 8.void myMethod() {} 9.} Choices: a. private or nothing(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 it as 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 as it is) on line 8. d. None of the above. A is correct. The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly(default modifier) it can only be private or friendly in the superclass. Secondly if the method in superclass is left as it is(i.e. friendly access) the method in subclass can be friendly, protected or public. 15 What results from the following code? 1.class MyClass 2.{ 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 can?t be overridden. 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. ―――――――――― D is correct. A is incorrect as void methods can be overridden without any problem. B is incorrect as char ch declaration is valid. C is incorrect as char type in java is internally stored as integer and there is a method which takes int as an input. D is correct, on line 9 char ch is widened to an int and passed to int version of the myMethod(). E is incorrect as int version of myMethod() is called. 16 What is displayed when the following is executed? class Parent { private void method1() { 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[]) { Parent p = new Child(); 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() ――――――― C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent?s method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child?s class method1() would be called. Thus C is correct answer. 18 1) class Person { 2) public void printValue(int i, int j) {/*,*/ } 3) public void printValue(int i){/*...*/ } 4) } 5) public class Teacher extends Person { 6) public void printValue() {/*...*/ } 7) public void printValue(int i) {/*...*/} 8) public static void main(String args[]){ 9) Person t = new Teacher(); 10) t.printValue(10); 11) } 12) } Which method will the statement on line 10 call? A. on line 2 B. on line 3 C. on line 6 D. on line 7 翻译 第十行的声明将调用哪些方法。 答案 D 解析 变量t是一个Person对象,但是它是用Teacher实例化的,这个问题涉及到java的 编译时多态和运行时多态的问题,就编译时多态来说,t实际上是一个Person类,这涉及到类 型的自动转换(将一个子类的实例赋值给一个父类的变量是不用进行强制类型转换,反之则需 要进行强制类型转换,而且被赋值的变量实际上应该是一个子类的对象),如果对t调用了子 类中新增的方法则造成编译时错误编译将不能通过,而在运行时,运行时系统 将根据t实际指 向的类型调用对应的方法,对于本例来说,t.print(10)将调用t实际指向的Teacher类的对应 方法。在java中,可以用一个子类的实例实例化父类的一个变量,而变量在编译时是一个父类 实例,在运行时可能是一个子类实例。 19 Which one can construct a BufferedOutputStream object: A. new BufferedOutputStream(); B. new BufferedOutputStream(“o.txt”); C. new BufferedOutputStream(new Writer(“o.txt”)); D. new BufferedOutputStream(new FileOutputStream(“o.txt”)); Answer: D when you compile a java program you should use command: A.java *.java B.javab *.java C.javac *.java D.javad *.java Answer: C 20.which statement will compile wrong: A(if(3>5)System.out.println(“hello”); B(if 3 System.out.println(“hello”); C(if(true)System.out.println(“hello”); D(if(false)System.out.println(“hello”); Answer: B 21 You want to limit access to a method of a public class to members of the same class. Which access accomplishes this objective? A. public B. private C. protected D. default access Answer: B 22 Which one is valid declaration within an interface definition? A. public void methoda(); B. protected void methoda(double d1); C. public final double methoda(); D. private void methoda(double d1); Answer: A 23 Which is a valid identifier? A. false B. default C. _object D. a-class Answer: C 24 What is the numerical range of a byte? A. 0...32767 B. 0...65535 C. -128...127 D. -256...255 Answer: C 25 Given: int i =1, j =10; do { if (i++> --j) { continue; } } while (i <5); System.out.println ("i = “+i+ "and j = "+j); What is the result? A. i = 6 and j = 5 B. i = 5 and j = 5 C. i = 6 and j = 5 D. i = 5 and j = 6 Answer: D 26 Given: String s=“0123”;char c=s.charAt(1); what the result of c: A.0 B.1 C.2 D.3 Answer: B 27 In java which one is not a Layout Management class: A. BorderLayout B. FlowLayout C. CardLayout D. LayoutManage Answer: D 28 Given: public class Test { public static void main(String args[]) { class Foo { public int i = 3; } Object o = (Object)new Foo(); Foo foo = (Foo)o; System.out.println ("i = " + foo.i); } } What is the result? A. i = 3 B. Compilation fails. C. i=6; D. i=7; Answer: A 29 You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective? A. public B. private C. protected D. transient E. default access Answer: E 30 Given: 1. class Base { 2. Base() { System.out.print(“Base”); } 3. } 4. public class Alpha extends Base { 5. public static void main( String[] args ) { 6. new Alpha(); 7. new Base(); 8. } 9. } What is the result? A. Base B. BaseBase C. Compilation fails. D. The code runs with no output. E. An exception is thrown at runtime. Answer: B 31 Which three statements are true? (Choose three) A. The default constructor initializes method variables. B. The default constructor has the same access as its class. C. The default constructor invoked the no-arg constructor of the superclass. D. If a class lacks a no-arg constructor, the compiler always creates a default constructor. E. The compiler creates a default constructor only when there are no other constructors for the class. Answer: B, C, E 32 Which statement is true? A. A try statement must have at least one corresponding catch block. B. Multiple catch statements can catch the same class of exception more than once. C. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method. D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute. E. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block must always run to completion. 33 You want to limit access to a method of a public class to members of the same class. Which access accomplishes this objective? A. public B. private C. protected D. transient E. default access Answer: B 34 Given: 11. switch(x) { 12. default: 13. System.out.println(“Hello”); 14. } Which two are acceptable types for x? (Choose two) A. byte B. long C. char D. float E. Short F. Long Answer: A, C 35 Given: 1. public class Foo { 2. public void main( String[] args ) { 3. System.out.println( “Hello” + args[0] ); 4. } 5. } What is the result if this code is executed with the command line? java Foo world A. Hello B. Hello Foo C. Hello world D. Compilation fails. E. The code does not run. Answer: E 36 Which statement is true? A. Programs will not run out of memory. B. Objects that will never again be used are eligible for garbage collection. C. Objects that are referred to by other objects will never be garbage collected. D. Objects that can be reached from a live thread will never be garbage collected. E. Objects are garbage collected immediately after the system recognizes they are eligible. Answer: D 37 Given: 1. public class Outer{ 2. public void someOuterMethod() { 3. // Line 3 4. } 5. public class Inner{} 6. public static void main( String[]argv ) { 7. Outer o = new Outer(); 8. // Line 8 9. } 10. } Which instantiates an instance of Inner? A. new Inner(); // At line 3 B. new Inner(); // At line 8 C. new o.Inner(); // At line 8 D. new Outer.Inner(); // At line 8 Answer: A 38 Which two are benefits of fully encapsulating a class? (Choose two) A. Performance of class methods is improved. B. Implementation details of the class are hidden. C. Access modifiers can be omitted on class data members. D. Code that uses the encapsulation class can access data members directly. E. Internal operation of the class can be modified without impacting clients of that class. Answer: B, E 39 Which two statements are reserved words in Java? (Choose Two) A. Run B. Import C. Default D. Implement Answer: B, C 40 Which two statements are true? (Choose Two) A. An inner class may be declared as static. B. An anonymous inner class can be declared as public. C. An anonymous inner class can be declared as private. D. An anonymous inner class can extend an abstract class. E. An anonymous inner class can be declared as protected. Answer: A, D 41 Which two statements are true regarding the creation of a default constructor? (Choose Two) A. The default constructor initializes method variables. B. The compiler always creates a default constructor for every class. C. The default constructor invokes the no-parameter constructor of the superclass. D. The default constructor initializes the instance variables declared in the class. E. When a class has only constructors with parameters, the compiler does not create a default constructor. Answer: D, E 42 Which two create an InputStream and open file the “file.txt” for reading? (Choose Two) A. InputStream in=new FileReader(“file.txt”); B. InputStream in=new FileInputStream(“file.txt”); C. InputStream in=new InputStreamFileReader (“file.txt”, “read”); D. FileInputStream in=new FileReader(new File(“file.txt”)); E. FileInputStream in=new FileInputStream(new File(“file.txt”)); Answer: B, E 43 Which statement is true? A. The Error class is a untimeException. B. No exceptions are subclasses of Error. C. Any statement that may throw an Error must be enclosed in a try block. D. Any statement that may throw an Exception must be enclosed in a try block. E. Any statement that may thro a runtimeException must be enclosed in a try block. Answer: D 44 Given: 1. public class Foo { 2. public void main (String [] args) { 3. system.out.printIn(“Hello World.”); 4. } 5. } What is the result? A. An exception is thrown. B. The code does no compile. C. “Hello World.” Is printed to the terminal. D. The program exits without printing anything. Answer: A 45 Which two declarations prevent the overriding of a method? (Choose Two) A. Final void methoda() {} B. Void final methoda() {} C. Static void methoda() {} D. Static final void methoda() {} E. Final abstract void methoda() {} Answer: A, D 46 Which two demonstrate encapsulation of data? (Choose Two) A. Member data have no access modifiers. B. Member data can be modified directly. C. The access modifier for methods is protected. D. The access modifier to member data is private. E. Methods provide for access and modification of data. Answer: D, E 47 Which two statements are true? (Choose Two) A. An anonymous inner class can be declared inside of a method B. An anonymous inner class constructor can take arguments in some situation. C. An anonymous inner class that is a direct subclass that is a direct subclass of Object can implement multiple interfaces. D. Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface. E. Event if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements multiple interfaces. Answer: A, B
/
本文档为【Java选择判断题库】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索