20110916_0556PM (1220311)/Lab3

advertisement

Hyun Seok (Harry) Oh

Quiz 3

(1) 02 a.

The first condition is true, so system will print 0. X has a Boolean value true, because x || !x means True or False, and in an OR, only one value needs to be true in order for the whole OR to be true. So, the system prints 2 and not 3 on the same line. Hence 02.

(2) 0 a.

Since the initial condition for the first if statement is true, it will only print out 0, because the entire else is enclosed in brackets.

(3) 4 a.

Because first condition is true, remainder of the enter if statement does not print.

So, only 3 + 1, or 4, is printed.

(4) 43 a.

This time, the since there is no else after the initial condition has been printed, we are still in the “true section” up until the ‘else’. So we also print just x, which is 3.

(5) 432 a.

The first three System.out.println are all a part of the true section of the first condition, and since the first condition is true, those three are printed. The remainder of the code is all a part of the “else” of the third if statement, but since the values were true, the computer does not need to compute the stuff in the else section.

(6) 439 a.

A tricky one: The first two if statements return 4 and 3, just like the previous problems, but since the third if statement ends because it is the ‘else’ of the second if, the four if statement is treated as a new if statement of its own. Hence, 9 is printed on the same line.

(7) 100 a.

Since an OR can be true as long as one of those values are true, the condition is true. Hence the true section of the if statement is printed.

(8) 200 a.

Due to the fact that the (false || true) is in a conjunction with a false value, despite

(false || true) being true, the overall expression is false, by definition of an AND.

Therefore, the false section of the if statement is chosen.

(9) toString()

(10) 1 1 2 1 3 1

Quiz 4

(1) 1 1 2 2 3 3 a.

The variable values are all kept track of, adding one on each time the method is executed.

(2) 1 1 1 1 2 2 a.

The last two numbers are different, because alpha.fun() actually keeps track of the variables while beta is only run once, so the values are only added once.

(3) 1 1 1 2 2 3 a.

The static variable makes the beta.fun() method return values that are larger.

(4) The program does not compile a.

This is because One does not take on any inputs.

(5) The program compiles and runs and outputs 3 0 a.

The One with no input prints 3 (Alpha) and the One that takes on an integer prints

0, hence 3 0.

(6) The program compiles and runs and outputs 3 3 a.

Because changes in a static variable changes all other instances of it, the values of

“value” in both alpha and beta are equal, hence 3 3.

(7) The program compiles and runs and outputs 0 0 a.

Since no values were assigned to alpha or beta, the system simply prints 0 0.

(8) Two variables and a method. a.

The two variables are a and b, which are equal to 0 and 1, respectively. The method is the println method.

(9) Beta.class and Gamma.class only a.

This seems to be because we didn’t name a class for Alpha in the file. For example, if we want to create a class for Alpha, then we must write class Alpha.

Since there is no class for Alpha, the compiler doesn’t produce Alpha.class.

(10) “greater than 2” a.

on the first if statement, the condition holds true, so we go to the if statement immediately below. But this time, the condition returns false, so we go to the third if statement, since that is where the ‘else’ is. Since the condition holds true this time, the system prints “greater than 2”.

Quiz 5

(1) A line of 10 numbers a.

This is because, although the loop stops at 9, the program also prints 0. Inclusively, there will be a total of 10 numbers.

(2) A line of 10 numbers a.

This is the same as the previous question, but each number will not print on a new line, but all on the same line

(3) One line of 40 numbers on it a.

The inner for loop will generate ten lines, 0 to 9, and the process will repeat a total of four times all on the same line

(4) 40 lines of 1 number each a.

The inner for loop prints at each line, so there are 10 lines each time it is executed.

It is executed a total of four times with one character per line, there is ten times four lines with 1 number each.

(5) 4 lines of 10 numbers each a.

The inner for loop prints 10 numbers all on the same line, and then the outside for loop will restart the entire process on a new line. Hence, there are four lines with ten numbers on each of them.

(6) 10 lines of 4 numbers each a.

The inner for loop only prints four numbers on the same line, and the outside loop will print it a total of ten times.

(7) 4 lines of 10 numbers each that count the lines a.

The inner for loop prints the value of j, so each line will have 0, 1, 2, and 3.

(8) 4 lines of 10 numbers that count the columns a.

This time, the value of j ranges from 0 to 9, and these numbers are all printed in one line, a total of four times because of the outside for loop.

(9) Numbers in the shape of an arrow a.

The top row, since j is 0, it will print 0 ten times. Vertically, since at the beginning, i is always 0, the first line will be zero. Then, the line, will go diagonally to create an arrow.

(10) An organized X sign, a.

Too difficult to explain, but simply put, the first line and last line have only one value, because the condition is satisfied just once. But on the remainder, besides the center, all have two values, because I +j ==10 on 1+9, 2 + 8, etc.

(11) A single number will be printed, the number 5. a.

This is because only when both i and j are 5, is I ==j and I + j = 10.

Download