Java question, Please answer everything. Thank you
Answer the following questions as briefly (but completely) as possible:
1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 );
1: long value = Long.MAX_VALUE + 1; 2: System.out.println( value );
1: public class ShowErrors { 2: public static void main ( String [] args ) { 3: ShowErrors t = new ShowErrors( 5 ); 4: } 5: }
1: public class ShowErrors { 2: public static void main ( String [] args ) { 3: ShowErrors t = new ShowErrors(); 4: t.x(); 5: } 6: }
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: }
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: }
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.)
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: }
Expert Answer
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
Get Answers For Free
Most questions answered within 1 hours.