Question

In: Computer Science

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: }

Solutions

Expert Solution

Checked exceptions are checked at compile-time. It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.

Unchecked exceptions are not checked at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. Most of the times these exception occurs due to the bad data provided by user during the user-program interaction. It is up to the programmer to judge the conditions in advance, that can cause such exceptions and handle them appropriately. All Unchecked exceptions are direct sub classes of RuntimeException class.

Class NullPointerException

Thrown when an application attempts to use null in a case where an object is required. These include: ... Accessing or modifying the field of a null object. Taking the length of null as if it were an array. Accessing or modifying the slots of null as if it were an array.

System.out.println(1 / 0); // Throws an exception 
System.out.println(1.0 / 0); // Will not throw an exception

Adding 1 to Long.MAX_VALUE exceeds the maximum value allowed by a long value. But the current versions of Java does not report this as an exception.

Constructor is used to initialize an object whereas method is used to exhibits functionality of an object. Constructors are invoked implicitly whereas methods are invoked explicitly. Constructor does not return any value where the method may/may not return a value.

When you pass a primitive, you actually pass a copy of value of that variable. ... When you pass an object you don't pass a copy, you pass a copy of 'handle' of that object by which you can access it and can change it. This 'handle' is a 'reference'. In this changes will be reflected in original.


Related Solutions

Answer the following questions briefly. You do not have to repeat everything in the book. Just...
Answer the following questions briefly. You do not have to repeat everything in the book. Just highlight the key points and identify distinction in various approaches. a) How many levels of virtualization can one consider? Comment on their advantages, shortcoming, and limitation. b) What are the typical systems that you know of that have been implemented at each level in the past? c) What are the difference between full virtualization and paravirtualization? Explain at the advantages, and shortcomings, and limitation...
Please answer all the questions. Thank you Use the following data to answer the questions below:...
Please answer all the questions. Thank you Use the following data to answer the questions below: Column 1 Column 2 Column 3 Y in $ C in $ 0 500 500 850 1,000 1,200 1,500 1,550 2,000 1,900 2,500 2,250 3,000 2,600 What is mpc and mps? Compute mpc and mps. Assume investment equals $ 100, government spending equals $ 75, exports equal $ 50 and imports equal $ 35. Compute the aggregate expenditure in column 3. Draw a graph...
7. (Please, if you are not willing to answer the question completely, please leave the question...
7. (Please, if you are not willing to answer the question completely, please leave the question to someone who is!) a. At the time of his daughter's birth, a man deposited $ 1,000 in an account that pays 6%; this amount is set every birthday. When he turned 12, he increased his appropriations to $ 1,500. Calculate the amount that will be available to her at age 18. b. José earned $ 4,000,000 from the Puerto Rican lotus and will...
Please answer everything in R programming language. Show the code to me as well. Thank You...
Please answer everything in R programming language. Show the code to me as well. Thank You 1. Problem Open dataset stat500. Package: faraway. Use R (a) Calculate the correlation matrix. 10. (b) Plot total vs hw, to see how strong the relationship. (c) Build a simple linear regression: total regressed against midterm. Print model output. (d) Calculate directly the coefficients as in (3) (e) Calculate the Residual standard error s, as in (4). (f) Calculate the standard error of beta_1,...
please answer the following question on the bottom. thank you. 4. Why is the mix of...
please answer the following question on the bottom. thank you. 4. Why is the mix of output produced in competitive markets more desirable than that in monopolistically competitive markets? 5. Prior to 1982, AT&T kept local phone rates low by subsidizing them from long-distance profits. Was such cross-subsidization in the public interest? Explain. 6. Why is there resistance to (a) local phone companies providing video and data services and (b) mergers of local cable and telephone companies?
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1/...
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1/ What is one similarity and one difference between voluntary motor system that innervate the head versus voluntary motor system that innervate the body? 2/ Does olfactory bulb (direct) relay to primary sensory cortex via the thalamus? 3/ Write a short paragraph using the following terms: opiates; endorphins, pain relief. 4/ In your own words, explain one way in which neuroplasticity allows learning and memory...
Can you please answer all questions and please answer as soon as possible THANK YOU 1/...
Can you please answer all questions and please answer as soon as possible THANK YOU 1/ what do you think wernicke’s area of an infant develops prior to Broca’s? 2/ Create a short paragraph using the following terms: fovea, cones, rods, peripheral retina, acuity, center of the visual field? 3/ Why cone receptors are able to send information about different frequencies of light? 4/why do you think it is easier to name a taste in food than a smell in...
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1-...
CAN YOU PLEASE ANSWER AS SOON AS POSSIBLE AND PLEASE ANSWER ALL QUESTIONS THANK YOU 1- What is one similarity and one difference between voluntary motor system that innervate the head versus voluntary motor system that innervate the body? 2- Does olfactory bulb (direct) relay to primary sensory cortex via the thalamus? 3- Write a short paragraph using the following terms: opiates; endorphins, pain relief. 4- In your own words, explain one way in which neuroplasticity allows learning and memory...
Please answer all the following questions in a paragraph or two. Thank you! What is Netflix...
Please answer all the following questions in a paragraph or two. Thank you! What is Netflix and how has it changed? What are the sources of Netflix’s competitive advantage? Why or why not Netflix? (choose why you choose or choose why you do not choose netflix) If you do not use Netflix do you use a streaming service?
please answer question 3 and 4 based on the article below. thank you Questions What type...
please answer question 3 and 4 based on the article below. thank you Questions What type of international strategy would you say Inditex is following? Justify your answer. What is Zara’s business strategy? Explain. How is the international strategy of Inditex related to the business strategy of Zara? Explain. Why is it that no other fashion retailer can match Zara's performance? Inditex is a Spanish company started more than forty years ago, which owns Zara and other brands. The first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT