Question

In: Computer Science

Java questions. Please answer everything. It mean the world to me. Thank you very much Identify...

Java questions. Please answer everything. It mean the world to me. Thank you very much

  1. 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) );
  2. 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 );
  3. 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: }
  4. 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: }
  5. If a class contains only private data fields and no “set” methods, is the class considered to be immutable?
  6. If a class contains only data fields that are both private and primitive, and no “set” methods, is the class considered to be immutable?
  7. 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: }
  8. 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

Q.1 Identify the errors in the following code fragment:

Answer: ArrayList<String> list = new ArrayList<String>();

Q.2 Explain why the following code fragment displays [1, 3] rather than [2, 3].

Answer: It displays [1,3] becuase when we call list.remove(1) where, 1 is not value of ArrayList. 1 is ArrayList location.

Q.3 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:

 Answer:
Passing a parameter of a primitive type Passing a parameter of a reference type
When we pass primitive type argument as parameter to the method then a passing by value occurs. It is a copy of the argument is made. When we pass an object or used-defined type argument as parameter to method, then we can not pass copy, we pass acopy of handle of that object by which we can access it and can change it. And this handle is a reference. In this changes will be reflected in original.

The Output of Program:(Before execution, class Test should make public)

count is 101

times is 0

Q4. What is wrong in the following code?

Answer:

"java.util.Date[] dates = new java.util.Date[10]" is create date obejct but dates is null. It causes NULLPointerException .

Q5. If a class contains only private data fields and no "set" methods, is the class considered to be immutable?

Answer:

No. It must also contain get moethods that would return a reference to a mutable data field. Because Immutable class means we create object once, we cannot change its content. In Java, Example of immutable class is String.

Q6. If a class contains only data fields that are both private and primitive, and no “set” methods, is the class considered to be immutable?

Answer:

No. Because values is a reference type. Because Immutable class means we create object once, we cannot change its content. We can create our own immutable class as well.

Q7. What is wrong in the following code?

Answer:

In the defualt constructor, we call to "this(0);" as second statement in constructor but it must be first statement in constructor.

Q8. What is wrong in the following code?

Answer:

In public void m2() function, we try to access "id variable" directly by "class Test" that is not possible. Because non-static variable id cannot be referenced from a static context.


Related Solutions

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: What is a checked exception, and what is an unchecked exception? What is NullPointerException? 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 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
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 a statistic problem(please answer all subquestions). Thank you very much. NCbirths, cont' d. This...
Please answer a statistic problem(please answer all subquestions). Thank you very much. NCbirths, cont' d. This time we will use a subset of the dataset after removing premature birth (36 weeks or sooner) and continue to use MomRace as the explanatory variable to predict birth weight. To get NCbirths: install.packages("Stat2Data") library(Stat2Data) data("NCbirths") (a) Use the following R command to remove premature birth and create a dataset noPremie. noPremies<-subset(NCbirths, NCbirths$Premie==0) ##subset(dataName, condition) (b) Construct a side-by-side boxplot of birth weights separated...
Please type answer for i can copy it. Thank you very much. -Question Respond to the...
Please type answer for i can copy it. Thank you very much. -Question Respond to the following in a minimum of 175 words: Discuss the following:   Differences, advantages, and disadvantages of Java® compared to another language of your choice (e.g., C++, Python, Ruby) Database connectivity and file handling in Java® and in your other chosen language  
Please Read This Article and answer the questions that follow: Thank You so much for taking...
Please Read This Article and answer the questions that follow: Thank You so much for taking the time to do this I will rate well!! In many cities, finding an available parking spot on the street seems about as likely as winning the lottery. But iflocal governments relied more on the price system, they might be able to achieve a more efficient allocation of this scarce resource. A Meter So Expensive, It Creates Parking Spots By Michael Cooper and Jo...
Please type the answer for i can copy it. Thank you very much. -Question 1 Respond...
Please type the answer for i can copy it. Thank you very much. -Question 1 Respond to the following in a minimum of 175 words: Read the following pseudocode class definitions: Class Plant Public Module message() Display "I'm a plant." End Module End Class Class Tree Extends Plant Public Module message() Display "I'm a tree." End Module End Class -Question 2 Given these class definitions, determine what the following pseudocode will display: Declare Plant p Set p = New Tree()...
Please type answer for i can copy it. Thank you very much. -Question 1 What is...
Please type answer for i can copy it. Thank you very much. -Question 1 What is a polymorphism ? -Question 2 Can you start an algorithm in pseudo code? language is in (OOP) object oriented programming
*Please answer questions to case study ASAP. Thank you so much! When you think of the...
*Please answer questions to case study ASAP. Thank you so much! When you think of the most sustainable corporations in the world, Bell Canada may not jump to mind. Nonetheless, Corporate Knights listed Bell as one of only eight Canadian companies to make the Global 100 list of sustainable compa- nies for 2011. Bell Canada Enterprises (BCE) was also named byMaclean’s/Jantzi-Sustainalytics as one of Canada’s Top 50 Socially Responsible Corporations. It also made the prestigious FTSE4Good Global Index. It is...
PLEASE ANSWER ALL 4 THANK YOU VERY MUCH!!!! 1. How was the Periodic Table originally organized?...
PLEASE ANSWER ALL 4 THANK YOU VERY MUCH!!!! 1. How was the Periodic Table originally organized? Explain the current organization of the Periodic Table and why it is a more accurate layout of the elements. 2. What is the difference between groups and periods? What are the common names for groups IA, IIA, VIIA, VIIIA? 3. Why were elements in group 18, VIIIA, formerly known as inert gases? How is the current name of “noble gases” more appropriate? Provide examples...
Audit and Investigation- Please assist me in answering this in essayform please. Thank you so much...
Audit and Investigation- Please assist me in answering this in essayform please. Thank you so much As part of the planning an audit and designing an audit approach one major part of the planning is to understand the client’s business and industry. Select a specific business example and provide a bullet point analysis of what would be of interest in the understanding of that client business and industry and its impact on the audit
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT