181 - 210 Flashcards
is null an object?
no. null is not an object
any method invocation on a null results in a NullPointerException
output? ---------- public class TestClass { public static void main(String[] args){ TestClass arr[] = new TestClass[4]; System.out.println(arr[0]); } }
null
output? -------- public class TestClass { public static void main(String[] args){ int arr[] = new int[4]; arr[1] = 1; arr[2] = 2; arr[3] = 3; System.out.println(arr[0]); } }
0
valid?
int i = ‘8’;
yes. It is valid
System.out.println(i); –> 56
while(int k = 5; k >0){} –> valid?
invalid
should be
int k = 5
while(k >0){}
- when is a class defined in a default package?
2. can a class from a default package be used in a named packaged class?
- when no package name is defined
- a class from a default package cannot be used in any named packaged class, regardless of whether they are defined within the same directory or not –> can never access a class that is defined in the default package from a class in any other package
package statement:
(if present)
- where?
- how many in a class?
- can use the import statement to access multiple classes or interfaces with the same names from different packages?
- first statement in a class
- exactly once in a class
- cannot
- but can duplicate import
–> example:
import java.util.name
import java.util.name - example of a wrong case:
import java.util.name
import java.util.xyz.name
difference between:
- import
- import static
- import is for class
- import static is for static members of a class
which give NullPointerException?
String[] a = new String[2]; a[0] = null; System.out.println(a[0]); a[0].length();
a[0].length() –> NullPointerException
System.out.println(a[0]) –> print null
main method:
- must Java application have a main method?
- must public class?
- Every Java application must have a main method. It’s the starting point for the execution of the code in the application
output:
String str1 = “java”;
char[] char1 = {‘j’, ‘a’, ‘v’, ‘a’};
String str2 = “”;
for(char i:char1){
str2 +=i;
}
System.out.println(str2);
System.out.println(str1 == str2); //1
System.out.println(str1.equals(str2)); //2
//1 –> false
//2 –> true
is constructor inherited?
no. Never
default constructor of derived class calls default constructor of base class?
unless you use super(…) a constructor calls the empty constructor of its parent. Note: It does this on all you classes, even the ones which extend Object
what is checked exception?
- a subclass of class java.lang.Exception, but it is not a subclass of java.lang.RuntimeException
o the class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions o Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions
- unacceptable condition FORSEEN by the author of a method at the time of writing this method but outside the immediate control of the code
- exceptional conditions external to an application that a well written application should anticipate and from which it can recover.
what happens if a method uses another method that may throw a checked exception?
if a method uses another method that may throw a checked exception, one of the two following things should be true
- the method should be enclosed within a try-catch block
- the method should specify this exception to be thrown in its method signature
–> only applied to checked exception