Microsoft Word - midtermPastPaper-1 The University of Newcastle School of Electrical Engineering and Computing SENG1110/SENG6110 Object Oriented Programming Past Midterm Exam - 1 Instructions 1) You...

Microsoft Word - midtermPastPaper-1
The University of Newcastle
School of Electrical Engineering and Computing
SENG1110/SENG6110 Object Oriented Programming


Past Midterm Exam - 1


Instructions
1) You have 2 hours to finish the exam.
2) Marks for each question are outlined at each question. The total is 100.
3) You must return this examination question paper at the conclusion of the examination.


(20 points, 4 each) Part A - Multiple Choice
Identify the letter of the choice that best completes the statement or answers the question.

Consider the code:
int x = 5;
int y = 30;

do
x = x * 2;
while(x < y);

1. What is the final value of x in the code above?
a. 5 c. 20
b. 10 d. 40



2. If y = 0, how many times would the loop above execute?
a. 0 c. 2
b. 1 d. 3


Consider the code
if(5 > 4 +3)
System.out.println(“Hello”);
System.out.println(“World”);

3. What is the output of the code above?
a. Hello c. World
b. Hello World d. There is no output.


4. How can a method send a primitive value back to the caller?
a. It cannot send primitive values back to the caller
b. By using the return statement
c. By assigning the value to one of its parameters
d. It can call its caller with the value


5. Consider the method heading public String exampleMethod(int n, char ch), what is the return type
of the value returned?
a. int c. String
b. Char d. Nothing will be returned







Part B. Programming exercises (Marks are outlined at the beginning of each question)
Consider the following templates:
Using TIO:
public class NameOfYourClass{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
// your code
// to read you can use: console.nextInt(), console.next(); etc.
// to print you can use: System.out.print();
}
}
Using GUI:
public class NameOfYourClass {
public static void main (String[] args) {
// to read use: JOptionPane.showInputDialog("message")
// to write use:
//
OptionPane.showMessageDialog(null,str,"message",JOptionPane.INFORMATION_MESSAGE);
}
}

XXXXXXXXXXAn online music store charges:
a. $1.20 per song if the number of songs is less than 10.
b. $1.00 per song if the number of songs is more or equal to 10.
c. If the number of songs is more than 50, the client will receive an extra 10% discount.
Write a Java program that asks for the number of songs to buy and displays the total amount due. If the user
enters a negative number, the program should show a message and ask the input again.

XXXXXXXXXXWrite a program in Java that asks a value n and calculates the following expression. You program should
check if n is a positive and even number. If it is not, ask the user to input again.

2/3 + 4/5 + 6/7 + … + n/(n+1)

XXXXXXXXXXConsider a class called Fruit. This class will have the following instance variables: name (name, a
String), price (price, an double) and quantity available (qtd, an int). The methods will be setName (the
parameter will be a String that will be stored in name), getName (it will return the name), setPrice (the
parameters will be an double that will be stored in price), getPrice (it will return the price), setQtd (the
parameters will be an int that will be stored in qtd) and getQtd (it will return the quantity). All variables are
private and all methods are public.
a. Write the implementation of this class in Java.
b. Suppose now another class called Store (it is not necessary to implement it). Suppose the class Store
has the following line:

Fruit p = new Fruit ();

i. Is it correct the statement p.qtd=30? If it is not correct, write the correct statement to store 30
in the qtd variable. Justify your answer.
ii. Is it correct the statement System.out.println(p.getPrice())? If it is not correct, write the correct
statement to print the price from p. Justify your answer.

Prof Regina Berretta
May 13, 2021

Submit New Assignment

Copy and Paste Your Assignment Here