181 - 210 Flashcards

1
Q

is null an object?

A

no. null is not an object

any method invocation on a null results in a NullPointerException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
output?
----------
public class TestClass {
    public static void main(String[] args){
    	TestClass arr[] = new TestClass[4];
    	System.out.println(arr[0]);
    }
}
A

null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
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]);
    }
}
A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

valid?

int i = ‘8’;

A

yes. It is valid

System.out.println(i); –> 56

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

while(int k = 5; k >0){} –> valid?

A

invalid

should be
int k = 5
while(k >0){}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. when is a class defined in a default package?

2. can a class from a default package be used in a named packaged class?

A
  1. when no package name is defined
  2. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

package statement:
(if present)

  1. where?
  2. how many in a class?
  3. can use the import statement to access multiple classes or interfaces with the same names from different packages?
A
  1. first statement in a class
  2. exactly once in a class
  3. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

difference between:

  • import
  • import static
A
  • import is for class

- import static is for static members of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

which give NullPointerException?

	String[] a = new String[2];
	a[0] = null;
	System.out.println(a[0]);
	a[0].length();
A

a[0].length() –> NullPointerException

System.out.println(a[0]) –> print null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

main method:

  1. must Java application have a main method?
  2. must public class?
A
  1. Every Java application must have a main method. It’s the starting point for the execution of the code in the application
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

//1 –> false

//2 –> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

is constructor inherited?

A

no. Never

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

default constructor of derived class calls default constructor of base class?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is checked exception?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what happens if a method uses another method that may throw a checked exception?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Error:

  1. what is?
  2. should be a part of a method signature?
  3. should be caught?
A
  1. is a serious exception thrown by the JVM as a result of an error in the environment state that processes your code
  2. need not be a part of a method signature
  3. can be caught by an exception handler, but it should not be
17
Q

are IOException and FileNotFoundException a RuntimeException?

which one is the subclass of the other?

A

no

FileNotFoundException is a subclass of IOException

18
Q

can a well written application recover from checked exception?

A

checked exception is exceptional conditions EXTERNAL to an application that a well written application should anticipate and from which it CAN RECOVER

19
Q

how to make a good encapsulation?

  • variables
  • functionality that is to be exposed outside
  • setter methods
A
  • data variable should be either private or protected
  • functionality that is to be exposed outside should be public
  • setter methods should be coded such that they don’t leave the data members inconsistent with each other
20
Q

double h = 9.0/5 - 1; System.out.println(“h = “ + h);

double k = 9/5 - 1; System.out.println(“k = “ + k);

A
h = 0.8
k = 0.0

lưu ý:

  • double –> muốn hiện kết quả số thập phân thì phải khai báo có .
  • float –> muốn hiện kết quả số thập phân thì phải khai báo có f ở suffix
21
Q

default value of primitive:

  • integer types
  • decimal types
  • boolean
  • char
A
  • integer types (byte, short, int, long) –> 0
  • decimal types (float and double) –> 0.0 –> not 0.0f nor 0.0d
  • boolean –> false
  • char –> /u0000 –> decimal value –> 0
22
Q

abstract method

  1. can be set static?
  2. ## what kind of access modifiers can be set to the method k?abstract class abc{
    abstract void k();
    }
A
  1. cannot be set to static

2. only public or protected

23
Q

can variables (instance, static, local, and method parameters) be defined as abstract?

A

no

none of the different types of variables (instance, static, local, and method parameters) can be defined as abstract

24
Q

works?

public int calculation(int z){return 0;}
public static void main(String[] args){
int h = calculation(10);
}

A

no. static method cannot use non-static method, variables,…

fix:

public static int calculation(int z){return 0;}

25
Q

works?

public static int calculation(int z){return 0;}
	public int recher(int m){
		return calculation(10);
	}
A

yes. non-static method can use static-method, variables but not vice-versa

26
Q
output
---------
class A{}
class B extends A{}
public class TestClass {
    public static void main(String[] args){
    	A a = new B();
    	System.out.println(a.getClass()); //1
    	B b = (B) new A();
    	System.out.println(b.getClass()); //2
    }
}
A
//1. class B
--> getClass() gets class of the actual object

//2. ClassCastException: A cannot be cast to B

27
Q
works?
-----
try{
    		int i = 0;
    		i++;
    	}
    	finally{
    	System.out.println(i);
    	}
A

no

28
Q

local variables:
1. specify visibility?

  1. initialized with their default values?
A
  1. cannot apply any modifier except final to a local variable –> no static, no public,…
  2. local variables are not initialized with their default values –> print before initializng = compilation error
29
Q

Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions?

A

yes

Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions

30
Q

only Throwable and its subclasses can be the argument type in a catch clause?

A

yes