java_questions.
md 2025-09-08
Assessment Answers | Java Explorer Badge
1. You have an array of Clothing objects called items. The Clothing class has public elds for size and
price. You create a loop to calculate the total price of items. How would you add an if statement to
ensure an item is only added to the total if it's the same size as the customer c1? Assume the
Customer class has a public String eld size.
// 1
for (Clothing c: items) {
if([Link]([Link])){
//add to total
}
}
// 2
for (Clothing c: items) {
if (items[c].[Link]([Link])) {
//add to total
}
}
// 3
for (Clothing c: items) {
if([Link](c1)) {
//add to total
}
}
// 4
for (Clothing c: items) {
if(c == c1) {
//add to total
}
}
2. You've encapsulated the Customer class's size eld. The related setSize method requires an integer
argument. This argument is translated into a String, which is used to set the class's size String eld.
You instantiate a Customer c1 in the main method, which is written in another class. How should you
set the size of customer c1 from the main method?
// 1
[Link]('');
// 2
[Link] = 3;
// 3
[Link] = "S";
// 4
[Link](3);
/
java_questions.md 2025-09-08
3. Given: Clothing iteml = new Clothing ("Blue Jacket", 20.9, "M"); How would you declare the clothing
constructor?
// 1
public Clothing (String description, double price, String size) {...}
// 2
public Clothing (String description, double price) {...}
// 3
public Clothing (String description, String size, double price) {...}
// 4
public void Clothing (description, size){...}
// 5
public void Clothing (String description, double price, String size) {...}
4. A Customer class contains an encapsulated array eld of Clothing items. How would you write a
getItems method in the Customer class to return these items?
// 1
public void getItems (Clothing items) {
return items;
}
// 2
public void getItems() {
return items;
}
// 3
public Clothing [] getItems () {
return items;
}
// 4
public Clothing getItems() {
return items;
}
5. Given: Employee[] department = new Employee [10]; What exception occurs when you try adding an
eleventh employee to the department?
// 1
NullPointerException
// 2
Array IndexOutOfBoundsException
// 3
IOException
// 4
ArithmeticException
/
java_questions.md 2025-09-08
6. Given: public Customer (String name, int measurement) {...} How would you instantiate a Customer
object using this constructor?
// 1
Customer c1 = new Customer ("Pinky", 3, items);
// 2
Customer c1 = new Customer ("Pinky", "S");
// 3
Customer c1 = new Customer ("Pinky", "S", items);
// 4
Customer c1 = new Customer();
// 5
Customer c1 = new Customer ("Pinky", 3);
// 6
Customer c1 = new Customer ("Pinky");
7. You've overridden the tostring method in the Clothing class. From which class does this method
originate?
// 1
ShopApp
// 2
Comparable
// 3
Object
// 4
Customer
8. Which is a valid print statement?
// 1
[Link](Welcome to Duke's Choice Shop!)
// 2
[Link]("Welcome to Duke's Choice Shop!")
// 3
[Link] (Welcome to Duke's Choice Shop!).println;
// 4
[Link]("Welcome to Duke's Choice Shop!");
// 5
[Link]("Welcome to Duke's Choice Shop!");
9. How would you declare the MIN_PRICE eld of the Clothing class? The value of this eld should never
change. Accessing this eld should not require you to create a Clothing instance.
/
java_questions.md 2025-09-08
// 1
public static final double MIN PRICE = 10.0;
// 2
public double MIN_PRICE = 10.0;
// 3
public double static final MIN PRICE = 10.0;
// 4
public static double MIN PRICE = 10.0;
// 5
public double MIN PRICE = 10.0;
10. You have a Customer class with a public String eld name. What would you write in a main method to
create a Customer instance and set its name eld to Pinky?
// 1
Customer c1 = new Customer();
c1= "Pinky";
// 2
Customer cl;
[Link] = "Pinky";
// 3
Customer c1 = "Pinky";
// 4
Customer c1 = new Customer();
[Link] = "Pinky";
11. You've setup a Clothing class with a has a public double eld price. You've created two instances in
your program's main method where item1 is a jacket and item2 is a shirt. You've also created a double
for a tax rate in the main method. How would you accurately calculate the after-tax total for two shirts
and a jacket from the main method?
// 1
double total = ([Link]*2+ [Link])*(1+tax);
// 2
double total = (item2*2+ item1*(1+tax);
// 3
int total [Link]+[Link]+[Link] *tax;
// 4
double total = [Link]+[Link]+[Link] *tax;
// 5
int total = ([Link]*2+ [Link])*(1+tax);
12. According to this course, what library should you add to your project so that it can be deployed on the
Oracle Cloud?
/
java_questions.md 2025-09-08
// 1
Helidon
// 2
Helium
// 3
Mastodon
// 4
Mastodon
13. You have a Customer class with a public String eld size. In you program's main method, you create a
Customer instance c1. What is printed as a result of running this code from the main method?
int measurement = 3;
switch (measurement) {
case 3:
[Link] = "S";
case 6:
[Link] = "M";
case 9:
[Link] = "L";
break;
default:
[Link] = "X":
}
[Link]([Link]);
// 1
L
// 2
M
// 3
X
// 4
S
14. You have a Customer class with a public String eld size. In your program's main method, you create a
Customer instance c1. How could you consolidate this switch statement, which is used to set sizes
based on an int variable measurement?
switch (measurement) {
case 1:
[Link] "S";
break;
case 2:
[Link] = S";
/
java_questions.md 2025-09-08
break;
case 3:
[Link] = S";
break;
}
// 1
switch (measurement) {
case 1, case 2, case 3,
[Link] = "S";
break;
}
// 2
switch (measurement) {
case 1: case 2:
case 3:
[Link] = "S";
break;
}
// 3
switch (measurement) {
case 1 || case 2 || case 3:
[Link] = "S";
break;
}
15. How would you create a Customer class with a string eld name?
// 1
public class Customer{
String name;
}
// 2
public class Customer{
String = "name";
}
// 3
public class Customer{ }
name String;
// 4
public class Customer { String = name; }
16. You're encapsulating the Clothing class's price eld by writing a setPrice method. The price eld must
always be set above a certain minimum. How would you implement this method?
// 1
public void setPrice (double price) {
/
java_questions.md 2025-09-08
[Link] = price;
}
// 2
public void setPrice() {
if (price < MIN PRICE) {
price = MIN_PRICE;
}
else{
price = [Link];
}
}
// 3
public void setPrice (double price) {
if (price < MIN PRICE) {
[Link] = MIN PRICE;
}
[Link] = price:
}
// 4
public void setPrice (double price) {
if (price < MIN_PRICE) {
[Link] = MIN_PRICE:
}
else{
[Link] = price;
}
}
17. You write a method to calculate the average price of all Clothing items owned by a customer. This
method adds the price of all items, then divides this value by the number of items. If there are no
items, your method will attempt to divide by zero. What type of exception must you guard against in
this scenario?
// 1
NullPointerException
// 2
ArithmeticException
// 3
ArrayIndexOutOfBoundsException
// 4
IOException
18. When a class implements to Comparable interface, what method must also be implemented?
/
java_questions.md 2025-09-08
// 1
compare
// 2
toString
// 3
compareTo
// 4
equals
19. How would you loop through the Clothing array items to calculate the total price of all items after tax?
The Clothing class has a public double eld price. Assume the double variables total and tax are
already declared.
// 1
for (Clothing c: items) {
total = [Link]*(1+tax);
}
// 2
forEach (Clothing c in items) {
total += [Link]* (1+tax);
}
// 3
for (Clothing c: items[]) {
total total + [Link]*tax;
}
// 4
for (Clothing c: items) {
total = total + [Link]* (1+tax);
}
20. You've created a Clothing class along with two instances item1 and item2. How could you create a
Clothing array to store these two items?
// 1
Clothing [] items = (iteml, item2};
// 2
Clothing items (iteml, item2});
// 3
Clothing items = [iteml, item2];
// 4
Clothing[] items (iteml, item2);