1.12 Review Exercises

  1. What will this program print?

    class JustWriting
    {
    	public static void main (String[] args)
    	{
    		System.out.print("Here ye are ");
    		System.out.println("(finally),\n");
    		System.out.print(IIat the end\nof\n");
    		System.out.println("\nChapter 1");
    	}
    }
             
  2. Write a single Java statement that will print the following, exactly as shown.

    The handsome stranger said,
    "My name is Kim, Paul Kim."
  3. State the type of each legal constant. If the constant is illegal, explain why.

  4. What is the smallest type of integer that could be used to represent each of the following values?
    (a) 400          (c) -100
    (b) 50 000       (d) 3 000 000 000
    
  5. Rewrite in standard decimal form
    (a) 8.77e-4   (b) 299.04eO
    (c) -0.0443E6 (d) -23e-4
    			
  6. Identify, with reasons, any identifiers that should not be used for Java variables.
    (a) 1stTime            (b) Perimeter
    (c) valueln$           (d) beginning
    (e) average height     (f) case
    
  7. Which of the following conversions always requires a cast?
    (a) long to double      (b) byte to char
    (c) float to double     (d) byte to long
    (e) short to char       (f) double to long
  8. What would be printed by each fragment?
    (a) double x = 0.093e-1, y -3e-3;
    System.out.println("x: " + x + "y: " + y);
    (b) int i = 5, j = -77;
    System.out.println("first is "+i+"\nsecond is "+j);
    
  9. The following program contains five known syntax errors. Rewrite the program with those errors corrected.
    class BadNews
    /* another mess */
    {
    	public static main (String[] args);
    	{
    		int i = 7; j = 3;
    		System.out.print("Smaller is " + i);
    		System.out.println(" Larger is " + j)
    	}
    
  10. Draw diagrams like those in Section 1.8 to illustrate the result of executing the following statements.

  11. Write and run an interactive program that asks the user to provide a string, a character, an integer, and a floating point value. After reading the values, the program should print each one on a separate line with a suitable message for each line of output.