Thursday, April 4, 2013

Common Errors while programming

Chapter 1: Introduction to Computers, Programs, and Java
1.  Q: When you compile your program, you receive an error as follows:
2.   
3.  %javac Welcome.java
4.  javac not found
5.   
6.  What is wrong?
  1. A: Two possible reasons: 1. Java is not installed on your system; or 2. Java is installed, but the environment variable PATH is not set.
8.  Q: When you compile your program, you receive an error as follows:
9.   
10.%javac Welcome.java
11.javac: file not found: Welcome.java
12.Usage: javac 
13.use -help for a list of possible options
14. 
15.What is wrong?
  1. A: File Welcome.java does not exist in the directory. Check if Welcome.java is in the current directory.
17.Q: When you run your program, you receive an error as follows:
18. 
19.%java Welcome
20.Exception in thread "main" java.lang.NoClassDefFoundError: Welcome
21. 
22.What is wrong?
  1. A: File Welcome.class does not exist. You have to first compile Welcome.java to produce Welcome.class in the current directory.
24.Q: When you run your program, you receive an error as follows:
25. 
26.%java Welcome
27.Exception in thread "main" java.lang.NoSuchMethodError: main
28. 
29.What is wrong?
  1. A: Please check if you have a main method in Welcome.java or you have spelled the method public static void main(String[] args) correctly.
31.Q: When you compile your program, you receive an error as follows:
32. 
33.%javac Welcome.java
34.Welcome.java:2: cannot find symbol
35.symbol  : class string
36.location: class Welcome
37.  public static void main(string[] args) {
38.                          ^
39. 
40.1 error
41. 
42.What is wrong?
  1. A: Java is case-sensitive. string should be spelled as String.
Chapter 2: Primitive Data Types and Operations
1.  Q: What is wrong in the following code?
2.     
3.  public class Test {
4.    public static void main(String[] args) {
5.      i = 1;
6.      j = i * i * i;
7.    }
8.  }
  1. A: You will get a compile error that indicates symbol i cannot be resolved. Every variable must be declared before being used.
10.Q: What is wrong in the following code?
11.  
12.1  public class Test {
13.2    public static void main(String[] args) {
14.3      int i;
15.4      j = i * i * i;
16.5   }
17.6 }
  1. A: You will get a compile error that indicates variable i is not initialized in line 3.
19.Q: What is wrong in the following code?
20.  
21.1  public class Test {
22.2    public static void main(String[] args) {
23.3      int i;
24.4      int j = 5;
25.5      System.out.println(j * 4.5);
26.6   }
27.7 }
  1. A: The program compiles and runs fine. However, i is never used. This might be a potential programming error. Most IDE now can alert you of this situation.
29.Q: Why does this program display 0?   
30.1  public class Test {
31.2    public static void main(String[] args) {
32.3      int i = 9;
33.4      int j = 5 / 6 * (i * 4);
34.5      System.out.println(j);
35.6   }
36.7 }
  1. A: This type of mistakes is very common for my students. We have covered this repeatedly in the text. Here just to remind you again 5 / 6 is 0, because both 5 and 6 are integers.
38.Q: What is the output of this program?   
39.  public class Test {
40.    public static void main(String[] args) {
41.      System.out.println(2147483647 + 1);
42.    }
43.  }
  1. A: Run this program. You will see the output is -2147483648. Why? 2147483647 is the largest int value. Adding 1 to this value causes overflow. It is not a runtime error in Java This type of mistakes is very common for my students. We have covered this repeatedly in the text. Here just to remind you again 5 / 6 is 0, because both 5 and 6 are integers.
45.Q: What is the output of the following code?   
46.1  public class Test {
47.2    public static void main(String[] args) {
48.3      System.out.println("1 + 2 is " + i + j);
49.4   }
50.5 }
  1. A: The program displays 1 + 2 is 12. If you want the output to be 1 + 2 is 3, put i + j inside the parenthese (i + j) to force i + j to be evaluated first.
52.Q: What is the output of this program?   
53. 
54.  public class Test {
55.    public static void main(String[] args) {
56.      char ch = 'a';
57.      System.out.println('a' + 1);
58.      System.out.println(++ch);
59.    }
60.  }
  1. A: for 'a' + 1, 'a' is converted into int. so 'a' + 1 is 97 + 1 = 98. For ++ch, the result is 'b'.
Chapter 3: Selections
1.  Q: What is wrong in the following code?
2.  1  public class Test {
3.  2    public static void main(String[] args) {
4.  3      int i, j, k;
5.  4      i = j = k = 3;
6.  5      if (i < j < k)
7.  6        System.out.println("i, j, and k are in increasing order");
8.  7   }
9.  8 }
  1. A: i < j evaluates to a boolean value, which cannot be compared with k. The correct expression should be (i < j && j < k).
11.Q: What is the output of the following code?
12.  public class Test {
13.    public static void main(String[] args) {
14.      System.out.println(5.0 - 1.0 - 1.0 - 1.0 - 1.0 - 1.0 == 5.0);
15.    }
16.  }
  1. A: Run the program. You will be surprised to see the output is false. Why? because floating-point numbers are not stored in complete accuracy. You are dealing with approximation for floating-point numbers. Don't compare two floating-point numbers using equality == operators.
18.Q: What is the output of the following code?
19.  public class Test {
20.    public static void main(String[] args) {
21.      boolean done = false;
22.      if (done = true) {
23.        System.out.println("done");
24.      }
25.    }
26.  }
  1. A: Run the program. You will see the output is done. Why? you assign true to done. So, the condition is true. To check whether done is true, you should use done == true, or simply
28.if (done) {
29.  System.out.println("done");
30.}
31.Q: What is the output of the following code?
32.  public class Test {
33.    public static void main(String[] args) {
34.      boolean done = false;
35.      if (done);
36.      {
37.        System.out.println("done");
38.      }
39.    }
40.  }
  1. A: Run the program. You will see the output is done. Why? because the if statement ends with
42.if (done);
  
The semicolon ends the if statement. The lines
{
  System.out.println("done");
}
     
are not part of the if statement.
43.Q: What is the output of the following code?
44.  public class Test {
45.    public static void main(String[] args) {
46.      char = 'a';
47.      switch (ch) {
48.        case: 'a': System.out.print(ch);
49.        case: 'b': System.out.print(ch);
50.        case: 'c': System.out.print(ch);
51.      }
52.    }
53.  }
  1. A: Run the program. You will see the output is aaa. Why? There is no break statement for the case statement. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This phenomenon is referred to as the fall-through behavior.
Chapter 4: Loops
1.  Q: What is wrong in the following code?
2.    public class Test {
3.      public static void main(String[] args) {
4.        int i = 0;
5.        while (i < 10) {
6.          System.out.println("Welcome to Java!");
7.        }
8.      }
9.    }
  1. A: i is always 0. This is an infinite loop. This is a common error for while and do-while loops. Make sure the control variable changes to cause the loop to stop eventually.
11.Q: What is wrong in the following code?
12.  public class Test {
13.    public static void main(String[] args) {
14.      int i = 0;
15.      while (i < 10)
16.        System.out.println("Welcome to Java!");
17.      i++;
18.    }
19.  }
  1. A: i++ is not in the body of the while loop. So, this is an infinite loop. If the loop body contains more than one statement, use the braces to group them.
Chapter 5: Methods
1.  Q: What is the output of the following code?
2.    public class Test {
3.      public static void main(String[] args) {
4.        int x = 1;
5.        p(x);
6.        System.out.println("x is " + x);
7.      }
8.   
9.      public static void p(int x) {
10.      x = x + 1;
11.    }
12.  }
13. 
A: x is 1. Invoking p(x), the value of x is passed to the parameter in the method. Variable x defined in the main method and x defined in method p are two independent variables.
14.Q: Does Math.sin(90) return the sine value for angle 90 degree?
15.  
A: No. To return the sine value for angle 90 degree, use Math.sin(Math.PI / 2).
Chapter 6: Arrays
1.  Q: What is wrong in the following code?
2.    public class Test {
3.      public static void main(String[] args) {
4.        int[] x = new int[10];
5.       
6.        for (int i = 0; i <= x.length; i++)
7.          System.out.print(x[i]);
8.      }
9.    }
10. 
A: This is a common error, known as off-by-one. Here the array index is from 0 to 9, but the last i in the loop will be x.length (10). At 10, the index is out of range.
11.Q: What is wrong the following code?
12.  public class Test {
13.    public static void main(String[] args) {
14.      int[] x = new int[10];    
15.      p(x);
16.      System.out.print(x[0]);
17.    }
18.   
19.    public static void p(int[] list) {
20.      list[0] = 10;
21.    }
22.  }
23. 
A: The output is 10. When invoking p(x), the reference value of array x is passed to list. The first element in the array is changed in the method.

No comments:

Post a Comment