Review Exercises (Set A2)

  1. Name the 8 primitive data types in Java and indicate which type can be added to an int variable without producing an error or a value of type other than int.

  2. What is the minimum number of characters in the expression #3do* that must be removed for it to represent a legal name for a variable in Java?

  3. What does the following code print?

    public class Main {
        public static void main(String[] args) {
            double y = 1;
            double x = (27 % 7) + (y += (15.6 % 4.1));
            System.out.println(x);
        }
    }
    
  4. Convert 1101 to binary.

  5. Write the 8-bit two's complement version of -100

  6. Give an example of a "literal" for each of the following types: int, double, float, boolean, and char

  7. What java keyword is used to indicate a constant value?

  8. Convert the hexadecimal number 110101011011111000011100111 to hexadecimal.

  9. Write the truth table for (P && Q) ^ (! R)

  10. What does the following code print?

    public class Main { 
        public static void main(String[] args) {
            int x = 1;
            int y = 2;
            int z = 3;
            System.out.println( z / y == x ? x : y);
            System.out.println( 1.0 * x / 2);
        }
    }
    
  11. What does the following print?

    public class Main {
        public static void main(String[] args) {
            int x = 2 + 'B';
            int y = 5;
            switch (x) {
                case 'B': System.out.print(1);
                case 'C': System.out.print(2);
                case 'D': System.out.print(y + y + "B");
                default : System.out.print("B" + y + y);
            }
            if ((y > 4) && (y <= 6))
                if (y == 6)
                    System.out.print("E");
                else
                    System.out.print("F");
        }
    }
    
  12. What will the following print?

    public class Main {
        public static void main(String[] args) {
            int s = 0;
            for (int d = 1; d < 50; d++) {
                if (50 % d == 0)
                    s += d;
            }
            System.out.println(s);
        }
    }
    
  13. What will the following print?

    public class Main {
        public static void main(String[] args) {
            int s = '\u0052' - '\u0042';
            System.out.println(s);
        }
    }
    
  14. What will the following print?

    public class Main { 
        public static void main(String[] args) {
            char c = 'K';
            c = (char) (((7 + (c - 'A') * 5) % 26) + 'A');
            System.out.println(c);
        }
    }
    
  15. What will the following print?

    public class Main { 
        public static void main(String[] args) {
            double pi = 3.1459;
            System.out.printf("%.2f + \\\n %.1f" + "\"", pi, pi);
        }
    }
    
  16. If a and b are int variables, and b != 0, circle the choices below that equal a%b. (There may or may not be more than one.)

    a)  a - ( a / b ) * b
    b) ( a / b ) * b
    c)  a - a / b
    d) (double) a / b - a / b 
    
  17. What does the following print?

    public class Main {
        public static void main(String[] args) {
            int sum = 0;
            int item = 0;
            do {
               item++;
               sum += item;
               if (sum > 4)
                  break;
            } while (item < 5);
            System.out.println(sum);
    
        }
    }
    
  18. Suppose x, y, and z are of type int. Using an appropriate function from the Math class, write a single Java statement which defines a variable named max of type int and assigns it a value equal to the maximum of x, y, and z.

  19. The following program was intended to check the discriminant of the quadratic equation x^2 + 0.4x + 0.04 = 0 to say something about the number of solutions. The output printed was "zero or two solutions", when we know there is only one solution (namely, x = -0.2). Explain what went wrong.

    public class Main {
        public static void main(String[] args) {
             double a = 1;
             double b = 0.4;
             double c = 0.04;
             if (b*b - 4*a*c == 0) {
                 System.out.println("one solution");
             }
             else {
                 System.out.println("zero or two solutions");
             }
        }
    }
    
  20. Complete the following code so that a triangle of x's whose height and base match the user input is printed, as shown in the sample runs given.

    public class Main {
        public static void main(String[] args) {
             int n = Integer.parseInt(args[0]);
             
             
             
             
             
             
        }
    }
    
    Sample Runs: 
    
    > java Main 5         <-- Note: this is a command to run the program, not output           
    x                     
    xx
    xxx
    xxxx
    xxxxx 
    
    > java Main 3         <-- Note: this is a command to run the program, not output
    x
    xx
    xxx
    

Answers

  1. byte*, short*, int*, long, float, double, char*, boolean

  2. 4

  3. 10.3

  4. 10001001101

  5. 10011100

  6. 1, 1.0, 1.0F, true, 'a', respectively

  7. final

  8. 6ADF0E7

  9.  
    P Q R (P&&Q) (!R) (P&&Q)^(!R)
    F F F    F     T      T
    F F T    F     F      F
    F T F    F     T      T
    F T T    F     F      F
    T F F    F     T      T
    T F T    F     F      F
    T T F    T     T      F
    T T T    T     F      T
    
  10.  
    1
    0.5
    
  11.  
    10BB55F
    
  12.  
    43
    
  13.  
    16
    
  14.  
    F
    
  15.  
    3.15 + \
     3.1"
    
  16. (a)
  17.  
    6
    
  18.  
    int max = Math.max(x, Math.max(y,z));
    
  19. you can't compare doubles with ==, they are stored approximately

  20.  
    public class Main {
        public static void main(String[] args) {
             int n = Integer.parseInt(args[0]);
             for (int row = 0; row < n; row++) {
                 for (int col = 0; col <= row; col++)
                      System.out.print('x');
                 System.out.println();
             }
        }
    }