Question

Identify and correct the error in the following codes, and state what type of error you...

Identify and correct the error in the following codes, and state what type of error you will get?

public class Assignmet2{

     public static void main(String []args){

       int x=2;

       for(int year=1; year>=0; year--)

         for (int day=2; day!=3; year++)

           {if ( x>=1) System.out.println("Yes");

           else System.out.println("No");

           x--;} }}

int[] grade = new int[20];

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

grade[counter]=20;

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

The error with the code is that the inner for loop will run infinitely and will give infinite run error as in the inner for loop, the value of year is incremented and hence, the value of day is not changed. So, the inner for loop will run infinitely.

So, in order to correct the program, we need to change the value of day at the end of each execution of the loop.

So, the correct code is

public class Assignmet2{

     public static void main(String []args)

     {

       int x=2;

        for(int year=1; year>=0; year--)

        {

            // increment the value of day after each loop iteration

            for (int day=2; day!=3; day++)

            {

               if ( x >= 1)

                   System.out.println("Yes");

                else

                    System.out.println("No");

                x--;

            }

        }

    }

int[] grade = new int[20];

// also there is no variable i. This will give ElementNotFound error

//for(int counter=0; counter<=grade.length; i++)

for(int counter=0; counter<=grade.length; counter++)

    grade[counter]=20;

Add a comment
Know the answer?
Add Answer to:
Identify and correct the error in the following codes, and state what type of error you...
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
  • Identify a logical error in the following code and fix it. public class test1 {    ...

    Identify a logical error in the following code and fix it. public class test1 {     public static void main(String[] args){         int total;         float avg;         int a, b, c;         a=10;         b=5;         c=2;         total=a+b+c;         avg=total/3;         System.out.println("Average: "+avg);     } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...

  • Write the output of the two codes below, unless there is a compiler error or a...

    Write the output of the two codes below, unless there is a compiler error or a runtime error. In those cases, write ​CE​ or ​RE​, respectively, and explain why those errors occur. If the code produces an infinite loop, write “​LE:​ ​infinite loop​”. public class ForLoopsDemo{ public static void main(String[] args){ int x = 4; fint x = 4; for( int i = 0; i < x; i++ ){ } } for( int j = 0; j < 2*i+1; j++)}...

  • What is the output for the following program codes? a) [ 0.5 Mark ] class A...

    What is the output for the following program codes? a) [ 0.5 Mark ] class A { int i; } class B extends A { int j; void display() { super.i = j + 1; System.out.println(j + " " + i); }} class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); }} OUTPUT: b) [ 0.5 Mark ] class Parent { public void getBike(){ System.out.println("Suzuki Bike"); }} class Child extends Parent {...

  • What is the output of the following codes? Line1: public class ArrayCompare ! Line2: public static...

    What is the output of the following codes? Line1: public class ArrayCompare ! Line2: public static void main (String[] args) { Line 3: int arr1[] = {1, 2, 3); Line4: int arr2[] = {1, 2, 3); Line5: if (arri == arr2) Line 6: System.out.println("Same"); Line 7: else Line 8: System.out.println("Not same"); Line9 : Line10: ) } Not same Same

  • What is the output of the following codes? Line1: public class ArrayCompare { Line2: public static...

    What is the output of the following codes? Line1: public class ArrayCompare { Line2: public static void main (String[] args) { Line 3: int arrill {1, 2, 3}; Line 4: int arr2[1 {1, 2, 3}; Line5: if (arri == arr2) Line 6: System.out.println("Same"); Line: else Line 8: System.out.println("Not game"); Line9 : Line10:) حسین Same Not same

  • 2. Write a counter controlled loop to solve the following problems. Each one will involve an...

    2. Write a counter controlled loop to solve the following problems. Each one will involve an array (MinMax.java) Read in 25 ints from the keyboard, and store them in an array. Then, find the maximum and minimum values in such an array, and display them on the screen. public class Array-Assignment { public static void main(String [] args) {     int [] x = new int[3];     int [] y = {3, 5, 9, 2};     x[2] = y[3];    ...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • Hello, Can you please error check my javascript? It should do the following: Write a program...

    Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Consider the following short program. If there's an error (either compilation or runtime) in the program,...

    Consider the following short program. If there's an error (either compilation or runtime) in the program, then tell me what the problem is. But if the program works fine, then describe in 1 or 2 sentences what's the goal of the program. public class Main { public static void main(String[] args) { String[] arr = args; m(arr); public static void m(String... a) { for (int i = 0; i < a.length; i++) System.out.println(a[i] + a[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
ADVERTISEMENT