Java Exam 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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.

How many lines are printed on the console when the following for loops are executed? for (int i = 1; i < 5; i += 2) { for (int j = 0; j < i; j++) { System.out.println(j); } }

4

How many times will the while loop that follows be executed if months has a value of 5? int i = 1; while (i < months) { futureValue = futureValue * (1 + monthlyInterestRate); i = i+1; }

4

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

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%

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

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.

Which of the following is true?

Java programs can run on different platforms, like Windows and Unix, without being recompiled.

According to standard naming conventions, which of the following is a typical class name?

Product

What does testing an application entail?

Running it to see if it works correctly.

Which of the following statements creates a Scanner object named sc?

Scanner sc = new Scanner(System.in);

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);

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.

When is the code within a catch block executed?

When the exception specified in the catch block is thrown in the try block

The has methods of the Scanner class let you

a and b only

To handle an exception using the try statement, you must

a and c only

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

Which of these data types are used to store whole numbers (no decimal positions)?

all of the above

You can use the Double class to

all of the above

In a while loop, the Boolean expression is tested

before the loop is executed

Which of these data types are used to store true/false values?

boolean

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

end

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 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

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? double price = total / quantity;

15.625

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

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 inner for loop" print to the console?

2

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 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"

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

The int data type can be used to store

whole numbers only

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

To achieve platform independence, the Java virtual machine

interprets the bytecode

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

An error that occurs after an application is running is known as a

runtime error

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?

stack trace

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);

static void getTax(double price, double rate, int qty)

To refer to a Java class from an application without qualification, you need to import the class unless that class

stored in a package that has already been imported

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 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 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

Assume that the variable named entry has a starting value of 9 and number has a value of 3. What is the value of entry after the following statements are executed? if ((entry > 9) || (entry/number == 3)) { entry--; } else if (entry == 9) { entry++; } else { entry = 3; }

8

Which class in the following list of classes are all exceptions subclasses of?

Exception

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; int j = i; while (j >= 1) { result *= j; j--; } System.out.println("Factorial " + i + " = " + result); }

Factorial 3 = 6

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?

FutureValueApp.main calls java.util.Scanner.nextDouble calls java.util.Scanner.next calls java.util.Scanner.throwFor

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

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

In an IDE, what tool do you use to enter and edit source code?

code editor

When you run a project, most IDEs automatically

compile the project

Which of the following can you not use the Java API documentation to research?

debuggers

You typically use comments in a Java application to

describe code that is difficult to understand

Which of the following statements converts a String object named subtotalString to a double variable named subtotal?

double subtotal = Double.parseDouble(subtotalString);

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


Kaugnay na mga set ng pag-aaral

Themes and Motifs in "Night" by Elie Wiesel

View Set

Chapter 26 - Soft Tissue Injuries

View Set

CH 11 Cardiac Monitoring and Cardiopulmonary Resuscitation

View Set

Series 7: All Missed ExamFX Questions

View Set

NR228 Edapt Fat Soluble Vitamins

View Set

14040A, BLUEPRINT READING AND SKETCHING. -Chapter 1 - Assignment

View Set