1 / 26

Java Chapter 2 Review

Java Chapter 2 Review. What will be the output for the following assuming all variables are of type int? Z = X = 2 * 6 / 3 + (5 + 5) % 5 10 points. 4 for both z and x. What will be the value of x if we execute the following? double x = 3 / 9; 20 points. X will equal 0.0.

dwight
Télécharger la présentation

Java Chapter 2 Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Chapter 2 Review

  2. What will be the output for the following assuming all variables are of type int?Z = X = 2 * 6 / 3 + (5 + 5) % 5 10 points 4 for both z and x.

  3. What will be the value of x if we executethe following?double x = 3 / 9; 20 points X will equal 0.0

  4. Assume a is of type int and b is of type double,which of the following are allowed? • a = b • b = a • a = (int) b • b = (double) a • 30 points You can do all except 1, an error will occur when you assign a double to an int.

  5. Assume the following declaration is given, what would the output be?int x = 4; double y = 3.4; x = (int) y % x; System.out.println(x + y); 40 points 6.4 would be the output

  6. What is the output if you are given the following declarations:int x, y; x = y = 3; double answer; answer = (int) ((double) x * y); System.out.println(answer); 50 points 9.0

  7. Write a simple program that generates a randomnumber between 5 and 25 inclusive. 10 points import java.util.Random; Random g = new Random(); System.out.print(g.nextInt(21)+5);

  8. Write code that will display a number to 2 decimal places. 20 points import java.text.DecimalFormat; DecimalFormat fmt = new DecimalFormat(“0.##”);double x = 4.3567; System.out.print(fmt.format(x));

  9. Write code that will display a number in currency format. 30 points import java.text.NumberFormat; NumberFormat fmt = NumberFormat.getCurrencyInstance();double x = 4.3567; System.out.print(fmt.format(x));

  10. Write a statement that will ask for an integer from the user and then display the value of 2x 40 points import cs1.Keyboard; X = Keyboard.readInt(); System.out.print(Math.pow(2,X));

  11. Write a line of code that will always round a variable number up. 50 points System.out.print(Math.ceil(x));

  12. What is the output for each of the following: • System.out.print(“” + 4 + 3.0); • System.out.print(-4 + 3.0); • 10 points • 43.0 • -1.0

  13. What would the output be for the following?String junk = new String(“Java”); junk = junk.concat(junk.toLowerCase()); junk = junk + junk.length(); 20 points Javajava8

  14. What will be output by the following code?String junk = new String(“Java Rocks”); System.out.println(junk.charAt(junk.length())); 30 points You will get an error, the length is 10 but the s is at spot 9.

  15. What will be the output of the following? String junk = new String(“Java Rocks”); boolean stuff; stuff = junk.equals(junk.toUpperCase()); System.out.println(“stuff = “ + stuff); 40 points stuff = false, the two strings must be identical.

  16. What will be the output of the following? String junk = new String(“Java Rocks”); boolean stuff; stuff = junk.equalsIgnoreCase(junk.toUpperCase()); System.out.println(“stuff = “ + stuff); 50 points stuff = true

  17. What is casting? 10 points Converting a primitive data type to another type.

  18. What are the two wrapper classes? 20 points Integer and Double

  19. What package is automaticaly imported into every Java program? 30 points Java.lang

  20. Show the code before and after the followingline that would format the variable x to money. 40 pointsSystem.out.println( x ) import java.text.NumberFormat; NumberFormat m = NumberFormat.getCurrencyInstance();System.out.println(m.format(x));

  21. What is a package? 50 points A bunch of code already written and is a bunch of related classes combined into one package.

  22. What do we mean when we use the term abstract? 10 points You don’t know the details of how a class is written, you just know what parameters to send it and what is should return.

  23. Explain what the following mean? \n \” \r 20 points This is how you use non printable characters. They are called escape sequences.

  24. What will the output be for the following line? System.out.print("Java Rocks\rL"); 30 points Lava Rocks

  25. Identify the classes, objects, parameters and methods for the following statements? public class MyBank { public static void main (String [ ] args) { Account Customer1 = new Account("Sam", 500.12); Account Customer2 = new Account("Sue", 1500.12); System.out.println(Customer1); Customer1.deposit(200); System.out.println(Customer1); } //end of main method }//end of MyBank class 40 points I will just show you the answer

  26. Give me an example of how you would definea constant? 50 points final int ARRAY_SIZE = 65;

More Related