Practice Exam I Questions
If number is 20, what is printed to the console after this code is executed? for (int i = 3; i <= number; i += 2) { 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; break; }
8
If the variable named percent is 0.0755, what is the value of p after this statement is executed? String p = NumberFormat.getPercentInstance().format(percent);
8%
If the variable named percent is 0.0755, what is the value of p after this statement is executed? String p = NumberFormat.getPercentInstance().format(percent);
8%
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));
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
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
What is the main reason for coding a method to validate a specific type of entry?
It saves you from writing variations of the same code again and again to check multiple data entries.
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.) If the user enters "Y" when asked if he/she wants to continue, the application will
It will terminate
Which of the following is not true of using a IDE to develop Java programs
It's harder to detect syntax errors
In Java, exceptions are
Objects
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 uses a Scanner variable named sc to return the next value the user enters at the consolse as a String object and store it in a variable named city?
String city = sc.next();
Which of the following statements converts a double variable named subtotal to a String object named subtotalString?
String subtotalString = Double.toString(subtotal);
Which of the following statements prints a variable named total to the console, followed by a next line character?
System.out.println(total);
Consider the code that follows. What does it do? String value = "2"; boolean tryAgain = true; while (tryAgain == true) { try { int num = Integer.parseInt(value); tryAgain = false; } System.out.println("Valid integer"); catch(NumberFormatException nfe) { System.out.println("Invalid integer"); System.out.print("Enter an integer"); value = sc.next } }
The code doesn't compile
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.) What caused the exception to occur?
The user didn't enter the type of data the program was expecting
What does the following statement do if userNumber has an integer value of 14 and userEntry has a string value of "two"? userNumber = Integer.parseInt(userEntry);
Throws an exception
Which of the following is not a benefit of a typical IDE for Java?
Your code compiles and runs faster
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.) What is this output called?
a stack trace
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.) What is the order of method calls?
a. FutureValueApp.main calls java.util.Scanner.nextDouble calls java.util.Scanner.next calls java.util.Scanner.throwFor
In a do-while loop, the Boolean expression is tested
after the loop is executed
Using the BigDecimal class, you can
avoid rounding errors that may occur otherwise in business application
Which of these data types are used to store true or false values
boolean
The goal of testing is to
find all errors in the application
Which of these data types are used to store numbers with decimal positions?
float
Which of the following formats can you not apply using the NumberFormat class?
fraction
Which of the following is not a binary operator?
++
What happens in the method that follows when s is "two"? public double parseInterval(String s) { double interval = 0.0; try { interval = Double.parseDouble(s); } catch(NumberFormatException e) { } return interval; }
0.0 is returned
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.587F, 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 = totalTwo.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
Given a double variable named total with a value of 62.5 and an integer variable named quantity with a value of 4, what will the value of price be after you execute this statement?
15.625
When you're using an IDE's debugger and execution 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 happens when you use both integer and double values in an arithmetic expression?
the integer values are cast to double values
A syntax error occurs when
there's a syntax error in a Java statement
If a class contains a main() method, that method is executed
when the class is run
The int data type can be used to store
whole numbers only
Which of the following statements calculates the change in sales between two double variables named this YTDSales 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);
The extension for a Java source file is
java
What 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
Explicit cases can be use to perform
narrowing conversions
Unlike a data type, a reference type
points to the data in another area of internal storage
An IDE typically stores all of the files for an application in a
project
An error that occurs after an application is running is known as a
runtime error
You typically use comments in a Java application to
describe code that is difficult to understand
You can use a System.out object to:
display a blank line on a console
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 118 at the console prompt, what is the third line of the resulting console display?
53.64 kgs
If the double variables x = 2.5 and y = 4.0, what is the value of z after this statement 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 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.) What type of data validation does this program do?
Both range and valid data type checking
Which of the following is a valid calss name
CustomerMaintApp
What is the third line printed to the console in the code that follows? int items = 3; for (int i = 1; i <= items; i++) { int result = 1; for (int j = i; j >= 1; j--) { result *= j; } System.out.println("Factorial " + i + " = " + result); }
Factorial 3=6
What does debugging an application entail
Identifying and fixing any bugs
Which of the following statements creates an Integer object named countObject from an int variable named count?
Integer countObject = new Integer(count);
Which of the following statements do you use to jump to the end of a loop from within the loop?
break
A runtime error occurs when
bytecode can't be interpreted properly
Block scope means that the variable
can't be used outside of the set of braces it's declared in
Unlike an if/else statement, a switch statement
can't perform an operation based on he result of a boolean expression
Unlike a variable, a constant can't
change in value as an application runs
Unlike a variable, a constant can't
change in value as the application runs
In an IDE, what tool do you use to enter and edit source code?
code editor
Which of the following are two types of desktop applications that you can develop in Java?
console and GUI
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; } }
displays an error message from the main() method
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;
Based on the naming recommendations in the book, which of the following is a good identifier for a variable that will be used to hold an employee's phone number?
employeePhoneNumber