1.
What does the following code print to the console?
LocalDate date = LocalDate.of(1976, Month.JANUARY,
1);
DateTimeFormatter dtf =
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
System.out.println(dtf.format(date));
a. |
01/01/1976 |
|
b. |
Jan 1, 1976 |
|
c. |
1976-01-01 |
|
d. |
01/01/76 |
2.
What is the value of s2 after the code that follows is
executed?
String s1 = "118-45-9271";
String s2 = "";
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != '-') {
s2 +=
s1.charAt(i);
}
}
s2 = s2.replace('-', '.');
a. |
118.45.9271 |
|
b. |
118-45-9271 |
|
c. |
118 45 9271 |
|
d. |
118459271 |
3.
What is the minimum modification needed to make the code that
follows compile?
public void readData() {
boolean dataNotFound = true;
if (dataNotFound) {
throw new
IOException();
}
catch(IOException e) {
System.out.println("data
file can't be found");
}
}
a. |
Place the if statement within a try block. |
|
b. |
Add a throws clause to the method declaration. |
|
c. |
Set dataNotFound to false. |
|
d. |
No modification is needed |
4.
How many streams are used in the following statement?
DataInputStream in = new DataInputStream(
new
BufferedInputStream(
new
FileInputStream(file)));
a. |
3 |
|
b. |
2 |
|
c. |
1 |
|
d. |
4 |
5.
If the getBalance() method in the BankDB class throws an
IOException and the BalanceException class is derived from the
Exception class, which of the following is a valid method
declaration for a method that contains only this code?
balance = BankDB.getBalance(accountNumber);
if (balance < 0) {
throw new BalanceException("Negative
balance");
}
a. |
public void processTransaction() throws IOException{...} |
|
b. |
public void processTransaction() throws BalanceException{...} |
|
c. |
public void processTransaction() throws IOException, BalanceException{...} |
|
d. |
public void processTransaction(){...} |
1] Format style enum has four constants SHORT, MEDIUM, FULL, LONG. They are used to format date and time in different length. Predefined style for MEDIUM DateTimeFormatter is : month day, year.
b] jan 1, 1976
import java.util.*; import java.lang.*; import java.io.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; class DateFormat { public static void main (String[] args) throws java.lang.Exception { LocalDate date = LocalDate.of(1976, Month.JANUARY, 1); DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); System.out.println(dtf.format(date)); } }
2] s1 = "118-45-9271"
s1.length() = 11
Loop : 1st iteration
for i =0 if (s1.charAt(0) != '-') (where s1.charAt(0) = '1' ) therefore we will enter into if block and s2 = '1'
2nd iteration
for i =1 if( s1.charAt(1) != '-') (where s1.charAt(1) = '1' ) therefore we will enter into if block and s2 = '11'
3rd iteration
for i =2 if( s1.charAt(2) != '-') (where s1.charAt(2) = '8' ) therefore we will enter into if block and s2 = '118'
4th iteration
for i =3 if(s1.charAt(1) != '-') (where s1.charAt(1) = '-' ) therefore we will not enter into if block and s2 = '118'
and so on, finally we will have s2 = 118459271
Statement s2 = s2.char.At('-' ,'.') means replace every '-' with '.' in string s2. But it won't have any effect as s2 doesn't contain character '-' .
OUTPUT : d] 118459271
import java.util.*; import java.lang.*; import java.io.*; class Output { public static void main (String[] args) throws java.lang.Exception { String s1 = "118-45-9271"; String s2 = ""; for (int i = 0; i < s1.length(); i++) { if (s1.charAt(i) != '-') { s2 += s1.charAt(i); } } s2 = s2.replace('-', '.'); System.out.println(s2); } }
3] Option a) Place the if statement within a try block.
In the given code snippet we can clearly see a Catch block. Catch block must only be used after a try block. Multiple catch blocks can be used with a single try block.
Try block is used to enclose a part of code which might throw exception and the exception is catched by a catch block.
When an exception occurs JVM checks if the exception is handled. Try-Catch blocks are used to handle exception.
4] Answer should be 3
import java.io.BufferedInputStream
import java.io.DataInputStream
import java.io.FileInputStream
1. What does the following code print to the console? LocalDate date = LocalDate.of(1976, Month.JANUARY, 1);...
List the output for the following lines of code (Hint: 4 lines of code should print): public class MPL3 { public static void main(String[] args) { String s1 = "Java isn't just for breakfast."; String s2 = "JAVA isn't just for breakfast."; String s3 = "A cup of java is a joy forever."; if (s3.compareToIgnoreCase(s1) < 0) { System.out.println("\"" + s3 + "\""); System.out.println("precedes"); System.out.println("\"" + s1 + "\""); System.out.println("in alphabetic ordering"); } else System.out.println("s3 does not precede s1."); }...
Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...
Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() { super(); array = new Integer[0]; } Array(Array other) { super(); array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) { ...
Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...
#1. Operators and values, expressions Please correct the following code to make it print the given expressions and their answers well (3pts). public class DataTypes { public static void main(String[] args) { System.out.println("5*8/6 = " + 5*8/6); System.out.println("(2*6)+(4*4)+10 = " + (2*6)+(4*4)+10); System.out.println("6 / 2 + 7 / 3 = " + 6 / 2 + 7 / 3); System.out.println("6 * 7 % 4 = " + 6 * 7 % 4 ); System.out.println("22 + 4 * 2 = "...
8-7. Modify exercise 7-2 on comparing strings so it is now done in an applet instead of an application. The prompts will now be in labels and the input will now be textfields. The user will hit enter in the secondtextfield to get the results in two other answer labels. The results (which is greater and the difference amount) will be in these two other answer labels. Declare everything at the top, use the init to add the components to...
Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...
What will the code shown below print to the console? public class Cascade public static void print(int arg){ for(int i=0; i<arg; i++){ System.out.print(""); } System.out.println(""); if(arg > 1){ print(arg-1); } } public static void main(String[] args) { print(4); } } E B IV AA- IES XX, SE V GT 12pt Paragraph -
Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...
Analyze the code to determine what the code does, how the data is structured, and why it is the appropriate data structure (i.e. linked list, stack or queue). Note that these are examples of classes and methods and do not include the "main" or "test" code needed to execute. When you are done with your analysis, add a header describing the purpose of the program (include a description of each class and method), the results of your analysis, and add...