Question

In: Computer Science

Show the output of the following code segment. int count=0;                         for (int i=2; i <=...

  1. Show the output of the following code segment.

int count=0;

                        for (int i=2; i <= 4; i++ ) {

                                    StdOut.println(i);

                                    for (int j=1; j <3; j++) {

                                                count++;

                                                StdOut.println(i +" " + j +" "+ count);

                                    }

                        }

count

i

j

I print

0

2

1

3

1

1

1

2

3

2

2

3

3

3

3

4

3

  1. Show the output of the function call statements shown.
  1.             double x = funk2( -2 , 5.0, 7) );

StdOut.println( “ call 1” + x );

  1.             StdOut.println( “ call 2” + funk2(­ 1, 2.0, 4 );

public static double funk2( int a, double b, int c) {

                        int x = 15;

                        double result;

                        result = b - a*c;

                        if ( result <= x)

                                    StdOut.println( " result " + b);

                        else

                                    StdOut.println(" comp " + result);

                        return result;

            }

Solutions

Expert Solution

Please find the answer for the above given questions..

a )

OUTPUT :

CODE :

class Main {

public static double funk2( int a, double b, int c) {

int x = 15;

double result;

result = b - a*c;

if ( result <= x)

System.out.println( "result " + b);

else

System.out.println("comp " + result);

return result;

}

public static void main(String[] args) {

double x = funk2( -2 , 5.0, 7);

System.out.println("call 1 : " + x );

}

}

b )

OUTPUT :

CODE :

class Main {

public static double funk2( int a, double b, int c) {

int x = 15;

double result;

result = b - a*c;

if ( result <= x)

System.out.println( "result " + b);

else

System.out.println("comp " + result);

return result;

}

public static void main(String[] args) {

double x = funk2( 1, 2.0, 4);

System.out.println("call 2 : " + x );

}

}

Thanks..


Related Solutions

Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
What is the Θ( ) for each code segment below? (a) for( int i = 1,...
What is the Θ( ) for each code segment below? (a) for( int i = 1, i<= n, i = i*2){       some constant operation } (b) for( int i = 1, i<= n, i++){      for( int j = 2*i, j<=n, j++){           some constant operation      } } (c) for( int i = 1, i<= n, i = i*2){      for( int j = 1, j<=n, j = j*2){           some constant operation      } }
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
* Show the output of the following BASH code: i=7 i=$((i-5)) if [ $i -eq 4...
* Show the output of the following BASH code: i=7 i=$((i-5)) if [ $i -eq 4 ] # -eq is == then         echo "Four" elif [ $i -eq 2 ] then         echo "Two" else         echo $i fi
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100; i++) myAry = i + 2; for(int i=100; i>0; i--) cout << myAry[i] << '\t'; The first for loop assigns myAry 99 values and the null character. The second for loop prints out myAry elements backwards. The index in the second for loop is out of bounds. Only the first loop needs the null character. Q3. A value returning function that takes one parameter,...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int x, int y) { x = 1; y = 3; } For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.) a. By value. b. By reference. c. By value-result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT