Sei sulla pagina 1di 6

Solution of Mid-Paper

Question 1: Find and correct the error(s) in each of the following segments of code: a) for ( i = 100, i >= 1, i++ )
System.out.println( i );

Errors: Semicolon should be used instead of commas. Infinite loop. Correct code:
for ( i = 100; i >= 1; i-- ) System.out.println( i );

b) The following code should print whether integer value is odd or even:
switch ( value % 2 ) { case 0: System.out.println( "Even integer" ); case 1: System.out.println( "Odd integer" ); }

Error: No break inserted. Correct code:


switch ( value % 2 ) { case 0: System.out.println( "Even integer" ); break; case 1: System.out.println( "Odd integer" ); }

c) The following code should output the odd integers from 19 to 1:


int i = 19; while (i >= 1){ System.out.println( i ); i += 2; }

Error: i should be decremented not incremented.

Correct code:
int i = 19; while (i >= 1){ System.out.println( i ); i -= 2; }

d) The following code should output the even integers from 2 to 100:
counter = 2; do { System.out.println( counter ); counter += 1 ; } While ( counter < 100 );

Errors: On each iteration 2 should be added to counter not 1. The condition should also contain equality. While should be replaced with while. Correct code:
counter = 2; do { System.out.println( counter ); counter += 2 ; } while ( counter <= 100 );

Question 2:
A mail-order house sells five products whose retail prices are as follows: Product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49 and product 5, $6.87. Write an error free Java Program that inputs the quantity of each type of these products sold and displays the total sales. Sample Execution of the code
Enter Enter Enter Enter Enter the the the the the quantity quantity quantity quantity quantity of of of of of product product product product product 1 2 3 4 5 sold: sold: sold: sold: sold: 23 15 14 9 3

The total sales are $: 336.78

Java Program
import java.util.*; public class sales { public static void main(String args []){ float a,b,c,d,e,sales; Scanner in = new Scanner(System.in); System.out.println("Enter a=in.nextInt(); System.out.println("Enter b=in.nextInt(); System.out.println("Enter c=in.nextInt(); System.out.println("Enter d=in.nextInt(); System.out.println("Enter e=in.nextInt(); the quantity of product 1 sold"); the quantity of product 2 sold"); the quantity of product 3 sold"); the quantity of product 4 sold"); the quantity of product 5 sold");

sales = (float) (2.98*a + 4.5*b + 9.98*c + 4.49*d + 6.87*e); System.out.println("Total sales are: $" + sales); } }

Question 3:
Write the output of the following program:
public class Question3 { public static void main( String args[] ) { int a, b, c; a = b = c = 1; a = ++b + 2*c; System.out.println("a = " + a + ", b = " + b + ", c = " + c); b = a++ - c + 2*a; System.out.println("a = " + a + ", b = " + b + ", c = " + c); c = 3*a + a++ + --b + 5*b; System.out.println("a = " + a + ", b = " + b + ", c = " + c); } // end main } // end class Question3

Output
a = 4, b = 2, c = 1 a = 5, b = 13, c = 1 a = 6, b = 12, c = 92

Question 4:
Write the output of the following program:
public class Question4 { public static void main( String args[] ) { int row = 10; int column; while ( row >= 1 ) { column = row; while ( column <= 10 ) { System.out.print( row % 2 == 1 ? "<" : ">" ); ++column; } // end while --row; System.out.println(); } // end while } // end main } // end class Question4

Output
> << >>> <<<< >>>>> <<<<<< >>>>>>> <<<<<<<< >>>>>>>>> <<<<<<<<<<

Question 5:
A palindrome is a sequence of characters that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write an error free Java Program that reads in a five-digit integer and prints whether it is a palindrome or not. [Caution: Reading the number as String and calling methods of String class for comparison within a loop or reading 5 digits separately will not be given any credit.] [Hint: Read number as an integer and use arithmetic operations to separate all digits.]

Java Program
import java.util.*; public class Palindrome { public static void main(String args[]){ int a,b,c,d,num; Scanner in = new Scanner(System.in); System.out.println("Enter a 5-digit number:"); num = in.nextInt(); a=num%10; b=(num/10)%10; c=(num/1000)%10; d=num/10000; if(a==d && b==c) System.out.println("The number is a Palindrome"); else System.out.println("The number is not a Palindrome"); } }

Potrebbero piacerti anche