Expressions
Code Tracing
Methods/Parameters
if-else
100

6 * 2 - 3 * (5 - 10)

2 + 20 / 5 - 3 * 4 / 2

"ut" + "cs" + 1 + "dh"

27

0

"utcs1dh"

100

What is output by the following code when it is run?


int x1 = 3;

int y1 = x1 * 2 + 3 * x1;

x1 = -1 + x1;

y1++;

System.out.print(x1 + " " + y1);

2 16

100

What output is produced by the following program?

public class MysteryTouch {
    public static void main(String[] args) { 
        String head = "shoulders"; 
        String knees = "toes"; 
        String elbow = "head"; 
        String ear = "eye"; 
        touch(ear, elbow); 
        touch(knees, "Toes"); 
        touch(head, "knees " + knees); 
    }


    public static void touch(String elbow, String ear) { 
        System.out.println("touch your " + elbow + " to your " + ear); 
    }
}
touch your eye to your head
touch your toes to your Toes
touch your shoulders to your knees toes


100

x = -6;

if (x > 0) {

    System.out.println("Positive");

} else if (x < 0) {

    System.out.println("Negative");

} else {

    System.out.println("Zero");

}


What is output by the code above when it is run?

Negative

200

6 / 10.0 + 6 / 10 + 25 / 100.0

50 % 15 + 33 % (10 * 6)

"Java" + 3 * 4

0.85

38

"Java12"

200

What is output by the following code when it is run?

int x7 = 6;

int y7 = 3;

int z7 = y7 * 10 % (x7 - (y7 * 2));

System.out.print(z7);

runtime error

200

Write a method called printNumbers that accepts a maximum number as a parameter and prints each number from 1 up to that maximum, inclusive, boxed by square brackets. For example, consider the following calls:

printNumbers(15);

printNumbers(5);


These calls should produce the following output:

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]

[1] [2] [3] [4] [5]


You may assume that the value passed to printNumbers is 1 or greater.

public static void printNumbers(int num) {

    for (int i = 1; i <= num; i++) {

        System.out.print("[" + i + "] ");

    }

}

200

int a = 6;

if (a < 6) {

    a = a + 1;

    System.out.println("a incremented.");

}

if (a > 6) {

    System.out.println("a is too high.");

} else {

    System.out.println("a is correctly set.");

}


What is output by the code above when it is run?

a is correctly set.

300

3 * -4 + "DHF" + (6 - 10)

"val" + (2 * 1.5) + (6 % 2)

Math.pow(Math.floor(2.95), Math.ceil(4.0))

"-12DHF-4"

"val3.00"

16.0

300

How many asterisks does the following code print out? Don't show the output. Simply state the number of asterisks that are printed out when the code runs.


for (int i = 1; i <= 5; i++) {

    System.out.print("**");

    for (int j = 1; j <= 10; j++) {

        System.out.print("**");

        System.out.print("***");

    }

    System.out.print("*");

}

265

300

Write a method called pythagoreanTheorem that accepts two doubles, a and b, as parameters and computes the c value as per the Pythagorean theorem. 

For example, the call of pythagoreanTheorem(3.0, 4.0) would return 5.0 and the call of pythagoreanTheorem(4.5, 6) would return 7.5.

public static double pythagoreanTheorem(double a, double b) {

    return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));

}

300

Write a method called printTriangleType that accepts three integer arguments representing the lengths of the sides of a triangle and prints what type of triangle it is. The three types are equilateral, isosceles, and scalene. An equilateral triangle has all three sides the same length, an isosceles triangle has two sides the same length, and a scalene triangle has three sides of different lengths. Here are some example calls to printTriangleType:


printTriangleType(5, 7, 7);

printTriangleType(6, 6, 6);

printTriangleType(5, 7, 8);

printTriangleType(12, 18, 12);

The output produced should be the following:


isosceles

equilateral

scalene

isosceles

public static void printTriangleType(int side1, int side2, int side3) {

    int count = 0;

    if(side1==side2) {

        count++;

    }

    if(side3==side2) {

        count++;

    }

    if(side1==side3) {

        count++;

    }

    if(count==0) {

        System.out.println("scalene");

    } else if(count==3) {

        System.out.println("equilateral");

    } else {

        System.out.println("isosceles");

    }

}