TomLadendorfquizzes

advertisement
Tom Ladendorf
tladendo
1.)
int x, y; // declare variables
x = 2; y = 3;
y = x;
// initialize x at 2 and y at 3.
// sets y = 2
System.out.println(x + y);
// 2 + 2 = 4
// The program will return 4.
2.)
int x, y;
x = 2; y = 3;
// Same setup as #1.
y = x;
System.out.println(x + " versus " + y);
both ends of " versus "
// 2 will be concatenated to
// So, the output will be "2 versus 2"
3.)
int x, y;
x = 2; y = 3;
// Similar setup.
System.out.println("1" + x + y);
= 2 and y = 3.
// So the output will be "123".
4.)
// Concatenates the string "1" to x
int x, y;
x = 2; y = 3;
// Similar setup.
System.out.println("1" + (x + y));
quantity 2 + 3, which equals 5.
// Concatenates "1" to the
// So, the output will be "15".
5.)
char a, b, c;
// Declares character variables.
a = 'B'; b = 'C'; c = a; // a is the char 'B', b is the char 'C', and
c will be the same as a = 'B'
System.out.println(a + c + 'C');
'B', 'B' and 'C' will be added.
// The numbers that correspond to
// The output is 199.
6.)
5 * (7 + 4 / 2) // These are all integers.
// The type will be int.
7.)
i != 3 // != Should always return a boolean.
// The type will be boolean.
8.)
int a, b;
a = 12; b = 5; // Variables are declared and initialized.
System.out.println(a / b);
decimals)
// The output will be 2.
// Integer division (No remainders or
9.)
int a, b;
a = 12; b = 5; // Same setup as above.
System.out.println(a % b); // Modulo returns the remainder of integer
division.
10.)
int a; a = 'C' - 'A'; // a is initialized as an integer equal to the
subtraction of the values corresponding to the characters 'C' and 'A'.
// The output is 2.
System.out.println(a);
Quiz 2.
1.)
int x = 3; // x initialized at 3.
if (2 > x) // This is false.
System.out.print(1);
else
System.out.print(2);
// So, this will print.
if (x < 2) // This is false.
System.out.println(3);
System.out.print(4); // This prints either way.
// The output will be 24.
2.)
int x = 3; // Same setup.
if (2 > x) // This is false.
System.out.print(1);
else
System.out.print(2); // So this will print.
if (x < 2) {
// This is false, so the next two lines won't print.
System.out.println(3);
System.out.print(4); }
// The output will be 2.
3.)
int x = 3; // Same setup.
if (x > 5) // This is false.
if (x < 10) // This block of code won't run.
System.out.print(1);
else // Neither will this. This goes with the second if, since no
brackets were used.
System.out.print(2);
System.out.print(3);
// And this runs regardless of the ifs.
// The output will be 3.
4.)
int x = 3; // Same setup.
if (x > 5) { // This is false, so this next block won't run.
if (x < 10)
System.out.print(1); }
else // This will run.
System.out.print(2);
System.out.print(3);
// The output is 23.
// This runs either way.
5.)
if (2 <= 3) // This is true.
if (0 != 1) // So is this...
System.out.print(0); // This line will run.
else
System.out.print(1);
// This line won't.
System.out.print(2); // This runs regardless of the ifs.
if (2 > 3) // This is false.
if (0 == 1) // So, nothing in here runs.
System.out.print(3);
else
System.out.print(4);
System.out.print(5);
// This runs no matter what.
// The output will be 025.
6.)
boolean x; // x is declared to be a boolean.
if (true) // True is true...
System.out.print(0); // So this line will run.
else
System.out.print(1); // This one won't.
x = (1 < 2) && (4 < 3); // True and false is false.
if (x) // This is false,
System.out.print(2); // So this line is skipped.
else
System.out.print(3);
// And this one runs.
// The output will be 03.
7.)
boolean x; // Same setup.
if (true) // True is true...
System.out.print(3); // So this line runs.
else
System.out.print(2); // This one doesn't.
x = (1 < 2) || (4 < 3); // true or false is true, so x is now true.
if (x) // True is true...
System.out.print(1); // So this line runs,
else
System.out.print(0); // And this one doesn't.
// The output will be 31.
8.)
boolean x;
if (true)
System.out.print(3);
else
System.out.print(2);
x = (1 < 2) || (4 < 3); // Everything up to this point is the same as
the last problem.
if (x)
System.out.print(1);
// (the else has been erased)
System.out.print(0); // So this line runs.
// The output is 310.
9.)
boolean x;
if (true) // true is true, so 3 is printed.
System.out.print(3);
// (else has been erased.)
System.out.print(2);
// This runs regardless of the if.
x = (1 < 2) || (4 < 3); // x = true
if (x) // true is true.
System.out.print(1); // This line runs.
// (else has been erased.)
System.out.print(0);
// This runs regardless of the if.
// The output will be 3210.
Download