当前位置:文档之家› JAVA练习题含答案 ANSWERS TO PRACTICE 2

JAVA练习题含答案 ANSWERS TO PRACTICE 2

JAVA练习题含答案 ANSWERS TO PRACTICE 2
JAVA练习题含答案 ANSWERS TO PRACTICE 2

?Multiple Choice

1)Which operator is used to concatenate two strings?

(a)+

(b)–

(c)*

(d)/

Answer:A(see page35)

2)Which operator returns the remainder of integer division?

(a)%

(b)/

(c)*

(d)none of the above

Answer:A(see page26)

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 page40)

4)The escape sequence the represents the new-line character is:

(a)\r

(b)\t

(c)\n

(d)\\

Answer:C(see page43)

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 page47)

6)In Java,a block comment is delimited by:

(a)*//*

(b)/*/*

(c)/**/

(d)*/*/

Answer:C(see page50)

7)To mark a block comment for inclusion in the Javadoc documentation,the block must

be delimited by:

(a)/***/

(b)*/**/

(c)**//*

(d)**/*/

Answer:A(see page50)

8)Valid arguments to the System.out object’s println method include:

(a)“Anything with double quotes”

(b)String variables

(c)Variables of type int

(d)All of the above

Answer:D(see page59)

9)Which statement or group of statements produces the output:Java programming is

fun!

(a)System.out.print(Java programming);

System.out.print(is fun!);

(b)System.out.println(Java programming is fun!);

(c)System.out.println(“Java programming”);

System.out.println(“is fun!”);

(d)System.out.print(“Java programming”)

System.out.println(“is fun!”);

Answer:D(see page60)

10)If a hyphen is added after the%in a format specifier,the output will be_________.

(a)Left justified

(b)Right justified

(c)Centered

(d)None of the above

Answer:A(see page64)

11)The statement:System.out.printf("%6.2f",597.7231);displays:

(a)597.723

(b)597.72

(c)000597.72

(d)None of the above

Answer:B(see page64)

12)The Java method printf is based on the________language.

(a)Pascal

(b)C++

(c)C

(d)ADA

Answer:C(see page67)

13)The class NumberFormat allows you to specify a constant representing which country’s

currency format should be used.To use this constant you must import:

(a)java.util.Locale

(b)java.util.Currency

(c)java.util.Properties

(d)None of the above.

Answer:A(see page71)

14)Standard code libraries in Java are called:

(a)Methods

(b)Classes

(c)Packages

(d)Statements

Answer:C(see page72)

15)What Java package includes the class Scanner?

(a)awt

(b)swing

(c)io

(d)util

Answer:D(see page78)

?True/False

1)Objects of type String are strings of characters that are written within single quotes.

Answer:False(see page34)

2)In Java,Strings are immutable objects.Immutable objects can be changed.

Answer:False(see page44)

3)An advantage of using the Unicode character set is that it easily handles languages

other than English.

Answer:True(see page44)

4)Java uses the ASCII character set.

Answer:False(see page44)

1)Efficiency is lost in importing the entire package instead of importing the classes you

use.

Answer:False(see page73)

2)Every Java program automatically imports the java.util package.

Answer:False(see page73)

3)The new line character is represented as‘\n’.

Answer:True(see page61)

4)The method printf is used the same way as the method println but has the added feature

that allows you to add formatting instructions.

Answer:False(see page61)

5)The printf method can be used to output multiple formatted values.

Answer:True(see page64)

6)The Scanner class has a method next that allows an entire line of string text to be read.

Answer:False(see page80)

7)Echoing input is good programming practice because it can reveal problems in the

input.

Answer:True(see page80)

?Short Answer/Essay

1)How is the%(modulus)operator used?What is the common use of the modulus

operator?

Answer:The modulus operator is used to return the remainder in integer division.

For example,the modulus operator is commonly used to determine if a number is an even or an odd value.

2)What is the output of the following Java statements?

//String method examples

String str="Java Programming!";

System.out.println(str.equals("Java Programming!"));

System.out.println(str.toLowerCase());

System.out.println(str.toUpperCase());

System.out.println(str.substring(5,8));

System.out.println(https://www.doczj.com/doc/631922109.html,stIndexOf("m"));

Answer:

true

java programming!

JAVA PROGRAMMING!

Pro

12

3)Write a Java statement to access the7th character in the String variable myString and

place it in the char variable c.

Answer:

c=myString.charAt(6);

4)Write a Java statement to determine the length of a string variable called input.Store

the result in an integer variable called strLength.

Answer:int strLength=input.length();

5)Write ONE Java statement to display your first and last name on two separate lines.

Answer:

System.out.print("Wally\nWonders");

6)Write a complete Java console application that prompts the user for two numbers,

multiplies the numbers,and then displays the result to the user.

Answer:

import java.util.Scanner;

public class ConsoleMultiply

{

public static void main(String[]args)

{

//Create the scanner object for console input

Scanner keyboard=new Scanner(System.in);

//Prompt the user for the first number

System.out.print("Enter the first integer:");

//Read the input

int firstNumber=keyboard.nextInt();

//Prompt the user for the second number

System.out.print("Enter the second integer:");

//Read the second number

int secondNumber=keyboard.nextInt();

System.out.println(firstNumber+"*"+secondNumber+"is"

+firstNumber*secondNumber);

}

}

7)Write a Java statement to create and initialize a Scanner object named input.

Answer:Scanner input=new Scanner(System.in);

8)Consider the following code snippet.

int i=10;

int n=i++%5;

1)What are the values of i and n after the code is executed?

Answer:i is11,and n is0.

2)What are the final values of i and n if instead of using the postfix increment operator (i++),you use the prefix version(++i))?

Answer:i is11,and n is1.

9)Programming projects:question2and3(see page93)

import java.util.Scanner;

public class Question2

{

public static void main(String[]args)

{

//Variable declarations

Scanner scan=new Scanner(System.in);

String first;

String last;

System.out.println("Enter your first name:");

first=scan.nextLine();

System.out.println("Enter your last name:");

last=scan.nextLine();

System.out.println(first+""+last+"turned to Pig Latin is:");

//First convert first name to pig latin

String pigFirstName=first.substring(1,first.length())+first.substring(0,1)+ "ay";

//Then capitalize first letter

pigFirstName=pigFirstName.substring(0,1).toUpperCase()+

pigFirstName.substring(1,pigFirstName.length());

//Repeat for the last name

String pigLastName=last.substring(1,last.length())+last.substring(0,1)+"ay";

//Then capitalize first letter

pigLastName=pigLastName.substring(0,1).toUpperCase()+

pigLastName.substring(1,pigLastName.length());

System.out.println(pigFirstName+""+pigLastName);

}

}//Question2

import java.util.Scanner;

public class Question3

{

public static void main(String[]args)

{

Scanner keyboard=new Scanner(System.in);

System.out.println("Enter first number to add:");

int first=keyboard.nextInt();

System.out.println("Enter second number to add");

int second=keyboard.nextInt();

int result=first+second;

System.out.println("Adding"+first+"+"+second+

"equals"+result);

System.out.println("Subtracting"+first+"-"+second+

"equals"+(first-second));

System.out.println("Multiplying"+first+"*"+second+

"equals"+(first*second));

}

}//Question3

java测试试卷(二)

JAVA语言基础内部测试题 一.单项选择题(请选择最符合题目要求的答案)(每题2分) 1.定义类头时,不可能用到的关键字是( )。 A) class B)private C)extends D)public 2.下列类定义中,不正确的是( )。 A)class x { .... } B)class x extends y { .... } C)static class x implements y1,y2 { .... } D)public class x extends Applet { .... } 3.设 A为已定义的类名,下列声明A类的对象a的语句中正确的是( )。 A)float A a; B)public A a=A( ); C) A a=new int( ); D) A a=new A( ); 4.有一个类A,以下为其构造方法的声明,其中正确的是( )。 A)public A(int x){...} B)static A(int x){...} C)public a(int x){...} D)void A(int x){...} 5.下列语句哪一个正确() A)Java程序经编译后会产生machine code B)Java程序经编译后会产生byte code C)Java程序经编译后会产生DLL D)以上都不正确 6.下列选项中,定义接口MyInterface的语句正确的是:() A)interface MyInterface{ } B) implements MyInterface { } C) class MyInterface{ } D) implements interface My{ } 7.如果子类中的方法mymethod()覆盖了父类中的方法mymethod(),假设父类方法头部定 义如下:void mymethod(int a),则子类方法的定义不合法的是:() A)public void mymethod(int a) B)protected void mymethod(int a) C)private void mymethod(int a) D)void mymethod(int a) 8.在异常处理中,如释放资源、关闭文件、关闭数据库等由( )来完成。 A.try子句 B.catch子句 C.throw子句 D.finally子句 9.给定以下JAVA代码,这段代码编译运行后输出的结果是() public class Test { public static int aMethod(int i) throws Exception { try{ return i/10; }catch(Exception ex){ throw new Exception ("exception in a aMothod"); }finally{ System.out.print("finally"); } }

java基础笔试测试题与答案

Java 一章至五章考试 一. 填空题(8 分) 1. 面向对象的三大原则是( 封装),( 继承) 和( 多态).2 分 2. 如果想在对象实例化的同时就初始化成员属性,则使用( 构造函数).2 分 3. ( 实体) 方法和( 构造) 方法不能修饰为abstract ?2分 二.选择题(60 分) 1) 在Java 语言中,下列(a,d )是不满足命名规范的变量名。(选择二项) a) 姓名 b) $Name c) _instanceof d) instanceof 2) 下列Java 代码片段的输出结果是( a ) 。 char c='a'; int i=c; float f=i; byte b=(byte)c; System.out.println(c+","+i+","+f+","+b); a) 编译错误 b) a,97,97,97 c) a,97,97.0,97 d) a,97,97.0f,97 3) 下列Java 代码中,空白处的代码是(b,c )。( 选择两项) public interface Fee{ public float calLabFee(float unitPrice, float time); } public class FeeImpl implements Fee { public float calLabFee(float unitPrice, float time){ return unitPrice * time; } } public class FeeInterfaceTest { public static void main(String[] args){ ________________ Float labFee = fee.calLabFee(400.00,5); } }

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.在单一文件中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/631922109.html,ng 包中定义了Runnable 接口和Thread 类。

分析《JAVA程序设计》期末考试试题_(二)

《JAVA程序设计》期末考试试题(二) 一.判断题 1.Java的源代码中定义几个类,编译结果就生成几个以.class为后缀的字节码文件。(√)2.Java程序里,创建新的类对象用关键字new,回收无用的类对象使用关键字free。(×)分析: java中没有回收(FREE)内存的功能, 它会自动回收的, 只要把对象close掉就等着回收就行了. 由于对象是使用new运算符动态分配的,java方法是自动为你的处理存储单元重新分配问题。完成这项工作的技术被称为无用单元收集(garbage collection)。它的工作原理:当不存在对一个对象的引用时,我们就假定不再需要那个对象了,那个对象所占有的存储单元可以被收回。在java中并不像在C++中一样需要明确地销毁对象。无用单元收集仅在偶尔执行程序时出现。它不会出现,是因为存在一个或多个不再使用的对象。还有,不同的Java运行时实现将会使用不同的无用单元收集方法,但是在大多数情况下,在编程时不必考虑它。如果一个对象正在持有某些非Java资源,如文件处理或窗口字符字体,那么Java提供了一种称为结束(finalization)的机制。 3.Java有垃圾回收机制,内存回收程序可在指定的时间释放内存对象。(×) 分析: 不对,JAVA虽然有垃圾回收机制,但是不能在指定的时间释放内存对象,只能在程序运行期间,当虚拟机空闲的时候回收。 4.构造函数用于创建类的实例对象,构造函数名应与类名相同,返回类型为void。(×)分析: 无返回类型 5.在异常处理中,若try中的代码可能产生多种异常则可以对应多个catch语句,若catch 中的参数类型有父类子类关系,此时应该将父类放在后面,子类放在前面。(√) 分析: 个人觉得原因是子类更加具体一些, 可以更好得描述这个异常, 所以会把子类写在前面, 如果把父类写在前面, 那么这个父类就会接受很多的异常了, 后面的子类就接收不到了6.拥有abstract方法的类是抽象类,但抽象类中可以没有abstract方法。(√) 7.Java的屏幕坐标是以像素为单位,容器的左下角被确定为坐标的起点。(×) 分析: 左上角被确定为坐标的起点 8.静态初始化器是在其所属的类加载内存时由系统自动调用执行。(√) 9.在Java中对象可以赋值,只要使用赋值号(等号)即可,相当于生成了一个各属性与赋值对象相同的新对象。(×) 分析: 并不是生成一个新对象, 只是引用相同而已, 真正的对象的空间是一样的. 二.单项选择题 1.Java application中的主类需包含main方法,以下哪项是main方法的正确形参?()A、String args B、String ar[] C、Char arg D、StringBuffer args[] 分析: 比较常见的是String[] args, 但是String args[]也是可以的, ar和args并没有什么区别. 2.以下关于继承的叙述正确的是()。 A、在Java中类只允许单一继承 B、在Java中一个类只能实现一个接口 C、在Java中一个类不能同时继承一个类和实现一个接口 D、在Java中接口只允许单一继承 3.paint()方法使用哪种类型的参数? () A、Graphics B、Graphics2D C、String D、Color 分析:这是GUI里面的, 不考.

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的值是()

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笔试题及答案.doc

java笔试题及答案 有了下面java笔试题及答案,进行java笔试时就容易多了,请您对下文进行参考: 1、作用域public,private,protected,以及不写时的区别 答:区别如下: 作用域当前类同一package子孙类其他package public 7 7 7 7 protected 7 7 7 X friendly 7 7 X X private 7 X X X 不写时默认为friendly 2、Anonymouslnner Class (匿名内部类)是否可以exte nd s (继承)其它类,是否可以imple ment s (实现)i nterf ace (接口) 答:匿名的内部类是没有名字的内部类。不能exte n ds (继承)其它类,但一个内部类可以作为一个接口,由另一个内部类实现 3、Sta ti cNestedC las s 和Inner Clas s 的不同答: Nes tedC lass (一般是C+ +的说法),In ne rClass (—般是JAVA的说法)。J ava内部类与C++嵌套类最大的不同就在于是否有指向外部的引用上。注:静态内部类(I

nn erClass)意味着1创建一个st atic内部类的对象,不需要一个外部类对象,2不能从一个st atic内部类的一个对象访问一个外部类对象 4、和的区别 答:是位运算符,表示按位与运算,是逻辑运算符,表示遷辑与(and ) 5、Coll ect ion 和Col lect ions 的区别 答:Coll ect ion是集合类的上级接口,继承与他的接口主要有Set和List. Col lections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作 6、什么时候用assert 答:asserti on (断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。在实现中,a ssertion 就是在程序中的一条语句,它对一个boolea n表 达式进行检查,一个正确程序必须保证这个bool ean表达 式的值为tr ue;如果该值为fal se,说明程序己经处于不正确的状态下,系统将给出警告或退出。一般来说,

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.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 4.提供Java存取数据库能力的包是() A.Java.sql /sql/数据库还有Oracle 也是一种数据库 B.java.awt C.https://www.doczj.com/doc/631922109.html,ng D.java.swing 答案:A 5.下列运算符合法的是() A.&& B.<> C.if D.:= 答案:A 6.执行如下程序代码 a=0;c=0; do{ --c; a=a-1; }while(a>0); 后,C的值是() A.0 B.1 C.-1 D.死循环

答案:C 7.下列哪一种叙述是正确的() A.abstract修饰符可修饰字段、方法和类 B.抽象方法的body部分必须用一对大括号{}包住 C.声明抽象方法,大括号可有可无 D.声明抽象方法不可写出大括号 答案:D 8.下列语句正确的是() A.形式参数可被视为localvariable B.形式参数可被字段修饰符修饰 C.形式参数为方法被调用时,真正被传递的参数 D.形式参数不可以是对象 答案:A 9.下列哪种说法是正确的() A.实例方法可直接调用超类的实例方法 B.实例方法可直接调用超类的类方法 C.实例方法可直接调用其他类的实例方法 D.实例方法可直接调用本类的类方法 答案:D 二、多项选择题 1.Java程序的种类有() A.类(Class) B.Applet C.Application D.Servlet 2.下列说法正确的有() A.环境变量可在编译sourcecode时指定 B.在编译程序时,所能指定的环境变量不包括class path C.javac一次可同时编译数个Java源文件 D.javac.exe能指定编译结果要置于哪个目录(directory)答案:BCD 3.下列标识符不合法的有() A.new B.$Usdollars C.1234 D.car.taxi 答案:ACD 4.下列说法错误的有() A.数组是一种对象 B.数组属于一种原生类 C.intnumber=[]={31,23,33,43,35,63} D.数组的大小可以任意改变 答案:BCD 5.不能用来修饰interface的有()

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测试试卷二

类的继承习题 一、选择题 1. Java语言的类间的继承关系是( B )。 A) 多重的B) 单重的C) 线程的D) 不能继承 2. 以下关于Java语言继承的说法正确的是( C )。 A)Java中的类可以有多个直接父类B)抽象类不能有子类 C)Java中的接口支持多继承D)最终类可以作为其它类的父类 3. 现有两个类A、B,以下描述中表示B继承自A的是(D)。 A) class A extends B B) class B implements A C) class A implements B D) class B extends A 4. 下列选项中,用于定义接口的关键字是( A)。 A)interface B) implements C) abstract D) class 5. 下列选项中,用于实现接口的关键字是( B)。 A)interface B) implements C) abstract D) class 6. Java语言的类间的继承的关键字是( B )。 A) implements B) extends C) class D) public 7. 以下关于Java语言继承的说法错误的是( A )。 A)Java中的类可以有多个直接父类B)抽象类可以有子类 C)Java中的接口支持多继承D)最终类不可以作为其它类的父类 8. 现有两个类M、N,以下描述中表示N继承自M的是(D)。 A) class M extends N B) class N implements M C) class M implements N D) class N extends M 9. 现有类A和接口B,以下描述中表示类A实现接口B的语句是(A)。 A) class Aimplements B B) class B implements A C) class Aextends B D) class B extends A 10. 下列选项中,定义抽象类的关键字是( C)。 A)interface B) implements C) abstract D) class 11. 下列选项中,定义最终类的关键字是(D)。 A)interface B) implements C) abstract D) final 12. 下列选项中,哪个是java语言所有类的父类(C) A)String B) Vector C) Object D) KeyEvent 13. java语言中,用于判断某个对象是否是某个类的实例的运算符是(A) A)instanceof B) + C) isinstance D) && 14. 下列选项中,表示数据或方法可以被同一包中的任何类或它的子类访问,即使子类在不同的包中也可以的修饰符是(B) A)public B) protected C) private D) final 15. 下列选项中,表示数据或方法只能被本类访问的修饰符是(C) A)public B) protected C) private D) final 16. 下列选项中,接口中方法的默认可见性修饰符是(A) A)public B) protected C) private D) final 17. 下列选项中,表示终极方法的修饰符是:(B) A)interface B) final C) abstract D) implements 18. 下列选项中,定义接口MyInterface的语句正确的是:(A) A)interface MyInterface{ } B) implements MyInterface { } C) class MyInterface{ } D) implements interface My{ } 19. 如果子类中的方法mymethod()覆盖了父类中的方法mymethod(),假设父类方法头部定义如下:void mymethod(int a),则子类方法的定义不合法的是:(C) A)public void mymethod(int a) B) protected void mymethod(int a) C) private void mymethod(int a) D) void mymethod(int a) 二、填空题 1. 如果子类中的某个变量的变量名与它的父类中的某个变量完全一样,则称子类中的这个变量________了父类的同名变量。(隐藏) 2. 属性的隐藏是指子类重新定义从父类继承来的__________。(同名变量或属性)

Java开发工程师笔试题(带答案)

Java开发工程师笔试试题 (请不要在试题上留任痕迹,所有答案均写在答题纸上) 一.编程题(共26分) 1.任意写出一种排序算法。(6分) public void sort(int [] array){ //代码区 } 2.求1+2+3+..n(不能使用乘除法、for 、while 、if 、else 、switch 、case 等关键字 以及条件判断语句)(8分) public int sum(int n){ //代码区 return 0; } 3.完成下面法,输入一个整数,输出如下指定样式图案。(12分) 输入:3, 输出: 1*2*3 7*8*9 4*5*6

输入:4 输出: 1*2*3*4 9*10*11*12 13*14*15*16 5*6*7*8 public void drawNumPic(int n){ //代码区 } 二.选择题(定项选择每题3分,不定项选择每题4分,共63分) 1.在基本JAVA类型中,如果不明确指定,整数型的默认是__类型,带小数的默认是__类型?( B ) A.int float B.int double C.long float D.long double 2.只有实现了__接口的类,其对象才能序列化( A ) A.Serializable B.Cloneable https://www.doczj.com/doc/631922109.html,parable

D.Writeable 3.代码System. out. println(10 % 3 * 2);将打印出?( B ) A. 1 B.2 C.4 D.6 4.以下程序运行的结果为( A ) public class Example extends Thread{ @Override public void run(){ try{ Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } System.out.print("run"); } public static void main(String[] args){ Example example=new Example(); example.run(); System.out.print("main"); } } A.run main B.main run C.main D.run E.不能确定 5.下面有关java实例变量,局部变量,类变量和final变量的说法,错误的是?( B ) A.实例变量指的是类中定义的变量,即类成员变量,如果没有初始化,会有默认值

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) {

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