Question

In: Computer Science

What (if anything) is wrong with the following code: int redCount = 0, blueCount = 0;...

  1. What (if anything) is wrong with the following code:

    int redCount = 0, blueCount = 0; 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Please make a selection: ");
    String option = scan.next(); 
    
    while(!option.startsWith("q") || !option.startsWith("Q"))
    {
        if(option.equals("red"))
        {
             redCount++;
        }
        else
        {
             blueCount++;
        }
        System.out.println("Please enter another value: ");
        option = scan.next(); 
    }
    System.out.println("Red: " + redCount + ", Blue: " + blueCount); 

    The condition is not a boolean expression.

    A variable is out of scope.

    There is nothing wrong with this code.

    It will result in an infinte loop.

    It contains a logic error that will cause it to function incorrectly.

  2. What (if anything) is wrong with the following code:

    int redCount = 0, int blueCount = 0; 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Please make a selection: ");
    String option = scan.next(); 
    
    while(!option.toLowerCase().startsWith("q"))
    {
        if(option.equals("red"))
        {
             redCount += 1;
        }
        else
        {
             blueCount += 1;
        }
    
    
        System.out.println("Please enter another value: ");
        option = scan.next(); 
    }
    System.out.println("Red: " + redCount + ", Blue: " + blueCount); 

    A variable is out of scope.

    The condition is not a boolean expression.

    It will result in an infinte loop.

    There is nothing wrong with this code.

    It contains a logic error that will cause it to function incorrectly.

Solutions

Expert Solution

QUESTION (1):-

OPTION (D)  It will result in an infinte loop.

correct code given below

int redCount = 0, blueCount = 0;
       Scanner scan = new Scanner(System.in);
       System.out.println("Please make a selection: ");
       String option = scan.next();
      
       while(!(option.startsWith("q") || option.startsWith("Q")))
       {
       if(option.equals("red"))
       {
       redCount++;
       }
       else
       {
       blueCount++;
       }
       System.out.println("Please enter another value: ");
       option = scan.next();
       }
       System.out.println("Red: " + redCount + ", Blue: " + blueCount);

QUESTION (2):-

OPTION (D) There is nothing wrong with this code.

correct code given below

int redCount = 0,blueCount = 0;
       Scanner scan = new Scanner(System.in);
       System.out.println("Please make a selection: ");
       String option = scan.next();
      
       while(!option.toLowerCase().startsWith("q"))
       {
       if(option.equals("red"))
       {
       redCount += 1;
       }
       else
       {
       blueCount += 1;
       }
      
       System.out.println("Please enter another value: ");
       option = scan.next();
      
       }
       System.out.println("Red: " + redCount + ", Blue: " + blueCount);

NOTE: second code have invalid syntax in line number 1

int redCount = 0, int blueCount = 0;  

int declare two times , so option not available in list , i remove one int then program working fine.

// If any doubt please comment


Related Solutions

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; }
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
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 Show the output of the function call statements shown.             double x =...
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...
What is the value of n after this code runs? 1 Point int n = 0;...
What is the value of n after this code runs? 1 Point int n = 0; int j = 7; if ((j!=0) && (n < 25)) { n = 1; if (j > 4) { n = 2; } else { n = 3; } } else { n = 4; if (j%2 >= 2) { n = 5; } else { n = 6; } }
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 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;
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
Explain and defend your views on the following: Is there anything wrong in offering unauthorized immigrants...
Explain and defend your views on the following: Is there anything wrong in offering unauthorized immigrants "a path to citizenship"? write a 250 word response agreeing there is something wrong
What—if anything—is wrong with taking off your clothes to earn some money? Is there an ethical...
What—if anything—is wrong with taking off your clothes to earn some money? Is there an ethical difference between stripping for Playboyor Playgirl magazine and Reed’s disrobing? If so, what is it? If not, why are they ethically the same? Use the concepts of prurience and objectification to answer. Your boss wants to sleep with you, and it’s clear that visiting a hotel will help your career. What are two arguments against? What’s an argument in favor?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT