文档库 最新最全的文档下载
当前位置:文档库 › JAVA练习题含答案-answer to pratice 3

JAVA练习题含答案-answer to pratice 3

JAVA练习题含答案-answer to pratice 3
JAVA练习题含答案-answer to pratice 3

Chapter 3

Flow of Control

Multiple Choice

1)An if selection statement executes if and only if:

(a)the Boolean condition evaluates to false.

(b)the Boolean condition evaluates to true.

(c)the Boolean condition is short-circuited.

(d)none of the above.

Answer:B (see page 97)

2) A compound statement is enclosed between:

(a)[ ]

(b){ }

(c)( )

(d)< >

Answer:B (see page 98)

3) A multi-way if-else statement

(a)allows you to choose one course of action.

(b)always executes the else statement.

(c)allows you to choose among alternative courses of action.

(d)executes all Boolean conditions that evaluate to true.

Answer:C (see page 100)

4)The controlling expression for a switch statement includes all of the following types except:

(a)char

(b)int

(c)byte

(d)double

Answer:D (see page 104)

Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved. 1

2

5)To compare two strings lexicographically the String method ____________ should be used.

(a)equals

(b)equalsIgnoreCase

(c)compareTo

(d)==

Answer:C (see page 112)

6)When using a compound Boolean expression joined by an && (AND) in an if statement:

(a)Both expressions must evaluate to true for the statement to execute.

(b)The first expression must evaluate to true and the second expression must evaluate to false for

the statement to execute.

(c)The first expression must evaluate to false and the second expression must evaluate to true for

the statement to execute.

(d)Both expressions must evaluate to false for the statement to execute.

Answer:A (see page 116)

7)The OR operator in Java is represented by:

(a)!

(b)&&

(c)| |

(d)None of the above

Answer:C (see page 116)

8)The negation operator in Java is represented by:

(a)!

(b)&&

(c)| |

(d)None of the above

Answer:A (see page 116)

9)The ____________ operator has the highest precedence.

(a)*

(b)dot

(c)+=

(d)decrement

Answer:B (see page 123)

10)The looping mechanism that always executes at least once is the _____________ statement.

(a)if…else

(b)do…while

(c)while

(d)for

Answer:B (see page 132)

.

Chapter 3 Flow of Control 3

11) A mixture of programming language and human language is known as:

(a)Algorithms

(b)Recipes

(c)Directions

(d)Pseudocode

Answer:D (see page 134)

12)When the number of repetitions are known in advance, you should use a ___________ statement.

(a)while

(b)do…while

(c)for

(d)None of the above

Answer:C (see page 141)

13)To terminate a program, use the Java statement:

(a)System.quit(0);

(b)System.end(0);

(c)System.abort(0);

(d)System.exit(0);

Answer:D (see page 148)

True/False

1)An if-else statement chooses between two alternative statements based on the value of a Boolean

expression.

Answer:True (see page 96)

2)You may omit the else part of an if-else statement if no alternative action is required.

Answer:True (see page 97)

3)In a switch statement, the choice of which branch to execute is determined by an expression given in

parentheses after the keyword switch.

Answer:True (see page 104)

4)In a switch statement, the default case is always executed.

Answer:False (see page 104)

5)Not including the break statements within a switch statement results in a syntax error.

Answer:False (see page 104)

6)The equality operator (==) may be used to test if two string objects contain the same value.

Answer:False (see page 111)

4

7)Boolean expressions are used to control branch and loop statements.

Answer:True (see page 116)

8)The for statement, do…while statement and while statement are examples of branching mechanisms.

Answer:False (see page 130)

9)An algorithm is a step-by-step method of solution.

Answer:True (see page 134)

10)The three expressions at the start of a for statement are separated by two commas.

Answer:False (see page 137)

Short Answer/Essay

1)What output will be produced by the following code?

public class SelectionStatements

{

public static void main(String[] args)

{

int number = 24;

if(number % 2 == 0)

System.out.print("The condition evaluated to true!");

else

System.out.print("The condition evaluated to false!");

}

}

Answer:The condition evaluated to true!

.

Chapter 3 Flow of Control 5

2)What would be the output of the code in #1 if number was originally initialized to 25?

Answer:The condition evaluated to false!

3)Write a multi-way if-else statement that evaluates a persons weight on the following criteria: A

weight less than 115 pounds, output: Eat 5 banana splits! A weight between 116 pounds and 130 pounds, output: Eat a banana split! A weight between 131 pounds and 200 pounds, output: Perfect!

A weight greater than 200 pounds, output: Plenty of banana splits have been consumed!

Answer:

if(weight <= 115)

System.out.println("Eat 5 banana splits!");

else if(weight <= 130)

System.out.println("Eat a banana split!");

else if(weight <=200)

System.out.println("Perfect!");

else

System.out.println("Plenty of banana splits have been consumed!");

4)Write an if-else statement to compute the amount of shipping due on an online sale. If the cost of

the purchase is less than $20, the shipping cost is $5.99. If the cost of the purchase over $20 and at most $65, the shipping cost is $10.99. If the cost of the purchase is over $65, the shipping cost is $15.99.

Answer:

if(costOfPurchase < 20)

shippingCost = 5.99;

else if((costOfPurchase > 20)&&(costOfPurchase <= 65))

shippingCost = 10.99;

else

shippingCost = 15.99;

5)Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3))

Answer:True

6

6)Write Java code that uses a do…while loop that prints even numbers from 2 through 10.

Answer:

int evenNumber = 2;

do

{

System.out.println(evenNumber);

evenNumber += 2;

}while(evenNumber <= 10);

7)Write Java code that uses a while loop to print even numbers from 2 through 10.

Answer:

int evenNumber = 2;

while(evenNumber <= 10)

{

System.out.println(evenNumber);

evenNumber += 2;

}

Answer:

8)Write Java code that uses a for statement to sum the numbers from 1 through 50. Display the total

sum to the console.

Answer:

.

Chapter 3 Flow of Control 7 int sum = 0;

for(int i=1; i <= 50; i++)

{

sum += i;

}

System.out.println("The total is: " + sum);

9)What is the output of the following code segment?

public static void main(String[] args)

{

int x = 5;

System.out.println("The value of x is:" + x);

while(x > 0)

{

x++;

}

System.out.println("The value of x is:" + x);

}

Answer:.

10)Discuss the differences between the break and the continue statements when used in looping

mechanisms.

Answer:When the break statement is encountered within a looping mechanism, the loop

immediately terminates. When the continue statement is encountered within a looping mechanism, the current iteration is terminated, and execution continues with the next iteration of the loop.

8

Programming projects:

1. Write a complete Java program that prompts the user for a series of numbers to determine the smallest

value entered. Before the program terminates, display the smallest value.

Answer:

import java.util.Scanner;

public class FindMin

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

int smallest = 9999999;

String userInput;

boolean quit = false;

System.out.println("This program finds the smallest number"

+ " in a series of numbers");

System.out.println("When you want to exit, type Q");

.

Chapter 3 Flow of Control 9 while(quit != true)

{

System.out.print("Enter a number: ");

userInput = keyboard.next();

if(userInput.equals("Q") || userInput.equals("q"))

{

quit = true;

}

else

{

int userNumber = Integer.parseInt(userInput);

if(userNumber < smallest)

smallest = userNumber;

}

}

System.out.println("The smallest number is " + smallest);

System.exit(0);

}

10

}

2. Write a complete Java program that uses a for loop to compute the sum of the even numbers and the

sum of the odd numbers between 1 and 25.

public class sumEvenOdd

{

public static void main(String[] args)

{

int evenSum = 0;

int oddSum = 0;

//loop through the numbers

for(int i=1; i <= 25; i++)

{

if(i % 2 == 0)

{

//even number

evenSum += i;

}

else

{

oddSum += i;

.

Chapter 3 Flow of Control 11 }

}

//Output the results

System.out.println("Even sum = " + evenSum);

System.out.println("Odd sum = " + oddSum);

}

}

/**

* Question2.java

*

* This program simulates 10,000 games of craps.

* It counts the number of wins and losses and outputs the probability

* of winning.

*

* Created: Sat Mar 05, 2005

*

* @author Kenrick Mock

* @version 1

*/

public class Question2

{

private static final int NUM_GAMES = 10000;

/**

* This is the main method. It loops 10,000 times, each simulate

* a game of craps. Math.random() is used to get a random number,

* and we simulate rolling two dice (Math.random() * 6 + 1 simulates

* a single die).

*/

public static void main(String[] args)

{

// Variable declarations

int numWins = 0;

12

int numLosses = 0;

int i;

int roll;

int point;

// Play 10,000 games

for (i=0; i

{

// Simulate rolling the two dice, with values from 1-6

roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;

// Check for initial win or loss

if ((roll == 7) || (roll == 11))

{

numWins++;

}

else if ((roll==2) || (roll==3) || (roll==12))

{

numLosses++;

}

else

{

// Continue rolling until we get the point or 7

point = roll;

do

{

roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;

if (roll==7)

{

numLosses++;

}

else if (roll==point)

{

numWins++;

}

} while ((point != roll) && (roll != 7));

}

}

// Output probability of winning

System.out.println("In the simulation, we won " + numWins +

" times and lost " + numLosses + " times, " +

" for a probability of " +

(double) (numWins) / (numWins+numLosses));

}

} // Question2

.

Chapter 3 Flow of Control 13 /**

* Question6.java

*

* Created: Sun Nov 09 16:14:44 2003

* Modified: Sat Mar 05 2005, Kenrick Mock

*

* @author Adrienne Decker

* @version 2

*/

import java.util.Scanner;

public class Question6

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

while ( true )

{

System.out.println("Enter the initial size of the green crud"

+ " in pounds.\nIf you don't want to " +

"calculate any more enter -1.");

int initialSize = keyboard.nextInt();

if ( initialSize == -1)

{

break;

} // end of if ()

System.out.println("Enter the number of days: ");

int numDays = keyboard.nextInt();

int numOfRepCycles = numDays / 5;

int prevPrevGen = 0;

int prevGen = initialSize;

int finalAnswer = initialSize;

for ( int i = 0; i < numOfRepCycles; i++ )

{

finalAnswer = prevPrevGen + prevGen;

14

prevPrevGen = prevGen;

prevGen = finalAnswer;

} // end of for ()

System.out.println("The final amount of green crud will be: "

+ finalAnswer + " pounds.\n");

} // end of while ()

}

} // Question6

/**

* Question7.java

*

* Created: Sun Nov 09 16:14:44 2003

* Modified: Sat Mar 05 2005, Kenrick Mock

*

* @author Adrienne Decker

* @version 2

*/

import java.util.Scanner;

public class Question7

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

String line;

do

{

System.out.println("Enter the value of X for this calculation.");

double x = keyboard.nextDouble();

double sum = 1.0;

double temp = 1.0;

for ( int n = 1; n <= 10; n++)

{

System.out.print("For n equal to: " + n

.

Chapter 3 Flow of Control 15

+ ", the result of calculating e^x is: ");

for ( int inner = 1; inner <= n; inner++)

{

temp = 1.0;

for ( double z = inner; z > 0; z--)

{

temp = temp * (x/z);

} // end of for ()

sum += temp;

} // end of for ()

System.out.println(sum);

sum = 1.0;

} // end of for ()

System.out.print("For n equal to 50, the result of "

+ "calculating e^x is: ");

for ( int n = 1; n <= 50; n++)

{

temp = 1.0;

for ( double z = n; z > 0; z--)

{

temp = temp * (x/z);

} // end of for ()

sum += temp;

} // end of for ()

System.out.println(sum);

sum = 1;

System.out.print("For n equal to 100, the result of "

+ "calculating e^x is: ");

for ( int n = 1; n <= 100; n++)

{

temp = 1.0;

for ( double z = n; z > 0; z--)

{

temp = temp * (x/z);

} // end of for ()

sum += temp;

} // end of for ()

System.out.println(sum);

System.out.println("\nEnter Q to quit or Y to go again.");

16

keyboard.nextLine(); // Clear buffer

line = keyboard.nextLine();

} while (line.charAt(0)!='q' && line.charAt(0)!='Q'); // end of while () }

} // Question7

.

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语言练习题库(含答案)

单选题 1. 为了保证方法的线程安全,声明方法的时候必须用哪个修饰符? (A) new (B) transient (C) void (D) synchronized 2. 编译Java源文件使用哪个? (A) javac (B) jdb (C) javadoc (D) junit 3. 哪一种类的对象中包含有Internet地址。 (A) Applet (B) Datagramsocket (C) InetAddress (D) AppletContext 4. 有关GUI容器叙述,不正确的是? (A) 容器是一种特殊的组件,它可用来放置其它组件 (B) 容器是组成GUI所必需的元素 (C) 容器是一种特殊的组件,它可被放置在其它容器中

(D) 容器是一种特殊的组件,它可被放置在任何组件中 5. 使用javadoc生成的文档的文件格式是? (A) XML格式 (B) 自定义格式 (C) 二进制格式 (D) HTML格式 6. 下列有关类、对象和实例的叙述,正确的是哪一项? (A) 类就是对象,对象就是类,实例是对象的另一个名称,三者没有差别 (B) 对象是类的抽象,类是对象的具体化,实例是对象的另一个名称 (C) 类是对象的抽象,对象是类的具体化,实例是类的另一个名称 (D) 类是对象的抽象,对象是类的具体化,实例是对象的另一个名称 7. 在事件委托类的继承体系中,最高层次的类是哪项? (A) java.util.EventListener (B) java.util.EventObject (C) java.awt.AWTEvent (D) java.awt.event.AWTEvent 8. Java语言中异常的分类是哪项? (A) 运行时异常和异常 (B) 受检异常和非受检异常

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、在下列说法中,选出最正确的一项是(A )。 A、Java 语言是以类为程序的基本单位的 B、Java 语言是不区分大小写的 C、多行注释语句必须以//开始 D、在Java 语言中,类的源文件名和该类名可以不相同 2、下列选项中不属于Java 虚拟机的执行特点的一项是(D )。 A、异常处理 B、多线程 C、动态链接 D、简单易学 3、下列选项中,属丁JVM 执行过程中的特点的一项是( C )。 A、编译执行 B、多进程 C、异常处理 D、静态链接 4、在Java 语言中,那一个是最基本的元素?( C ) A、方法 B、包 C、对象 D、接口 5、如果有2 个类A 和B,A 类基于B 类,则下列描述中正确的一个是( B )。 A、这2 个类都是子类或者超类 B、A 是B 超类的子类 C、B 是A 超类的子类 D、这2 个类郡是对方的子类 6、使用如下哪个保留字可以使只有在定义该类的包中的其他类才能访问该类?(D ) A、abstract B、private (本类) C、protected(本包及其他包的子类) D、不使用保留字 7、编译一个定义了3 个类的Java 源文件后,会产生多少个字符码文件,扩展名是什么?(D ) A、13 个字节码文件,扩展名是.class B、1 个字节码文件,扩展名是.class C、3 个字节码文件,扩展名是.java D、3 个字节码文件,扩展名是.class 8、下列关于Java 程序结构的描述中,不正确的一项是( C )。 A、一个Java 源文件中可以包括一个package 语句 B、一个Java 源文件中可以包括多个类定义,但是只能有一个public 类 C、一个Java 源文件中可以有多个public 类 D、源文件名与程序类名必须一致 9、下列说法正确的一项是( C )。 A、java.1ang.Integer 是接口 B、java.1ang.Runnable 是类 C、Doulble 对象在iava.1ang 包中 D、Double 对象在java.1ang.Object 包中 10、以下关于面向对象概念的描述中,不正确的一项是( B )。 A、在现实生活中,对象是指客观世界的实体

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 只能 在所在类中使用 则该成员变量必须使用的修饰是

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

一、判断题(每题1分,共15分) 1、Java允许创建不规则数组,即Java多维数组中各行的列数可以不同。( 1 ) 2、接口和类一样也可以有继承关系,而且都只能支持单继承。(0 ) 3、所有类至少有一个构造器,构造器用来初始化类的新对象,构造器与类同名,返回类型只能为void。(0 ) 4、包是按照目录、子目录存放的,可以在程序中用package定义包,若没有package一行,则表示该文件中的类不属于任何一个包。(0 ) 5、Java对事件的处理是采用委托方式进行的,即将需要进行事件处理的组件委托给指定的事件处理器进行处理。( 1 ) 6、在异常处理中,若try中的代码可能产生多种异常则可以对应多个catch语句,若catch中的参数类型有父类子类关系,此时应该将父类放在前面,子类放在后面。(0 ) 7、在实例方法或构造器中,this用来引用当前对象,通过使用this可引用当前对象的任何成员。( 1 ) 8、我们可以方便地编写Java客户机/服务器程序,在客户机/服务器模式中,客户机一般通过套接字(Socket)使用服务器所提供的服务,Socket由两部分组成:IP地址和端口号。 ( 1 ) 9、Java的屏幕坐标是以像素为单位,容器的左下角被确定为坐标的起点。(0 ) 10、Java程序里,创建新的类对象用关键字new,回收无用的类对象使用关键字free。(0 ) 11、当一个方法在运行过程中产生一个异常,则这个方法会终止,但是整个程序不一定终止运行。 (1 ) 12、如果f是父类Flower的对象,而r是子类Rose的对象,则语句f=r是正确的。(1 ) 13、Java系统的标准输入对象是System.in,标准输出对象有两个,分别是System.out和System.err。 (1 ) 14、final类中的属性和方法都必须被final修饰符修饰。(0) 15、子类可以定义与父类同名的方法,称为方法的覆盖,方法覆盖要求子类的方法与父类的方法名字和参数都相同,但返回值类型可以不同。(0 ) 二、单项选择题(每题2分,共30分) 1、若在某一个类定义中定义有如下的方法: final void aFinalFunction( );则该方法属于( c )。 A、本地方法 B、解态方法 C、最终方法 D、抽象方法 2、main方法是Java Application程序执行的入口点,关于main方法的方法头以下哪项是合法的( b )。 A、p ublic static void main() B、public static void main(String[ ] args) C、public static int main(String[ ] args) D、p ublic void main(String arg[ ]) 3、在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺序各不相同,传回的值也可以不相同。这种面向对象程序的特性称为( c )。

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

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.wendangku.net/doc/16199859.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的有()

java习题及答案第1章 习题参考答案

第1章习题解答 1.Java语言有那些特点? 答:Java语言的特点包括:平台无关性、面向对象、简单性、安全性、分布式、健壮性、解释型、多线程。 2.为什么说Java是结构中立的,具有跨平台特性? 答:无论哪种编程语言编写的程序最终都需要操作系统和处理器来完成程序的运行,平台无关性是指软件的运行不因操作系统、处理器的变化导致程序无法运行或出现运行错误。 以C++程序为例,C++编译器针对源程序所在平台进行编译、连接,然后生成机器指令,这样就无法保证C++编译器产生的可执行文件在所有平台上都被正确执行。如果更换了平台,可能需要修改源程序,并针对新的平台重新编译源程序。 相反,Java源代码不会针对一个特定平台进行编译,而是生成一种字节码中间文件(class 文件),这种文件是平台无关且体系结构中立的。也就是说,无论一个Java程序是在Windows、Solaris、Linux还是其他具有Java编译器的操作系统下编译,作为编译结果的字节码文件都是相同的,都可以在任何具有Java虚拟机(JVM)的计算机上运行。JVM能够识别这些字节码文件,JVM将字节码文件进行转换,使之能够在不同平台上运行。任何操作系统只要安装了JVM,就可以解释并执行这种与体系结构无关的字节码文件,实现了跨平台。 跨平台特性保证了Java的可移植性,任何Java源程序都可以移植到其他平台上。除此之外,Java的数据类型与机器无关,原始数据类型存储方式是固定的,避开了移植时可能产生的问题。例如,在任何机器上,Java的整型都是32位的,而C++中整型的存储依赖于目标计算机。另外Java的字符串采用标准的Unicode格式保存,也保证了Java的可移植性。 3.简述Java的3种主要平台,这些适合开发那种应用? 答:Java的开发平台(JDK)是开发人员用来构建Java应用程序的软件包,它包括:Java 虚拟机(JVM)、Java编译器(javac)、Java归档(jar)实用程序、Java文档(javadoc)实用程序等。目前,Java的运行平台主要分为下列3个版本。 (1)Java标准版 Java标准版即Java SE,曾被称为J2SE。Java SE提供了标准的JDK开发平台,利用该平台可以开发桌面应用程序、低端的服务器应用程序以及Java Applet程序。学习Java应当从Java SE开始,本书主要介绍Java SE。

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开发工程师笔试试题 (请不要在试题上留任痕迹,所有答案均写在答题纸上) 一.编程题(共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.wendangku.net/doc/16199859.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 练习题答案 一、填空 1、对象的状态和行为是对象的主要属性;前者对应类的变 量,行为又称为对象的操作,对应着类的方法。类的定义包括变量声明和方法声明。 2、要嵌入在HTML文件中运行的程序是Java Applet (Java Application 、Java Applet )。 3、安装JDK后,Java 开发工具在Bin 目录。 4、声明接口的保留字是interface 。 5、类的声明“ public class Test extends Applet implements Runable{} ” 中,定义的类名是Test ,其父类是Applet ;实现了Runable 接口。这个类的源程序必须保存为Test.java (写出包括扩展名的文件名)。 6、一个完整的Java 应用程序由一个或多个类组成;其中Java Application 至少有一个主类,这个类中包含一个名为main 的方法 7、JDK下解释执行Java 的程序是java.exe 。 8、语句如下: int[] c1=int[10]; int[] c2={1,2,3,4,5,6,7,8,9,0}; 数组c1 中的元素有10 个;c2 中的元素有10 个; 已初始化赋 值的是c2 (c1 c2 )。 9、执行完下列程序后i 的值为5

int i=0; while(i<5) { i++; 10、运行下列程序段后,结果 c 的取值为120 int a = 100, b = 20,c; char oper ='+'; switch(oper) { case '+': c = a+b; break; case '-': c = a - b; break; default: c = a * b; break; } 11、为了能使用Java 中已提供的类,我们需要用import 语句来引入所需要的类。语句import java.io.* ;中引入了java.io 包的所有类。 二、选择题 1、属于访问控制的关键字是( D )。 A、static B 、final C、abstract D 、private 2、对成员的访问控制保护最强的是(C ) A、public 、 B、缺省、 C private D protected 3、可用做Java 标识符的是( B )。 A、try B_isYour C 2time D my name

大学JAVA期末考试试题带答案

《JA V A程序设计》期末考试试题(三 一、单项选择题 1、如下哪个是Java中的标识符(D A、public B、super C、3number D、width 2、如下哪个是Java中的标识符(A A、fieldname B、super C、3number D、#number 3、已知如下定义:String s = "story"; 下面哪个语句不是合法的( C A、s += "books"; B、s = s + 100; C、int len = s.length; D、String t = s + “abc”; 4、如下哪个是Java中有效的关键字( C A、name

B、hello C、false D、good 5、下面的代码段执行之后count的值是什么( D int count = 1; for (int i = 1; i <= 5; i++ { count += i; } System.out.println(count; A、5 B、1 C、15 D、16 6、定义一个类,必须使用的关键字是( B A、public B、class C、interface D、static 7、定义一个接口必须使用的关键字是(C

A、public B、class C、interface D、static 8、如果容器组件p的布局是BorderLayout,则在p的下边中添加一个按钮b,应该使用的语句是(C A、p.add(b; B、p.add(b,"North"; C、p.add(b,"South"; D、b.add(p,"North"; 9、声明并创建一个按钮对象b,应该使用的语句是(A A、Button b=new Button(; B、button b=new button(; C、Button b=new b(; D、b.setLabel(“确定”; 10、Frame对象默认的布局管理器是(B A、FlowLayout B、BorderLayout C、CardLayout

相关文档