260 likes | 353 Views
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.
E N D
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 executethe following?double x = 3 / 9; 20 points X will equal 0.0
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.
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
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
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);
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));
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));
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));
Write a line of code that will always round a variable number up. 50 points System.out.print(Math.ceil(x));
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
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
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.
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.
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
What is casting? 10 points Converting a primitive data type to another type.
What are the two wrapper classes? 20 points Integer and Double
What package is automaticaly imported into every Java program? 30 points Java.lang
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));
What is a package? 50 points A bunch of code already written and is a bunch of related classes combined into one package.
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.
Explain what the following mean? \n \” \r 20 points This is how you use non printable characters. They are called escape sequences.
What will the output be for the following line? System.out.print("Java Rocks\rL"); 30 points Lava Rocks
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
Give me an example of how you would definea constant? 50 points final int ARRAY_SIZE = 65;