# COP 2551 Exam 1 Name _____________________

```COP 2551 Exam 1
Name _____________________
Each question worth 10 points. This is a closed book exam.
1.What is printed by the following program:
public class loops {
public static void main(String[] args) throws IOException {
int count=0;
int i, j;
int n=10;
for(i=0; i &gt; n+1; i++) {
count++;
}
System.out.println(count);
count = 0;
for(i=0; i&lt; 20; i++) {
count++;
if (count &gt; 5) i = 10;
}
System.out.println(count);
count = 0;
for(i=0; i&lt;3; i++) {
for (j = 1; j &lt; i; j++) {
count++;
}
}
System.out.println(count);
count = 0;
i = n;
while (i &gt; 1) {
i--;
count++;
}
System.out.println(count);
count = 0;
while (n &gt; 1) {
if (n &gt; 1)
count++;
}
System.out.println(count);
} // end main
} // end
2. What is printed by the following program if the user enters the following (assume we have a
getInt() method).
a) 0
b) r
c) 5
public class q2 {
public static void main(String[] args) throws IOException {
int n; double x;
try {
n = getInt();
}
catch (NumberFormatException ex) {
System.out.println(“Hi”);
}
while (n !=5) {
n = 5;
x = n/2;
}
System.out.println(x);
} // main
}
3. Write a Java program to input an integer n (greater than 0) from the keyboard (assume you
have a getInt() method to do this) and print the smallest integer that is less than or equal to the
square root of n. [You may not use any pre-defined square root function from Java to do this!]
4. Write a Java program to input an integer n from the keyboard and then prints the outline of
a square whose sides each have n characters (assume you have a getInt() method to do this). So
if we input 3, the output should be:
xxx
x x
xxx
5.a) Write a class definition that defines a animal object with two data items representing their
gender and age, with a constructor that assigns these items the values passed to the constructor.
b) Write a class called horse that uses the animal class as a base class and which also has a
data item for name and a method that returns the name. Include a constructor method.
c) Show the commands needed to create two horse objects (in a “main”)
d) How would you modify b) to determine if two horses have the same name?
```