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;...

  • this needs to be in Java 1) What displays from this code segment when n=6? for...

    this needs to be in Java 1) What displays from this code segment when n=6? for (int i = 0; i < n; i++) System.out.print (i + “ ”); System.out.println (); for (int j=n; j > 0; j--) System.out.print ( j + “ “); 2) What displays from this code segment when n=6? for (int i = 0; i < n; i++) System.out.print (i + “ ”); System.out.println (); for (int j=n; j > 0; j--) System.out.print ( j +...

  • 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,...

  • What is the value of GPA when grade is 'B' in the following code segment? int...

    What is the value of GPA when grade is 'B' in the following code segment? int GPA=0; switch (grade) { case 'A': case 'a': GPA = GPA + 1; case 'B': case 'b': GPA = GPA + 1; case 'C': case 'c': GPA = GPA + 1; case 'D': case 'd': GPA = GPA + 1; } 4 3 2 1 None of the above #2.   Branching - switch statements... Extra info/hint? It's free What is the value of GPA when...

  • 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) {...

  • (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 {...

  • QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2,...

    QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2, 3};         int *p = a;         int **r = &p;         printf("%p %p", *r, a); A. Different memory addresses printed B. 1 2 C. Same memory address printed twice D. 1 1 2 points    QUESTION 10 What will be the output of following code snippet? int arr[4] = {1, 2, 3, 4};         int *p;         p = arr + 3;         *p = 5;         printf("%d\n", arr[3]); A....

  • 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;...

  • 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++)...

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