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      } }
What output is produced by the following code fragment? int num = 0, max = 20;...
What output is produced by the following code fragment? int num = 0, max = 20; while (num < max) { System.out.println(num); num += 4; }
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...
The output of the following code is "11 0" But I am not sure why, can...
The output of the following code is "11 0" But I am not sure why, can someone please explain to me clearly and concisely why this is the case? main(){ int n=1; if(fork()) wait(&n); else n=n+10; printf("%d\n", n); exit(0); }
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;
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT