JAVA练习题含答案-answers to practise 1
- 格式:doc
- 大小:46.00 KB
- 文档页数:4
java基础50道经典练习题及答案《Java基础50道经典练习题及答案》Java作为一种广泛应用于软件开发的编程语言,对于初学者来说,掌握其基础知识是非常重要的。
为了帮助大家更好地学习和掌握Java基础知识,我们整理了50道经典的练习题及答案,希望能够帮助大家更好地理解和掌握Java编程。
1. 请写出Java中的8种基本数据类型。
2. 什么是Java中的包(package)?3. Java中的“==”和“equals()”有什么区别?4. 什么是Java中的多态(polymorphism)?5. 请写出Java中的四种访问修饰符及其作用。
6. 什么是Java中的接口(interface)?7. 请写出Java中的四种循环语句。
8. 什么是Java中的异常处理(exception handling)?9. Java中的“String”和“StringBuffer”有什么区别?10. 请写出Java中的三种注释方式。
这些练习题涵盖了Java基础知识的各个方面,包括数据类型、包、运算符、多态、访问修饰符、接口、循环语句、异常处理、字符串处理等内容。
通过做这些练习题,可以帮助大家更好地理解和掌握Java编程的基础知识。
在学习Java编程的过程中,不仅要掌握理论知识,还要多做练习,多动手实践。
只有通过实际操作,才能更好地理解和掌握Java编程的各种知识点。
希望大家能够认真对待这些练习题,多加练习,相信一定能够取得不错的成绩。
最后,希望大家在学习Java编程的过程中,保持耐心和坚持,相信通过不懈的努力,一定能够掌握好Java编程的基础知识,成为一名优秀的Java程序员。
祝大家学习进步,早日掌握Java编程!。
java考试编程题库及答案Java考试编程题库及答案1. 基础语法题- 题目:编写一个Java程序,实现两个整数的加法运算,并打印结果。
- 答案:```javapublic class Addition {public static void main(String[] args) {int num1 = 5;int num2 = 10;int sum = num1 + num2;System.out.println("The sum is: " + sum);}}```2. 控制结构题- 题目:编写一个Java程序,判断一个整数是否为素数,并打印结果。
- 答案:```javapublic class PrimeCheck {public static void main(String[] args) {int number = 29;if (isPrime(number)) {System.out.println(number + " is a primenumber.");} else {System.out.println(number + " is not a prime number.");}}public static boolean isPrime(int n) {if (n <= 1) return false;for (int i = 2; i <= Math.sqrt(n); i++) {if (n % i == 0) return false;}return true;}}```3. 数组与循环题- 题目:编写一个Java程序,打印数组中所有元素的平方。
- 答案:```javapublic class SquareElements {public static void main(String[] args) {int[] numbers = {1, 2, 3, 4, 5};for (int i = 0; i < numbers.length; i++) {int square = numbers[i] * numbers[i];System.out.println("Square of " + numbers[i] + " is " + square);}}}4. 面向对象题- 题目:定义一个名为`Car`的类,包含属性`color`和`speed`,以及一个方法`increaseSpeed(int increment)`来增加速度。
java试题库及答案Java试题库及答案一、单选题1. Java语言的特点是什么?A. 面向过程B. 面向对象C. 编译型语言D. 解释型语言答案:B2. 在Java中,用哪个关键字可以定义一个类?A. publicB. classC. voidD. int答案:B3. 下列哪个是Java的合法标识符?A. 2classB. class#2C. _class2D. class:2答案:C4. Java中的main()方法必须定义为什么类型的参数?A. intB. StringC. voidD. None答案:D5. 在Java中,哪个关键字用于实现异常处理?A. tryB. catchC. throwD. All of the above答案:D二、多选题6. 下列哪些是Java的基本数据类型?A. intB. StringC. floatD. boolean答案:A, C, D7. 在Java中,哪些是合法的数组初始化方式?A. int[] arr = new int[10];B. int arr[] = {1, 2, 3};C. int arr = {1, 2, 3};D. int arr = new int[3]{1, 2, 3};答案:A, B8. 下列哪些是Java的控制流语句?A. if-elseB. switch-caseC. forD. try-catch答案:A, B, C三、简答题9. 简述Java的垃圾回收机制。
答案:Java的垃圾回收机制是一种自动内存管理功能,它周期性地执行,回收不再使用的对象所占用的内存空间。
垃圾回收器会跟踪每个对象的引用,当对象的引用计数为0时,即没有任何引用指向该对象,垃圾回收器就会在下一次执行时回收该对象占用的内存。
10. 什么是Java的接口?它有什么作用?答案:Java中的接口是一种完全抽象的类,它不包含任何实现代码,只包含常量和抽象方法的声明。
java基础练习题及答案Java基础练习题及答案Java作为一种广泛应用的编程语言,其基础知识的掌握对于程序员而言是至关重要的。
为了帮助大家更好地理解和掌握Java的基础知识,下面将提供一些常见的Java基础练习题及其答案,希望对大家有所帮助。
一、数据类型与变量1. 编写一个Java程序,声明一个整型变量x并赋值为10,然后输出该变量的值。
```javapublic class Main {public static void main(String[] args) {int x = 10;System.out.println(x);}}```2. 编写一个Java程序,声明一个字符型变量ch并赋值为'A',然后输出该变量的值。
```javapublic class Main {public static void main(String[] args) {char ch = 'A';System.out.println(ch);}```3. 编写一个Java程序,声明一个布尔型变量isTrue并赋值为true,然后输出该变量的值。
```javapublic class Main {public static void main(String[] args) {boolean isTrue = true;System.out.println(isTrue);}}```二、运算符1. 编写一个Java程序,计算两个整数的和并输出结果。
```javapublic class Main {public static void main(String[] args) {int a = 10;int b = 20;int sum = a + b;System.out.println("两个整数的和为:" + sum);}```2. 编写一个Java程序,计算两个整数的商并输出结果。
java基础练习题带答案第1章Java开发入门一、填空题1、Java EE、Java SE、Java ME2、JRE3、javac4、bin5、path> classpath二、选择题1、A BCD、C、D、B、 B三、简答题1、面向对象、跨平台性、健壮性、安全性、可移植性、多线程性、动态性等。
2、JRE,它相当于操作系统部分,提供了Java程序运行时所需要的基本条件和许多Java基础类,例如,10类、GUI控件类、网络类等。
JRE是提供给普通用户使用的,如果你只想运行别人开发好的Java程序,那么,你的计算机上必须且只需安装JRE。
JDK,它包含编译工具、解释工具、文档制作工具、打包工具多种与开发相关的工具,是提供给Java开发人员使用的。
初学者学习和使用Java语言时,首先必须下载和安装JDKo JDK中已经包含了JRE部分,初学者安装JDK后不必再去下载和安装JRETo四、编程题public class HelloWorld {} public static void main { } System, out. printin;第2章Java编程基础一、填空题1、class2、true 和false3、单行注释、多行注释、文档注释4、基本数据类型、引用数据类型5、1、2、4、86、& && |7、08、9、10、6二、判断题1、错、对、错、对、错三、选择题1、AD、AD、C、ABCD、 C 、A、AC、A、 B 10、A四、程序分析题1、编译不通过。
int值4和b相加时,由于变量b 的类型为byte,取值范围没有int类型大,存不下int 类型的值,因此编译不通过。
2、编译不通过。
这是因为y是在最里层的代码块中定义的一个变量,只有在那个代码块中才可使用,在使用y = x;语句时已经超过了y变量的作用域,所以编译无法通过。
3、打印结果为:3o4、打印结果为:987五、问答题1、J ava语言的八种基本数据类型有:byte字节型,占一个字节。
java基础入门考试题目及答案一、选择题(每题2分,共20分)1. Java中,哪个关键字用于声明一个类?A. classB. interfaceC. structD. object答案:A2. 下列哪个是Java中的保留字?A. appleB. publicC. javaD. dog答案:B3. Java中,哪个方法用于获取字符串的长度?A. length()B. size()C. count()D. length答案:A4. 在Java中,哪个关键字用于定义一个方法?A. functionB. methodC. defD. void答案:D5. Java中,哪个关键字用于声明一个接口?A. classB. interfaceC. structD. object答案:B6. Java中,哪个关键字用于声明一个包?A. packageB. importC. classD. public答案:A7. Java中,哪个关键字用于实现多重继承?A. extendsB. implementsC. extends and implementsD. None of the above答案:B8. 在Java中,哪个关键字用于声明一个变量?A. varB. letC. defD. val答案:A9. Java中,哪个关键字用于声明一个常量?A. finalB. constC. staticD. var答案:A10. 在Java中,哪个关键字用于抛出一个异常?A. throwB. throwsC. catchD. try答案:B二、填空题(每题2分,共20分)1. Java是一种_________语言,它支持跨平台运行。
答案:面向对象2. Java程序的执行入口是_________方法。
答案:main3. 在Java中,所有的类都是_________类的子类。
答案:Object4. Java中,用于声明一个私有方法的关键字是_________。
初级Java考试简答题及答案:
1. 什么是Java?
答案:Java是一种面向对象的编程语言,由Sun Microsystems 公司开发。
它被设计成一种跨平台的语言,可以在多个操作系统上运行,并且具有垃圾回收机制,可以自动管理内存。
2. 什么是Java虚拟机(JVM)?
答案:Java虚拟机(JVM)是一种运行Java程序的软件平台。
它是一种虚拟的计算机,能够执行Java字节码,并将其转换为特定操作系统的机器代码执行。
3. 什么是Java类?
答案:Java类是Java编程的基本单元,它定义了一个对象的属性和方法。
每个Java程序都从public class声明开始,其中包含类的名称和属性和方法的定义。
4. 什么是Java对象?
答案:Java对象是通过类的实例化而创建的。
每个对象都有一组属性,这些属性可以在运行时被改变和访问。
对象还可以执行方法,这些方法定义了对象的行为。
5. 什么是Java数组?
答案:Java数组是一种用于存储固定大小的相同类型元素的数据结构。
数组可以是一维或多维的,并且可以通过索引访问元素。
JAVA练习题含答案-answertopratice3Chapter 3Flow of ControlMultiple Choice1)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)doubleAnswer:D (see page 104)Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved. 125)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 forthe statement to execute.(c)The first expression must evaluate to false and the second expression must evaluate to true forthe 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 aboveAnswer:C (see page 116)8)The negation operator in Java is represented by:(a)!(b)&&(c)| |(d)None of the aboveAnswer:A (see page 116)9)The ____________ operator has the highest precedence.(a)*(b)dot(c)+=(d)decrementAnswer: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)forAnswer:B (see page 132).Chapter 3 Flow of Control 311) A mixture of programming language and human language is known as:(a)Algorithms(b)Recipes(c)Directions(d)PseudocodeAnswer: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 aboveAnswer: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/False1)An if-else statement chooses between two alternative statements based on the value of a Booleanexpression.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 inparentheses 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)47)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/Essay1)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!");elseSystem.out.print("The condition evaluated to false!");}}Answer:The condition evaluated to true!.Chapter 3 Flow of Control 52)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: Aweight 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!");elseSystem.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 ofthe 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;elseshippingCost = 15.99;5)Evaluate the Boolean equation: !( ( 6 < 5) && (4 < 3))Answer:True66)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 totalsum 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 loopingmechanisms.Answer:When the break statement is encountered within a looping mechanism, the loopimmediately 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.8Programming projects:1. Write a complete Java program that prompts the user fora series of numbers to determine the smallestvalue 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 thesum 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 numbersfor(int i=1; i <= 25; i++){if(i % 2 == 0){//even numberevenSum += i;}else{oddSum += i;.Chapter 3 Flow of Control 11 }}//Output the resultsSystem.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 declarationsint numWins = 0;12int numLosses = 0;int i;int roll;int point;// Play 10,000 gamesfor (i=0; i<="" p="">{// Simulate rolling the two dice, with values from 1-6roll = (int) (Math.random() * 6) + (int) (Math.random() * 6) + 2;// Check for initial win or lossif ((roll == 7) || (roll == 11)){numWins++;}else if ((roll==2) || (roll==3) || (roll==12)){numLosses++;}else{// Continue rolling until we get the point or 7point = 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 winningSystem.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;14prevPrevGen = 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.");16keyboard.nextLine(); // Clear bufferline = keyboard.nextLine();} while (line.charAt(0)!='q' && line.charAt(0)!='Q'); // end of while () }} // Question7.。
java期末复习题库及答案一、选择题1. Java是一种()。
A. 编译型语言B. 解释型语言C. 标记语言D. 汇编语言答案:B2. Java程序的执行流程是()。
A. 编译 -> 链接 -> 执行B. 编译 -> 执行C. 编译 -> 解释 -> 执行D. 解释 -> 执行答案:C3. 下列哪个是Java的关键字?()A. classB. functionC. includeD. namespace答案:A4. Java中的类是()。
A. 抽象的B. 具体的C. 既是抽象的也是具体的D. 以上都不是答案:A5. Java中,哪个关键字用于定义一个类?()A. publicB. classC. voidD. int答案:B二、填空题1. Java语言的跨平台特性主要得益于______机制。
答案:JVM(Java虚拟机)2. 在Java中,所有的类都是继承自______类。
答案:Object3. Java中的______关键字用于定义一个方法。
答案:void 或者其他返回类型4. 在Java中,______关键字用于定义一个类是公共的。
答案:public5. Java中的______关键字用于定义一个变量是静态的。
答案:static三、简答题1. 简述Java语言的特点。
答案:Java语言具有面向对象、跨平台、健壮性、多线程、安全性、简单性等特点。
2. 什么是Java的垃圾回收机制?答案:Java的垃圾回收机制是指JVM自动检测内存中不再被使用的对象,并释放它们占用的内存资源,以防止内存泄漏。
3. 什么是Java的异常处理机制?答案:Java的异常处理机制是指程序在执行过程中,当出现错误或异常情况时,Java运行时环境提供的一种处理机制,包括try、catch、finally和throw等关键字的使用。
四、编程题1. 编写一个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、abstractB、private (本类)C、protected(本包及其他包的子类)D、不使用保留字7、编译一个定义了3 个类的Java 源文件后,会产生多少个字符码文件,扩展名是什么?(D )A、13 个字节码文件,扩展名是.classB、1 个字节码文件,扩展名是.classC、3 个字节码文件,扩展名是.javaD、3 个字节码文件,扩展名是.class8、下列关于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 )。
Chapter 1 Getting Started
Multiple Choice
1)Java is an object-oriented programming language. An object-oriented language
(a)Uses structured programming.
(b)Views a program as consisting of objects which communicate through interactions.
(c)Functionally breaks down problems into smaller, more manageable problems.
(d)All of the above.
Answer:B (see page 3)
2)In Java, the equal sign is used as the ___________ operator.
(a)increment
(b)decrement
(c)assignment
(d)negation
Answer:C (see page 7)
3)In Java, source code is compiled into object code called ______________.
(a)Bit-code
(b)Class code
(c)Method code
(d)Byte-code
Answer:D (see page 9)
4)The hardest kind of error to detect in a computer program is a:
(a)Syntax error
(b)Run-time error
(c)Logic error
(d)All of the above
Answer:C (see page 12)
1
5)Identify the invalid Java identifier.
(a)1Week
(b)Week1
(c)amountDue
(d)amount_due
Answer:A (see page 14)
6)What is the value of the variable amountDue?
double price = 2.50;
double quantity = 5;
double amountDue = 0;
amountDue = price * quantity;
(a)12
(b)12.25
(c)12.5
(d)13
Answer:C (see page 19)
7)What is the value of 7.52e-5?
(a)752000.0
(b)0.0000752
(c)0.000752
(d)0.00752
Answer:B (see page 22)
8)What is the Java expression for 4a2 + 2b * c?
(a)(4 * a) + (2 * b) * c
(b)(4 * a * a) + ((2 * b) * c)
(c)((4 * a * a) + (2 * b)) * c
(d)(4 + a * a) + ((2 + b) * c)
Answer:B (see page 25)
9)What is the Java expression for 27xy?
(a)27 + (x * y)
(b)27 * (x + y)
(c)27 * x * y
(d)27x * y
Answer:C (see page 25)
10)The value of the expression (int) 27.6 evaluates to:
(a)28
(b)27
(c)26
(d)None of the above.
Answer:B (see page 20)
⏹True/False
1)Java began as a language for home appliances.
Answer:True (see page 3)
2)Applets were designed to run as stand-alone applications.
Answer:False (see 5)
3)The Java programming language allows you to concatenate two strings using the plus sign.
Answer:True (see page 35)
4)Java is an interpreted language.
Answer:True (see page 9)
5)Java does not require that a variable be declared before it is used within a program.
Answer:False (see page 14)
6) A variable of type boolean can be explicitly converted to that of type int.
Answer:False (see page 21)
7)The modulus operator, %, returns the remainder of integer division.
Answer:True (see page 26)
8)The result of integer division is truncated in Java.
Answer:True (see page 27)
⏹Short Answer/Essay
1)There are two kinds of Java programs, applications and applets. Define and discuss each.
Answer:An application is just a regular. An applet is a little Java program that runs in a Web
browser. Applets and applications are almost identical. The difference is that applications are
meant to be run on your computer like any other program, whereas an applet is meant to be run from
a We
b browser. An applet can be sent to another location on the Internet and run there.
2)What are the values of the variables a, b, c, and d after the execution of the following expressions?
int a = 3;
int b = 12;
int c = 6;
int d = 1;
d = d * a;
c = c + 2 * a;
d = d - b / c;
c = c * b % c;
b = b / 2;
Answer:
a: 3
b: 6
c: 0
d: 2
3)what is the output produced by the following code?
class TestP{
public static void main(String args[]){
int x=5,y=32;
float a=8.6f,b=4.0f;
System.out.println("x="+x+"y="+y);
System.out.println("\ta="+a+"\tb="+b);
System.out.println("\nx+y="+x+y+"\ta*b="+a*b);
}
}
Answer:
x=5y=32
a=8.6 b=4.0
x+y=532 a*b=34.4。