You are on page 1of 3

1.

Find the errors in the following if statements:

a. if (quarters>0) then System.out.println(quarters+quarters); Then b. if(1+x> Math.pow(x,Math.sqrt(2)) y=y+x; missing c. if(x=1)y++; else if(x=2) y=y+2; = should be == d. if(x&&y==0) {x=1; y=1;} should be & e. if(1<=x<=10) 1<=x&&x<=10 System.out.println(x);

No )

&&

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; instead of null i. language=English; first if {} if (country.equals(US)) if(state.equals(PR)) language=Spanish; else if (country.equals(China)) language=Chinese; 0 after

2. True or False? A && B is the same as B && A for any Boolean conditions A and B. True 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 5. Consider the following method that is intended to swap the values of two floating point numbers : public static void falseSwap(double a, double b) { double temp =a; a=b; b=temp; } public static void main(String args[]) { double x=3; double y=4; falseSwap(3,4) System.out.println(x is + x + y is + y ); } Why doesnt the following method swap the contents of x and y? 6. Can you convert a superclass reference to a subclass reference? A subclass reference into a superclass reference? If so, give examples. If not, explain why not

7. Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods getName(), addQuiz(int score), getTotalScore(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took. Supply a StudentTester class that tests all methods.

8. Implement a class Product. A product has a name and a price, for example new Product(toaster,2299.99). Supply methods getName, getPrice and reducePrice. Supply a program ProductPrinter that makes two products, prints the name and price, reduces their prices by 500, and then prints the price again.

9. What does the following code print ? Explain for (int i=0; i<10; i++) { for (int j=0;j<10; j++) System.out.print(i*j%10); System.out.println(); }

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

You might also like