Wednesday, December 5, 2012

Java Chapter 2 Review


Review Questions

1. When data cannot be changed after a class is compiled, the data is constant. (a)

2. Which of the following is not a primitive data type in Java? sector (d)

3. Which of the following elements is not required in a variable declaration? an assigned value (c)

4. The assignment operator in Java is = (a)

5. Assuming you have declared shoeSize to be a variable of type int, which of the following is a valid assignment statement in Java? shoeSize = 9; (a)

6. Which of the following data types can store a value in the least amount of memory? byte (d)

7. A boolean variable can hold the value true or false. (d)

8. The value 137.68 can be held by a variable of type Two of the preceding answers are correct. (d)

9. An escape sequence always begins with a(n) backslash (c)

10. Which Java statement produces the following output?
w
xyz

System.out.println("w\nxyz"); (c)

11. The remainder operator is none of the above. (d)

12. According to the rules of operator precedence, when division occurs in the same arithmetic statement as subtraction, the division operator always takes place first. (c)

13. The "equal to" relational operator is == (b)

14. When you perform arithmetic* with values of diverse types, Java implicitly converts the values to a unifying type. (b) *Typo, missing word: operations.

15. If you attempt to add a float, an int, and a byte, the result will be a(n) float. (a)

16. You use a type cast to explicitly override an implicit type. (b)

17. In Java, what is the value of 3 + 7 * 4 + 2? 33 (b)

18. What assignment is correct in Java? double value = 2.12; (c)

19. Which assignment is correct in Java? All of the above are correct. (d)

20. Which assignment is correct in Java? char aChar = '*'; (c)

Exercises

1.
a. 22
b. 14
c. 16
d. 8
e. 8.5
f. 5.6
g. 0
h. 1
i. 3
j. 10
k. 20
l. 4

2.
a. true
b. true
c. true
d. false
e. true
f. false
g. true
h. false
i. true
j. false

3.
a. int, no decimals
b. long, big number, cents are not important.
c. float, don't need too much accuracy, need decimal.
d. char, only need one character to represent initial.

public class Room
/* Computes the square foot of a room */
{
 public static void main(String[] args)
 {
  int length = 15;
  int width = 25;
  double area = (length * width);
  System.out.println("The floor space is " + area + " square feet.");
 }
}

import java.util.Scanner;

public class Room2
/* Computes the square foot of a room using user input */
{
 public static void main(String[] args)
 {
  int length;
  int width;
  Scanner keyBoard = new Scanner(System.in);
  System.out.print("Enter the length of the room: ");
  length = keyBoard.nextInt();
  System.out.print("Enter the width of the room: ");
  width = keyBoard.nextInt();
  double area = (length * width);
  System.out.println("Here are the dimensions you entered");
  System.out.println("Length: " + length + " feet.");
  System.out.println("Width: " + width + " feet.");
  System.out.println("The floor space is " + area + " square feet.");
 }
}
public class Carpet
/* Computes the cost to cover an area with carpet */
{
 public static void main(String[] args)
 {
  int length = 15;
  int width = 25;
  double costPerSquareFoot = 3.25;
  int area = (length * width);
  double totalCost = (area * costPerSquareFoot);
  System.out.println("Here is a breakdown of the cost");
  System.out.println("Length: " + length + " feet");
  System.out.println("Width: " + width + " feet");
  System.out.println("Total area: " + area + " feet");
  System.out.println("Cost per square foot: $" + costPerSquareFoot);
  System.out.println("The total cost of covering a room of " + 
   area + " feet in carpet will be: $" + totalCost);
 }
}
import java.util.Scanner;

public class Carpet2
/* Computes the cost to cover an area with carpet using user input values*/
{
 public static void main(String[] args)
 {
  int length;
  int width;
  double costPerSquareFoot;
  Scanner keyBoard = new Scanner(System.in);
  System.out.print("Enter length: ");
  length = keyBoard.nextInt();
  System.out.print("Enter width: ");
  width = keyBoard.nextInt();
  System.out.print("Enter price per square foot: $");
  costPerSquareFoot = keyBoard.nextDouble();
  int area = (length * width);
  double totalCost = (area * costPerSquareFoot);
  System.out.println("Here is a breakdown of the cost");
  System.out.println("Length: " + length + " feet");
  System.out.println("Width: " + width + " feet");
  System.out.println("Total area: " + area + " feet");
  System.out.println("Cost per square foot: $" + costPerSquareFoot);
  System.out.println("The total cost of covering a room of " + 
   area + " feet in carpet will be: $" + totalCost);
 }
}
public class Time
/* Computes hours and minutes worked from 
   the total amount of minutes worked */
{
 public static void main(String[] args)
 {
  final byte MINUTES_IN_ONE_HOUR = 60;
  int totalMinutesWorked = 197;
  int hoursWorked = totalMinutesWorked / MINUTES_IN_ONE_HOUR;
  int minutesWorked = totalMinutesWorked % MINUTES_IN_ONE_HOUR;
  System.out.println("Total time worked: " + hoursWorked 
   + " hours " + minutesWorked + " minutes");
 }
}
import java.util.Scanner;

public class Time2
/* Computes hours and minutes worked from the total 
   amount of minutes worked input from user */
{
 public static void main(String[] args)
 {
  final byte MINUTES_IN_ONE_HOUR = 60;
  int totalMinutesWorked;
  Scanner keyBoard = new Scanner(System.in);
  System.out.print("Enter total minutes worked: ");
  totalMinutesWorked = keyBoard.nextInt();
  int hoursWorked = totalMinutesWorked / MINUTES_IN_ONE_HOUR;
  int minutesWorked = totalMinutesWorked % MINUTES_IN_ONE_HOUR;
  System.out.println("Total time worked: " + hoursWorked 
   + " hours " + minutesWorked + " minutes");
 }
}
public class Initials
/* Display initials with a dot after each initial */
{
 public static void main(String[] args)
 {
  char firstInitial = 'A';
  char middleInitial = 'N';
  char lastInitial = 'M';
  System.out.println(firstInitial + "." +
   middleInitial + "." + lastInitial + ".");
 }
}
import java.util.Scanner;

public class Fees
/* Display student's total fees */
{
 public static void main(String[] args)
 {
  final byte PER_CREDIT_HOUR = 85;
  final byte ALTHETIC_FEE = 65;

  System.out.print("How many credit hours are you enrolled in: ");
  Scanner keyBoard = new Scanner(System.in);
  int numberOfCreditHoursEnrolled = keyBoard.nextInt();
  System.out.print("How much did you spend on books? $");
  double totalSpentOnBooks = keyBoard.nextInt();

  System.out.println("Here is a breakdown of the costs");

  System.out.println("Total Credit hours: " + numberOfCreditHoursEnrolled);
  System.out.println("Cost per credit hour: $" + PER_CREDIT_HOUR);
  double totalCostForCredits = (PER_CREDIT_HOUR * numberOfCreditHoursEnrolled);
  System.out.println("Total cost for credits: $" + totalCostForCredits);
  System.out.println("Total cost of books: $" + totalSpentOnBooks);
  System.out.println("Althetic fee: $" + ALTHETIC_FEE);

  double totalAmountDue = (totalCostForCredits + totalSpentOnBooks + ALTHETIC_FEE);

  System.out.println("Total amount due: $" + totalAmountDue);
 }
}
import java.util.Scanner;

public class Payroll
{
 public static void main(String[] args)
 {
  final float tax = 0.15F;

  Scanner keyBoard = new Scanner(System.in);
  System.out.print("Enter your pay rate: ");
  float payRate = keyBoard.nextFloat();
  System.out.print("Enter number of hours worked: ");
  float hoursWorked = keyBoard.nextFloat();
  float grossPay = (hoursWorked * payRate);
  System.out.println("Your gross pay is: $" + grossPay);
  float withheldTax = (grossPay * tax);
  System.out.println("Total withheld tax: $" + withheldTax);
  float netPay = (grossPay - withheldTax);
  System.out.println("Total net pay: $" + netPay);
 }
}
import java.util.Scanner;

public class Dollars
/* Calculate dollars into currency demoninations */
{
 public static void main(String[] args)
 {
  Scanner keyBoard = new Scanner(System.in);
  System.out.print("Enter amount of dollars: $");
  int dollars = keyBoard.nextInt();

  int twenties = dollars / 20;
  System.out.println("20s: " + twenties);
  int tens = ((dollars % 20) / 10);
  System.out.println("10s: " + tens);
  int fives = ((dollars % 10) / 5);
  System.out.println("5s: " + fives);
  int ones = (dollars % 5);
  System.out.println("1s: " + ones);

 }
}
import java.util.Scanner;

public class FahrenheitToCelsius
/* Convert temperature from Fahrenheit to Celsius from user input Fahrenheit */
{
 public static void main(String[] args)
 {
  System.out.print("Enter temperature in Fahrenheit: ");
  Scanner keyBoard = new Scanner(System.in);
  double Fahrenheit = keyBoard.nextDouble();
  double Celsius = (Fahrenheit - 32) * 5/9;
  System.out.println("Fahrenheit: " + Fahrenheit);
  System.out.println("Celsius: " + Celsius);
 }
}
import javax.swing.JOptionPane;

public class TicketNumber
/* Identifies invalid ticket numbers */
{
 public static void main(String[] args)
 {
  String fullTicketString = JOptionPane.showInputDialog(null, "Ticket number", "Enter 6 digit ticket number", JOptionPane.QUESTION_MESSAGE);
  String lastDigit = fullTicketString.substring(fullTicketString.length() - 1);
  String trimTicketString = fullTicketString.substring(0, fullTicketString.lastIndexOf(lastDigit));
  int ticketNumber = Integer.parseInt(trimTicketString);
  int integerLastDigit = Integer.parseInt(lastDigit);
  boolean isValid = (ticketNumber % 7) == integerLastDigit;
  JOptionPane.showMessageDialog(null, isValid + "", "Validation", JOptionPane.INFORMATION_MESSAGE);
 }
}
import java.util.Scanner;

public class MadLib
/* Create a Mad Libs type game */
{
 public static void main(String[] args)
 {
  Scanner keyBoard = new Scanner(System.in);

  System.out.print("Enter a noun: ");
  String firstNoun = keyBoard.nextLine();

  System.out.print("Enter another noun: ");
  String secondNoun = keyBoard.nextLine();

  System.out.print("Enter an adjective: ");
  String adjective = keyBoard.nextLine();

  System.out.print("Enter a past-tense verb: ");
  String pastTenseVerb = keyBoard.nextLine();

  System.out.println("Mary had a little " + firstNoun);
  System.out.println("Its " + secondNoun + " was " + adjective + " as snow");
  System.out.println("And everywhere that Mary " + pastTenseVerb);
  System.out.println("The " + firstNoun + " was sure to go.");
 }
}

14 comments:

  1. Its really helpful for me to understand where we i lost in my previous interview. Thanks.
    If anyone wants to Learn Java in Chennai go to the Besant Technologies which is No.1 Training Institute in Chennai.

    http://www.besanttechnologies.in/java-training-in-chennai.html

    ReplyDelete
  2. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  3. This post is so interactive and informative.keep update more information...
    DevOps course in Tambaram
    DevOps Training in Chennai

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. we are giving online IT and non IT courses with placement support. Get your dream job in just 3 months
    we are giving online IT and non IT courses with placement support. Get your dream job in just 3 months. Our trainners having 11 years of experience in both IT and non IT courses. we have digital marketing experts also who has above 10 years of experience in digital marketing feild.
    training and placement courses in bangalore
    Courses

    Machine Learning



    Digital Marketing



    AWS



    Python



    Data Science



    Medical Coding



    Cyber Security



    MuleSoft



    Java-Testing



    Dotnet & SQL



    Cloud computing



    Salesforce

    ReplyDelete
  7. This is an excellent post - thank you for sharing it! I always relish the opportunity to read such superb content filled with valuable information. The ideas presented here are truly best and exceptionally captivating, making the post thoroughly enjoyable. Please continue with your fantastic work.
    Read also: Java vs. Other Programming Languages: Which Course Should You Take?


    ReplyDelete