Question

Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...

Java question, Please answer everything. Thank you

Answer the following questions as briefly (but completely) as possible:

  1. What is a checked exception, and what is an unchecked exception?
  2. What is NullPointerException?
  3. Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output?
    1: System.out.println( 1 / 0 );
    2: System.out.println( 1.0 / 0 );
  4. Point out the problem in the following code. Does the code throw any exceptions?
    1: long value = Long.MAX_VALUE + 1;
    2: System.out.println( value );
  5. What are the differences between constructors and methods?
  6. What is wrong with each of the following programs?
    1. 1: public class ShowErrors {
      2:    public static void main ( String [] args ) {
      3:       ShowErrors t = new ShowErrors( 5 );
      4:    }
      5: }
    2. 1: public class ShowErrors {
      2:    public static void main ( String [] args ) {
      3:       ShowErrors t = new ShowErrors();
      4:       t.x();
      5:    }
      6: }
    3. 1: public class ShowErrors {
      2:    public void method1 () {
      3:       Circle c;
      4:       System.out.println( "What is radius "
      5:          + c.getRadius() );
      6:       c = new Circle();
      7:    }
      8: }
    4.  1: public class ShowErrors {
       2:    public static void main(String[] args) {
       3:       C c = new C(5.0);
       4:       System.out.println(c.value);
       5:    }
       6: }
       7: 
       8: class C {
       9:    int value = 2;
      10: }
  7. Which of the following statements are valid?
    1. int i = new int(30);
    2. double d[] = new double[30];
    3. char[] r = new char(1..30);
    4. int i[] = (3, 4, 3, 2);
    5. float f[] = {2.3, 4.5, 6.6};
    6. char[] c = new char();
  8. Given an array of doubles, write Java statements to do the following:
    1. Assign the value 5.5 to the last element in the array.
    2. Display the sum of the first two elements of the array.
    3. Write a loop that computes the sum of all elements in the array.
    4. Write a loop that finds the minimum element in the array.
    5. Randomly generate an index and display the element of this index in the array.
    6. Use an array initializer to create another array with the initial value 3.5, 5.5, 4.52, and 5.6.
  9. Use the following illustration as an example, show how to apply the binary search approach to a search first for key 10 and then key 12, in the list:
    [2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79].
    key is 11
             0   1   2   3   4   5   6   7   8   9  10  11  12
    11<50  [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
           low=0                   mid=6                   hi=12
    
    11>7   [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
           low=0    mid=2      hi=5
    
    11=11  [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
                       low=3    hi=5
                           mid=4

    (Note how binary search eliminates half of the list from further consideration after each comparison.)

  10. What types of array can be sorted using the Java.util.Arrays.sort method? Does this sort method create a new array?
  11. Which of the following statements are valid?
    1. int[][] r = new int[2];
    2. int[] x = new int[];
    3. int[][] y = new int [3][];
    4. int[][] z = {{1, 2}};
    5. int[][] m = {{1, 2}, {2, 3}};
    6. int[][] n = {{1, 2}, {2, 3}, };
  12. How do you do the following tasks?
    1. Create an ArrayList for storing double values?
    2. Append an object to a List?
    3. Insert an object at the beginning of a List?
    4. Find the number of objects in a List?
    5. Remove a given object from a List?
    6. Remove the last object from a List?
    7. Check whether a given object is in a List?
    8. Retrieve an object at a specified index from a List?
  13. Identify the errors in the following code fragment:
    1: ArrayList<String> list = new ArrayList<String>();
    2: list.add( "Denver" );
    3: list.add( "Austin" );
    4: list.add( new java.util.Date() );
    5: String city = list.get( 0 );
    6: list.set( 2, "Dallas" );
    7: System.out.println( list.get(2) );
  14. Explain why the following code fragment displays [1, 3] rather than [2, 3].
    1: ArrayList<Integer> list = new ArrayList<Integer>();
    2: list.add(1);
    3: list.add(2);
    4: list.add(3);
    5: list.remove(1);
    6: System.out.println( list );
  15. Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type. Then show the output of the following program:
     1: class Test {
     2:     public static void main ( String [] args ) {
     3:         Count myCount = new Count();
     4:         int times = 0;
     5:         for ( int i = 0; i < 100; i++ )
     6:             increment( myCount, times );
     7:         System.out.println( "count is " + myCount.count );
     8:         System.out.println( "times is " + times );
     9:     }
    10:     public static void increment ( Count c, int times ) {
    11:         c.count++;
    12:         times++;
    13:     }
    14: }
    15: 
    16: class Count {
    17:     public int count;
    18:     public Count ( int c ) {
    19:         count = c;
    20:     }
    21:     public Count () {
    22:         count = 1;
    23:     }
    24: }
  16. What is wrong in the following code?
    1: public class Test {
    2:    public static void main ( String [] args ) {
    3:       java.util.Date[] dates = new java.util.Date[10];
    4:       System.out.println( dates[0] );
    5:       System.out.println( dates[0].toString() );
    6:    }
    7: }
  17. If a class contains only private data fields and no “set” methods, is the class considered to be immutable?
  18. If a class contains only data fields that are both private and primitive, and no “set” methods, is the class considered to be immutable?
  19. What is wrong in the following code?
     1: public class C {
     2:     private int p;
     3: 
     4:     public C () {
     5:         System.out.println( "C's no-arg constructor invoked" );
     6:         this(0);
     7:     }
     8: 
     9:     public C ( int p ) {
    10:         p = p;
    11:     }
    12: 
    13:     public void setP ( int p ) {
    14:         p = p;
    15:     }
    16: }
  20. What is wrong in the following code?
    1: public class Test {
    2:     private int id;
    3:     public void m1 () {
    4:         this.id = 45;
    5:     }
    6:     public void m2 () {
    7:         Test.id = 45;
    8:     }
    9: }

Expert Answer

Homework Answers

Answer #1

Answer 1:
checked exceptions should be handled by the programmer otherwise
compiler will throw compilation error
unchecked exceptions are not detected by compilers and it will not give
any errors so we can compile without any error


Answer 2: NullPointerException is when an reference is not holding any object
than we tried accessing variables or methods from that object
than we will get NullPointerException
Student s;
s.getName();
in the above code s is not holding any object
but we are calling getName so here we will get NullPointerException


Answer 3: Line 1 will throw ArithmeticException as we are dividing
it by 0
Answer 4: Here we will get overflow
as we are crossing the range of double value so
we will get some junk number

As per policy we can answer 1 question per post. Please post the remianing questions as separate post.Thanks

Know the answer?
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
Similar Questions
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT