Complete Test Bank Ch2 Data And Expressions - Java Foundations 4th Edition | Test Bank with Answer Key by John Lewis by John Lewis, Peter DePasquale, Joe Chase. DOCX document preview.
Chapter 2: Data and Expressions
Multiple Choice Questions:
1) Which of the following are examples of invalid string literals?
a) "Hello World!"
b) "4 score and 7 years ago, our forefathers brought forth..."
c) "z"
d) ""
e) none of the above
2) A(n) ________________ is a piece of data that we send to a method.
a) parameter
b) object
c) escape sequence
d) service
e) expression
3) Which of the following is an example of an invalid assignment or declaration statement?
a) int age = 30;
b) int money, dollars = 0, cents = 0;
c) int years = 1; months = 12; days = 365;
d) int length, meters, centimeters, millimeters;
e) none of the above
4) Java has two basic kinds of numeric values: _____________, which have no fractional part, and ___________________ which do.
a) shorts, longs
b) doubles, floating points
c) characters, bytes
d) integers, floating points
e) integers, longs
5) To assign a value stored in a double variable to an int variable, use
a) a cast operator
b) promotion
c) a print statement
d) a widening conversion
e) nothing. Java will do this automatically
6) Which of the following is not an arithmetic operation in Java?
a) +
b) -
c) *
d) %
e) These are all arithmetic operations in Java
7) Which modifier must be used in the declaration of a variable to make it a constant?
a) public
b) final
c) static
d) void
e) private
8) Which of the following data types only allows one of two possible values to be assigned?
a) char
b) int
c) boolean
d) float
e) long
9) Which of the following is an example of an invalid expression in Java?
- result = a + b;
- result = (14 + 9) * 5;
- result = ((19 + 4) - 5;
- result = firstNum % secondNum;
- result = firstNum / secondNum % thirdNum;
10) A _______________ is a list of characters in a particular order. Examples include ASCII and Unicode.
a) character literal
b) character set
c) char data type
d) control character
e) none of the above
11) A user types the number -12.6 in response to a prompt in a program. Which Scanner class method should be used to read the user input as a numeric value?
- nextInt()
- nextDouble()
- nextNegative()
- next()
- any of these methods will work
12) Which of the following lines allows a programmer to use the Scanner class in a Java program?
- import java.util.Scanner;
- using Scanner;
- include Scanner;
- include java.util.Scanner;
- any of the above will allow the programmer to use the Scanner class
13) Consider the following snippet of code:
System.out.println("30 plus 25 is " + 30 + 25);
What is printed by this line?
- 30 plus 25 is 55
- 30 plus 25 is 30
- 30 plus 25 is 25
- 30 plus 25 is 3025
- this snippet of code will result in a compiler error
14) Consider the following snippet of code:
int firstNum = 25;
int seconNum = 3;
double result = 25/3;
System.out.println(result);
What is output by this code?
- 8.0
- 8.333333333
- 8
- 8.3
- This snippet of code will result in a compiler error
15) Information is most likely to be lost in what kind of data conversion?
a) A widening conversion
b) A narrowing conversion
c) promotion
d) assignment conversion
e) no information will be lost in any of the conversions listed above
1) The print and the println methods are identical and can be used interchangeably.
2) A String literal may span multiple lines in the program code.
3) Java is a strongly-typed language.
4) Variables declared with the final modifier cannot have new values assigned to them.
5) The byte type can be assigned a larger range of numbers than the int type.
6) Java uses the ASCII character set to represent character data.
7) After the execution of the code below:
int counter = 9;
int result = counter++;
variable result contains the value 9.
8) The type of result produced by a mathematical expression depends on the types of the operands.
9) Promotion is a widening data conversion that is explicitly requested by the programmer.
10) The Scanner class must be imported using the import statement before it can be used in a program.
1) Write an application that prints out the following using a single call to the print method.
Hello!
How
are you?
2) Write a single println statement that prints out the following line.
"There is Thingumbob shouting!" the Bellman said,
3) Write a short program that declares a single integer variable called age and assigns it a value equal to your age. Also have it print out your age using the age variable. The output of your program should look similar to the following:
I am 30 years old.
4) Write a single line of Java code that computes the average of three integer variables – num1, num2, and num3 – and stores the result in a variable of type double called result.
5) Suppose your numeric grade is calculated using the following formula:
Test 1: 15%
Test 2: 15%
Final Exam: 30%
Homework: 10%
Programming Projects: 30%
Determine a good variable name to represent each of these values. Write a single expression to compute your grade assuming the variables have been declared and each one stores its value as an integer in the range 0 to 100.
6) Write a short application that converts inches to centimeters. It should read the number of inches from the user as a floating point number and output the number of inches and the number of centimeters at the end of the program. Note that there are 2.54 centimeters in an inch.
7) Consider the following snippet of code.
int iResult;
float fResult;
int rResult;
int iNum1 = 25;
int iNum2 = 8;
iResult = iNum1/iNum2;
rResult = iNum1%iNum2;
fResult = (float) iNum1/iNum2;
What values are stored in iResult, rResult, and fResult? Explain your answers.
8) Write a short program that allows the user to enter the year that they were born (as an integer) and outputs the age that they will be at the end of this year. Declare the current year as a constant.
9) What are some reasons that you might want to declare a variable as final?
10) Write a short application that computes the perimeter of a rectangle. It should allow the user to input the length and width of the rectangle as a double.
11) Consider the following snippet of code:
int firstNum = 5;
int secondNum = firstNum++;
int thirdNum = 6*(++firstNum);
What values are stored in firstNum, secondNum and thirdNum after these lines are executed? Explain your answer.
12) For the following expression, indicate the order that in which the operators will be evaluated.
a + b * c / (d - e)
13) Explain why it might not be safe for a programmer to use a narrowing conversion.
14) How are primitive data types and object data types related?
15) Explain the difference between the print and the println methods.
Document Information
Connected Book
Java Foundations 4th Edition | Test Bank with Answer Key by John Lewis
By John Lewis, Peter DePasquale, Joe Chase