Spring Semester 2005


Lab Three: More Practice Quizzes

Here are some questions for you to work on for next time:

Question 1.
What is the output produced by the following three lines (when correctly embedded in a complete program)?

int x, y;
x = 2; y = 3;
y = x;
System.out.println(x + y); 
Please type your answer here: ____________________


Question 2.
What is the output produced by the following three lines (when correctly embedded in a complete program)?
int x, y;
x = 2; y = 3;
y = x;
System.out.println(x + " versus " + y); 
Please type your answer here: ____________________


Question 3.
What is the output produced by the following two lines (when correctly embedded in a complete program)?
int x, y;
x = 2; y = 3;
System.out.println("1" + x + y); 
Please type your answer here: ____________________


Question 4.
What is the output produced by the following two lines (when correctly embedded in a complete program)?
int x, y;
x = 2; y = 3;
System.out.println("1" + (x + y)); 
Please type your answer here: ____________________


Question 5.
What is the output produced by the following two lines (when correctly embedded in a complete program)?

Please note that the variables are of type char.

char a, b, c;
a = 'B'; b = 'C'; c = a;
System.out.println(a + c + 'C'); 
Please type your answer here: ____________________


Question 6.
What is the type of this expression?
5 * (7 + 4 / 2)
Please type your answer here: ____________________

(Please use the Java keyword for the type).


Question 7.
What is the type of this expression?
i != 3
Please type your answer here: ____________________

(Please use the Java keyword for the type. Also, assume i is a double).


Question 8.
What is the output produced by the following two lines (when correctly embedded in a complete program)?

Please note that the variables are of type int.

int a, b;
a = 12; b = 5;
System.out.println(a / b); 
Please type your answer here: ____________________


Question 9.
What is the output produced by the following two lines (when correctly embedded in a complete program)?

Please note that the variables are of type int.

int a, b;
a = 12; b = 5;
System.out.println(a % b); 
Please type your answer here: ____________________


Question 10.
What is the output produced by the following two lines (when correctly embedded in a complete program)?

int a; a = 'C' - 'A';
System.out.println(a); 
Please type your answer here: ____________________


Question 11.
What is the output produced by the following code when embedded in a complete program?
int x = 3;
if (2 > x)
  System.out.print(1);
else
  System.out.print(2);
if (x < 2)
  System.out.println(3);
System.out.print(4); 
Please type your answer here: ____________________

Note that the program may or may not be indented correctly so think about it carefully.


Question 12.
What is the output produced by the following code when embedded in a complete program?
int x = 3;
if (2 > x)
  System.out.print(1);
else
  System.out.print(2);
if (x < 2) {
  System.out.println(3);
System.out.print(4); } 
Please type your answer here: ____________________

Note that curly braces have been added to the code from the previous exercise, and the same comment about indentation applies.


Question 13.
What is the output produced by the following code when embedded in a complete program?
int x = 3;
if (x > 5)
  if (x < 10)
    System.out.print(1);
else
  System.out.print(2);
System.out.print(3); 
Please type your answer here: ____________________

Note that the program may or may not be indented correctly so think about it carefully.


Question 14.
What is the output produced by the following code when embedded in a complete program?
int x = 3;
if (x > 5) {

  if (x < 10)
    System.out.print(1); }
else
  System.out.print(2);
System.out.print(3); 
Please type your answer here: ____________________

Note that curly braces have been added to the code from the previous exercise, and the same comment about indentation applies.


Question 15.
What is the output produced by the following code when embedded in a complete program?
if (2 <= 3)
  if (0 != 1)
    System.out.print(0);
  else
    System.out.print(1);
System.out.print(2);
if (2 > 3)
  if (0 == 1)
    System.out.print(3);
  else
    System.out.print(4);
System.out.print(5); 
Please type your answer here: ____________________

Note that the program may or may not be indented correctly so think about it carefully.


Question 16.
What is the output produced by the following code when embedded in a complete program?
boolean x;
if (true)
  System.out.print(0);
else
  System.out.print(1);
x = (1 < 2) && (4 < 3);
if (x)
  System.out.print(2);
else
  System.out.print(3); 
Please type your answer here: ____________________

Note that the program may or may not be indented correctly so think about it carefully.


Question 17.
What is the output produced by the following code when embedded in a complete program?
boolean x;
if (true)
  System.out.print(3);
else
  System.out.print(2);
x = (1 < 2) || (4 < 3);
if (x)
  System.out.print(1);
else
  System.out.print(0); 
Please type your answer here: ____________________

Note that the program may or may not be indented correctly so think about it carefully.


Question 18.
Consider the program from the previous exercise where the else keyword has been erased:
boolean x;
if (true)
  System.out.print(3);
else
  System.out.print(2);
x = (1 < 2) || (4 < 3);
if (x)
  System.out.print(1);
else
  System.out.print(0); 
What is the program going to output when embedded in a complete program?

Please type your answer here: ____________________

Note that the program may or may not be indented correctly now so think about it carefully.


Question 19.
Consider the program from the previous exercise where both else keywords have been erased:
boolean x;
if (true)
  System.out.print(3);
else
  System.out.print(2);
x = (1 < 2) || (4 < 3);
if (x)
  System.out.print(1);
else
  System.out.print(0); 
What is the program going to output when embedded in a complete program?

Please type your answer here: ____________________

Note that the program may or may not be indented correctly now so think about it carefully.

For next time try to solve these problems and be ready to discuss the answers.


Last updated by Adrian German for A201/A597/I210 on Tue Jan 25 10:09:28 EST 2005