Java考试题1
- 格式:doc
- 大小:84.50 KB
- 文档页数:13
Java面向对象测试题(一)注:测试时间:3小时编程题在电脑上编写,保存在已学生名字为文件名的文件夹中一、选择题(共30分,每题1分<多选、少选、错选不得分>)1、下面有关Java异常处理模型的说法错误的是()。
a)一个try块只能有一条catch语句b)一个try块中可以不使用catch语句c)Catch块不能单独使用,必须始终与try块在一起d)Finally块不能单独使用,必须始终与try块在一起2、下列选项中关于Java中super关键字的说法错误的是()。
a)super关键字是在子类对象内部指代其父类对象的引用b)super关键字不仅可以指代子类的直接父类,还可以指代父类的父类c)子类可以通过super关键字调用父类的方法d)子类可以通过super关键字调用父类的属性3、下列选项中关于Java中this关键字的说法错误的是()。
a)this是在对象内部指代对象自身的引用b)this关键字可以在类中的任何位置使用c)this只和特定的对象关联,而不和类关联d)同一个类的不同对象有不同的this4、在Java中,Object类是所有类的父亲,用户自定义类默认扩展自Object类,下列选项中的()方法不属于Object类的方法。
a)equals(Object obj)b)getClass()c)toString()d)trim()5、给定如下Java代码,关于super的用法,以下描述正确的是()。
class C extends B{public C(){super();}}a)用来调用类B中定义的super()方法b)用来调用类C中定义的super()方法c)用来调用类B的无参构造方法d)用来调用类B中第一个出现的构造方法6、在Java中,有如下两个类Student和Person。
如果Student是Person的子类,则下列声明对象x的语句中错误的是()。
a)Person x = new Student()b)Person x = new Person()c)Student x = new Person()d)Student x = new Student()7、下列对Java中的继承描述错误的说法是()。
1. 基础知识填空题- Java中最基本的数据类型是_ _ _ _ _。
- 答案:int- 解析:int是Java中用来表示整数的数据类型,它可以存储不带小数点的数值。
- Java中用来表示小数的数据类型是_ _ _ _ _。
- 答案:double- 解析:double是Java中用来表示带有小数点的数值的数据类型,它可以存储更大范围的数值。
- 在Java中,用来表示逻辑值的数据类型是_ _ _ _ _。
- 答案:boolean- 解析:boolean类型只能存储true或false两个值,用于表示逻辑上的真或假。
2. 控制语句填空题- 在Java中,控制循环的关键字是_ _ _。
- 答案:for- 解析:for循环是Java中用来控制循环次数的关键字,通过设置循环的初始条件、循环条件和循环迭代来控制循环的执行。
- 使用_ _ _关键字可以在循环中跳出当前循环。
- 答案:break- 解析:break关键字可以在循环或switch语句中使用,用于跳出当前的循环或switch语句。
- 使用_ _ _关键字可以结束当前循环,继续执行下一次循环。
- 答案:continue- 解析:continue关键字可以用于结束当前循环的迭代,直接进入下一次循环的迭代。
3. 面向对象填空题- 在Java中,用于创建对象实例的关键字是_ _ _ _ _。
- 答案:new- 解析:使用new关键字可以在内存中分配空间来创建一个新的对象实例。
- Java中的继承关键字是_ _ _ _ _。
- 答案:extends- 解析:extends关键字用于表示一个类继承自另一个类,子类可以继承父类的属性和方法。
- 在Java中,用于表示一个类不可以被继承的关键字是_ _ _ _ _。
- 答案:final- 解析:使用final关键字可以表示一个类不可以被继承,或者表示一个方法不可以被重写。
4. 异常处理填空题- 在Java中,用于捕获异常的关键字是_ _ _ _ _。
JA V A考试题一填空题1 定义类用的关键字是class;继承类用的关键子是__extends_____;实现接口用的关键字是_implements________2 线程的五种状态分别是产生就绪__运行_____ 阻塞_______ 结束;3 对窗口的监听用的是________接口,该接口中的方法较多,为了便于实现该接口,可以使用__________;4 Panel和Applet容器的默认布局模式是_FlowLayout_______,Window、Frame、Dialog容器的默认布局模式是_______.5 开发与运行JAVA程序需要经过的三个主要步骤为编写源程序, _编译生成字节码___________和__解释运行字节码。
____________。
6 JA V A中类成员的限定词有以下几种:public , __protected________ ,默认和private。
其中,_private_________ 的开放范围最小。
7 在面向对象概念中,每个对象都是由__属性_________和_方法_________两个最基本的部分组成的。
8 一个if语句可以跟随__多_________个else if语句,但是只能有一个__else________语句。
9 在Java的基本数据类型中,char型采用Unicode编码方案,每个Unicode码占用__2个_______字节内存空间,这样,无论是中文字符还是英文字符,都是占用____2个_____字节内存空间。
10 程序的错误分为两种_________ ,__________。
11 面向对象的三大特征分别是继承,__多态___________ 和__抽象和封装__________。
12 JA V A中类成员的限定词有以下几种:private, public, ___________ , __________ 。
13 若x = 5,y = 10,则x < y和x >= y的逻辑值分别为__true ______和___false________。
JA V A试题(100道)——————————————————————————————————————题目1:下面不属于基本类型的是:c (选择1项)A) booleanB) longC) StringD) byte题目2:d如下程序中:(1)public class CharToInt(2){(3)public static void main(String[] args)(4){(5)int a,b=10;(6)char c='语';(7)a=b+c;(8)System.out.println("结果是:"+a);(9)}(10)}下面的说法中正确的是:(选择1项)A) 程序运行到第6行会出现编译错误,因为c为char类型,不能把一个汉字赋给一个字符型。
B) 程序运行到第7行会出现编译错误,因为字符型与整型不能进行加法运算。
C) 程序能够正常编译和运行,但是不会输出任何结果。
D) 程序将字符型转换为unicode编码并和b的数值相加的和输出。
题目3:下面的说法中,错误的是:d(选择1项)A) 在Java中标志符可以是字母、下划线、数字或$符号。
B) 在Java中布尔类型不能和数字之间不能来回转换,即false和true不对应任何零或非零的值。
C) 双精度类型double比单精度类型float具有更高的精度和更大的表示范围,但float类型具有速度快、占用内存小的优点。
D) 在Java中布尔值可以用true或false来表示,但是同时也可以用1或0来表示。
题目5:b程序如下:class Test{public static void main(String[] args){int i=1000;for(;;){if(i>999){_______________}}}}下面对程序的横线处填写不同语句的分析正确的是:(选择一项)A) 填充break 使程序能够有退出循环的可能性。
java一级考试试题及答案1. Java中哪个关键字用于声明一个类?A. classB. interfaceC. structD. package答案:A2. 在Java中,下列哪个选项是正确的数据类型?A. intB. integerC. stringD. bool答案:A3. Java程序的执行入口是?A. main方法B. constructor方法C. init方法D. start方法答案:A4. 下列哪个选项是Java中的集合类?A. ArrayListB. VectorC. Both A and BD. None of the above答案:C5. 在Java中,哪个关键字用于实现继承?A. extendsB. implementsC. inheritsD. None of the above答案:A6. Java中用于捕获异常的关键字是什么?A. tryB. catchC. throwD. Both A and B答案:D7. 下列哪个选项是Java中的访问修饰符?A. publicB. privateC. protectedD. All of the above答案:D8. 在Java中,哪个关键字用于声明一个接口?A. classB. interfaceC. abstractD. None of the above答案:B9. 在Java中,下列哪个选项是正确的方法声明?A. public void myMethod();B. public int myMethod;C. public void myMethod;D. public int myMethod()答案:A10. Java中用于定义一个常量的关键字是什么?A. finalB. constC. staticD. Both A and C答案:A。
Java试题1) java程序中,main方法的格式正确的是()。
(选择一项)a)static void main(String[] args)b)public void main(String[] args)c)public static void main(String[]s)d)public static void main(String[] args)2)给定java代码,如下:public byte count(byte b1,byte b2){return______;}要使用这段代码能够编译成功,横线处可以填入()。
(选择一项)a)(byte) (b1-b2)b)(byte) b1-b2c) b1-b2d) (byte) b1/b23)在Java中,在包com.db下定义一个类,要让包com.util下的所有类都可以访问这个类,这个类必须定义为()。
(选择一项)a)protectedb)privatec)publicd)friendly4)在Java中,下列()语句不能通过编译。
(选择一项)a) String s= “join”+ “was”+ “here”;b) String s= “join”+3; “”+new Person() toString()c) int a= 3+5d) float f=5+5.5; 5.5 double 5.5f float6)给定java代码如下,运行时,会产生()类型的异常。
(选择一项)String s=null;s.concat(“abc”);a)ArithmeticExceptionb)NullPointerExceptionc)IOExceptiond)EOFException 已到文件尾,再读取抛出7) 在java中,()对象可以使用键/值的形式保存数据。
(选择一项)a)ArrayList List 有序可重复b) HashSet Set 无序不可重复同一对象是重复的c) HashMap Map(key/value)重复定义:hashCode、equals(业务)d) LinkedList List8)给定如下java代码,编译运行之后,将会输出()。
《Java 程序设计》综合习题一一、单项题1.使用Java 语言编写的源程序保存时的文件扩展名是( )。
(A ).class (B ).java (C ).cpp (D ).txt3.设有数组的定义int[] a = new int[3],则下面对数组元素的引用错误的是( )。
(A )a[0]; (B )a[a.length-1]; (C )a[3]; (D )int i=1; a[i];4.在类的定义中可以有两个同名函数,这种现象称为函数( )。
(A )封装 (B )继承(C )覆盖 (D )重载 5.在类的定义中构造函数的作用是( )。
(A )保护成员变量 (B )读取类的成员变量 (C )描述类的特征 (D )初始化成员变量6.下面关键字中,哪一个不是用于异常处理语句( )。
(A )try (B )break (C )catch (D )finally7.类与对象的关系是( )。
(A )类是对象的抽象 (B )对象是类的抽象 (C )对象是类的子类 (D )类是对象的具体实例 8.下面哪一个是Java 中不合法的标识符( )。
(A )$persons (B )twoNum(C )_myVar (D )*point 9.为AB 类的一个无形式参数无返回值的方法method 书写方法头,使得使用类名AB 作为前缀就可以调用它,该方法头的形式为( )。
(A )static void method( ) (B )public void method( )(C )final void method( ) (D )abstract void method( )10.欲构造ArrayList 类的一个实例,此类继承了List 接口,下列哪个方法是正确的( )。
(A )ArrayList myList=new Object( ) (B )List myList=new ArrayList( )(C )ArrayList myList=new List( ) (D )List myList=new List( )11.Java 源文件和编译后的文件扩展名分别为( )(A).class 和 .java(B ).java 和 .class(C).class 和 .class(D) .java 和 .java12.对于一个Java 源文件,import, class 定义以及package 正确的顺序是: (A ) package,import,class(B) class,import,package (C) import,package,class (D) package,class,import13.下面哪个是非法的:(A) int I = 32;(B) float f = 45.0; //应为float f=45.0f; (C) double d = 45.0;(D ) char c = ‘u’;14.Java 语言使用的字符码集是(A) ASCII (B) BCD(C) DCB (D ) Unicode15. 如果一个类的成员变量只能在所在类中使用,则该成员变量必须使用的修饰是(A) public (B) protected (C ) private (D) static16.下面关于main 方法说明正确的是(A) public main(String args[ ])(B ) public static void main(String args[ ]) (C) private static void main(String args[ ])(D) void main()17.关于抽象方法的说法正确的是( )(A)可以有方法体 (B) 可以出现在非抽象类中(C) 是没有方法体的方法(D ) 抽象类中的方法都是抽象方法18.Javaapplication 中的主类需包含main 方法,以下哪项是main 方法的正确形参?( )A 、 String argsB 、String args[]C 、Char argD 、StringBuffer args[]19.以下代码段执行后的输出结果为( )int x=-3; int y=-10;System.out.println(y%x);A 、 -1B 、2C 、1D 、320.以下关于继承的叙述正确的是( )。
Part 1: Single-choice questions(15 points)1.The constructor of a class that adheres to the Singleton design pattern musthave _____ visibility.A. privateB. protectedC. packageD. public2.Which method is used to define the execution body of a thread?A. start()B. init()C. run()D. main()3.Which modifier should be applied to a method for the lock of the object this to be obtained prior to executing any of the method body?A. finalB. staticC. abstractD. protectedE. synchronized4. A Button is positioned in a Frame. Its size is not affected whenthe Frame is resized. Which layout manager should be used?A. FlowLayoutB. North and South of BorderLayoutC. East and West of BorderLayoutD. GridLayout5.Given the following code:1) class Example{2) String str;3) public Example(){4) str= "example";5) }6) public Example(String s){7) str=s;8) }9) }10) class Demo extends Example{11) }12) public class Test{13) public void f () {14) Example ex = new Example("Good");15) Demo d = new Demo("Good");16) }Which line will cause an error?A. line 3B. line 6C. line 10D. line 14E. line 156.What would be the result of attempting to compile and run the following piece of code?public class Test {public int x;public static void main(String args[]){System.out.println("Value is " + x);}}A. The output "Value is 0" is printed.B. Non-static variable x cannot be referenced from a static context..C. An "illegal array declaration syntax" compiler error occurs.D. A "possible reference before assignment" compiler error occurs.E. An object of type ArrayIndexOutOfBoundsException is thrown.7. You need to create a class that will store a unique object elements.You do not need to sort these elements but they must be unique. What interface might be most suitable to meet this need?A. SetB. ListC. MapD. Vector8. You have these files in the same directory. What will happen when you attempt to compile and run Class1.java//Base.javapackage Base;class Base{protected void amethod(){System.out.println("amethod");}}//Class1.javapackage Class1;public class Class1 extends Base{public static void main(String argv[]){Base b = new Base();b.amethod();}}A. Compile Error: Methods in Base not foundB. Compile Error: Unable to access protected method in base classC. Compilation followed by the output "amethod"D. Compile error: Class1 can not find class Base9.In which of the following ways can items be added to a collection implemented by java.util.ArrayList?I .Items can be inserted at the beginning of the collection.II .Items can be inserted between two existing items in the collection.III .Items can be appended to the end of the collection.A. I, II, and IIIB. I and III onlyC. I onlyD. III only10. Assuming any exception handling has been set up, which of the following will create an instance of the RandomAccessFile classA. RandomAccessFile raf=new RandomAccessFile("myfile.txt","rw");B. RandomAccessFile raf=new RandomAccessFile( newDataInputStream());C. RandomAccessFile raf=new RandomAccessFile("myfile.txt");D. RandomAccessFile raf=new RandomAccessFile(new File("myfile.txt")); 11.Given the following command to run a correct class: java MyTest a b cWhich statements are true?A. args[0] = "MyTest a b c"B. args[0] = "MyTest"C. args[0] = "a"D. args[1]= 'b'12.Which of the following statements are true about a variable created with the static modifier?A.Once assigned the value of a static variable may not be alteredB. A static variable created in a method will keep the same value between callsC. Only one instance of a static variable will exist for any amount of class instancesD. The static modifier can only be applied to a primitive value13.Which of the following statements are true?A.The elements in a Java array can only be of primitive types, not objectsB. Arrays are initialized to default values wherever they are createdC. An array may be dynamically resized using the setSize methodD. You can find out the size of an array using the size method14. A design pattern is typically used toA. allow the use of object-orientated concepts in a language that is not object-orientedB. describe a practical solution to a common design problemC. ensure that code executes at optimal speed during runtimeD. reduce the number of classes in the design of a program15. Which of the following is a Java event that is generated when a JButton component is pressed?A. ClickEventB. WindowEventC. ActionEventD. ButtonEventPart 2: Multi-choice questions(30 points)1. What might cause the current thread to stop executing.A. An InterruptedException is thrown.B. The thread executes a wait() call.C. The thread constructs a new Thread.D. A thread of higher priority becomes ready.2. Which of the following method can be used to define a new thread class?A. implement the Runnable interfaceB. add a run() method in the classC. create an instance of ThreadD. extend the Thread class3. Given the following method body://you add code here{if (sometest()) {unsafe();}else {safe();}}The method "unsafe" might throw an IOException (which is not a subclass of RunTimeException). Which correctly completes the method of declaration when added at line one?A. public void methodName() throws ExceptionB. public void methodname()C. public void methodName() throw IOExceptionD. public void methodName() throws IOExceptionE. public IOException methodName()4.Which classes are used to implement TCP/IP client and servers?A. ServerSocketB. ServerC. SocketD. DatagramPacketE. DatagramSocket5. Which of the following event listeners have event adapters defined in Java?A. MouseListenerB. KeyListenerC. ActionListenerD. ItemListenerE. WindowListener6. Which statements of the event listener are true?A. Multiple listeners can be attached to one component.B. Only one listener can be attached to one component.C. One listener can receive and process the events from multiple components.D. One listener can receive and process the events from only one component.7.Which of the following can be parts of the Java source file Calculation1.java?A. public class Calculation1{//...}B. import java.io.*;C. package myPackage;D. import java.awt.*;E. static class Arg{//...}F. class Calcu{//...}G. public class Calculation2{//...}8. public void fun (){int i;try{i=System.in.read ();System.out.println("Location 1");} catch (IOException e) {System.out.println("Location 2");} finally {System.out.println("Location 3");}System.out.println("Location 4");}If an IOException occurs, what will be printed?A. Location 1B. Location 2C. Location 3D. Location 49. The Person, Student and Teacher are class names. These classes have the following inheritance relation : Student and Teacher extend Person. In Java source file a specific method has an argument. In order to handle all these classes in this method which type of argument of this method should be used?A. PersonB. StudentC. TeacherD. ObjectE. None of them can be used.10.Which statements are correct?A. In Java single inheritance is allowed, which makes code more reliable.B. A subclass inherits all methods ( including the constructor ) from the superclass.C. A class can implement as many interfaces as needed.D. When a class implements an interface, it can define as many methods of the interface as needed.11. Consider the following code:Integer s = new Integer(9);Integer t = new Integer(9);Long u = new Long(9);Which test would return true?A. (s==u)B. (s==t)C. (s.equals(t))D. (s.equals(9))E. (s.equals(new Integer(9))12. Which of the following statements are true?A. If a class has any abstract methods it must be declared abstract itself.B. All methods in an abstract class must be declared as abstractC. When applied to a class, the final modifier means it cannot be sub-classedD. An interface cannot be instantiated13. Which of the following statements are true?A. The default constructor has a return type of voidB. The default constructor takes a parameter of voidC. The default constructor takes no parametersD. The default constructor is not created if the class has any constructors of its own.14. Which methods are correct overloading methods of the following method:public void example(){...}A. public void example( int m){...}B. public int example(){...}C. public void example2(){...}D. public int example ( int m, float f){...}15. Which one of the following is correct to create an array?A. float f[][] = new float[6][6];B. float f[][] = new float[][6];C. float [][]f = new float[6][6];D. float [][]f = new float[6][];Part 3: Read Programs(15 points)Please reading the following program carefully, give the correct answer and explain why.1. (7 points)public class Exam1 {public static int counter;static{System.out.println("Static " + counter);counter = 10;}public static int getTotalCount() {return counter;}public int getCount() {return counter;}public Exam1() {counter += 10;System.out.println("Constructor "+counter);}public static void main(String[] args) {System.out.println("Number of counter is " +Exam1.getTotalCount());Exam1 count1 = new Exam1();System.out.println("getCounter " + count1.getCount());Exam1 count2 = new Exam1();count2.counter++;System.out.println("getTotalCount " + count2.getTotalCount()); }}2. (8 points)//class Animalclass Animal {private String type;public Animal() {type = "An Animal";System.out.println("Type: "+ type);}public Animal(String aType) {type = new String(aType);System.out.println("Type: "+ type);}public String toString() {return "This is a " + type;}}//class Dogclass Dog extends Animal {private String name;private String breed;public Dog(String aName) {super("Dog");name = aName;breed = "Unknown";}public Dog(String aName, String aBreed) {name = aName;breed = aBreed;}public String toString() {return super.toString()+ "," + this.breed;}}//class Exam2public class Exam2 {public static void main(String[] args) {Dog aDog = new Dog("Fido", "Chihuahua");Animal animal = new Dog("Lassie");System.out.println(aDog.toString());System.out.println(animal.toString());}}Part 4: Programming(40 points)1(30 points)Please design an application based on the following specifications:a) Class Person has 2 attributes: id and nameb) Class Project models project, has 3 basic attributes:projectID , projectName ,startDate.c) Class ProjectTask models a engineer’s task of a project,has 3 basic attributes: engineerID, engineerName, engineerT askd) A Engineer is a Persone) A Engineer can take many projectsf) A projcet contains many engineers tasks (Tips: cantains aattribute which type is ArrayList<ProjectTask>)g) The tasks of all engineers in a project exist in a Plain Text File,the format of each task is:“engineerID_engineerName_engineerTask”,for example, “0318_zhang_module3”.h) Project project1’s tasks file name is “project1.txt” , Projectproject2’s tasks file name is “project2.txt”,and so on.Here is what you should do:1) Draw the UML Class Diagram according the specifications2) Complete the following Classes in Java:Person , Engineer, ProjectTask , Project3) Define a method in class Project for reading the tasks of eachproject from specified file name, and save them in Arraylist<ProjectTask> tasks. using java.util.StringTokenizer to getengineerID, engineerName, engineerT askThe signature of this method is:public void getTasks(String aProjectTasksFile)4) Design a GUI Class in Java, using BorderLayout, the left is thelist of all Project Name, the right is a TextArea.5) Complete the following event processing inner Class in Java,When user selects a Project Name, show all tasks of the project in the TextArea, each line will be a task of a engineer.class InnerDisplayListener implementsListSelectionListener{ public void valueChanged(ListSelectionEvent event) { //you code here}}。