In: Computer Science
Java questions. Please answer everything. It mean the world to me. Thank you very much
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) );
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 );
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: }
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: }
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: }
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: }
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.