Unit 3 Test Review Complete PDF

Document Details

UnselfishAmethyst2558

Uploaded by UnselfishAmethyst2558

Tags

java programming conditional statements programming review programming examples

Summary

This document is a review of Java programming concepts. It includes examples of conditional statements, calculations, and variable types, accompanied by questions and answers. This is a student review document.

Full Transcript

## Unit 3 Test Review ### 1. Using the following variable values, what will be the value of *blah*? Assume *blah* is properly declared. Box your answer. * int height = 15; * String fruit = "mango"; * int width = 28; * double speed = 7.5; * String bird = "sparrow"; * double distance = 18; * Stri...

## Unit 3 Test Review ### 1. Using the following variable values, what will be the value of *blah*? Assume *blah* is properly declared. Box your answer. * int height = 15; * String fruit = "mango"; * int width = 28; * double speed = 7.5; * String bird = "sparrow"; * double distance = 18; * String dessert = "cookie"; **(a)** blah = height + width == 43; **True** **(b)** blah = !(distance > 20) || speed == 7.5; **True** **(c)** blah = bird.compareTo("Sparrow") < 0; **False** **(d)** blah = height % 3 == 0 && width / 4 >= 7; **True** **(e)** blah = dessert.indexOf('k') == 0; **False** **(f)** blah = speed * 2 != distance && ! (width < 25); **True** **(g)** blah = height != width; **True** **(h)** blah = fruit.equals("Mango") || dessert.length() > 5; **True** **(i)** blah = Math.pow(speed, 2) <= height; **False** **(j)** blah = Math.min(height, width) > Math.sqrt(distance); **True** ### 2. What will be printed to the console if the following lines of code are executed? **(a)** * isVerified = false; * duration = 2; * animal = "elephant"; **Console** * H * K * L **(b)** * isVerified = true; * duration = 6; * animal = null; **Console** * B * D * I * L **(c)** * isVerified = false; * duration = 3; * animal = ""; **Console** * A * D * L **(d)** * isVerified = true; * duration = 8; * animal = "zebra"; **Console** * F * G * K * L **(e)** * isVerified = false; * duration = 7; * animal = "cat"; **Console** * H * J * L **(f)** * isVerified = true; * duration = 5; * animal = "snake"; **Console** * F * G * J * L ### 3. What will be printed to the console after the following lines of code are executed? **(a)** * Device dev = new Device(); * System.out.print(dev.process(3)); * dev.process(); **Console** * 59 * Device: active - basic [low] **(b)** * Device dev = new Device (64, "premium", false); * System.out.print(dev.process(10)); * dev.process(); **Console** * 171 * Device: active - premium [low] **(c)** * Device dev = new Device("proModel"); * System.out.print(dev.process(5)); * dev.process(); **Console** * 33 * Device: active - proModel [low] **(d)** * Device dev = new Device (9, "simple"); * System.out.print(dev.process(8)); * dev.process(); **Console** * 7 * Device: inactive - Simple [low] ### 4. Evaluate the following boolean expression. Must show work or provide explanation for credit. Box your final answer. !true || true && !false && ! (true || true) **False** ### 5. Write a conditional structure that prints the correct message based on the current value of the variable *gradeValue*. The table to the right shows the various messages that correspond to the values of *gradeValue*. Assume that *gradeValue* has been declared and given a value. | gradeValue | message | |---|---| | 90-100 | A | | 80-89,9999... | B | | 70-79.9999... | C | | Everything else | Not Passing | ```java if (gradeValue >= 90) { System.out.print("A"); } else if (gradeValue >= 80) { System.out.print("B"); } else if (gradeValue >= 70) { System.out.print("C"); } else { System.out.print("Not Passing"); } ``` ### 6. Write a method called *computeTaxes()* that takes in an individual's yearly income and outputs how much they were supposed to pay in taxes. The table on the right illustrates how an individual's income is to be taxed. Don't forget to return the taxes value (DO NOT PRINT IT!). | Income Range | Taxed at this rate | |---|---| | < $30,000 | 10% | | $30,000-74,999.99 | 15% | | $75,000-149,999.99 | 20% | | $150,000.00+ | 30% | ```java public double computeTaxes(double income) { double taxes = 0; if (income > 150000){ taxes += (income - 150000) * 0.3; income = 150000; } if (income > 75000) { taxes += (income - 75000) * 0.2; income = 75000; } if (income > 30000) { taxes += (income - 30000) * 0.15; income = 30000; } taxes += income * 0.1; return taxes; } ``` ### 7. Write a method called *triangleType()* that takes in three parameters, decimals values for the length of the sides of a triangle, and returns a *String* that describes the type of triangle it is. You may assume that side *c* is the longest side if they are not all equivalent. How the triangles are being classified is as follows: * "Equilateral Triangle" - all sides of the triangle are equal * “Isosceles Triangle” - only two sides of the triangle are equal * “Scalene Triangle” - none of the sides of the triangle are equal * “Right Triangle” - if the sides of the triangle satisfies the Pythagorean Theorem * If the Pythagorean Theorem is satisfied and the triangle is also Isosceles or Scalene, you should **ONLY** return "Right Triangle" ```java public String triangleType(double a, double b, double c) { if (Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2)) { return "Right Triangle"; } else if (a == b && b == c) { return "Equilateral Triangle"; } else if (a == b || a == c || b == c) { return "Isosceles Triangle"; } else { return "Scalene Triangle"; } } ``` ### 8. Use De Morgan's Laws to simplify the expressions as much as possible. Assume that the variables have been declared as follows: *numPets* is an *int*, *gpa* is a *double*, and *isFunny* and *isCool* are *booleans*. **(a)** ! (numPets > 3 || isFunny) **Solution:** numPets <= 3 && !isFunny **(b)** ! (isCool && gpa != 4.0) **Solution:** !isCool || gpa == 4.0 ### 9. Using the following variable values, what will be the value of *blah*? Assume *blah* is properly declared. If an error would occur, simply state **Error** and give a BRIEF explanation. Box your answer. * String word = "bang"; * String quote = "null"; * String phrase = new String("bang"); * String ahh; * String text = phrase; * String ugh = null; **(a)** blah = word.equals(phrase.substring(0,3)); **False** **(b)** blah = phrase == text; **True** **(c)** blah = word == text; **False** **(d)** blah = ahh == ugh; **Error, ahh not initialized** **(e)** blah = text != ugh; **True** **(f)** blah = quote != null; **True** **(g)** blah = ugh.equals(ahh); **Error, ahh not initialized** **(h)** blah = word.equals(phrase); **True** **(i)** blah = word.compareTo(quote) < 0; **True** **(j)** blah = word.compareTo(phrase) > 0; **False** **(k)** blah = ugh.equals(word); blah = text.length(); **Error, can't call method on null String.** **(l)** blah = word.substring(text.length()/2); blah = word.equals(ugh); **False** ### 10. What will be printed to the console if the following lines of code are executed? **Assumptions** * boolean isVerified; * int duration; * String animal; **(a)** ```java if(animal == null || animal.length() == 0) { if(duration < 4 || !isVerified) { System.out.println("A"); } else if(Math.abs(duration - 5) <= 2) { System.out.println("B"); } else { System.out.println("C"); } System.out.println("D"); } else if(isVerified) { int len = animal.length(); String lastLetter; lastLetter = animal.substring(len - 1); if(lastLetter.compareTo("z") >= 0) { System.out.println("E"); } else if(Math.pow(duration, 2) < 100) { System.out.println("F"); System.out.println("G"); } else { System.out.println("H"); } if(duration % 3 == 0) { System.out.println("I"); } else if(duration % 2 != 0) { System.out.println("J"); } else { System.out.println("K"); } System.out.println("L"); } ``` **Output** * D **(b)** ```java if(animal == null || animal.length() == 0) { if(duration < 4 || !isVerified) { System.out.println("A"); } else if(Math.abs(duration - 5) <= 2) { System.out.println("B"); } else { System.out.println("C"); } System.out.println("D"); } else if(isVerified) { int len = animal.length(); String lastLetter; lastLetter = animal.substring(len - 1); if(lastLetter.compareTo("z") >= 0) { System.out.println("E"); } else if(Math.pow(duration, 2) < 100) { System.out.println("F"); System.out.println("G"); } else { System.out.println("H"); } if(duration % 3 == 0) { System.out.println("I"); } else if(duration % 2 != 0) { System.out.println("J"); } else { System.out.println("K"); } System.out.println("L"); } ``` **Output** * F * G * L **(c)** ```java if(animal == null || animal.length() == 0) { if(duration < 4 || !isVerified) { System.out.println("A"); } else if(Math.abs(duration - 5) <= 2) { System.out.println("B"); } else { System.out.println("C"); } System.out.println("D"); } else if(isVerified) { int len = animal.length(); String lastLetter; lastLetter = animal.substring(len - 1); if(lastLetter.compareTo("z") >= 0) { System.out.println("E"); } else if(Math.pow(duration, 2) < 100) { System.out.println("F"); System.out.println("G"); } else { System.out.println("H"); } if(duration % 3 == 0) { System.out.println("I"); } else if(duration % 2 != 0) { System.out.println("J"); } else { System.out.println("K"); } System.out.println("L"); } ``` **Output** * F * G * L **(d)** ```java if(animal == null || animal.length() == 0) { if(duration < 4 || !isVerified) { System.out.println("A"); } else if(Math.abs(duration - 5) <= 2) { System.out.println("B"); } else { System.out.println("C"); } System.out.println("D"); } else if(isVerified) { int len = animal.length(); String lastLetter; lastLetter = animal.substring(len - 1); if(lastLetter.compareTo("z") >= 0) { System.out.println("E"); } else if(Math.pow(duration, 2) < 100) { System.out.println("F"); System.out.println("G"); } else { System.out.println("H"); } if(duration % 3 == 0) { System.out.println("I"); } else if(duration % 2 != 0) { System.out.println("J"); } else { System.out.println("K"); } System.out.println("L"); } ``` **Output** * E * L **(e)** ```java if(animal == null || animal.length() == 0) { if(duration < 4 || !isVerified) { System.out.println("A"); } else if(Math.abs(duration - 5) <= 2) { System.out.println("B"); } else { System.out.println("C"); } System.out.println("D"); } else if(isVerified) { int len = animal.length(); String lastLetter; lastLetter = animal.substring(len - 1); if(lastLetter.compareTo("z") >= 0) { System.out.println("E"); } else if(Math.pow(duration, 2) < 100) { System.out.println("F"); System.out.println("G"); } else { System.out.println("H"); } if(duration % 3 == 0) { System.out.println("I"); } else if(duration % 2 != 0) { System.out.println("J"); } else { System.out.println("K"); } System.out.println("L"); } ``` **Output** * J * L **(f)** ```java if(animal == null || animal.length() == 0) { if(duration < 4 || !isVerified) { System.out.println("A"); } else if(Math.abs(duration - 5) <= 2) { System.out.println("B"); } else { System.out.println("C"); } System.out.println("D"); } else if(isVerified) { int len = animal.length(); String lastLetter; lastLetter = animal.substring(len - 1); if(lastLetter.compareTo("z") >= 0) { System.out.println("E"); } else if(Math.pow(duration, 2) < 100) { System.out.println("F"); System.out.println("G"); } else { System.out.println("H"); } if(duration % 3 == 0) { System.out.println("I"); } else if(duration % 2 != 0) { System.out.println("J"); } else { System.out.println("K"); } System.out.println("L"); } ``` **Output** * K * L

Use Quizgecko on...
Browser
Browser