Comp 170                               TEST I                         3 June 1999



 I     For each of the following segments of code, determine the associated output when the code is executed.  Be precise, carefully indicating the position of every character on a given line of output.

(A)

System.out.print("abc");
System.out.print("def");
System.out.println("hi");
System.out.print("j");
System.out.println( "k" );
System.out.println("lmn");
System.out.println("opq");
 

(B)

System.out.println("abc" + 3 + 4);
System.out.println(5 + 6 + "def");
System.out.println("7+9=" + 7 + 9 );
System.out.println("7+9=" + (7 + 9));
System.out.print("Never" + " divide ");
System.out.print("b" + "y ");
System.out.println(44 - 43 + 54 - 55);
 
 

(C)

System.out.println( 3/(6-5) +1);
System.out.println( 4 / 9 / 3 + 9 / 3 / 4);
System.out.println(1 + 2 * 3 - 5 * 4 / 5);
System.out.println(3 * 2  +  4 / 5 * 5);
 

(D)

int x = 3;
int y = 4;
int z;
z = x + y;
y = x + z;
x = y + z;
System.out.println("x=" + x);
System.out.println("y=" + y);
System.out.println("z=" + z);

II

For each of the following segments of code, determine the associated output when the code is executed.  Be precise, carefully indicating the position of every character on a given line of output.

(A)

String test = "abcdefg";
System.out.println (test.length());
System.out.println(test.charAt(1));
System.out.println(test.substring(5));
System.out.println(test.substring(2,5));
System.out.println(test.indexOf("def"));
System.out.println(test.indexOf("fgh"));
 
 

(B)

String greeting = "How do you do";
String name = "Seven of Nine";
System.out.println(name.charAt(greeting.indexOf("do")));
System.out.println(greeting + " " + name);
if (greeting.length() > name.length())
     System.out.println("Wow.");
if (greeting.length() <= name.length())
    System.out.println("Nevermore.");
 
 

(C)

double x = 5.1,  y = 7.8, z = 8.9;
int  a;
if (x < y)
     y = x - 1;
if (x >= y)
     x = y - 1;
a = (int)z;
z = (double)a;
System.out.println ("x=" + x);
System.out.println ("y=" + y);
System.out.println ("z=" + z);
System.out.println ("a=" + a);
 
 
 
 

III     Describe explicitly the output when the following program fragment is executed:
 
 

String name = "Albertine";

int count = 1;

while (count < 5) {

    int index = name.indexOf("bert");
 
    System.out.println("index=" + index);

    System.out.println("count=" + count);

    if (count < name.length())

        name = name.substring(count);

    System.out.println(name);

    count++;
}
 
 

 IV     For each of the following Java programs, determine the associated output when the code is executed.  Be precise, carefully indicating the position of every character on each line of output.

(A)

 public class Calculate{
    public static void main (String[] args) {
   int n = 1234;
        while (n>0) {
            System.out.print(n%10);
            n = n/10;
        }
        System.out.println();
    }
}
 
 
 
 

(B)

 public class Calculate{
    public static void main (String[] args) {
    int n = 3456;
         int sum = 0;
         while (n>0){
            sum = sum + n%10;
            n = n/10;
        }
        System.out.println("sum =" + sum);
    }
}
 
 

Course Home Page          Department Home Page        Loyola Home Page