You are on page 1of 2

OBJ2000-1

1. Fill in the blank in each question. The blank's answer is a single word. (5 points)
a. In Java, the _______ operator is used to allocate memory for an object.
b. In Java, the _______ class is used to read input from the keyboard.
c. The _______ keyword is used to declare a variable whose value cannot be changed.
d. A _______ is a special type of method that is called automatically when an object is created.
e. The _______ keyword is used to prevent a class from being inherited.

2. Is the following sentence true or false? (5 points)


a. When creating a method in Java, it can return multiple values. (_____)
b. The super keyword in Java is used to call a method in the superclass. (_____)
c. A method can have multiple return statements. (_____)
d. A static method in Java cannot access non-static members of the class. (_____)
e. main () method does not have to return a value, but it can return an integer to indicate the exit status of
the program. (_____)

3. What is the default value of these primitive data types? (5 points)


Data type Default value
int _____
float _____
double _____
char _____
Boolean _____

4. Write a program that asks the user’s name, and then greets the user by name. Before outputting the
user’s name, convert it to upper case letters. For example, if the user’s name is Ingrid, then the program
should respond “Hello, INGRID, nice to meet you!” (5 points).

5. Suppose the string str is initialized by:


String str = "Norway is beautiful"
Compute the value of:
str.charAt(3);
str.substring(0,6);
str.substring(6);
str.isEmpty();
str.contains("B");

6. Write a method named ArrayAverage() that takes an array as a parameter and returns the average of the
array elements (the average of all elements of the array is the sum of all elements of the array divided by
the number of elements). (10 points)

7. What will be the output of this program? (10 points)


public class Test {
public static void main(String[] args) {
String name;
int i;
boolean startWord;
name = "USN Campus Vestfold";
startWord = true;
for (i = 0; i < name.length(); i++) {
if (startWord)
System.out.println(name.charAt(i));
if (name.charAt(i) == ' ')
startWord = true;
else
startWord = false;
}
}
}

8. Write a program that uses nested for loops to print the following pattern:
1
23
456
7 8 9 10

9. Write a Java program that prompts the user to input an integer number and determines if the number is a
perfect number or not. If the number is perfect, the program should display the message 'The number is
perfect'. If the number is not perfect, the program should display the message 'The number is not perfect'.
(15 points)

10. Create two classes named Employee and PartTimeEmployee. The Employee class should have the
following properties:
• firstName: a string representing the employee's first name.
• lastName: a string representing the employee's last name.
• empID: an integer representing the employee's unique identifier.
• salary: a double representing the employee's salary.
The Employee class should have a constructor that takes all data fields as parameters to initialize them.
Additionally, the Employee class should have a calculatePay() method to return the salary of the employee.
The PartTimeEmployee class should inherit from the Employee class and should have the following
additional properties:
• hourlyRate: a double representing the employee's hourly rate.
• hoursWorked: an integer representing the number of hours the employee has worked.
The PartTimeEmployee class should have a constructor that takes in values for all its data fields and should
validate the hourlyRate and hoursWorked data fields to ensure that they are greater than zero. Additionally,
the hoursWorked field should be validated to ensure that it is less than or equal to 40 (since this is the
maximum number of hours that a part-time employee can work per week).
The calculatePay () method in the PartTimeEmployee class should override the calculatePay () method in
the Employee class. The calculatePay () method should calculate the payment based on the hourlyRate and
hoursWorked properties.
Create a test class with a main method and create one object of PartTimeEmployee. Display the payment
using the calculatePay () method. (20 points)

You might also like