menu

Comp 202 Midterm Practice

Melanie Lyman-Abramovitch • Kaleem Siddiqi • Dan Pomerantz
Use ↑ ↓ to navigate through questions; use ← → to show/hide answers
Hold shift and use arrows to jump to the top/bottom or show/hide all answers.
Click a question to jump to it and show/hide the answer
Questions marked with * are added by me, and were not a part of the original practice
True/False Section
If you only expect numbers as command line arguments, you can change
public static void main(String[] args)
to
public static void main(double[] args)
and run the program.
False
The main method always has parameter String[]. If you need a double from an argument at index i, use Double.parseDouble(args[i])
The following is a valid header for a main method. (Valid → will compile and run).
public static void main(String[] commandArguments)
True
Variable names are completely up to you, so long as you use commandArguments[0] instead of args[0]
You can put an if-block inside of another if-block.
True
Example
if (number > 0) { //number is positive
    if (number % 2 == 0) { //number is divisible by 2
        System.out.println("Number is even and positive");
    } else { //number is not divisible by 2
        System.out.println("Number is odd and positive");
    }
}
A method can contain more than one return statement.
True
A very common way of coding is to return values immediately, given a specific number. This also removes the need for if statements, since the method will no longer continue once a return is issued
//get absolute value of integer
public static int absoluteValue(int number) {
    if (number > 0) return number; //number is already positive
    return -number; //negate value and return
}
When iterating through the elements of array via its index, you must use a for loop as opposed to a while loop.
False
For, while, and do are different ways of doing the same thing. They have their differences, but you can accomplish any loop with any implementation.
* The following loop is invalid
int i = 0;
for (; i < 10; i++)
//do something in the loop
False
To compile a for loop, you must have three components in the statement, separated by semicolons. However, those components may be blank. The code is equivalent to how you might write a while loop
int i = 0;
while (i < 15) ... i++;
Multiple Choice Section
More than one choice may be valid per question
Which of the following are examples of compile-time errors?
  1. Accessing an array at index -1
  2. Dividing an integer by 0
  3. Passing an int value as input to a method that expects a double input argument
  4. Accessing a variable outside of its scope.
  5. Omitting a semi-colon at the end of a statement.
D, E
Compile-time errors are errors that can be found prior to evaluating a statement. They mostly contain syntax errors, or passing incorrect types into methods.
  1. is a runtime error, producing an ArrayIndexOutOfBoundsException
  2. is a runtime error, producing an ArithmeticException
  3. is not an error, as integers can be automatically casted into a double; the opposite is not true
Short Answer Section
What is the value of x?
double x = (double) (1/2) + (1/2) + (double)(1/2);
0 or 0.0
Order matters, so when you execute (double) (1/2), you calculate 1/2 first (as integers), then cast it into a double. All three values will result in 0, and the sum will also be 0.
The following method contains five variables. Which variables (if any) exist on the line marked ****HERE****?
public static void method(String[] input) {
    int x = Integer.parseInt(input[0]);
    int y = Integer.parseInt(input[1]);
    if (x < y) {
        String s = "The first number is smaller";
    } else {
        String t = "The second number is smaller";
    }
    ****HERE ****
}
input, x, y
Variables exist within their closest surrounding braces {}. Both s and t exist only within the if else statements.
What prints when the following code executes?
public static void main(String[] args){
    int x = 4;
    int y = 5;
    y = swap(x,y);
    System.out.println("x is " + x + " y is " + y);
}
public static int swap(int x, int y){
    int temp = x;
    x = y;
    y = temp;
    return x;
}
x is 4 y is 5
Note that the swapping only affects the values within the swap method, and do not affect the primitive values in the main method. More on references here. Also keep in mind that y = swap(x, y) does change y, but since the value returned from the method is 5, y's value does not change.
What prints when the following code executes?
public static void method(){
    int n = 5;
    if(n > 0){
        System.out.print("one ");
        n = 0;
    }
    if(n <= 0){
        System.out.print("two ");
    } else {
        System.out.print("three ");
    }
}
one two
n > 0 so "one" prints and n becomes 0.
The next if statement is not nested, so it still executes; n ≤ 0, so "two" prints, and the else is skipped.
For the code below,
  1. How many times is the condition in the inner loop evaluated?
  2. How many times does the text "Hello" get printed?
for(int i=0; i<5; i+=2){
    for(int j=0; j<=i; j++){
        System.out.println("Hello");
    }
}
12 evaluations, 9 prints
We may first look at the print statement. There are three values for i: 0, 2, 4, which are then passed to the inner loop. When i = 0, the print statement is called once. When i = 2, Hello prints 3 times, and when i = 4, Hello prints 5 times, totalling 9.
In regards to evaluation, keep in mind that the loop needs to evaluate until the middle condition is not met. If we look at i = 0, the inner condition is still evaluated when j = 1, where it knows that j > 0 and stops. As a result, you will have 2, 4, and 6 evaluations when i is 0, 2, and 4 respectively, totalling 12 evaluations.
* What prints?
public static void main(String[] args) {
    int[] a = {1, 2, 3, 4, 5, 6};
    test(a);
    //prints the values rather than the reference
    System.out.println(Arrays.toString(a));
}

static void test(int[] a) {
    for (int i = 0; i < a.length; i++)
        a[i] = a[(i + 1) % a.length];
}
[2, 3, 4, 5, 6, 2]
The numbers are shifted left, so every number at index i is the original value at index (i + 1). However, notice that at the end, the last index takes the value at a[0], which is now 2, not 1.