a. to work with one-dimensional arrays
b. to work with all the elements of an array
c. to work with jagged arrays
d. to work with arrays that contain integers
Answer: b. to work with all the elements of an array
Which of the following is a benefit of using Javadoc comments?
a. They show other programmers how to code a class.
b. They make it easy for other programmers to learn about your class.
c. They allow other programmers to view the source code for your class.
d. They make your classes easier to debug.
Answer: b. They make it easy for other programmers to learn about your class.
a. code an import statement as the first statement of the class
b. code a package statement as the first statement in the class file
c. code the name of the package in the class declaration
d. code a package statement as the last statement in the class file
Answer: b. code a package statement as the first statement in the class file
a. can be both a superclass and a subclass
b. can be inherited only by other classes in the Java API
c. can have only one subclass
d. can be inherited only by user-defined classes
Answer: a. can be both a superclass and a subclass
a. is used to initialize a static variable that can't be initialized in the declaration
b. is executed when a static method of the class is called
c. is executed when an instance of the class is created
d. all of the above
e. a and b only
You can create a ____ partition to hold files that are created temporarily, such as files used for printing documents (spool files) and files used to record monitoring and administration data, often called log files.
If you have configured your prompt so that it does not show your working directory, you can use the ____ command to verify in what directory you are located, along with the directory path.
a. make sure the application works with invalid data
b. fix all errors in the application
c. make sure the application works with valid data
d. find all errors in the application
In the Linux Bash shell, the ____ key combination deletes the content of the command line from the current cursor position to the end of the command line.
a. no value is returned since the catch block doesn't return a value
b. a compile-time error occurs since the catch block isn't properly coded
c. 0.0 is returned
d. 2.0 is returned
a. FutureValueApp.main calls java.util.Scanner.nextDouble calls java.util.Scanner.next calls java.util.Scanner.throwFor
b. java.util.Scanner.throwFor calls java.util.Scanner.next calls java.util.Scanner.nextDouble calls FutureValueApp.main
c. you can't tell from the information given
a. a method log
b. a stack trace
c. an exception handler
d. an exception hierarchy
Answer: a stack trace
Output example:
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)
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
}
}
a. It prints "Invalid integer" to the console.
b. The code compiles but causes a runtime error.
c. The code doesn't compile.
d. It prints "Valid integer" to the console.
Answer: The code doesn't compile.
Output example:
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)
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 == false)
{
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 == false)
{
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;
}
}
If the user enters -1 at the first console prompt, what does the code do?
a. figures the weight in kilograms
b. displays an error message from the main method
c. catches an exception
d. displays an error message from the getDouble method
Answer: b. displays an error message from the main method
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 == false)
{
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 == false)
{
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;
}
}
If the user enters "two hundred" at the console prompt, what does the code do?
a. figures the weight in kilograms
b. displays an error message from the main method
c. displays an error message from the getDouble method
d. throws an InputMismatchException
Answer: c. displays an error message from the getDouble method
a. When the exception specified in the catch block is thrown in the try block
b. When a runtime error occurs
c. When the try block finishes executing
d. When the code in the try block doesn't compile
Answer: a. When the exception specified in the catch block is thrown in the try block
What is the main reason for using a generic data validation method?
a. It runs faster than the validation code in the main method.
b. It saves you from writing variations of the same code again and again to check multiple data entries.
c. It prevents NumberFormatExceptions from being thrown.
d. None of the above.
Answer: b. It saves you from writing variations of the same code again and again to check multiple data entries.
To handle an exception using the try statement, you must
a. code a try block around the statement that may throw the exceptions
b. code a finally block that contains the statements that will be executed at the end of the try statement
c. code a catch block that contains the statements that you want to be executed when the exception occurs
d. all of the above
e. a and c only
To determine the cause of an unhandled exception, you can
a. use the name of the exception class that's displayed
b. use the error message that's displayed
c. use the information in the stack trace
d. all of the above
In the Linux Bash shell, the ____ key combination deletes the content of the command line from the current cursor position to the end of the command line.
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)
What is the order of method calls?
a. java.util.Scanner.throwFor calls java.util.Scanner.next calls java.util.Scanner.nextDouble calls FutureValueApp.main
b. FutureValueApp.main calls java.util.Scanner.nextDouble calls java.util.Scanner.next calls java.util.Scanner.throwFor
c. you can't tell from the information given
Answer: b. FutureValueApp.main calls java.util.Scanner.nextDouble calls java.util.Scanner.next calls java.util.Scanner.throwFor
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)
Which statement would you look at to find the source of the problem?
a. line 818 in the Scanner class
b. line 2324 in the Scanner class
c. line 17 in the FutureValueApp class
d. line 1420 in the Scanner class
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)
What caused the exception to occur?
a. You can't tell from the information given.
b. The user didn't enter the type of data the program was expecting.
c. The program couldn't format the double value that the user entered.
Answer: b. The user didn't enter the type of data the program was expecting.
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) What is this output called?
a. a method log
b. a stack trace
c. an exception handler
d. an exception hierarchy
You should validate user entries rather than catch and handle exceptions caused by invalid entries whenever possible because
a. all of the above
b. data validation code should only be used for situations that are truly exceptional
c. your code will run faster
d. you can more accurately determine the cause of an invalid entry
To handle an exception using the try statement, you must
a. code a try block around the statement that may throw the exceptions
b. code a finally block that contains the statements that will be executed at the end of the try statement
c. code a catch block that contains the statements that you want to be executed when the exception occurs
d. all of the above
e. a and c only
To determine the cause of an unhandled exception, you can
a. use the name of the exception class that's displayed
b. use the error message that's displayed
c. use the information in the stack trace
d. all of the above
a. check if the user has entered data at the console
b. check if the data entered at the console can be converted to a specific data type
c. retrieve and discard data that isn't required by the application
d. all of the above
e. a and b only
Which statement dynamically assigns an image named car in the drawable folder to an ImageView control?
a. car.setImageResource(R.drawable.image);
b. image.setImageResource(R.drawable.car);
c. image.setImageResource(car.R.drawable);
d. SetImageResource.image(R.drawable.car);
Answer: b. image.setImageResource(R.drawable.car);
Which of the following correctly completes the statement below to instantiate a SharedPreferences object? SharedPreferences sharedPref =
a. PreferenceManager.getDefaultSharedPreferences(this);
b. SharedPreferences.PreferenceManager.getDefault(this);
c. SharedPreferences.getDefaultPreferenceManager(this);
d. PreferenceManager.SharedPreferences.getDefault(this);
Answer: a. PreferenceManager.getDefaultSharedPreferences(this);
What's the first step when writing persistent data using a SharedPreferences file?
a. Create a SharedPreferences.Editor object
b. Obtain an instance of the SharedPreferences file
c. Assign values into SharedPreferences objects
d. Save the values to the preferences file
Answer: b. Obtain an instance of the SharedPreferences file
Before an app attempts to connect to a network connection, what action should be taken?
a. The app should scan the quality of the network.
b. No action needs to be taken.
c. The app should see if an Internet connection is available.
d. The app should open a network's web browser.
Answer: c. The app should see if an Internet connection is available.
Which XML code statement places an icon named ic_city.png to the right of the text in a ListView display?
a. android:drawableRight="@drawable/ic_city"
b. icons:drawableRight="@drawable/ic_city"
c. icons:iconRight="@icon/ic_city"
d. android:iconRight="@icon/ic_city"
Answer: a. android:drawableRight="@drawable/ic_city"
What statement should you put at the end of a switch statement if you want a block of code to be executed in the event none of the case statement values matches?