Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the output that these lines of code generate (I\'ve included the line of c

ID: 3909737 • Letter: W

Question

Write the output that these lines of code generate (I've included the line of code that each generates. could you please explain the thought process for each in detail)

A. String s = new String("Pittsburgh"); int index = 0; while( s.charAt(index) != 's' ) { System.out.print(s.charAt(index)); index++; }

Outputs: Pitt

******************************************************************

B. String city = new String("Colorado Springs"); System.out.println( city.substring(11, city.length()-1) );

Outputs: ring

******************************************************************

C. int i = 15; do{ System.out.print( i + " " ); i = i * 2 / 3; }while( i > 10 );

Outputs: 15

******************************************************************

D. for( int j=0 ; j<10 ; j++ ) if( j/3 == 0 ) System.out.print( j + " " );

outputs: 0 1 2

Explanation / Answer

A.

String s = new String("Pittsburgh");
        int index = 0;
        while( s.charAt(index) != 's' )
        {
            System.out.print(s.charAt(index));
            index++;
        }

Output

Pitt

String s is assigned with "Pittsburgh". Variable index is initialized to 0.

The method charAt(int index) returns the character at the specified index. The index value should lie between 0 and length()-1. For e.g. s.charAt(0) would return the first character of the string “s”.

Here the while loop works till a character 's' is found in string s and "Pitt" is printed. When character 's' is found in string s the while loop exits.

B.

       String city = new String("Colorado Springs");
       System.out.println( city.substring(11, city.length()-1) );

Output

ring

String city is assigned with "Colorado Springs".

Method substring() is used for getting a substring of a particular String. There are two variants of this method:

String substring(int beginIndex, int endIndex): Returns the substring starting from the given index(beginIndex) till the specified index(endIndex). For e.g. "Hello".substring(2,4) would return "ll".

The java string length() method returns length of the string. It returns count of total number of characters.

So here city.length()-1 returns 15

So here city.substring(11, city.length()-1) will print charcters in string city between11 and 15 which is "ring".

C.

int i = 15;
       do
       {
           System.out.print( i + " " );
           i = i * 2 / 3;
       }while( i >10 );

Output

15

The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is checked after loop body.

Syntax:

The variable i is initialized to 15. Since it is do-while loop, code inside loop is executed. Hence value of i is printed which is 15.

Now the expression i = i * 2 / 3; is evaluated and value of i becomes 10.

Then the condition in while loop is evaluated which is while( i > 10 );

Here the condition(10>10) fails  and program terminates.

D.

for( int j=0 ; j<10 ; j++ )
           if( j/3 == 0 )
               System.out.print( j + " " );

Output

0   1     2

In the for loop j is initialized to 0. Now the condition j<10 is evaluated. It returns true. Hence body of loop is executed.

Inside the loop there is a condition checking if( j/3 == 0 ).

Since this is integer division this condition(0/3) evaluates to true and value of 'j' which is '0' is printed.

In the next iteration of loop, j is incremented and value of j becomes 1. Now the condition j<10 is evaluated. It returns true. Hence body of loop is executed. The same procedure is repeated and 'j' is printed again, which is '1'.

In the next iteration of loop, j is incremented and value of j becomes 2. Now the condition j<10 is evaluated. It returns true. Hence body of loop is executed. The same procedure is repeated and 'j' is printed again, which is '2'.

In the next iteration of loop, j is incremented and value of j becomes 3. Now the condition j<10 is evaluated. It returns true. Hence body of loop is executed.

Inside the loop there is a condition checking if( j/3 == 0 ).

Since this is integer division this condition(3/3=1) evaluates to false and control goes to next iteration of loop.

The loop continues till value of j reaches 10 and is exited as the condition (j<10) becomes false.