Written Portion of Exam 1 - Sample

 

Answer all of the following questions. This is a "closed book" examination and you must work entirely on your own.
    1. (10 points) Choose the best answer to each of the following:
      (1) _____ In Java, + is
      1. A relational operator
      2. A unary operator
      3. A mathematical operator
      4. All of the above
      5. None of the above
      (2) _____ In Java, -1 the - is
      1. A unary operator
      2. A binary operator
      3. Has boolean operands
      4. All of the above
      5. None of the above
      (3) _____ In Java, a variable is
      1. The implicit loss of precision in arithemtic operations
      2. The way the decrement operator changes
      3. A named space for holding a value
      4. All of the above
      5. None of the above
      (4) _____ In Java, the statement doubl cost;
      1. Will cause a compile-time error
      2. Will cause a run-time error
      3. Will cause a repetitive stress error
      4. All of the above
      5. None of the above
      (5) _____ In the Java statement i = (int)d; the operator = is a/an:
      1. Assignment operator
      2. Relational operator
      3. Typecast operator
      4. All of the above
      5. None of the above
      (6) _____ In the Java statement i = (int)d; the (int) operator is a/an:
      1. Assignment operator
      2. Relational operator
      3. Typecast operator
      4. All of the above
      5. None of the above
      (7) _____ In the Java statement i = (int)4.0; the operand of the (int) operator is a/an:
      1. Assignment operator
      2. Literal
      3. Typecast operator
      4. All of the above
      5. None of the above
      (8) _____ In Java, what kind of statement is int i;?
      1. Assignment statement
      2. Declaration statement
      3. Typecast statement
      4. All of the above
      5. None of the above
      (9) _____ In Java, an assignment statement must contain which of the following?
      1. Curly brackets
      2. Parentheses
      3. An equal sign
      4. All of the above
      5. None of the above
      (10) _____ In Java, the main() method of the "main class" must have what return type?
      1. double
      2. int
      3. String
      4. void
      5. None of the above
    1. (10 points) Evaluate each of the following expressions (which are written in Java) assuming that multiplication has higher precednce than addition and that all operators use left-to-right associativity. If the expression contains a syntax error, write the word "Error".
      3 * 4 * 2

       

      3 * 4 +

       

      2 + 3 + 4 * 5

       

      2 + 3 +* 4 * 5

       

    2. (10 points) Provide a concrete example of each of the following (in Java). If appropriate, you may use the same example more than once.
      Compile-time error

       

      Logic error

       

      Run-time error

       

      Style error

       

      Syntax error

       

    3. (10 points) Provide a concise comparison of the two terms in each of the following.
      Actual parameters and formal parameters

       

      Declaration and assignment

       

      double and int

       

      Reference type and value type

       

      Class and method

       

    4. (5 points) Write a single statement for each of the following that:
      Declares price to be a double variable.

       

      Assigns the value 40.99 to a previously declared double variable named price.

       

      Reduces a previously declared double variable named price by 10 percent.

       

      Assigns the result of a call to a static method named salesTax() (that is passed a previously declared double actual parameter named price) in the Accounting class to a previously declared double variable named tax.

       

      Displays the value of a previously declared double variable named tax on the console (i.e., standard output) in a field that has a width of 6 with 2 digits to the right of the decimal place.

       

  1. (5 points) Show what will be printed by the following application (assuming it is compiled and executed properly).
    public class CardChecker{
    
        public static void main(String[] args) {
           int   digit2;
           ing   digit3;       
           int   group1;
           int   group2;
           int   group3;
           int   group4;
           
           group1 = 6271;
           group2 = 1882;
           group3 = 4132;
           group4 =   71;
           
           if (checkGroup(group1)) {
               System.out.printf("Step A\n");
           }
    
           digit2 = getDigit(group2);       
           if (digit2 > 1) {
    System.out.printf("Step B\n"); } digit3 = getDigit(group3); if (digit2 == digit3) { System.out.printf("Step C\n"); } if (group1 > group2) { if (group4 > 100) { System.out.printf("Step D\n"); } } else { System.out.printf("Step E\n"); } } public static int getDigit(int n) { int result; result = n / 1000; return result; } public static boolean checkGroup(int n) { boolean result; int remainder; remainder = n % 2; result = (remainder == 1); return result; } }

Back to Top