In: Computer Science
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: }
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.