Final Exam (Ch. 1-7, 11)
Assume userName equals "Tom" and userAge equals 22. What is printed on the console when the following statement is executed? System.out.println(userAge + " \nis " + userName + "'s age.");
22 is Tom's age.
What is the value of kilos[1] after the code that follows is exectued? double[] kilos = {200, 100, 48, 59, 72}; for (int i = 0; i < kilos.length; i++) { kilos[i] *= 2.2; }
220.0
What is the value of the variable named counter after the statements that follow are executed? double percent = 0.54; boolean valid = true; int counter = 1; if ((percent > 0.50) && (valid == true)) { counter += 2; if (valid == true) { counter++; } if else (percent >= 0.50) { counter += 3; } } else { counter++; }
4
If number is 20, what is printed to the console after this code is executed? for (int i = 3; i <= number; i +=) { if (number & i == 0) { System.out.println(i); if (i == number) { System.out.println("special"); } break; } }
5
If the value of the int variable named quantity is 5, what is the value of total after this statement is executed? int total = quantity++;
5
What is the value of x after the following statements are executed? int x = 5 switch(x) { case 5: x += 2; case 6: x++; break; default: x *= 2; }
8
What is the name of the class that contains this code? private Clock clock; private Alarm alarm; public AlarmClock(Date time) {...}
AlarmClock
Compared to console applications, GUI applications
All three of the answers given are correct
Which of the following can you assign to a String variable?
All three of the answers given are correct
Which of these data types are used to store whole numbers (no decimal postions)?
All three of the answers given are correct
You can use the Double class
All three of the given answers are correct
What is the value of names[4] in the following array? String[] names = {"Jeff", "Dan", "Sally", "Jill", "Allie"};
Allie
Based on the naming recommendations in the book, which of the following is a good identifier for a constant that will be used to hold a base shipping charge?
BASE_SHIPPING_CHARGE
Which of the following statements creates a BigDecimal variable named decimalSubtotal from a double variable named subtotal, making sure that conversion problems are avoided.
BigDecimal decimalSubtotal = new BigDecimal(Double.toString(subtotal));
Which of the following is NOT a feature of using an Installer program to deploy an application?
It automatically updates the application when a new version is available.
Which of the following is NOT a feature of using Java Web Start to deploy an application?
It has no significant security restrictions
Which of the following is a feature of using an executable JAR file to deploy an application?
It has no significant security restrictions.
What happens when the code that follows is executed? int[] nums = new int[4]; for (int i = 0; i <= nums.length; i++) { nums[i] = i; } System.out.println(nums[2]);
It throws an ArrayIndexOutOfBoundsException.
Which of the following is NOT true of using a IDE to develop Java programs?
It's harder to detect syntax errors
If you use the equals() or equalsIngnoreCase() method to test the value of a String object, you should include code that prevents a
NullPointerException
An executable JAR file contains
Only two of the three answers given are correct
To handle an exception using the try statement, you must
Only two of the three answers given are correct
You can use a logical operator to
Only two of the three answers given are correct
What does testing an application entail?
Running it to see if it works correctly.
What must you do if you code an infinite loop in an application?
Stop the application to end the loop
Which of the following statements prints a variable named total to the console, followed by a new line character?
System.out.println(total);
When is the code within a catch block executed?
When the exception specified in the catch block is thrown in the try block
Which of the following is NOT a benefit of a typical IDE for Java?
Your code compiles and runs faster.
To make it easier for a user to run an executable JAR file for a console application, you can create
a script file such as a BAT or SH file
If the instance variables of the Employee class are declared as follows, which of the following statements is most likely to be in the constructor of this class? private String name; private Address address; private long employeeNumber;
address = new Address();
When a statement within a try block causes an exception, the remaining statements in the try block
aren't executed
In a while loop, the Boolean expression is tested
before the loop is executed
Assume the user enters "X" at the first console prompt when the following code is executed. The value of numOfEntries will be String option = ""; int numOfEntries = 0; do { numOfEntries++; System.out.print("Enter 'C' to continue."); option = sc.next(); } while (choice.equalsIgnoreCase("C"));
equal to one
The goal of testing is to
find all errors in the application
Which of the following formats can you NOT apply using the NumberFormat class?
fraction
Which of the values below will NOT be printed to the console when this code is executed? for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { if (i == j) { continue; } System.out.println("i = " + i + " j = " + j); } }
i=1 j=1
What is the value of nums[2] after the following statements are executed? int[] values = {2, 3, 5, 7, 9}; int[] nums = values; values[2] = 1;
1
What is the value of routeNumber after the following statement is executed if the value of zipCode is 93705? switch (zipCode) { case 93705: case 93706: routeNumber = 1; break; case 93710: case 93720: routeNumber = 2; break; defualt: routeNumber = 0; break; }
1
What is the value of the variable named len after the code that follows is executed? int[] [] nums = { {1, 2, 3}, {3, 4, 5, 6, 8}, {1}, {8, 8} }; int len = nums[2].length;
1
What numbers does the code folows print to the console? int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; for (int i = 0; i < 9; i++) { System.out.println(numbers[i]); }
1-9
What is the range of values that x can hold after this statement is executed? double x = Math.random() * 10 + 100;
100<=x<110
If int variable x=100, float variable y=1.587, and double variable z=4.8, what does the following expression return? x + Math.round(y) + (int) z
106
Assume that before the following code is executed, the value of totalOne is 6.728, the value of totalTwo is 116, and both are BigDecimal objects. What is the value of totalFinal after the code is executed? totalOne = totalOne.setScale(2, RoundingMode.HALF_UP); BigDecimal totalFinal = total.Two.add(totalOne);
122.73
If the int variables a=5, b=2, and c=10, what is the value of c after the following statement is executed? c = c + a * b - 5
15
How many lines are printed on the console when the following for loop is executed? for (int j = 10; j < 40; j*= 2) { System.out.println(j); }
2
When you're using an IDE's debugger and exception reaches a breakpoint, you can click
the Step Into button to execute the current statement and move to the next statement
What feature of an IDE can help you enter a method by displaying a list of methods that are available from an object or class?
the code completion feature
What happens when you use both integer and double values in an arithmetic expression?
the integer values are cast to double values
What is the value of maxNumber after the code that follows is executed? int number = 0; int maxNumber = 0; for (int i = 0; i <= 10; i++ { number = (int) (Math.random() * 100); if (number >= maxNumber) { maxNumber = number; } }
the largest of eleven random numbers from 0 to 99
If you use a short-circuit operator to combine two expressions
the second expression is evaluated only if it can affect the result
Consider the following condition. It will be true when (!endProgram.equalsIgnoreCase("n"))
the value of endProgram doesn't equal "n" or "N"
A syntax error occurs when
there's a syntax error in a Java statement
What is the difference between these two constructors? public Transaction() {...} publick Transaction(int x, int y) {...}
they have different signatures
You must use the this keyword
to refer to an instance variable from a method when it has the same name as a mehtod parameter
If a class contains a main() method, that method is executed
when the class is run
You should validate user entries rather than catch and handle exceptions caused by invalid entries whenever possible because
your code will run faster
You use an escape sequence to
include a special character in a string
Which of the following statements uses a compound assignment operator?
index += 1;
In most modern implementations of the JVM, bytecode is
input to the JIT (just-in-time) compliler
The attributes of an object are often coded as
instance variables
Which of the following statements will NOT calculates the change in sales between two double variables named thisYTDSales and lastYTDSales, rounds the result to an integer, and stores it in an integer in an int variable named salesChange?
int salesChange = Math.round(thisYTDSales - lastYTDSales, 0);
Which of the following statements gets the maximum number of Customer objects that the following array can store? Customer[] customers = new Customer[55];
int size = customers.length;
To achieve platform independence, the Java virtual machine
interprets the bytecode
If the duesOwed instance variable in the code that follows is set in the setDuesOwed() method, which of the following is NOT true about the setDuesOwed() method? private String name; //instance variable private double duesOwed; //instance variable public Member(String s) { name = s; setDuesOwed(name); }
it's a static method
The extension for a Java source file is
java
Which package is automatically available to all Java programs?
java.lang
Output example 5-1 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:818) at java.util.Scanner.next(Scanner.java:1420) at java.util.Scanner.nextDouble(Scanner.java:2324) at FutureValueApp.main(FutureValueApp.java:17) (Refer to output example 5-1.) Which statement would you look at to find the source of the problem?
line 17 in the FutureValueApp class
An error that lets the application run but produces the wrong results is known as a
logic error
The class that contains the main() method for an application is known as the
main class
Explicit casts must be used to preform
narrowing conversions
If the constructor that follows is in the Employee class, what other form of constructor, if any, is implicitly provided? public Employee(String name, int salary){...}
none
Which of the following can you NOT assign to a numeric variable?
null
In Java, exceptions are
objects
Unlike a data type, a reference type
points to the data in another area of internal storage
Which of these statements is NOT true about the method that follows? public void deposit(Date date, double amt) { this.balance += amt; this.printDeposit(accountNum, date, amt); }
printDeposit is a static method
An error that occurs after an application is running is known as a
runtime error
To use the binarySearch() method of the Arrays class on an array of ints, you must first
sort the array
The values held by the attributes of an object describe the object's
state
If the variables named price and rate are doubles and qty is an int, what is a possible declaration for the getTax() method that's called by the statement that follows? double tax = TacCalculator.getTax(price, rate, qty);
static double getTax(double x, double y, int z)
How many lines are printed on the console when the following for loop is executed? for (int i = 2; i < 10; i++) { System.out.println(i); }
8
Code example 4-1 int limit = 0; for (int i = 10; i >= limit; i -= 2) { for (int j = i; j <= 1; j++) { System.out.println("In inner for loop"); } System.out.println("In outer for loop"); } (Refer to code example 4-1.) How many times does "In outer for loop" print to the console?
6
If the double variables x=2.5 and y=4.0, what is the value of z after this staement is executed? int z = (int) x + (int) y;
6
If the variable named input is 755, what is the value of r after these statements are executed? NumberFormat n = NumberFormat.getNumberInstance(); n.setMinimumFractionDigits(3); String r = n.format(input);
755.000
Code example 2-1 import java.util.Scanner; public class InvoiceApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equals("y")) { System.out.print("Enter subtotal: "); double subtotal = sc.nextDouble(); double salesTax = subtotal * .0875; double invoiceTotal = subtotal + salesTax; String message = "Subtotal = " + subtotal + "\n" + " Sales tax = " + salesTax + "\n" + "Invoice total = " + invoiceTotal + "\n\n System.out.println(message); System.out.print("Continue? Enter y or n: "; choice = sc.next(); } } } (Refer to code example 2-1.) The name of the file that contains this source code must be
InvoiceApp.java
You code an access modifier on a static method to indicate if it
can be called from other classes
Unlike an if/else statement, a switch statement
can't perform an operation based on the result of a Boolean expression
Unlike a variable, a constant can't
change in value as an application runs
In an IDE, what tool do you use to enter and edit source code?
code editor
What is the value of the string named lowest when the code that follows is executed? String[] types = {"lux", "eco", "comp", "mid"}; Arrays.sort(types); String lowest = types[0];
comp
When you run a project, most IDEs automatically
compile the project
Which of the following are two types of desktop applications that you can develop in Java?
console and GUI
Which of the following statements do you use to jump to the top of a loop from within loop?
continue
Which of the following can you NOT use the Java API documentation to research?
debuggers
Code example 5-1 import java.util.Scanner; import java.text.NumberFormat; public class WeightConverter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String prompt = "Enter weight in lbs: "; boolean isValid = false; double weightInPounds = 0.0; while (!isValid) { weightInPounds = getDouble(sc, prompt); if (weightInPounds > 0) { isValid = true; } else { System.out.println("Weight must be greater than 0."); } } double weightInKilos = weightInPounds / 2.2; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); String message = weightInPounds + " lbs\nequals\n" + nf.format(weightInKilos) + " kgs\n"; System.out.print(message); } public static double getDouble(Scanner sc, String prompt) } double d = 0.0; boolean isValid = false; while (!isValid) { System.out.print(prompt); if (sc.hasNextDouble()) { d = sc.nextDouble(); isValid = true; } else { System.out.println ("Error! Invalid decimal value. Try Again."); } sc.nextLine(); } return d; } } (Refer to code example 5-1.) If the user enters -1 at the first console prompt, what does the code do?
displays an error message from the main() method
Which of these data types are used to store with decimal positions?
double
Which of the following is a valid statement for declaring and initializing a double variable named length to a starting value of 120?
double length = 120.0;
Which of the following statements converts a String object named subtotalString to a double variable named subtotal?
double subtotal = Double.parseDouble(subtotalString);
The array that follows is an array of double[] grades = new double[3];
double values
If a user's computer has an appropriate version of the JRE installed, that user can run an executable JAR file for a GUI application by
double-clicking on the JAR file
When you call the equals() mehtod in the String class without knowing how it's coded, you're taking advantage of what object-oriented concept?
encapsulation