CSCI A201/A597 and I210

Spring Semester 2003


Directions:

Good luck and do well!

Write your name or username here:______________________


In the questions that follow:

  1. Wherever you see snippets of code whose output you are asked to predict, assume that they are embedded correctly in a complete program, which is then compiled and, if compilation succeeds, run.

  2. Wherever you are dealing with if statements please be careful! Indentation as presented may be misleading, and you are supposed to come up with the correct answer regardless of indentation. (Remember indentation is just acting as visual help for human readers of your code, including yourself, but the computer ignores it).

Question 1.
You compile and run this program. What is the output that it produces?
int x, y; 
x = 2; y = 3; y = y - x; 
System.out.println(x + y); 
21
23
3
5
other

Question 2.
You compile and run this program. What is the output that it produces?
int x, y; 
x = 2; y = 3; y = x; 
System.out.println(x + "y"); 
2x
2y
3x
3y
other

Question 3.
You compile and run this program. What is the output that it produces?
int x, y; 
x = 2; y = 3; 
System.out.println("1" + x * y);
16
123
error
other

Question 4.
You compile and run this program. What is the output that it produces?
System.out.println(3 * 2 / 5);
0
1
1.0
1.2
other

Question 5.
You compile and run this program. What is the output that it produces?
System.out.println(2 / 5 * 3);
0
1
1.2
0.1333...
other

Question 6.
You compile and run this program. What is the output that it produces?
int x = 2;
if (2 > x)
  System.out.print(1);
else
  System.out.print(2);
if (x < 2)
  System.out.println(3);
System.out.print(4); 
134
234
14
24
other

Question 7.
You compile and run this program. What is the output that it produces?
int x = 8;
if (x > 5)
  if (x < 10)
    System.out.print(1);
else
  System.out.print(2);
System.out.print(3);
13
23
3
123
other

Question 8.
You compile and run this program. What is the output that it produces?
int x = 8;
if (x > 5) {
  if (x < 10)
    System.out.print(1);
} else
  System.out.print(2);
System.out.print(3);
13
23
3
123
other

Question 9.
You compile and run this program. What is the output that it produces?
if (3 >= 2)
  if (1 > 0)
    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);
1235
1245
0235
0245
other

Question 10.
You compile and run this program. What is the output that it produces?
boolean x;
if (false)
  System.out.print(0);
else
  System.out.print(1);
x = (2 <= 1) && (3 <= 4);
if (x)
  System.out.print(2);
else
  System.out.print(3);
02
03
12
13
other

Question 11.
You compile and run this program. What is the output that it produces?
boolean x = false;
if (true)
  System.out.print(0);
else
  System.out.print(1);
x = !(x && !x);
if (x)
  System.out.print(2);
else
  System.out.print(3);
02
03
12
13
other

Question 12.
You compile and run this program. What is the output that it produces?
boolean a = true, b = false;
System.out.println(a && b); 
true
false
other

Question 13.
You compile and run this program. What is the output that it produces?
boolean a = true, b = false;
System.out.println(a || !b); 
true
false
other

Question 14.
You compile and run this program. What is the output that it produces?
boolean a = true, b = false;
System.out.println(a || b && !a); 
true
false
other

Question 15.
You compile and run this program. What is the output that it produces?
boolean a = true, b = false;
System.out.println((a || b) && !a); 
true
false
other

Question 16.
You compile and run this program. What is the output that it produces?
class One {
  int a = 3;
  void fun() {
    int b = 5;
    b = b - 1;
    a = a - 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One();
    alpha.fun();
    alpha.fun();
    alpha.fun();
  }
}
2 4 1 3 0 2
2 4 1 4 0 4
2 4 2 3 1 2
2 4 2 4 1 3
other

Question 17.
You compile and run this program. What is the output that it produces?
class One {
  int a = 3;
  int b = 5;
  void fun() {
    b = b - 1;
    a = a - 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One();
    alpha.fun();
    alpha.fun();
    alpha.fun();
  }
}
2 4 1 3 0 2
2 4 1 4 0 4
2 4 2 3 1 2
2 4 2 4 1 3
other

Question 18.
You compile and run this program. What is the output that it produces?
class One {
  int a = 3;
  int b = 5;
  void fun() {
    b = b - 1;
    a = a - 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
  }
}
2 4 1 3 0 2
2 4 1 4 0 4
2 4 2 3 1 2
2 4 2 4 1 3
other

Question 19.
You compile and run this program. What is the output that it produces?
class One {
  int a = 3;
  static int b = 5;
  void fun() {
    b = b - 1;
    a = a - 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
  }
}
2 4 1 3 0 2
2 4 1 4 0 4
2 4 2 3 1 2
2 4 2 4 1 3
other

Question 20.
You compile and run this program. What is the output that it produces?
class One {
  static int f(int x) { return x - 1; } 
  static int g(int x) { return 2 * x; }
  public static void main(String[] args) {
    System.out.println(f(g(2))); 
  }
}
2
3
4
10
other

Question 21.
You compile and run this program. What is the output that it produces?
class One {
  static int f(int x) { return x - 1; } 
  static int g(int x) { return 2 * x; }
  public static void main(String[] args) {
    System.out.println(g(f(2))); 
  }
}
2
3
4
10
other

Question 22.
You compile and run this program. What is the output that it produces?
class One {
  static int f(int x) { return x - 1; } 
  static int g(int x) { return 2 * x; }
  public static void main(String[] args) {
    System.out.println(f(g(2) + f(2))); 
  }
}
2
3
4
10
other

Question 23.
You compile and run this program. What is the output that it produces?
class One {
  static int f(int x) { return x - 1; } 
  static int g(int x) { return 2 * x; }
  public static void main(String[] args) {
    System.out.println(g(f(2) + g(2))); 
  }
}
2
3
4
10
other

Jordan Hall 124 7-9pm Feb 12 2003 A201/A597/I210 Midterm Exam One