Practice Exam II
murach.business
Directory structure 10-1 invoice InvoiceApp.java murach business Invoice.java LineItem.java Product.java database ProductDB.java presentation Validator.java (Refer to directory structure 10-1.) If the directory structure is for an Invoice application that uses packages, what is the name of the package that contains the Invoice.java class?
package murach.database
Directory structure 10-1 invoice InvoiceApp.java murach business Invoice.java LineItem.java Product.java database ProductDB.java presentation Validator.java (Refer to directory structure 10-1.) If the directory structure is for an application that uses packages, what statement must the first statement of the ProductDB class be?
a superclass object to a subclass object
Explicit casting is always required to cast
8
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); }
5
How many times will the while loop that follows be executed if months has a value of 6? int i = 1; while (i < months) { futureValue = futureValue * (1 + monthlyInterestRate); i = i+1; }
All objects
If a method accepts an Object object, what other types of objects can it accept?
All objects.
If a method accepts an Object object, what other types of objects can it accept?
The setAverage() method can't access the average instance variable.
If name is a String instance variable, average is a double instance variable, and numOfStudents is a static int variable, why won't the following code from a class compile? public Student(String s) { name = s; average = getAverage(name); numOfStudents++; } public double getAverage(String x) { numOfStudents++; double ave = StudentDB.getAverage(x); return ave; } public static void setAverage(double g) { average = g; }
Calls the calculatePayment() method in the Loan class
If the Loan class contains a final method named calculatePayment, what does the following code do? HomeLoan loan = new HomeLoan(amount, months); loan.calculatePayment(); Loan <----- HomeLoan
Print "false" to the console
If the Point class doesn't override the equals() method in the Object class, what will the following code do? Point pointOne = new Point(3, 4); Point pointTwo = new Point(3, 4); System.out.println(pointOne.equals(pointTwo));
Print "false" to the console.
If the Point class doesn't override the equals() method in the Object class, what will the following code do? Point pointOne = new Point(3, 4); Point pointTwo = new Point(3, 4); System.out.println(pointOne.equals(pointTwo));
It's a static method.
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); }
address = new Address();
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;
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);
static double getTax(double x, double y, int z)
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 = TaxCalculator.getTax(price, rate, qty);
the second expression is evaluated only if it can affect the result
If you use a short-circuit operator to combine two expressions
long
In the statement that follows, x can not be what data type? switch(x){statements}
html file
In what type of file is javadoc documentation stored?
A method that changes the annual interest rate for all bank accounts
In which of the following situations would it make most sense to code a static method rather than a regular method in a class that defines a bank account?
Automobile and Motorcycle
Inheritance hierarchy 8-1 (Refer to inheritance hierarchy 8-1.) If the MotorVehicle class contains a protected method named getEngineType, what other class or classes can access this method?
WaterCraft
Inheritance hierarchy 8-1 (Refer to inheritance hierarchy 8-1.) If the constructor that follows is called, it calls a constructor from which class? public Sailboat(int sails) { super(); this.sails = sails; }
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 this output called?
line 17 in the FutureValueApp class
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?
java
The extension for a Java source file is
state
The values held by the attributes of an object describe the object's
the instanceof keyword
To check if an object is created from the specified class, you can use
override the equals() method of the Object class
To compare the instance variables of two objects, you can
code a package statement as the first statement in the class file
To include a class in a package, you
web browser
To view javadoc documentation, you use a ________________.
All classes in the same package and to subclasses
What class or classes is a method that is declared protected available to?
All classes in the same package.
What class or classes is a variable available to if it is declared without an access modifier?
6
What does the following code display? public class RentalApp { public static void main(String[] args) { Rental r = new Rental(); r.setNumOfPersons(5); r.addPerson(r); System.out.println(r.getNumOfPersons()); } } public class Rental { private int numOfPersons; public int getNumOfPersons() { return numOfPersons; } public void setNumOfPersons(int numOfPersons) { this.numOfPersons = numOfPersons; } public void addPerson(Rental rental) { rental.numOfPersons++; } }
It makes the Scanner class available to the application without qualification.
What does the following statement do in a Java application? import java.util.Scanner;
polymorphism
What feature allows you to treat an Admin object as though it were an Account object in this inheritance hierarchy?
Polymorphism
What feature allows you to treat an Admin object as though it were an Account object in this inheritance hierarchy? Account <------(Development <------Production) Marketing, Admin
They have different signatures.
What is the difference between these two constructors? public Transaction(){...} public Transaction(int x, int y){...}
AlarmClock
What is the name of the class that contains this code? private Clock clock; private Alarm alarm; public AlarmClock(Date time){...}
Factorial 3 = 6
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); }
10
What is the value of x after the following statements are executed? int x = 5; switch(x) { case 5: x += 4; case 6: x++; break; default: x *= 2; break; }
extends
What keyword do you use in a class declaration to create a subclass?
it must override all of the abstract methods in the abstract class
When a subclass that is not declared as abstract inherits an abstract class,
Code an import statement for the class. Add the library to your project. (Both b and c)
When using an IDE, what must you do to use a class that's stored in a library?
b. Code an import statement for the class. c. Add the library to your project. (both b and c)
When using an IDE, what must you do to use a class that's stored in a library?
encapsulation
When you call the equals() method in the String class without knowing how it's coded, you're taking advantage of what object-oriented concept?
Immediately before the method
Where must you code the javadoc comment for a method?
a call to a private method of the superclass
Which of the following can you not code in a subclass?
They make it easy for other programmers to learn about your class.
Which of the following is a benefit of using javadoc comments?
CustomerMainApp
Which of the following is a valid class name?
CustomerMaintApp
Which of the following is a valid class name?
/** Calculates the balance due */
Which of the following is a valid javadoc comment?
salesTax
Which of the following is a valid variable name?
getFractionInstance()
Which of the following is not a constructor of the NumberFormat class?
To prevent other developers from using the class
Which of the following is not a reason to create a package?
to prevent other developers from using the class
Which of the following is not a reason to create a package?
It makes it easier to document the tasks performed by an application.
Which of the following is not an advantage of using a three-tiered architecture for your applications?
It includes the code that implements each documented method.
Which of the following is not true about generated Java documentation?
double calculateMilesPerGallon(double s){...}
Which of the following method declarations overrides this method? double calculateMilesPerGallon(double speed){...}
toString()
Which of the following methods can always be called from a Product object?
String city = sc.next();
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?
int, long, and short
Which of these data types are used to store whole numbers (no decimal positions)?
printDeposit is a static method
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); }
HTML tags
You can use ____________ within a javadoc comment to apply formatting to the text when it's displayed.
to prevent a class from being inherited, to prevent a method from being overridden, to prevent a method from assigning a new value to a parameter (all of the above)
You can use the final keyword
your code will run faster
You should validate user entries rather than catch and handle exceptions caused by invalid entries whenever possible because
library
A __________ is typically stored in a JAR file and contains one or more packages that you want to make available to other projects.
Method
A _____________ can operate directly on the data in a reference type that's passed to it and change that data.
bytecode can't be interpreted properly
A runtime error occurs when
there's a syntax error in a Java statement
A syntax error occurs when
Product
According to standard naming conventions, which of the following is a typical class name?
122.73
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);
BASE_SHIPPING_CHARGE
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?
employeePhoneNumber
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?
can't be used outside of the set of braces that it's declared in
Block scope means that a variable
6
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?
Displays an error message from the getDouble() method.
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 "two hundred" at the console prompt, what does the code do?
displays an error message from the getDouble() method
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 "two hundred" at the console prompt, what does the code do?
Displays an error from the main() method.
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
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?