Вы находитесь на странице: 1из 6

Week 12: sample questions

Review this subject:

Study the sample exam format.

Review the contents of this subject.

Work on some sample questions


In this week's class, students will be asked to work on some sample questions.
Use 15 minutes to do the follow (Part A & Part B) questions.

PART A: multiple-choice questions

1 Which of the following code fragments is a permissible main method declaration?

a. Public Static void main(String [ ] args)


b. public static void Main(String [ ] args)
c. public static void main(String [ ] args)
d. public static main(String [ ] args)

2. Which of the following methods can you use to compile and run a Java source file
“Hello.java” that contains a main() method ?

a. to compile use “javac Hello.java” then to execute use “java Hello”


b. to compile use “javac Hello” then to execute use “java Hello.java”
c. to compile use “javac Hello.java” then to execute use “java hello”
d. to compile use “javac Hello.java” then to execute use “java Hello.class”

3. The source file of a class in Java has “.class” extension.


a. True
b. False

1
4. Which of the following Java code fragments compile correctly?
a. String x = 2;
b. String s = "Hello World"
c. String myName; myname = "Harry";
d. String myName; myName = "George";

5. In Java, all statements are terminated with a _______


a. full stop – “.”
b. semi-colon – “;”
c. bracket – “(” or “)”
d. brace – “{” or “}”

PART B: Analyse Java Programs


(10 marks)
1. Consider the code below. Match and name each block of code: (numbers indicate line
numbers)

1 public class TestQuestion {


2 private String questionText;
3
4 public TestQuestion(String questionText) {
5 this.questionText = questionText;
6 }
7
8 public String toString() {
9 return questionText;
10 }
11 }

Match Question Items Answer Items

Line 1 a. Method

Line 2 b. Constructor

Lines 4-6 c. Class declaration

Lines 8-10 d. Field declaration

2
6. Consider the code below:

FILE: Message.java
public class Message {
String message;

public Message(String msg) {


message = msg;
}

public void setMessage(String msg) {


message = msg;
}

public String toString() {


return message;
}
}

FILE: Printer.java
public class Printer {
public static void main(String args[]) {
Message msg1 = new Message("A");
Message msg2 = new Message("B");

System.out.println(msg1);
System.out.println(msg2);

msg1 = msg2;
System.out.println(msg1);
System.out.println(msg2);

msg1.setMessage("C");
System.out.println(msg1);
System.out.println(msg2);

System.out.println();
}
}

What will the output after running the Printer application?

3
Use 30 minutes to do the 3 sample (part C) questions.
PART C: Write code
1. The class Department has the attributes deptName(String), numberOfPersons (int).

• Write code for a class that represents the Department with fields deptName and
numberOfPersons. The class must have a suitable constructor with parameters. The
class must also have a toString() method which formats the content of the object as
a printable String.

• Write a main( ) that creates one object of the Department class and calls the
toString() method to print this object.

2. The class Account is as follow:

class Account
{
private int accountNo; // account number
private String owner; // primary owner
private double balance; // account balance
protected String accountType; // "SINGLE"
public Account( int n, String ow, double b)
{
accountNo = n;
balance = b;
owner = ow;
accountType = "SINGLE";
}

public void deposit(double amt) // deposit amt into account


{
balance += amt;
}

public void withdraw(double amt) throws OutOfFundsException


{
double bal= 0;
bal = balance - amt;
if (bal < 0 )
throw new OutOfFundsException(“Insufficient balance for withdrawal”);

balance = bal;
}

public double getBalance() //responses to balance inquiry


{
return balance;
}

4
public int getAccountNumber()
{
return accountNo;
}

public String getOwnerName()


{
return owner;
}

public void setOwnerName(String ow)


{
owner = ow;
}

public String toString()


{
String s = "Account number= "+ accountNo +
" owner= " + owner + System.getProperty(“line.separator”);
s += " type= " + accountType + " balance= " + balance;
}

a. Write another class JointAccount that inherits from class Account. This new class
should have one instance variable - a name of the joint owner. The account number,
balance and account type are kept in the superclass, however account type ought to
be accessible from the derived class and have to be set to “JOINT”. You can change
account type to protected or hide the field to achieve this.

b. Write method toString() that overrides the method in the super class. It displays the
same information as the initial toString() method in the superclass, but with the
addition of the name of the co-owner.

5
3. The following program is to provide a user interface to calculate the interest. First, a
JLabel object is displayed with the message "Enter Balance: ", then the balance (e.g.
10000) should be entered to the balance field (via a JTextField). Second a JLabel
object is displayed with the message "Enter Interest Rate %: ", then the rate (e.g. 5.5)
should be entered to the rate field. Finally, the "Calculate Interest" button is clicked
and the result (e.g. "Interest earn: 550") is shown in the output field (via a JTextField).

- This is one of last week’s Lab exercises

You can find more questions from Lecture Notes and Lab exercises

Compile and run the three programs you written in Part C above

Вам также может понравиться