Practice: Midterm

You have 30 minutes to complete the exam.

When you are finished relax until the timer runs out.

You can download the Practice

Practice Midterm

or answer see the questions here.

What exactly would be the output of: System.out.print(answer); in each of the following cases? (Write your answers on the answer sheet.)

1. int answer = 30 + 20 / 5 * 2 - 2;

2. double answer = (30 + (20 / 5)) * 2 - 2;

3. int answer = ((30 + 20) / 5) % (2 + 1);

4. How many bits does the char data type use? (Write your answer on the answer sheet.)

5. Which of Java’s primitive data types uses the least memory to represent natural numbers? (Write your answer on the answer sheet.)

6. Consider the following code:

   double outcome;
   int entryA = 98;
   int entryB = 212;
   outcome = entryA + entryB;

The last line is an example of:

a. promotion

b. assignment conversion

c. concatenation

d. parameter passing

e. casting

f. a data type error

7. Consider the following code:

   String outcome;
   String entryA = "98";
   String entryB = "212";
   outcome = entryA + entryB;

The last line is an example of:

a. promotion

b. assignment conversion

c. concatenation

d. parameter passing

e. casting

f. a data type error

8. In Java, an API is:

a. An online environment for writing code

b. Documentation of the fields, methods and other information about a class

c. A chip on a computer’s motherboard

d. A type of error that is caught by an exception handler

9. An algorithm should meet which of the following criteria?

a. Unambiguous

b. Executable

c. Terminating

d. All of the above

e. b and c only

10. Imagine a program that checks if a person applying for Social Security is at least 65 years old and also retired. Assume that the program uses an integer variable called age and a boolean variable called retired, where retired is true if a person is retired and false otherwise. Write an if statement that would be true only if the age and retired criteria for obtaining Social Security were met. (Write your answer on the answer sheet.)

Use the following code to answer question 11. Assume that x was declared as an integer.

if (x <= 25) {
System.out.print(a);
} else if ( x > 25) {
System.out.print(b);
}
if ( ! (x == 25)) {
System.out.print(c);
}

11. What will the output be is x is equal to 24?