211 - 240 Flashcards

(85 cards)

1
Q

output:

	int a = 30 - 9/5 +1; System.out.println("a = " + a);
	double b = 30 - 9.0/5 + 1; System.out.println("b = " + b);    	
	int c = 30 - 9/5; System.out.println("c = " + c);
	double d = 30 - 9.0/5; System.out.println("d = " + d);
	int e = 1 + 9/5; System.out.println("e = " + e);
	double f = 1 + 9.0/5; System.out.println("f = " + f);
	int g = 9/5 - 1; System.out.println("g = " + g);
	double h = 9.0/5 - 1; System.out.println("h = " + h);
A
a = 30
b = 29.2
c = 29
d = 28.2
e = 2
f = 2.8
g = 0
h = 0.8

Lưu ý:

  • Phép trừ trước phân số
  • double –> số phải có .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. what is reachable statement in Java?
  2. ## which one is unreachable?if(false) System.out.println(“a”); //1
    while(false) System.out.println(“b”); //2
    int n = 5;
    while(n > 7) System.out.println(“c”); //3
A
  1. reachable: there must be some possible execution path from the beginning of the constructor, method, or static initializer that contains the statement to the statement it self
    - -> if unreachable –> compile-time error
  2. while(false) System.out.println(“b”); //2 –> unreachable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ArrayList

  1. final class?
  2. what is required to use ArrayList?
  3. resizable?
A
  1. no. It is not a final class –> can be extended
  2. import java.util.ArrayList;
  3. yes. Resizable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

array = reference variables?

A

yes

arrays themselves are reference variables, which refer to a collection of objects of similar type

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

Array - ArrayList

  1. size
  2. specify size
  3. insert primitive type
  4. generics
  5. object-oriented
A
  1. size
    - Array = fixed size
    - ArrayList = can grow or shrink its size dynamically. ArrayList is backed by an array
  2. specify size
    - creating Array –> need to specify its size or insert elements into it to make sure size is specified
    - ArrayList –> specifying size = optional. Initial size is taken care by ArrayList constructor. Initial size is 10
  3. insert primitive type
    - Arrays allow to insert primitive type of data like int, float, double, byte, short, long.
    - ArrayList does not allow this –> autobox to wrapper type i.e Integer
  4. Generics
    - Array –> cannot
    - ArrayList –> can
  5. ArrayList = object-oriented –> can extend from it and add functionality if needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
output
---
    	int a[][] = new int[2][2];
    	a[0][0] = 0;
    	a[0][1] = 1;
    	a[1][0] = 2;
    	a[1][1] = 3;
    	System.out.println("a = " + a[0][0] + " " + a[0][1] + " " + a[1][0] + " " + a[1][1]);
    	///////
    	int b[][] = {{0, 1},{2, 3}};
    	System.out.println("b = " + b[0][0] + " " + b[0][1] + " " + b[1][0] + " " + b[1][1]);
    	///////
    	int c[][] =  new int[][]{{0, 1},{2, 3}}; 
    	System.out.println("c = " + c[0][0] + " " + c[0][1] + " " + c[1][0] + " " + c[1][1]);
    	///////
    	int d[][] = new int[2][];
    	d[0] = new int[]{0, 1};
    	d[1] = new int[]{2, 3};
    	System.out.println("d = " + d[0][0] + " " + d[0][1] + " " + d[1][0] + " " + d[1][1]);
    	int e[][] = new int[2][];
    	int f[] = {0, 1};
    	int g[] = {2, 3};
    	e[0] = f;
    	e[1] = g;
    	System.out.println("e = " + e[0][0] + " " + e[0][1] + " " + e[1][0] + " " + e[1][1]);
A
a = 0 1 2 3
b = 0 1 2 3
c = 0 1 2 3
d = 0 1 2 3
e = 0 1 2 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

which one is correct declaration?

int i[][] = new int[][]{{0, 1},{2, 3}};
int j[][] = new int{{0, 1},{2, 3}};
int k[][] = new [][]{{0, 1},{2, 3}};
int h[][] = {{0, 1},{2, 3}};
int l[][] = new {{0, 1},{2, 3}};
int m[][] = new int[2][]{{0, 1},{2, 3}};

A

2 correct declaration:

int i[][] = new int[][]{{0, 1},{2, 3}};

int h[][] = {{0, 1},{2, 3}};

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

IOException

  1. checked or unchecked exception?
  2. what is?
  3. FileNotFoundException –> baseclass of IOException?
A
  1. checked exception
  2. throw the IOException whenever an input or output operation is failed or interpreted
  3. no. FileNotFoundException is a subclass of IOException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
which one is correct?
-----
class A{}
class B extends A{}
B a = (B) new B();
B b = (A) new B();
B c = (A) new A();
B e = new B();
B f = (B) new A();
A
- correct:
o B a = (B) new B();
o B e = new B();
o B f = (B) new A();
- wrong: 
o B b = (A) new B();
o B c = (A) new A();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ArrayList –> method:

  1. to add & add all?
  2. remove all the ArrayList elements –> = set null?
  3. remove an element
A
  1. add(pos, Collection) - addAll(pos, Collection)
  2. clear() + # set null
  3. remove(pos) or remove(Object)
    remove(Object) –> removes the first occurrence of the specified element if it’s present
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

method insert() –> ArrayList or StringBuilder?

A

StringBuilder

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

method delete() –> ArrayList or StringBuilder?

A

StringBuilder

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

method remove() –> ArrayList or StringBuilder?

A

ArrayList

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

arraycopy method signature

A

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

o src – This is the source array.

o srcPos – This is the starting position in the source array.

o dest – This is the destination array.

o destPos – This is the starting position in the destination data.

o length – This is the number of array elements to be copied.

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

passing primitive values to method

A

caller’s copy of primitive type arguments (int, char, etc.) DO NOT change when the corresponding parameter is changed

When you pass primitives to methods it is a straightforward pass by value. A method gets its own copy to play with and any modifications are not reflected outside the method

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

passing objects reference to method

A

the fields of the caller’s object DO change when the called method changes the corresponding fields of the object (reference) passed as a parameter

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

overridden method does not have any throw clause –> what cannot the overriding method do?

A

if overridden method does not have any throw clause, the overriding method cannot declare any CHECKED exceptions

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

exception in overriding method (2)

A
  • can choose NOT to throw any exception
  • can only throw
    o exceptions that are declared in the throws clause of the overridden method
    o exceptions that are SUBclasses of the declared exceptions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

if a method uses another method that may throw a checked exception –> what should be done?

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

9 examples of unchecked exception

A

unchecked exception = runtime exception

- IndexOutOfBoundsException
o ArrayIndexOutOfBoundsException
o StringIndexOutOfBoundsException
- IndexOutOfBoundsException
- ClassCastException
- IllegalArgumentException
- IllegalStateException
- NullPointerException
- NumberFormatException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

String a = “aaa”;
System.out.println(a.replace(‘a’, ‘b’)); //1
System.out.println(a.replace(‘k’, ‘b’)); //2

A
//1 --> bbb
//2 --> aaa --> nothing changes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

when does replace() method create a new String object?

A
  1. replace() method creates a new String object.
  2. replace() method returns the same String object if both the parameters are same, i.e. if there is no change.
    - -> “String”.replace(‘g’,’g’)==”String”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

when does concat() method create a new String object?

A

If the length of the argument string is 0, then this String object is returned. Otherwise, a NEW String object is created

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

String a = “aaa”;
System.out.println(a.concat(“m”)); //1
System.out.println(a.concat(‘m’)); //2

A
//1 --> aaam
//2 --> char is not accepted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
byte range?
-128 --> 127
26
List students = new ArrayList(); - reference type? - object type?
- reference type is List | - object type is ArrayList
27
for (initialization; termination; increment) { statement(s) } ======= termination = blank --> meaning?
true
28
switch(5){ case 6: System.out.println(5); continue; } ========== what is the problem?
continue cannot be used outside of a loop --> cannot be used with switch
29
while (expression) { } 1. how to make an infinite loop? 2. expression = blank --> can be?
1. while (true) | 2. no
30
what do ArrayList and StringBuilder extend?
- StringBuilder extends Object | - ArrayList extends AbstractList
31
final or not final class? 1. String, StringBuilder, and StringBuffer 2. wrapper classes (java.lang.Boolean, java.lang.Integer, java.lang.Long, java.lang.Short) 3. Array 4. ArrayList
1. String, StringBuilder, and StringBuffer --> final 2. wrapper classes (java.lang.Boolean, java.lang.Integer, java.lang.Long, java.lang.Short) --> final 3. Array --> final 4. ArrayList --> not final
32
use 'this' within non-static method?
keyword 'this' can only be used within non-static methods reason: static methods cannot access non static fields or methods
33
SubClass().methodX(a) or SubClass.methodX(a)
SubClass().methodX(a) lưu ý ()
34
``` class A{ A() { print(); } void print() { System.out.println("A"); } } class TestClass extends A{ int i = 4; public static void main(String[] args){ A a = new TestClass(); a.print(); } void print() { System.out.println(i); } } ```
``` 0 4 ----- 1-Q40 ----- A() { print(); } --> constructor khởi chạy khi tạo instance ở dòng A a = new TestClass(); ``` --> nếu xoá A() { print(); } thì kết quả chỉ còn là 4
35
``` class A{ public void mA(){ }; } ``` ``` class B extends A { public void mA(){ } public void mB() { } } ``` ``` class C extends B { public void mC(){ } } and the following declarations: A x = new B(); B y = new B(); B z = new C(); ``` ``` -------- which one is correct? 1. x.mA(); 2. x.mB(); 3. y.mA(); 4. z.mC(); 5. z.mB(); ```
1. x.mA(); 3. y.mA(); 5. z.mB();
36
interface Bozo{ int type = 0; public void jump(); } ``` public class TestClass implements Bozo{ public TestClass(){ type = 1; } public void jump(){ System.out.println("jumping..."+type); } public static void main(String[] args){ Bozo b = new TestClass(); b.jump(); } } ```
output: not compile Fields defined in an interface are ALWAYS considered as public, static, and final. (Methods are public and abstract.) Even if you don't explicitly define them as such. In fact, you cannot even declare a field to be private or protected in an interface. Therefore, you cannot assign any value to 'type' outside the interface definition.
37
interface: - fields - method
- Fields defined in an interface are ALWAYS considered as public, static, and final - Methods are public and abstract
38
int ia[][][] = new int[4][3][2]; System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
4, 3, 2
39
class SubClass extends BaseClass in BaseClass, (1 or more) abstract method is declared --> what should SubClass be?
SubClass should either: 1. be declared as an abstract class 2. implement all abstract methods
40
java file without public class --> work?
yes. It works
41
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
42
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
43
1. static method override non-static method? | 2. non-static method override static method?
both 1 and 2 lead to compile-error
44
can Error be caught by catch?
no
45
byte b = 1; char c = 1; short s = 1; int i = 1; ==== which of the following expressions are valid? 1. s = b * b ; 2. i = b + b ; 3. s *= b ; 4. c = c + b ; 5. s += i ;
A. Correct: 2. i = b + b ; --> cast to at least int 3. s *= b ; --> compound = internally do explicit cast (to left) 5. s += i ; --> compound = internally do explicit cast (to left) B. Incorrect: 1. s = b * b ; --> cast to at least int 4. c = c + b ; --> cast to at least int
46
which one belongs to String, which to StringBuilder? - concat - append
concat --> String | append --> StringBuilder
47
substring() - with and without ending position - lưu tâm với with ending position (2)
ending position = String.length() + 1 is acceptable not include the character at the end position in its return value
48
indexOf() - with and without starting position - what does it return? (2) - "" "" or ' ' ?
search for the occurrence of a char or a String: o first match o -1 if no match both " " and ' ' are accepted
49
charAt() - return? - accept long, byte, short, float?
only int and smaller than int
50
"blooperper".delete(3, 5)
bloerper ending position not included
51
``` class MyException extends Exception {} public class TestClass{ public static void main(String[] args){ TestClass tc = new TestClass(); try{ tc.m1(); } catch (MyException e){ tc.m1(); } finally{ tc.m2(); } } public void m1() throws MyException{ throw new MyException(); } public void m2() throws RuntimeException{ throw new NullPointerException(); } } ```
It will not compile because of unhandled exception. The catch block is throwing a checked exception (i.e. non-RuntimeException) which must be handled by either a try catch block or declared in the throws clause of the enclosing method. Note that finally is also throwing an exception here, but it is a RuntimeException so there is no need to handle it or declare it in the throws clause.
52
method A calls method B which throws Runtime Exception --> does A need to throw an exception?
A does NOT have to throw an exception
53
method A calls method B which throws checked Exception --> does A need to throw an exception?
class of A MUST throw an exception or wrap it in a try-catch block
54
example of checked exception
class MyException extends Exception{}
55
what must constructor do with the checked exception declared in the base constructor?
Constructor must declare all the checked exceptions declared in the base constructor (or the super classes of the checked exceptions). They may add other exception
56
how to write a value in binary?
If you want to write a value in binary, it must be prefixed with 0b or 0B example: int b = 0b01001110_00100000;
57
``` class Test{      static boolean a;      static boolean b;      static boolean c;     public static void main (String[] args){          boolean bool = (a = true) || (b = true) && (c = true);         System.out.print(a + ", " + b + ", " + c);     } } ```
true, false, false
58
interface method 1. public? 2. final? 3. static?
1. All interface methods have to be public 2. cannot be final 3. cannot be static
59
String a = "0123"; System.out.println(a.charAt(3)); //1 System.out.println(a.charAt(4)); //2
``` //1 - 3 //2 - StringIndexOutOfBoundsException ```
60
static method overrides non-static method?
cannot only: - static method overrides static method - non-static method overrides non-static method
61
overridden method --> which is related to actual class of object? Which to reference variable?
use overriden method - instance / non-static methods --> method of actual class of the object - static and instance fields and static methods --> class of reference variable
62
valid? ==== interface A{} interface B{} interface C extends A, B{}
yes An interface can extend any number of other interfaces and can be extended by any number of other interfaces (but class cannot extend multiple classes)
63
can static method be abstract?
static methods cannot be abstract
64
field declaration in the body of an interface is implicitly?
Every field declaration in the body of an interface is implicitly public, static and final (method is not included)
65
What will be the result of compiling and running the following program ? ``` class NewException extends Exception {} class AnotherException extends Exception {} public class ExceptionTest{ public static void main(String [] args) throws Exception{ try{ m2(); } finally{ m3(); } } public static void m2() throws NewException{ throw new NewException(); } public static void m3() throws AnotherException{ throw new AnotherException(); } } ```
It will compile but will throw AnotherException when run.
66
System.out.println(true); //1 | System.out.println(null); //2
System.out.println(true); //1 --> true System.out.println(null); //2 --> compilation error
67
which kind of variable remains uninitialized unless it is explicitly initialized?
local variable
68
SecurityException is a Runtime Exception?
yes
69
A programmer should never throw an Error explicitly --> right?
yes A programmer should never throw an Error explicitly. but he can
70
Wrapper is subclass of Object?
yes wrapper Class = subclass of Object
71
``` Given: public class Square { private double side = 0; // LINE 2 ``` ``` public static void main(String[] args) { // LINE 4 Square sq = new Square(); // LINE 5 side = 10; // LINE 6 } } What can be done to make this code compile and run? ```
Có các phương án: 1. Line 2 --> static 2. replace // LINE 6 with: sq.side = 10; không cần static ở line 2 và vẫn access được
72
what is wrong? for (int i=5; i=0; i--) { }
uses '=' instead of '==' for condition which is invalid. The loop condition must be of type boolean.
73
what is wrong? int j=5; for(int i=0, j+=5; i
uses 'j +=5'. Now, this statement is preceded by 'int i=0,' and that means we are trying to declare variable j. But it is already declared before the for loop. If we remove the int in the initialization part and declare i before the loop then it will work. But if we remove the statement int j = 5; it will not work because compiler will try to do j = j+5 and you can't use the variable before it is initialized. Although the compiler gives a message 'Invalid declaration' for j += 5, it really means the above mentioned thing.
74
``` What will the following code print? void crazyLoop(){ int c = 0; JACK: while (c 3) break JILL; else c++; } } ```
not compile Because break JILL; would be valid only when it is within the block of code under the scope of the label JILL. In this case, the scope of JILL extends only up till System.out.println(c); and break JILL; is out of the scope of the label.
75
switch(8); what is wrong?
invalid because a switch statement must have a body. The body may even be empty as shown int x = 0; switch(x){ }
76
int ia[][] = { {1, 2}, null }; System.out.println(ia[1][0]);//1 String[]k = {null}; System.out.println(k[0]); //2 int[] m = {1, null}; //a System.out.println(m[1]); //3
//1 --> NullPointerException vì null array //2 --> compile + print null --> giá trị 1 phần tử trong array là null //3 --> cannot compile --> int không thể khai báo là null
77
what is wrong? ``` interface A extends C{} interface B{} class C{} ```
interface cannot extends a class
78
Given that OurClass is a MyClass and OurClass has a YourClass object. Which of the following options are correct? is it true: MyClass contains a reference to OurClass?
No. OurClass contains a reference to YourClass
79
double x = 5, double y; correct?
no double x = 5; double y;
80
``` public class TestClass{ int m = 5; public static void main(String[] args){ TestClass i = new TestClass(); TestClass j = new TestClass(); i.m = 5; j.m = 6; System.out.println(i.m + " - " + j.m); } } ``` === nếu đổi m thành static thì print gì?
5 - 6 static m --> 6 - 6
81
if (x = 3) {} while (x = 5) {} //assume that x is an int what is wrong?
if(...) and while(...) needs a boolean
82
public void method1(){ int i; this.i = 4; } what is wrong?
i = local variable --> cannot this.i
83
what is wrong? ``` public class TestClass{ int m = 5; public static void main(String[] args){ TestClass.m = 5; } } ```
m must be static if want to use ClassName.variable
84
what is wrong? ``` public class TestClass{ int m = 5; public static void main(String[] args){ this.m = 5; } } ```
cannot use this. in a static context
85
different in use of this.variable and ClassName.variable
- this.variable --> use with both static and non-static but cannot be used in a static context - ClassName.variable --> only use with static but can be used in both static and non-static context