Question

1. What does the following code print to the console? LocalDate date = LocalDate.of(1976, Month.JANUARY, 1);...

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(){...}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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));     } }

Output Jan 1, 1976

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); } } 

Output 118459271

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

  

  

Add a comment
Know the answer?
Add Answer to:
1. What does the following code print to the console? LocalDate date = LocalDate.of(1976, Month.JANUARY, 1);...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • List the output for the following lines of code (Hint: 4 lines of code should print):...

    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."); }...

  • Modify your program in Lab Assignment 4A to throw an exception if the file does not...

    Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result.   Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. My code: import java.io.*; import java.util.*; public class Lab4B { public static void main(String[] args) throws IOException { final String input_file = "Lab_4A.txt"; final String output_file = "Lab_4A.txt"; Scanner x = null; FileWriter y = null; try {...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    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...

    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...

    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...

  • how to call maze1 and maze2 object to be use in print method maze2.maze[i][j] maze2 is...

    how to call maze1 and maze2 object to be use in print method maze2.maze[i][j] maze2 is object maze[i][j] is variable from different class i just need help in calling object to be used in the print method and main will call print method to display. import java.io.*; public class MazeSolver {        //=========================================== // object reader //==========================================    public static void read()throws FileNotFoundException,IOException{              reader maze1 = new reader(new FileReader("maze1.txt"));        reader maze2 = new reader(new...

  • #1. Operators and values, expressions Please correct the following code to make it print the given...

    #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 = "...

  • This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying...

    This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and how can I fix this?? import java.util.Scanner; import java.io.*; public class Topic7Hw {    static Scanner sc = new Scanner (System.in);       public static void main(String[] args) throws IOException {               PrintWriter outputFile = new PrintWriter ("output.txt");               outputFile.println("hello");               final int MAX_NUM = 10;...

  • 8-7. Modify exercise 7-2 on comparing strings so it is now done in an applet instead...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT