SSL Certificate
  • Indentify and correct the errors in each of the following pieces of code


3.15 Indentify and correct the errors in each of the following pieces of code. [Note: There may be more than one error in each piece of code.]
a) if ( age >= 65 );
        System.out.println( “Age is greater than or equal to 65” );
    else
        System.out.println( “Age is less than 65 )”;
 


b) int x = 1, total;
    while ( x <= 10 )
    {
          total += x;
          ++x;
   }
 


c) while ( x <= 100 )
           total += x;
           ++x;


 
d) while ( y > 0 )
     {
           System.out.println( y );
           ++y; 


 




3.16 What does the following program print? 


  1: // Exercise 3.16: Mystery.java
  2: public class Mystery
  3: {  
  4:    public static void main( String[] args )
  5:    { 
  6:       int y;
  7:       int x = 1;
  8:       int total = 0;
  9:       
 10:       while ( x <= 10 )
 11:       {  
 12:          y = x * x;
 13:          System.out.println( y );
 14:          total += y;
 15:          ++x;
 16:       } // end while
 17:      
 18:       System.out.printf( "Total is %d\n", total );
 19:    } // end main
 20: } // end class Mystery


 


4.9 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 );
 
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" );
}
 
c) The following code should output the odd integers from 19 to 1:


for  ( i = 19;  i >= 1;  i += 2 )
      System.out.println( i );
 


d)  The following code should output the even integers from 2 to 100:


counter = 2;
do
{
   System.out.println( counter );
   counter += 2;
} While ( counter < 100 );
 


4.10 What does the following program do?
1 // Exercise 4.10: Printing.java


2 public class Printing


3 {
4  public static void main( String args[] )


5  {


6        for ( int i = 1; i <= 10; i++ )


7      {


8            for ( int j = 1; j <= 5; j++ )


9     System.out.print( '@' );


10


11                   System.out.println();


12       } // end outer for


13         } // end main
14  } // end class Printing



 
 following project programs in separate files if possible.


2.16 Write an application that asks the user to enter two integers, obtains them from the user
and displays the larger number followed by the words "is larger". If the numbers are equal, print the message "These numbers are equal". Use the techniques shown in Fig. 2.15.


 


2.17 Write an application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers. Use the techniques shown in Fig. 2.15. [Note: The calculation of the average in this exercise should result in an integer representation of the average. So if the sum of the values is 7, the average should be 2, not 2.3333….]


Write a review

Please login or register to review

Indentify and correct the errors in each of the following pieces of code

  • $9.99


*All your data are SECURED & ENCRYPTED using a valid, trusted server certificate (Comodo SSL) and we don't store credit card information on our servers and all Payments are SECURED & handled by Paypal.
SSL CertificatePaypal

Tags: Multiple choice question, MCQ, java, object oriented programming, oop