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
boolean
operands - 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 assignment
statement 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?double
int
String
void
- 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 * 2
3 * 4 +
2 + 3 + 4 * 5
2 + 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 double
andint
Reference type and value type Class and method - (5 points) Write a single statement for each of the following that:
Declares price
to be adouble
variable.Assigns the value 40.99
to a previously declareddouble
variable namedprice
.Reduces a previously declared double
variable namedprice
by 10 percent.Assigns the result of a call to a static
method namedsalesTax()
(that is passed a previously declareddouble
actual parameter namedprice
) in theAccounting
class to a previously declareddouble
variable namedtax
.Displays the value of a previously declared double
variable namedtax
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.
- (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; } }