ITSC 1213 TEST 1 QUESTIONS AND CORRECT
ANSWERS
A constructor must have which of the following? - Answers - the exact same name as
the class name
After running this code, what will the value of count be?
Hobbit[] myHobbits = new Hobbit[20];
int count = 1;
while (count < 10) {
for (int i = 0; i < myHobbits.length; i+=3) {
myHobbits[i] = new Hobbit("Hobbit #" + count);
count++;
}
} - Answers - 15
Assume that (based on how the random assignment goes), Frodo has the ring and
Bilbo doesn't. What is the output of this program?
public class Hobbit {
private int age;
private String name;
public Hobbit(String n) {
name = n;
}
public void setAge(int x) {
age = x;
}
public int getAge() {
return age;
}
public boolean hasRing() {
if (Math.random() > 0.5) {
return true;
, } else {
return false;
}
}
}
public class Test {
public static void main(String[] args) {
Hobbit bilbo = new Hobbit("Bilbo");
Hobbit frodo = new Hobbit("Frodo");
Hobbit samwise = new Hobbit("Samwise");
Hobbit merry = new Hobbit("Merry");
Hobbit pippin = new Hobbit("Pippin");
bilbo.setAge(110);
frodo.setAge(38);
samwise.setAge(36);
merry.setAge(34);
pippin.setAge(32);
bilbo.setAge(frodo.getAge());
pippin.setAge(samwise.getAge());
frodo.setAge(pippin.getAge());
System.out.println("frod - Answers - frodo's age = 36
bilbo's age = 38
Phew!
Assume the following statement is added to the end of Test:
System.out.println(pippin.getAge() == merry.getAge());
What is the output of this statement?
public class Hobbit {
private int age;
private String name;
public Hobbit(String n) {
name = n;
}
ANSWERS
A constructor must have which of the following? - Answers - the exact same name as
the class name
After running this code, what will the value of count be?
Hobbit[] myHobbits = new Hobbit[20];
int count = 1;
while (count < 10) {
for (int i = 0; i < myHobbits.length; i+=3) {
myHobbits[i] = new Hobbit("Hobbit #" + count);
count++;
}
} - Answers - 15
Assume that (based on how the random assignment goes), Frodo has the ring and
Bilbo doesn't. What is the output of this program?
public class Hobbit {
private int age;
private String name;
public Hobbit(String n) {
name = n;
}
public void setAge(int x) {
age = x;
}
public int getAge() {
return age;
}
public boolean hasRing() {
if (Math.random() > 0.5) {
return true;
, } else {
return false;
}
}
}
public class Test {
public static void main(String[] args) {
Hobbit bilbo = new Hobbit("Bilbo");
Hobbit frodo = new Hobbit("Frodo");
Hobbit samwise = new Hobbit("Samwise");
Hobbit merry = new Hobbit("Merry");
Hobbit pippin = new Hobbit("Pippin");
bilbo.setAge(110);
frodo.setAge(38);
samwise.setAge(36);
merry.setAge(34);
pippin.setAge(32);
bilbo.setAge(frodo.getAge());
pippin.setAge(samwise.getAge());
frodo.setAge(pippin.getAge());
System.out.println("frod - Answers - frodo's age = 36
bilbo's age = 38
Phew!
Assume the following statement is added to the end of Test:
System.out.println(pippin.getAge() == merry.getAge());
What is the output of this statement?
public class Hobbit {
private int age;
private String name;
public Hobbit(String n) {
name = n;
}