Module 4: Final project and assessment: Programming with Java
Course quiz: Programming with Java
Graded Assignment
1. Complete the implementation of the area() method in the Circle subclass below:
- return radius * radius;
- return 2 * 3.14 * radius;
- return 3.14 * radius * radius;
- return radius;
2. You are tasked with developing a notification system for an application. Different types of notifications, such as email, SMS, and push must implement a Notification interface, as demonstrated by this design:
Which of the following statements about the given design is correct?
- The Notification interface should not have methods.
- The SMSNotification class should not implement the interface.
- The design correctly uses the Notification interface to handle different types of notifications.
- Each notification type should have a unique sendNotification() method without implementing an interface.
3. You are designing a class hierarchy for a car rental system. The system should represent different cars, and each car should have an engine. Which of the following code snippets best establishes a "has-a" relationship?
4. What is the primary role of the catch block in a try-catch structure?
- To declare exceptions.
- To ignore exceptions.
- To execute code regardless of an exception occurring.
- To catch and handle specific exceptions.
5. You are developing an application that processes customer orders. Sometimes, the orders contain invalid data (for example, negative quantities or missing customer information). Below is a code snippet for processing an order.
What approach should you take to handle potential issues with invalid data?
6. You are developing a software system for a construction company. You decide to use an abstract class Building that will be extended by House and Skyscraper. The Building class should include an abstract method calculateArea(). Which of the following design approaches is correct?
7. Complete the following code to properly read data from a text file using BufferedReader:
- System.out.println(line);
- continue;
- break;
- line = “No data”;
8. You are tasked with developing a system that logs server status updates to a text file. The system should efficiently append each update to the file on a new line without overwriting previous entries. Which of the following approaches correctly implements this requirement using BufferedWriter and FileWriter?
9. Which of the following statements about byte streams and/or character streams in Java is correct?
- Byte streams cannot be used to read text files.
- Character streams are used for binary data.
- Byte streams are used for binary data, while character streams are used for text-based data.
- Byte streams are used for text-based data.
10. The following code attempts to read a file, but it does not currently handle the potential FileNotFoundException. Complete the code snippet below to handle this exception properly:
11. Complete the code to create a new directory named "NewFolder" using the File class:
- dir.createDirectory();
- dir.makeDir();
- dir.createNewDirectory();
- dir.mkdir();
12. Which method from the File class is used to rename a file in Java?
- move()
- renameTo()
- renameFile()
- changeName()
13. Complete the following code to append data correctly to a file using FileWriter:
- writer.close();
- writer.append(“New data”);
- writer.flush();
- System.out.println(“Data appended successfully”);
14. Which File class method can be used to list all files and directories in a specific directory?
- readFiles()
- getFiles()
- listAll()
- listFiles()