RHS   Allan   SW construction Exercises Test03


 

Test 03


Name : ______________________________________________________

 

When answering the following questions be sure to read all introductions - they may include important declarations or information needed to solve the questions.

You should be able to answer all 27 questions (in 3 groups) in less then 30 mins.

 

Group 1
Check whether the following loops are valid.
If they are valid you should also write down the output produced by the loop when it is executed.

Assume that the following declaration has been made:  int j = 0;

 
Loop
Valid
Not valid Output
 
 

for (int i = 0, i < 5, i++)
  System.out.print(i + ” ”);

 

     
 

for (i = 0; i < 5; i++)
  System.out.print(i + ” ”);

 

     
 

for (int i = 0; i <= 5; i++)
  System.out.print(i + ” ”);

 

     
 

for (int i = 0; i < 5; i++);
  System.out.print(i + ” ”);

 

     
 

for (int i = 0; i < 5; i = i + 2)
  System.out.print(i + ” ”);

 

     
 

while (j <= 5)
  System.out.print(j + ” ”);

 

     
 

while (j < 5){
  System.out.print(j + ” ”);
  j++;
}

 

     
 

while (j < 5)
  System.out.print( (j++) + ” ”);

 

     

 

 

 

 

Group 2
Assume that the following two classes are fully implemented:

   


Also assume the following declaration have been made: Person pers = new Person("Peter",  "HighStreet 12", "Roskilde");


Which of the following statements are valid?

 
Statement
Valid
Not valid
 

pers.toString();

 

     
 

Person.toString();

 

     
 

pers.getName();

 

     
 

String s = pers.getAddress();

 

     
 

String s = Person.getAddress();

 

     
 

Address a = pers.getAddress();

 

     
 

Address a = Person.getAddress();

 

     
 

String city = Person.getAddress.getCity();

 

     
 

String city = (Address) pers.getCity();

 

     
 

String city = pers.getAddress().getCity();

 

     
 

String city = pers.getAddress.getCity();

 

     

 

 

 

 

Group 3
Which of the following implementations of the
toString() method for the class Person would be valid?

 
toString()
Valid
Not valid
 
 

public String toString(){
  String s = name + "\n" + street + "\n" + city;
}

 

     
 

public String toString(){
  return name + "\n" + street + "\n" + city;
}

 

     
 

public String toString(){
  return "Name" + name + "\n" + address;
}

 

     
 

public String toString(){
  return "Name" + name + "\n" + super.toString();
}

 

     
 

public String toString(){
  return name + "\n" + address.getStreet() + address.getCity().;
}

 

     
 

public String toString(){
  return "Name" + name + "\n" + address.street + address.city;
}

 

     
 

public String toString(){
  return super.toString() + address;
}

 

     
 

public String toString(){
  return "Name" + name +
  "\nStreet" + address.getStreet() +
  "\nCity" + address.getCity();
}

 

     

 

 


Maintained by: Allan Helboe Nielsen
Updated: 3 November, 2005 1:04