当前位置:文档之家› JAVA编程思想(第四版)练习题答案 unit 2 --- Think in JAVA

JAVA编程思想(第四版)练习题答案 unit 2 --- Think in JAVA

JAVA编程思想(第四版)练习题答案 unit 2 --- Think in JAVA
JAVA编程思想(第四版)练习题答案 unit 2 --- Think in JAVA

// TIJ4 Chapter Object, Exericise 3, page 90

// object/ATNTest.java

// Find the code fragments involving ATypeName and turn them into a program

// that compiles and runs.

public class ATNTest {

public static void main(String[] args) {

class ATypeName {

int i;

double d;

boolean b;

void show() {

System.out.println(i);

System.out.println(d);

System.out.println(b);

}

}

ATypeName a = new ATypeName();

a.i = 3;

a.d = 2.71828;

a.b = false;

a.show();

}

}

-------------------------------------------------

// TIJ4 Chapter Object, Exercise 9, page 90

// Write a program that demonstrates that autoboxing works for all the primitive // types and their wrappers.

public class AutoboxTest {

public static void main(String[] args) {

boolean b = false;

char c = 'x';

byte t = 8;

short s = 16;

int i = 32;

long l = 64;

float f = 0.32f;

double d = 0.64;

Boolean B = b;

System.out.println("boolean b = " + b);

System.out.println("Boolean B = " + B);

Character C = c;

System.out.println("char c = " + c);

System.out.println("Character C = " + C);

Byte T = t;

System.out.println("byte t = " + t);

System.out.println("Byte T = " + T);

Short S = s;

System.out.println("short s = " + s);

System.out.println("Short S = " + S);

Integer I = i;

System.out.println("int i = " + i);

System.out.println("Integer I = " + I);

Long L = l;

System.out.println("long l = " + l);

System.out.println("Long L = " + L);

Float F = f;

System.out.println("float f = " + f);

System.out.println("Float F = " + F);

Double D = d;

System.out.println("double d = " + d);

System.out.println("Double D = " + D);

}

}

---------------------------------------------------------

// TIJ4 Chapter Object, Exercise 10, page 90

// Write a program that prints three arguments taken from the command line. To do // this you'll need to index into the command-line array of Strings.

public class CommandArgTest {

public static void main(String[] args) {

System.out.println("args[0] = " + args[0]);

System.out.println("args[1] = " + args[1]);

System.out.println("args[2] = " + args[2]);

}

}

--------------------------------------------------

// object/DataOnlyTest.java

// TIJ4 Chapter Object Exercise 4 page 90

// Turn the DataOnly code fragments into a program that compiles and runs

public class DataOnlyTest {

public static void main(String[] args) {

class DataOnly {

int i;

double d;

boolean b;

void show() {

System.out.println(i);

System.out.println(d);

System.out.println(b);

}

}

DataOnly data = new DataOnly();

data.i = 3;

data.d = 2.71828;

data.b = false;

data.show();

}

}

--------------------------------------------------

// object.DocTest.java

// TIJ4 Chapter Object, Exercise 12, page 90

/* Find the code for the second version of HelloDate.java, which is the simple * comment documentation example. Execute Javadoc on the file and view the * results with your Web browser.

*/

import java.util.*;

/** The first Thinking in Java example program.

* Displays a string and today's date.

* @author Burce Eckel

* @author https://www.doczj.com/doc/7417488718.html,

* @version 4.0

*/

public class DocTest {

/** Entry poing to class & application.

* @param args array of string arguments

* @throws exceptions No exceptions thrown

*/

public static void main(String[] args) {

System.out.println("Hello, it's: ");

System.out.println(new Date());

}

} /* Output: (55% match)

*/

----------------------------------------------------

// object.Documentation1.java

// TIJ4 Chapter Object, Exercise 13 - 1

/* Run Documentation1.java, Documentation2.java and Documentation3.java * through Javadoc. Verify the resulting documentation with your Web browser. */

/** A class comment */

public class Documentation1 {

/** A field comment */

public int i;

/** A method comment */

public void f() {}

}

---------------------------------------------------

// object.Documentation1.java

// TIJ4 Chapter Object, Exercise 13 - 2

/* Run Documentation1.java, Documentation2.java and Documentation3.java * through Javadoc. Verify the resulting documentation with your Web browser. */

import java.util.*;

// object/Documentation2.java

/**

*

* Uses

* System.out.println(new Date());

*

*/

public class Documentation2 {

Date d = new Date();

void showDate() {

System.out.println("Date = " + d);

}

}

---------------------------------------------------

// object.Documentation1.java

// TIJ4 Chapter Object, Exercise 13 - 3

/* Run Documentation1.java, Documentation2.java and Documentation3.java * through Javadoc. Verify the resulting documentation with your Web browser. */

import java.util.*;

// object/Documentation3.java

/**

* You can even insert a list:

*

    *

  1. Item one

    *

  2. Item two

    *

  3. Item three

    *

*/

public class Documentation3 {

public static void main(String[] args) {

Date d = new Date();

System.out.println("d = " + d);

}

}

---------------------------------------------------

// object/Documentation4.java

// TIJ4 Chapter Object, Exercise 14, page 90

// Add an HTML list of items to the documentation in the previous exercise. import java.util.*;

// object/Documentation4.java

/**

* You can even insert a list:

*

    *

  1. Item one

    *

  2. Item two

    *

  3. Item three

    *

* Another test list

*

    *

  1. One

    *

  2. Two

    *

  3. Three

    *

*/

public class Documentation4 {

/** Let's try a public field list

*

    *

  1. One

    *

  2. Two

    *

  3. Three

    *

*/

public int i = 2;

/**

* A private field list (-private to see)

*

    *

  1. One

    *

  2. Two

    *

  3. Three

    *

*/

private int j = 3;

/**

* Another list can be inserted here to help explain the

* following method call

*

    *

  1. One

    *

  2. Two

    *

  3. Three

    *


* but may be formatted differently in Method Summary

*/

public static void main(String[] args) {

/**

* Let's try another test list here

*

    *

  1. One

    *

  2. Two

    *

  3. Three

    *

*/

Date d = new Date();

System.out.println("d = " + d);

}

}

--------------------------------------------------

// object/DOTest2.java

// TIJ4 Chapter Object, Exercise 5, page 90

// Modify the previous exercise so that the values of the data in DataOnly are

// assigned to and printed in main().

public class DOTest2 {

public static void main(String[] args) {

class DataOnly {

int i;

double d;

boolean b;

void show() {

System.out.println(i);

System.out.println(d);

System.out.println(b);

}

}

DataOnly data = new DataOnly();

data.i = 234;

data.d = 2.1234545;

data.b = true;

data.show();

}

}

----------------------------------------------------

// object/HelloDocTest.java

// TIJ4 Chapter Object, Exercies 15, page 91

/* Take the program in Exercise 2 and add comment documentation to it. Extract * this comment documentation into an HTML file using Javadoc and view it with * your Web browser.

*/

/**

* Public class contained in file of the same name that includes main()

*/

public class HelloDocTest {

/** main method executed by java

*/

public static void main(String[] args) {

System.out.println("Hello World!");

}

}

---------------------------------------------------

// TIJ4 Chapter Object, Exericise 2, page 89

// object/HelloWorld.java

// Following the HelloDate.java example in this chapter, create a "hello, world" // program that simply displays that statement.

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

}

}

---------------------------------------------------

// object/ITest.java

// TIJ4 Chapter Object, Exercise 7, page 90

// Turn the Incrementable code fragments into a working program.

class StaticTest {

static int i = 47;

}

class Incrementable {

static void increment() { StaticTest.i++; }

}

public class ITest {

public static void main(String[] args) {

System.out.println("StaticTest.i= " + StaticTest.i);

StaticTest st1 = new StaticTest();

StaticTest st2 = new StaticTest();

System.out.println("st1.i= " + st1.i);

System.out.println("st2.i= " + st2.i);

Incrementable sf = new Incrementable();

sf.increment();

System.out.println("After sf.increment() called: ");

System.out.println("st1.i = " + st1.i);

System.out.println("st2.i = " + st2.i);

Incrementable.increment();

System.out.println("After Incrementable.increment called: ");

System.out.println("st1.i = " + st1.i);

System.out.println("st2.i = " + st2.i);

}

}

----------------------------------------------------

// object/OneStaticTest.java

// TIJ4 Chapter Object, Exercise 8, page 90

/* Write a program that demonstrates that, no matter how many objects you * create of a particular class, there is only one instance of a particular

* static field of that class.

*/

class StaticTest {

static int i = 47;

}

class Incrementable {

static void increment() { StaticTest.i++; }

}

public class OneStaticTest {

public static void main(String[] args) {

System.out.println("StaticTest.i= " + StaticTest.i);

StaticTest st1 = new StaticTest();

StaticTest st2 = new StaticTest();

System.out.println("st1.i= " + st1.i);

System.out.println("st2.i= " + st2.i);

Incrementable.increment();

System.out.println("After Incrementable.increment() called: ");

System.out.println("st1.i = " + st1.i);

System.out.println("st2.i = " + st2.i);

Incrementable.increment();

System.out.println("After Incrementable.increment called: ");

System.out.println("st1.i = " + st1.i);

System.out.println("st2.i = " + st2.i);

st1.i = 3;

System.out.println("After st1.i = 3, ");

System.out.println("st1.i = " + st1.i);

System.out.println("st2.i = " + st2.i);

System.out.println("Create another StaticTest, st3.");

StaticTest st3 = new StaticTest();

System.out.println("st3.i = " + st3.i);

}

}

-----------------------------------------------------

// object/Overloading.java

// TIJ4 Chapter Object, Exercise 16, page 91

/* In the Initialization and Cleanup chapter, locate the Overloading.java

* example and add Javadoc documentation. Extract this comment documentation * into and HTML file using Javadoc and view it with your Web browser.

*/

// initialization/Overloading.java

// Demonstration of both constructor

// and ordinary method overloading.

/** creates type Tree wth two constructors and one info method

*/

class Tree {

int height;

/** no-argument constructor

* assigns height = 0

*/

Tree() {

System.out.println("Planting a seedling");

height = 0;

}

/** constructor taking an int argument,

* assigns height that int argument

*/

Tree(int initialHeight) {

height = initialHeight;

System.out.println("Creating new tree that is " + height + " feet tall");

}

/** method to print height of tree object

*/

void info() {

System.out.println("Tree is " + height + " feet tall");

}

/** overloaded method to print string argument

* and height of a tree object

*/

void info(String s) {

System.out.println(s + ": Tree is " + height + " feet tall");

}

}

/** class to test construction of tree objects

*/

public class Overloading {

public static void main(String[] args) {

for(int i = 0; i < 5; i++) {

Tree t = new Tree(i);

https://www.doczj.com/doc/7417488718.html,();

https://www.doczj.com/doc/7417488718.html,("overloading method");

}

// Overloaded constructor:

new Tree();

}

}

----------------------------------------------------

JAVA练习题含答案-answers to practice 2

Multiple Choice 1)Which operator is used to concatenate two strings? (a)+ (b)– (c)* (d)/ Answer: A (see page 35) 2)Which operator returns the remainder of integer division? (a)% (b)/ (c)* (d)none of the above Answer: A (see page 26) 3)What is the value of the variable c in the statements that follow? String phrase = "Make hay while the sun is shining."; char c = phrase.charAt(10); (a)w (b)h (c)i (d)None of the above Answer: B (see page 40) 4)The escape sequence the represents the new-line character is: (a)\r (b)\t (c)\n (d)\\ Answer: C (see page 43) 5)The syntax that declares a Java named constant named SALES_TAX is: (a)double SALES_TAX = 7.50; (b)public double SALES_TAX = 7.50; (c)public static double SALES_TAX = 7.50; (d)public static final double SALES_TAX = 7.50; Answer: D (see page 47)

JAVA多线程试题 答案

多线程 一.选择题 1.下列说法中错误的一项是(A) A.线程就是程序 B.线程是一个程序的单个执行流 B.多线程是指一个程序的多个执行流D.多线程用于实现并发 2.下列哪个一个操作不能使线程从等待阻塞状态进入对象阻塞状态(D) A.等待阴塞状态下的线程被notify()唤 B.等待阻塞状态下的纯种被interrput()中断 C.等待时间到 D.等待阻塞状态下的线程调用wait()方法 3.下列哪个方法可以使线程从运行状态进入其他阻塞状态(A) A.sleep B.wait C.yield D.start 4.下列说法中错误的一项是(D) A.一个线程是一个Thread类的实例 B.线程从传递给纯种的Runnable实例run()方法开始执行 C.线程操作的数据来自Runnable实例 D.新建的线程调用start()方法就能立即进入运行状态 5.下列关于Thread类提供的线程控制方法的说法中,错误的一项是(D) A.在线程A中执行线程B的join()方法,则线程A等待直到B执行完成 B.线程A通过调用interrupt()方法来中断其阻塞状态 C.若线程A调用方法isAlive()返回值为true,则说明A正在执行中 D.currentThread()方法返回当前线程的引用 6.下列说法中,错误的一项是() A.对象锁在synchronized()语句执行完之后由持有它的线程返还 B.对象锁在synchronized()语句中出现异常时由持有它的线程返还 C.当持有锁的线程调用了该对象的wait()方法时,线程将释放其持有的锁 D.当持有锁的线程调用了该对象的构造方法时,线程将释放其持有的锁 7.下面的哪一个关键字通常用来对对象的加锁,从而使得对对象的访问是排他的A A.sirialize B transient C synchronized D static 二.填空题 1.在操作系统中,被称做轻型的进程是线程 2.多线程程序设计的含义是可以将一个程序任务分成几个并行的任务 3.在Java程序中,run()方法的实现有两种方式:实现Runnable接口和继承Thread类 4.多个线程并发执行时,各个线程中语句的执行顺序是确定的,但是线程之间的相对执行顺序是不确定的 6.Java中的对象锁是一种独占的排他锁 7.程序中可能出现一种情况:多个线种互相等待对方持有的锁,而在得到对方的锁之前都不会释放自己的锁,这就是死锁 8.线程的优先级是在Thread类的常数MIN_PRIORITY和MAX_PRIORITY 之间的一个值 9.处于新建状态的线程可以使用的控制方法是start()和stop()。 10.一个进程可以包含多个线程

java期末考试试题及答案

1.谈谈final, finally, finalize的区别。 final关键字: a) 如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承。因此一个类不能既被声明为abstract的,又被声明为final的。 b) 将变量或方法声明为final,可以保证它们在使用中不被改变。 c) 被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。 d) 被声明为final的方法也同样只能使用,不能重载。 finally关键字:在异常处理时提供finally 块来执行任何清除操作。如果抛出一个异常,那么相匹配的catch 子句就会执行,然后控制就会进入finally 块。 finalize:方法名,不是关键字。Java技术允许使用finalize() 方法在垃圾收集器将对象从内存中清除出去之前做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。它是在Object 类中定义的,因此所有的类都继承了它。子类覆盖finalize() 方法以整理系统资源或者执行其他清理工作。finalize()方法是在垃圾收集器删除对象之前对这个对象调用的。 2.GC是什么? 为什么要有GC? GC是垃圾收集器。Java 程序员不用担心内存管理,因为垃圾收集器会自动进行管理。要请求垃圾收集,可以调用下面的方法之一: System.gc() Runtime.getRuntime().gc() 3.Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 写程序Math.round(11.5) = 12 Math.round(-11.5) = -11 4.给我一个你最常见到的runtime exception ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, ImagingOpException, IndexOutOfBoundsException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException

Java笔试题及答案

Java笔试题及答案 一、单项选择题 1.Java是从()语言改进重新设计。 A.Ada B.C++ C.Pasacal D.BASIC 答案:B 2.下列语句哪一个正确() A. Java程序经编译后会产生machine code B. Java程序经编译后会产生byte code C. Java程序经编译后会产生DLL D.以上都不正确 答案:B 3.下列说法正确的有() A. class中的constructor不可省略 B. constructor必须与class同名,但方法不能与class同名 C. constructor在一个对象被new时执行 D.一个class只能定义一个constructor 答案:C 详解:见下面代码,很明显方法是可以和类名同名的,和构造方法唯一的区别就是,构造方法没有返回值。 package net.study; public class TestConStructor { public TestConStructor() {

} public void TestConStructor() { } public static void main(String[] args) { TestConStructor testConStructor = new TestConStructor(); testConStructor.TestConStructor(); } } 4.提供Java存取数据库能力的包是() 答案:A 5.下列运算符合法的是() A.&& B.<> C.if D.:= 答案:A 详解: java 中没有<> := 这种运算符,if else不算运算符 6.执行如下程序代码 a=0;c=0; do{ --c; a=a-1; }while(a>0); 后,C的值是()

JAVA测试题(三套含答案)

1.在单一文件中import、class和package的正确出现顺序是:A A.package, import, class B.class, import, package C.import, package, class D.package, class, import 2.public static void main方法的参数描述是:A,D A.String args[] B.Strings args[] C.String args D.String []args 3.下面哪行代码会在编译的时候出现错误或警告:A,B,C,D A.float f=; B.char c="a"; C.byte b=257; D.boolean b=null; E.int i=10; 4. 编译,运行下列代码后的结果是:D public class Test { public static void main (String args []) { int age; age = age + 1; "The age is " + age); } } A.编译,运行后没有输出 B.编译,运行后输出:The age is 1 C.能通过编译,但运行时产生错误 D.不能通过编译 4.下面那些关键字不是JAVA的关键字B A if B then C goto D while E case 5.下面那些不是合法的变量定义:A,F C. _whatavariable D. _3_

6.内部数据类型byte的取值范围是B - 65, 535 B.(–128) – 127 C.(–32,768) – 32,767 D.(–256) – 255 7.下列哪些表达式返回true AB A."john" == "john" B."john".equals("john") C."john" = "john" D."john".equals(new Button("john")) 8.声明公用的abstract方法的正确格式是A A.public abstract void add(); B.public abstract void add() {} C.public abstract add(); D.public virtual add(); 9.下面的代码能打印出的结果为:C int i=1; switch (i) { case 0: "zero"); break; case 1: "one"); case 2: "two"); default: "default"); } B.one, default C.one, two, default D.default 10.下面的代码能打印出的结果为:B int i=9; switch (i) { default: "default"); case 0:

java线程练习题及答案

线程与线程类 1 线程的概念 线程的概念来源于计算机的操作系统的进程的概念。进程是一个程序关于某个数据集的一次运行。也就是说,进程是运行中的程序,是程序的一次运行活动。 线程和进程的相似之处在于,线程和运行的程序都是单个顺序控制流。有些教材将线程称为轻量级进程(light weight process)。线程被看作是轻量级进程是因为它运行在一个程序的上下文内,并利用分配给程序的资源和环境。 作为单个顺序控制流,线程必须在运行的程序中得到自己运行的资源,如必须有自己的执行栈和程序计数器。线程内运行的代码只能在该上下文内。因此还有些教程将执行上下文(execution context)作为线程的同义词。 所有的程序员都熟悉顺序程序的编写,如我们编写的名称排序和求素数的程序就是顺序程序。顺序程序都有开始、执行序列和结束,在程序执行的任何时刻,只有一个执行点。线程(thread )则是进程中的一个单个的顺序控制流。单线程的概念很简单,如图1所示。 多线程(multi-thread )是指在单个的程序内可以同时运行多个不同的线程完成不同的任务,图2说明了一个程序中同时有两个线程运行。 图1 单线程程序示意图 图2 多线程程序示意图 有些程序中需要多个控制流并行执行。例如, for(int i = 0; i < 100; i++) System.out.println("Runner A = " + i); for(int j = 0; j < 100; j++ ) System.out.println("Runner B = "+j); 上面的代码段中,在只支持单线程的语言中,前一个循环不执行完不可能执行第二个循环。要使两个循环同时执行,需要编写多线程的程序。 很多应用程序是用多线程实现的,如Hot Java Web 浏览器就是多线程应用的例子。在Hot Java 浏览器中,你可以一边滚动屏幕,一边下载Applet 或图像,可以同时播放动画和声音等。 2 Thread 类和Runnable 接口 多线程是一个程序中可以有多段代码同时运行,那么这些代码写在哪里,如何创建线程对象呢? 首先,我们来看Java 语言实现多线程编程的类和接口。在https://www.doczj.com/doc/7417488718.html,ng 包中定义了Runnable 接口和Thread 类。

JAVA期末试题及答案

Java 程序设计》课程试卷 1.使用 Java 语言编写的源程序保存时的文件扩展名是( )。 (A ) .class ( B ) .java C ) .cpp ( D ) .txt 2.设 int a=-2 ,则表达式 a>>>3 的值为( )。 (A ) 0 (B )3 (C ) 8 (D )-1 3.设有数组的定义 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 ) finally 7.类与对象的关系是( )。 (A )类是对象的抽象 (B )对象是类的抽象 15. Java 语言使用的字符码集是 (A) ASCII (B) BCD (C) DCB 16. 如果一个类的成员变量 (A) public (B) (C 对象是类的子类 (D )类是对象的具体实例 )。 8.下面哪一个是 Java 中不合法的标识符( ( A )$persons ( B ) twoNum ( C )_myVar ( D )*point 9.为 AB 类的一个无形式参数无返回值的方法 ( ) 。 ( A ) static void method( ) ( B ) public void method( ) ( C ) final void method( ) ( D ) abstract void method( ) 10.欲构造 ArrayList 类的一个实例,此类继承了 ( 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 (C).class 和 .class 12. 在 Java Applet 程序用户自定义的 (A) start( ) (B) stop( ) (C) init( ) 13. 对于一个 Java 源文件, (A) package,import,class (C) import,package,class 14. 下面哪个是非法的: (A) int I = 32; (C) double d = 45.0; method 书写方法头,使得使用类名 List 接口,下列哪个方法是正确的( ) ( B).java 和 .class (D) .java 和 .java Applet 子类中,一般需要重载父类的 (D) paint( ) import, class (B) class,import,package (D) package,class,import ( ) 定义以及 package 正确的顺序是: (B) float f = 45.0; (D) char c = // 符号错 AB 作为前缀就可以调用它,该方法头的形式为 方法来完成一些画图操作。 (D) Unicode 只能 在所在类中使用 则该成员变量必须使用的修饰是

javaSE练习题2及答案

一、单选 1、(1分)下列描述中,错误的是( A )。 A.Java语言的性能要比C语言差 B.Java能够实现一次编写,处处运行 C.Java是一种面向对象的编程语言 D.Java有多线程机制 2、下列不属于java语言特点的是(B ) A.自动垃圾回收机制 B.面向过程的编程语言 C.跨平台特性 D.去除了难理解的指针等概念 3、下列不是虚拟机执行过程特点的是( A ) A.单线程 B.多线程 C.动态链接 D.异常处理 4、(2分)阅读下列代码,选出该代码段正确的文件名(B )。 class A{ void method1(){ System.out.println("Method1 in class A"); } } public class B{ void method2(){ System.out.println("Method2 in class B"); } public static void main(String[] args){ System.out.println("main() in class B"); } } A: A.java B:A.class C: B.java D: B.class 5、结构化程序设计所规定的三种基本控制结构是(C ) A、输入.处理.输出 B、树形.网形.函数 C、顺序.选择.循环 D、主程序.之程序.函数 6、下面为数组初始化正确的写法是( d ): A、double c[] = new double[]{1,2} B、double c[] = new double{1,2} C、double c[] = new double[](1,2) D、double c[] = new double(1,2) 7、(2分):下面不属于java类中的类修饰符的是(C ): A. public B. final C. extends D. abstract

java期末考试复习题及答案(1)

《Java程序设计》课程试卷 1.使用Java语言编写的源程序保存时的文件扩展名是( B )。 (A).class (B).java (C).cpp (D).txt 2.设int a=-2,则表达式a>>>3的值为( C )。 (A)0 (B)3 (C)8 (D)-1 3.设有数组的定义int[] a = new int[3],则下面对数组元素的引用错误的是( C )。 (A)a[0]; (B)a[]; (C)a[3]; (D)int i=1; a[i]; 4.在类的定义中可以有两个同名函数,这种现象称为函数( D )。 (A)封装(B)继承(C)覆盖(D)重载 5.在类的定义中构造函数的作用是( D )。 (A)保护成员变量(B)读取类的成员变量(C)描述类的特征(D)初始化成员变量 6.下面关键字中,哪一个不是用于异常处理语句( B )。 (A)try (B)break (C)catch (D)finally 7.类与对象的关系是( A )。 (A)类是对象的抽象(B)对象是类的抽象(C)对象是类的子类(D)类是对象的具体实例 8.下面哪一个是Java中不合法的标识符( D )。 (A)$persons (B)twoNum (C)_myVar (D)*point 9.为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( A )。 (A)static void method( ) (B)public void method( ) (C)final void method( ) (D)abstract void method( ) 10.欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的( C )。 (A)ArrayList myList=new Object( ) (B)List myList=new ArrayList( ) (C)ArrayList myList=new List( ) (D)List myList=new List( ) 源文件和编译后的文件扩展名分别为( B ) (A) .class和 .java (B).java和 .class (C).class和 .class (D) .java和 .java 12.在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的( D )方法来完成一些画图操作。 (A) start( ) (B) stop( ) (C) init( ) (D) paint( ) 13.对于一个Java源文件,import, class定义以及package正确的顺序是: ( A ) (A) package,import,class (B) class,import,package (C) import,package,class (D) package,class,import 14.下面哪个是非法的:( D ) (A) int I = 32; (B) float f = ; (C) double d = ; (D) char c = ‘u’;如果一个类的成员变量只能在所在类中使用,则该成员变量必须使用的修饰是( C ) (A) public (B) protected (C) private (D) static 17.下面关于main方法说明正确的是( B ) (A) public main(String args[ ]) (B) public static void main(String args[ ]) (C) private static void main(String args[ ]) (D) void main() 18.哪个关键字可以对对象加互斥锁( B ) (A) transient (B) synchronized (C) serialize (D) static 19.关于抽象方法的说法正确的是( D ) (A)可以有方法体 (B) 可以出现在非抽象类中 (C) 是没有方法体的方法(D) 抽象类中的方法都是抽象方法 包的File类是( B ) (A)字符流类(B) 字节流类 (C) 对象流类 (D) 非流类 21.Java application中的主类需包含main方法,以下哪项是main方法的正确形参( B ) A、 String args B、String args[] C、Char arg D、StringBuffer args[] 22.以下代码段执行后的输出结果为( A ) i nt x=-3; int y=-10; 、-1B、2 C、1 D、3 23.以下关于继承的叙述正确的是()。

java基础考试题及答案

新员工考试 一、选择题(共30题,每题 2 分) 1. 下面哪些是合法的标识符?(多选题) A. $persons B. TwoUsers C. *point D. this E. _endline 答案A,B,E 分析Java 的标识符可以以一个Unicode 字符,下滑线(_),美元符($)开始,后续字符可以是前面的符号和数字,没有长度限制,大小写敏感,不能是保留字(this 保留字)。 2. 哪些是将一个十六进制值赋值给一个long 型变量?(单选题) A. long number = 345L; B. long number = 0345; C. long number = 0345L; D. long number = 0x345L 答案D 分析十六进制数以Ox开头,Io ng型数以L (大小写均可,一般使用大写,因为小写的 l 和数字1 不易区分)。 3. 下面的哪些程序片断可能导致错误? (多选题) A. String s = "Gone with the wind"; String t = " good "; String k = s + t; B. String s = "Gone with the wind"; String t; t = s[3] + "one"; C. String s = "Gone with the wind"; String standard = s.toUpperCase(); D. String s = "home directory"; String t = s - "directory"; 答案B,D 分析 A:String 类型可以直接使用+进行连接运算。 B:String 是一种Object ,而不是简单的字符数组,不能使用下标运算符取其值的某个元 素,错误。 C:toUpperCase()方法是String 对象的一个方法,作用是将字符串的内容全部转换为大写并返回转换后的结果(String 类型)。 D:String 类型不能进行减(- )运算,错误。 4. point x 处的哪些声明是句法上合法的? (多选题) cIass Person { private int a; pubIic int change(int m){ return m; } } pubIic cIass Teacher extends Person { public int b;

java练习题2及答案

试题编号: 重庆邮电大学××××学年××学期 一、选择题(每题1分,共30分) 1、下列哪些语句关于内存回收的说明是正确的? () A:程序员必须创建一个线程来释放内存; B:内存回收程序负责释放无用内存; C:内存回收程序允许程序员直接释放内存; D:内存回收程序可以在指定的时间释放内存对象。 2、有下列程序: public class aa{ public static void main(String[ ] args) { String s = "18.03"; try { double number1 = Double.parseDouble(s); System.out.print(number1); int number2 = Integer.parseInt(s); System.out.println(" " + number2); }catch(NumberFormatException nfe) { System.out.println(" Sorry"); }catch(Exception e){ } } } 执行结果是() A:18.03 B:Sorry C:18.03 Sorry D:出现编译错误 3、下面哪个选项可以将“hello”字符写入文件file.txt的末尾?() A:OutputStream out= new FileOutputStream ("file.txt"); Out.writeBytes ("hello"); B:OutputStream os= new FileOutputStream ("file.txt", true); DataOutputStream out = new DataOutputStream(os); out.writeBytes ("hello"); C:OutputStream os= new FileOutputStream ("file.txt"); DataOutputStream out = new DataOutputStream(os); out.writeBytes ("hello"); D:OutputStream os= new OutputStream ("file.txt", true); DataOutputStream out = new DataOutputStream(os); ×××××试卷第1页(共××页)

java笔试题含答案

班级:_______________ 学号:______________ 姓名:___________ Java 笔试题 (可多选) 1. 下面哪些是Thread类的方法( ABD) A start() B run() C exit() D getPriority() 2. 下面关于类的说法正确的是(A) A 继承自Throwable B Serialable C 该类实现了Throwable 接口 D 该类是一个公共类 3. 下面程序的运行结果是( false ) String str1 = "hello"; String str2 = "he" + new String("llo"); == str2); 4. 下列说法正确的有( C) A. class中的constructor不可省略

B. constructor必须与class同名,但方法不能与class同名C. constructor在一个对象被new时执行 D.一个class只能定义一个constructor 5. 指针在任何情况下都可进行>, <, >=, <=, ==运算( true ) 6. 下面程序的运行结果:(B) public static void main(String args[]) { Thread t = new Thread() { public void run() { pong(); } }; (); "ping"); } static void pong() { "pong"); } A pingpong

B pongping C pingpong和pongping都有可能 D 都不输出 7. 下列属于关系型数据库的是(AB) A. Oracle B MySql C IMS D MongoDB 8. GC(垃圾回收器)线程是否为守护线程( true ) 9. volatile关键字是否能保证线程安全( false ) 10. 下列说法正确的是(AC) A LinkedList继承自List B AbstractSet继承自Set C HashSet继承自AbstractSet D WeakMap继承自HashMap 11. 存在使i + 1 < i的数吗(存在) 12. 的数据类型是(B) A float B double C Float D Double

JavaSE测试题及答案

JavaSE 内部测试题 一.选择题每题2分,共40分 1.在Java中,定义接口的关键字是()。(一项) a)abstract b)interface c)class d)extends 2.在Java中,类可派生自一个父类,并实现()个接口(一项) a) 1 b) 2 c) 3 d)任意多 3.在Java中,已定义了两个接口B和C,要定义一个类A 实现接口B和C,以下语句正 确的是(一项) a)class A extends B extends C b)class A implements B implements C c)class A implements B,C d)class A extends B,C 4.在Java中,使用interface声明一个接口时,以下()可以用来修饰该接口。(一项) a)private b)protected c)private、protected d)public 5.下列说法错误的是()。(一项) a)Java支持多重继承 b)Java可以通过实现接口,间接实现多重继承 c)Java可以实现多个接口 d)Java实现接口,必须实现接口中的方法。 6.在Java中,关于接口和抽象类,正确的是()(二项) a)public interface A{ private int varA =10; void methodA(); } b)public abstract class B { private int varA=10; void methodA(){}; } c)public interfacde C{ int varA =10; protected void methodA(); } d)public abstract class D{

Java第七单元练习题-Java多线程机制

7Java多线程机制 7.1单项选择题 1. 线程调用了sleep()方法后,该线程将进入()状态。 A. 可运行状态 B. 运行状态 C. 阻塞状态 D. 终止状态 2. 关于java线程,下面说法错误的是() A. 线程是以CPU为主体的行为 B. java利用线程使整个系统成为异步 C. 创建线程的方法有两种:实现Runnable接口和继承Thread类 D. 新线程一旦被创建,它将自动开始运行 3. 在java中的线程模型包含() A. 一个虚拟处理器 B. CPU执行的代码 C. 代码操作的数据 D. 以上都是 4.在java语言中,临界区可以是一个语句块,或者是一个方法,并用()关键字标识。 A. synchronized B. include C. import D. Thread 5. 线程控制方法中,yield()的作用是() A. 返回当前线程的引用 B. 使比其低的优先级线程执行 C. 强行终止线程 D. 只让给同优先级线程运行 6. 线程同步中,对象的锁在()情况下持有线程返回 A. 当synchronized()语句块执行完后 B. 当在synchronized()语句块执行中出现例外(exception)时 C. 当持有锁的线程调用该对象的wait()方法时 D. 以上都是 7. 在以下()情况下,线程就进入可运行状态 A. 线程调用了sleep()方法时 B. 线程调用了join()方法时

C. 线程调用了yield()方法时 D. 以上都是 8. java用()机制实现了进程之间的异步执行 A. 监视器 B. 虚拟机 C. 多个CPU D. 异步调用 类的方法中,toString()方法的作用是() A. 只返回线程的名称 B. 返回当前线程所属的线程组的名称 C. 返回当前线程对象 D. 返回线程的名称 语言具有许多优点和特点,下列选项中,哪个反映了Java程序并行机制的特点() A. 安全性 B. 多线程 C. 跨平台 D. 可移值 11.以下哪个关键字可以用来对对象加互斥锁?() A. transient B. synchronized C. serialize D. static 12.下面关于进程、线程的说法不正确的是( )。 A.进程是程序的一次动态执行过程。一个进程在其执行过程中,可以产生多个线程——多线程,形成多条执行线索。 B.线程是比进程更小的执行单位,是在一个进程中独立的控制流,即程序内部的控制流。线程本身不能自动运行,栖身于某个进程之中,由进程启动执行。 C.Java多线程的运行与平台无关。 D.对于单处理器系统,多个线程分时间片获取CPU或其他系统资源来运行。对于多处理器系统,线程可以分配到多个处理器中,从而真正的并发执行多任务。 7.2填空题 1.________是java程序的并发机制,它能同步共享数据、处理不同的事件。 2.线程是程序中的一个执行流,一个执行流是由CPU运行程序的代码、__________所形 成的,因此,线程被认为是以CPU为主体的行为。 3.线程的终止一般可以通过两种方法实现:自然撤销或者是__________. 4.线程模型在java中是由__________类进行定义和描述的。 5.线程的创建有两种方法:实现_________接口和继承Thread类。 6.多线程程序设计的含义是可以将程序任务分成几个________的子任务。 7.按照线程的模型,一个具体的线程也是由虚拟的CPU、代码与数据组成,其中代码与数 据构成了___________,线程的行为由它决定。 8.ava中,新建的线程调用start()方法、如(),将使线程的状态从New(新建状态)转换为 _________。 9.多线程是java程序的________机制,它能同步共享数据,处理不同事件。 10.进程是由代码、数据、内核状态和一组寄存器组成,而线程是表示程序运行状态的

java考试试卷及答案

JA V A考试试卷及答案 选择题 3、在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的( D )方法来完成一些画 图操作。 A. start() B. stop() C. init() D. paint() 3、Java语言具有许多优点和特点,下列选项中,哪个反映了Java程序并行机制的特点?B A)安全性B)多线程C)跨平台D)可移植 4、下列哪个类声明是正确的?D A)abstract final class HI{···}B)abstract private move(){···} C)protected private number; D)public abstract class Car{···} 6、在Java语言中,下列哪些语句关于内存回收的说明是正确的? B A.程序员必须创建一个线程来释放内存; B.内存回收程序负责释放无用内存 C.内存回收程序允许程序员直接释放内存 D.内存回收程序可以在指定的时间释放内存对象 10、下列Object类中的方法,哪一项不是完全跟线程有关:A A.String toString() B.void notify() C.void notifyAll() D.void wait() 11、给出下面代码:C

public class Person{ static int arr[] = new int[10]; public static void main(String a[]) { System.out.println(arr[1]); } } 下列说法中正确的是? A.编译时将产生错误; B.编译时正确,运行时将产生错误; C.输出零; D.输出空。 12、字符串是Java已定义的类型,关于它的构造函数,下面说法不正确的是:B A.String(char[] value, int offset, int count) B.String(int[] codePoints,int offset, int count) C.String(String original) D.String(StringBuffer buffer) 13、下列说法中正确的是:C A.导入包会影响程序的性能 B.包存储在类库中 C.包是类的容器D.上述说法都不对 14、下列不是String类的常用方法是:C

java语言程序设计基础篇第二章程序练习题答案

java语言程序设计基础篇第二章程序练习题答案

2.1(将摄氏温度转化为华氏温度) import java.util.*; public class test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print("Enter a degree in celsius: "); double Celsius = input.nextDouble(); double Fahrenheit; Fahrenheit = (9.0/5) * Celsius + 32; System.out.println(Celsius + " Celsius is" + Fahrenheit + " Fahrenheit"); } } 2.2(计算圆柱体的体积) import java.util.*; public class test {

public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print("Enter the radius and length of a cylinder: "); double radius = input.nextDouble(); double length =input.nextDouble(); double area = radius * radius * Math.PI; double volume = area * length; System.out.println("The area is " + area); System.out.println("The volume is " + volume); } } 2.3(将英尺转换为米) import java.util.Scanner; public class test { public static void main(String[] args) {

相关主题
文本预览
相关文档 最新文档