Practice exam 1
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.
-
- (10 points) Choose the best answer to each of the following:
(1) _____In Java, +is- A relational operator
- A unary operator
- A mathematical operator
- All of the above
- None of the above
(2) _____In Java, -1 the - is - A unary operator
- A binary operator
- Has
booleanoperands - All of the above
- None of the above
(3) _____In Java, a variable is - The implicit loss of precision in arithemtic operations
- The way the decrement operator changes
- A named space for holding a value
- All of the above
- None of the above
(4) _____In Java, the statement doubl cost;- Will cause a compile-time error
- Will cause a run-time error
- Will cause a repetitive stress error
- All of the above
- None of the above
(5) _____In the Java statement i = (int)d;the operator=is a/an:- Assignment operator
- Relational operator
- Typecast operator
- All of the above
- None of the above
(6) _____In the Java statement i = (int)d;the(int)operator is a/an:- Assignment operator
- Relational operator
- Typecast operator
- All of the above
- None of the above
(7) _____In the Java statement i = (int)4.0;the operand of the(int)operator is a/an:- Assignment operator
- Literal
- Typecast operator
- All of the above
- None of the above
(8) _____In Java, what kind of statement is int i;?- Assignment statement
- Declaration statement
- Typecast statement
- All of the above
- None of the above
(9) _____In Java, an assignmentstatement must contain which of the following?- Curly brackets
- Parentheses
- An equal sign
- All of the above
- None of the above
(10) _____In Java, the main()method of the "main class" must have what return type?doubleintStringvoid- None of the above
- (10 points) Choose the best answer to each of the following:
-
- (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 * 23 * 4 +2 + 3 + 4 * 52 + 3 +* 4 * 5 - (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 - (10 points) Provide a concise comparison of the two terms in each of the following.
Actual parameters and formal parameters Declaration and assignment doubleandintReference type and value type Class and method - (5 points) Write a single statement for each of the following that:
Declares priceto be adoublevariable.Assigns the value 40.99to a previously declareddoublevariable namedprice.Reduces a previously declared doublevariable namedpriceby 10 percent.Assigns the result of a call to a staticmethod namedsalesTax()(that is passed a previously declareddoubleactual parameter namedprice) in theAccountingclass to a previously declareddoublevariable namedtax.Displays the value of a previously declared doublevariable namedtaxon 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.
- (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".
- (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; } }