Question

1. What is the output of the following code segment? int array[] = { 8, 6,...

1. What is the output of the following code segment?

int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 };

System.out.println( "Index Value" );

for ( int i = 0; i < array.length; i++ )

System.out.printf( "%d %d\n", i, array[ i ] );

2. What is the output of the following code segment?

char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' };

String output = "The sentence is: ";

for ( int i = 0; i < sentence.length; i++ )

System.out.printf( "%c ", sentence[ i ] );

System.out.println();

. What is the output of the following code segment?

int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

for ( int i = 0; i < array.length; i++ )

{

   for ( int j = 0; j < array [ i ]; j++ )

        System.out.print( "*" );

   System.out.println();

4. What is the output of the following code segment?

int array[] = { 3, 2, 5 };

for ( int i = 0; i < 3; i++ )

      array[ i ] *= 2;

for ( int j : array )

      System.out.print( "%d ", j );

System.out.println();

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; //initialize array values

System.out.println( "Index Value" ); // it will pring index value

for ( int i = 0; i < array.length; i++ ) // Iterate the the loop till array length ends

System.out.printf( "%d %d\n", i, array[ i ] ); //It will print i value and array value like i=0 and array =8

OUTPUT- ----------------------------------------

Index Value
0 8
1 6
2 9
3 7
4 6
5 4
6 4
7 5
8 8
9 10

---------------------------------------------------

2 ) char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; // initiate char array
       String output = "The sentence is: ";
         
       for ( int i = 0; i < sentence.length; i++ ) //iterate till sentence lenght
       System.out.printf( "%c ", sentence[ i ] ); // print the char value
         
       System.out.println();

OUTPUT- H o w a r e y o u

3)

int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //initialize an array
         
       for ( int i = 0; i < array.length; i++ ) // iterate the loop till array length
       {
       for ( int j = 0; j < array [ i ]; j++ ) // iterate the j value and check condition j < array [ i ]; suppose if i=0 then j will iterate till 1 because the array[0]=1 and will print only one * for next condition if =2 then j will iterate till 2 because array[1]=2 so will print two ** and so on.
       System.out.print( "*" ); // print *
         
       System.out.println();
   }

OUTPUT ---------------------------

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

----------------------------------

4) int array[] = { 3, 2, 5 }; //Initialize an array
         
       for ( int i = 0; i < 3; i++ ) // iterate the loop till 3
       array[ i ] *= 2; // muliply each array value by 2 and store value 6 4 10
       for ( int j : array ) // iteate the loop and here array = 6 4 10
       System.out.printf( "%d ", j ); // will print the array values
       System.out.println();

OUTPUT

-----------------------

6 4 10

---------------------------------

Add a comment
Know the answer?
Add Answer to:
1. What is the output of the following code segment? int array[] = { 8, 6,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What does the following code output? int[] array = { 1, 4, 3, 6, 8, 2,...

    What does the following code output? int[] array = { 1, 4, 3, 6, 8, 2, 5); int sum = array[0]; Il scan the array for (int i=0; i < array.length; i++) { if ( array[i] > sum) sum = array[i]; 3 System.out.println( sum );

  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

  • Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1;...

    Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...

  • QUESTION 8 What (if anything) will be the output of the following code: int count =...

    QUESTION 8 What (if anything) will be the output of the following code: int count = 3; while (count++ <= 6) { System.out.print(++count + " "); } 3 4 5 6 4 5 6 7 3 4 5 6 7 5 7 4 6 The above code contains a syntax error and will not run. 8 points    QUESTION 9 What (if anything) will be the output of the following code: int count = 0; while (count < 5) {...

  • 1. (TCO 1) What is the output of the following C++ code? int list[5] = {0,...

    1. (TCO 1) What is the output of the following C++ code? int list[5] = {0, 5, 10, 15, 20}; int j; for (j = 0; j < 5; j++)      cout << list[j]; (Points : 4)         0 1 2 3 4         0 5 10 15         0 5 10 15 20         5 10 15 20 Question 2.2. (TCO 1) What is stored in alpha after the following code executes? int alpha[5] = {0}; int j; for (j = 0; j...

  • 1. What is the output? System.out.print(3 + 3 * 3); a. 18 b. 12 c. 9 d. 0 e. 10             2.   What is output by the code below? System.out.print("\\dog\\cat&#...

    1. What is the output? System.out.print(3 + 3 * 3); a. 18 b. 12 c. 9 d. 0 e. 10             2.   What is output by the code below? System.out.print("\\dog\\cat"); a. dog b. dogcat c. \\dog\\cat d. \dog\cat e. catdog\\\\             3.   What is returned by the call     getIt(9) ? public static int getIt(int num){ int ans = 0; if( num >=2 ) {      if( num >= 7)         ans += 2;      else         ans += 3; } ans += 4; return ans; }...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • What is the output of this code segment? double [] a = {-1.2, 3.1, -4.7, 38.0,...

    What is the output of this code segment? double [] a = {-1.2, 3.1, -4.7, 38.0, 0.0}; double temp = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] < temp) { temp = a[i]; } } System.out.println (temp); What does this method do? In one short English sentence describe the task accomplished by this method. public static int foo(int [] a) { int temp = 0; for (int i = 0; i < a.length; i++)...

  • what is the output of the following code segment? C++ g. int arr[3][4]; for (int i...

    what is the output of the following code segment? C++ g. int arr[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) arr[i][j] =i*4 + j; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) cout << arr[i][j] << " "; h. int next(int & x) { x= x + 1; return (x + 1); int main() { int y = 10;...

  • 1. What is the output of each of the following code segments if the inputs are...

    1. What is the output of each of the following code segments if the inputs are as follows: 5 0 15 -5 2 Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”); int number = input.nextInt(); if(number <= 0) break; sum += number; } System.out.println(sum); Second Segment: Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT