Java编程:40道英文选择题及答案解析

5星 · 超过95%的资源 需积分: 50 13 下载量 42 浏览量 更新于2024-09-04 收藏 20KB DOCX 举报
"这是一份包含40道Java选择题的练习,主要考察Java基础知识,如语法、数据类型、方法调用等。题目全部为英文,并提供了对应的答案。" 第一题解析: 题目中的代码试图比较两个整数x和y的值。在Java中,`if(x=y)` 实际上是将y的值赋给了x,然后检查x是否等于y,由于x和y初始值分别为3和1,所以赋值后x不再等于y,条件为假。因此,if语句的else部分将被执行,输出"Equal"。但是,由于题目指出编译时会出现错误,答案应为C,即"An error at 'if(x=y)' causes compilation to fail"。 第二题解析: 这道题目考察的是Java的数据类型。A选项,float f=1.3; 是错误的,因为1.3是一个double类型的数值,不能直接赋值给float。B选项,char c="a"; 也是错误的,char只能存储单个字符,而"a"是一个字符串。C选项,byte b=257; 也不正确,因为byte的范围是-128到127,257超出了这个范围。D选项,int i=10; 是正确的,没有警告或错误。因此,答案是D。 第三题解析: 这段代码尝试在一个静态上下文中调用非静态方法`amethod`,这是不允许的。在Java中,非静态方法需要一个对象实例来调用,而静态方法可以直接通过类名调用。因此,编译时会报错,"Can't make static reference to the non-static method amethod." 答案是A。 第四题解析: 这道题目询问哪行代码可以无警告或错误地编译。选项没有给出完整,但我们可以根据Java语法推测。通常,如果一个表达式涉及变量声明、赋值或者合法的操作符,它可能可以编译成功。然而,由于题目要求选出无警告或错误的选项,我们需要考虑Java的类型系统和语法规则。例如,如果选项是一个变量声明,确保变量类型和初始化值匹配;如果是一个表达式,确保操作符的使用正确。没有完整的选项,我们无法提供确切答案,但可以推断出,正确答案应该是一个符合Java语法且没有类型转换问题的表达式。 总结来说,这些Java选择题涵盖了基本的语法概念,包括变量赋值、数据类型、方法调用以及编译错误的理解。通过解决这些问题,学习者可以巩固他们的Java基础知识。
2008-12-21 上传
1. The name of a Java source file (a) has no restrictions (b) must be the same as the class it defines, ignoring case (c) must use the extension .class (d) must be the same as the class it defines, respecting case 2. Which of the following statements is (are) true about the use of an asterisk (*) in a Java import statement? Ⅰ.It does not incur run-time overhead. Ⅱ.It can be used to import multiple packages with a single statement. Ⅲ.It can be used to import multiple classes with a single statement (a) I, II, and III (b) I and III only (c) I only (d) III only ..... 9. According to the Java code conventions, files that contain Java source code have the suffix _____, and compiled bytecode files have the suffix _____. (a) .class, .java (b) .class, .javac (c) .java, .class (d) .javac, .class 10. As an aid in debugging a program in Java, print statements may be used to display which of the following types of information? I. The names of methods being called II. The values of the parameters of a method Ⅲ. The values of the instance variables of a class (a) I and II only (b) I and III only (c) II and III only (d) I, II, and III 1. In a UML class diagram's representation of a class, the top, middle, and lower rectangular compartments respectively describe the _____ of the class. (a) name, attributes, and methods (b) name, methods, and constants (c) attributes, methods, and name (d) attributes, methods, and constants 2. UML class diagrams can describe which of the following? I. The internal structure of classes Ⅱ. Relationships between classes (a) I and II (b) II only (c) None (d) I only ....... 1.The term class variable is a synonym for (a) a private data field (b) a static data field (c) a read-only variable (d) an instance variable 2. Consider the following Java program segment. import java.io.*; public class Test { public Test( ) { System.out.println("default"); } public Test( int i ) { System.out.println("non-default"); } public static void main(String[] args) { Test t = new Test(2); } } ......... 9. When a subclass defines an instance method with the same return type and signature as a method in its parent, the parent's method is said to be (a) private (b) hidden (c) overloaded (d) overridden 10. Which is a Java access modifier used to designate that a particular data field will not be inherited by a subclass? (a) final (b) protected (c) private (d) default 1. Consider the following Java program segment. String[] str = {"Three","Two","One"}; for (int i = 0; i < str.length; ++i) { System.out.println(str[i]+"/"); } What will be output upon execution of the program segment? (a) Three/Two/One/ (b) Three,Two,One (c) One,Two,Three (d) One/Two/Three/ 2. Consider the following Java program segment. int[] arr; arr = new int[3]; arr[2]=19; arr[1]=17; arr[0]=15; Which of the following Java statements is syntactically correct and semantically identical to the program segment? (a) int[] arr= {15, 17, 19}; (b) int[3] arr = {15, 17, 19}; (c) int arr = {15, 17, 19}; (d) int arr[3]= {15, 17, 19}; ........ 2. Which of the following statements is (are) true about any abstract method in Java? I. It contains no definition. Ⅱ. It cannot be declared public. (a) I and II (b) I only (c) None (d) II only 3. Consider the following Java program fragment. public void drive(Vehicle v) { ... } ... drive(obj); The method call drive(obj) is valid if obj is which of the following? I. A descendent of class Vehicle II. An ancestor of class Vehicle Ⅲ. An object of class Vehicle (a) I and III only (b) I, II, and III (c) III only (d) II and III only .....