RHS -> Allan->SWC -> Tests -> Test02

Test 02

Your name: _________________________________________________

  Question 1  
   

Assume that the following method is declared:

public int myMethod(int x, double y) { .... }

Which of the following return-statements are valid for the method (note: more than one answer can be correct)?

  1. return 7;
  2. return x + y;
  3. return void;
  4. return Math.abs(y);
  5. return x / y;
 
   


Write your answer here:

 

 

 

 
  Question 2  
   

Which of the following declarations are constructors (note: more than one answer can be correct):

  1. public MyClass() {}
  2. private MyClass() {}
  3. public void MyClass() {}
  4. public MyClass(int x) {}
 
   


Write your answer here:

 

 

 
  Question 3  
   

Below is a class, MobilePhone, to represent a mobile phone.

It holds two instance variables, phoneNumber (a String) and PINcode (an int).
For both variables there is an accessor (getter) and a mutator (setter) called getPhoneNumber(), getPINcode(), setPhoneNumber() and setPINcode().

Also there is two constructors: one with no parameters (setting some default values) and one that takes two parameters (phoneNumber and PINcode).

Both constructors and methods have some documentation.

public class MobilePhone {

  /** Creates a new instance of MobilPhone with default values
  */
  public MobilePhone() {
    this.phoneNumber = "No number set!";
    this.PINcode = 1234;
  }

  /** Creates a new instance of MobilPhone with values from parameters
  *   @param phoneNumber the number for the phone
  *   @param PINcode an integer to unlock the phone
  */
  public MobilePhone(String phoneNumber, int PINcode) {
    this.phoneNumber = phoneNumber;
    this.PINcode = PINcode;
  }

  /** Sets (overwrites) the value for the phoneNumber
  *   @param phoneNumber the new value for the phoneNumber
  */  
  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }

  /** Reads and returns the value for the phoneNumber
  */
  public String getPhoneNumber() {
    return phoneNumber;
  }

  /** Sets (overwrites) the value for the PINcode
  *   @param PINcode the new value for the PINcode
  */
  public void setPINcode(int PINcode) {
    this.PINcode = PINcode;
  }

  /** Reads and returns the value for the PINcode
  */
  public int getPINcode() {
    return PINcode;
  }

   //Instance variables
  private String phoneNumber;
  private int PINcode;
}

 

Create the two methods toString() and equals().

 
   


Write your answer here:

 

 

 

 

 

 

 

 

 

 

 

 

 
  Question 4  
   

Assume that the following lines are executed:

int age = 12;
int heigth = 140;

What will be printed on the screen after the following lines of code has been executed?

if (age > 12)
  System.out.println("Statement 01");
else
  if ((heigth > age) && (age != 13)) {
    System.out.println("Statement 02");
  }
  else
    System.out.println("Statement 03");
    System.out.println("Statement 04");

 
   


Write your answer here:

 

 

 

 

Maintained by: Allan Helboe
Updated: 3 December, 2007 20:55