1. Find the errors in the following if statements: a. b. c. d. e. if (quarters>0) then System.out.println(quarters+”quarters”); if(1+x> Math.

pow(x,Math.sqrt(2)) y=y+x; if(x=1)y++; else if(x=2) y=y+2; if(x&&y==0) {x=1; y=1;} if(1<=x<=10) System.out.println(x); f. if(!s.equals(“nickels”) || s.equals(“pennies”)|| !s.equals(“dimes”)||!s.equals(“quarters”)) System.out.println(“Input error”);

g. if (input.equalsIgnoreCase(“N”) ||”No”) return; h. int x=Integer.parseInt(input); if (x!=null) y =y+x; i. language=”English”; if (country.equals(“US”)) if(state.equals(“PR”)) language=”Spanish”; else if (country.equals(“China”)) language=”Chinese”; 2. True or False? A && B is the same as B && A for any Boolean conditions A and B. 3. Write a program that reads in two floating-point numbers and tests (a) whether they are the same when rounded to two decimal places and (b) whether they differ by less than 0.01 Here are two sample runs : Enter two floating –point numbers 2.0 1.99998 They are same when rounded to two decimal places Enter two floating point numbers: 0.999 0.991

They are different when rounded to two decimal places .They differ by less than 0.01 4. Write a program that asks the user for an integer and then prints out all its factors in increasing order. For example when the user enters 150 , the following should print: 2355

out. and then prints the price again. explain why not 7. prints the name and price. double y=4. What does the following code print ? Explain for (int i=0.println(). falseSwap(3. } . getTotalScore().print(i*j%10). Supply methods getName. and getAverageScore(). a=b.4) System. double b) { double temp =a. for example new Product(“toaster”. System.5. i<10.2299. } Why doesn’t the following method swap the contents of x and y? 6. Supply an appropriate constructor and methods getName(). Consider the following method that is intended to swap the values of two floating –point numbers : public static void falseSwap(double a. addQuiz(int score).out. i++) { for (int j=0. } public static void main(String args[]) { double x=3. If not.out. To compute the latter. A product has a name and a price. give examples.99). getPrice and reducePrice. 8. For the purpose of this exercise. reduces their prices by 500. Supply a StudentTester class that tests all methods. Implement a class Product. Can you convert a superclass reference to a subclass reference? A subclass reference into a superclass reference? If so.j<10. Implement a class Student.println(“x is ” + x + “y is “ + y ). you also need to store the number of quizzes that the student took. j++) System. a student has a name and a total quiz score. b=temp. 9. Supply a program ProductPrinter that makes two products.

print(n). Explain the error message that you get after compiling the following program public class Print13 { public void print(int x) { System.println(x).10. } public static void main (String args[]) { int n=13.out. } } .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.