211 - 240 Flashcards

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
Q

byte range?

A

-128 –> 127

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

List students = new ArrayList();

  • reference type?
  • object type?
A
  • reference type is List

- object type is ArrayList

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

for (initialization; termination; increment) {
statement(s)
}

=======
termination = blank –> meaning?

A

true

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

switch(5){
case 6: System.out.println(5); continue;
}

==========
what is the problem?

A

continue cannot be used outside of a loop –> cannot be used with switch

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

while (expression) { }

  1. how to make an infinite loop?
  2. expression = blank –> can be?
A
  1. while (true)

2. no

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

what do ArrayList and StringBuilder extend?

A
  • StringBuilder extends Object

- ArrayList extends AbstractList

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

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

use ‘this’ within non-static method?

A

keyword ‘this’ can only be used within non-static methods

reason: static methods cannot access non static fields or methods

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

SubClass().methodX(a)

or

SubClass.methodX(a)

A

SubClass().methodX(a)

lưu ý ()

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q
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();
A
  1. x.mA();
  2. y.mA();
  3. z.mB();
36
Q

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(); } 
	}
A

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
Q

interface:

  • fields
  • method
A
  • Fields defined in an interface are ALWAYS considered as public, static, and final
  • Methods are public and abstract
38
Q

int ia[][][] = new int[4][3][2];

System.out.println( ia.length + “, “ + ia[0].length+”, “+ ia[0][0].length);

A

4, 3, 2

39
Q

class SubClass extends BaseClass

in BaseClass, (1 or more) abstract method is declared –> what should SubClass be?

A

SubClass should either:

  1. be declared as an abstract class
  2. implement all abstract methods
40
Q

java file without public class –> work?

A

yes. It works

41
Q

int indexOf(int ch, int fromIndex)

A

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

42
Q

int indexOf(int ch)

A

Returns the index within this string of the first occurrence of the specified character.

43
Q
  1. static method override non-static method?

2. non-static method override static method?

A

both 1 and 2 lead to compile-error

44
Q

can Error be caught by catch?

A

no

45
Q

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

A. Correct:

  1. i = b + b ; –> cast to at least int
  2. s *= b ; –> compound = internally do explicit cast (to left)
  3. s += i ; –> compound = internally do explicit cast (to left)

B. Incorrect:

  1. s = b * b ; –> cast to at least int
  2. c = c + b ; –> cast to at least int
46
Q

which one belongs to String, which to StringBuilder?

  • concat
  • append
A

concat –> String

append –> StringBuilder

47
Q

substring()

  • with and without ending position
  • lưu tâm với with ending position (2)
A

ending position = String.length() + 1 is acceptable

not include the character at the end position in its return value

48
Q

indexOf()

  • with and without starting position
  • what does it return? (2)
  • ”” “” or ‘ ‘ ?
A

search for the occurrence of a char or a String:
o first match
o -1 if no match

both “ “ and ‘ ‘ are accepted

49
Q

charAt()

  • return?
  • accept long, byte, short, float?
A

only int and smaller than int

50
Q

“blooperper”.delete(3, 5)

A

bloerper

ending position not included

51
Q
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();
   }
}
A

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
Q

method A calls method B which throws Runtime Exception –> does A need to throw an exception?

A

A does NOT have to throw an exception

53
Q

method A calls method B which throws checked Exception –> does A need to throw an exception?

A

class of A MUST throw an exception or wrap it in a try-catch block

54
Q

example of checked exception

A

class MyException extends Exception{}

55
Q

what must constructor do with the checked exception declared in the base constructor?

A

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
Q

how to write a value in binary?

A

If you want to write a value in binary, it must be prefixed with 0b or 0B

example: int b = 0b01001110_00100000;

57
Q
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); } }
A

true, false, false

58
Q

interface method

  1. public?
  2. final?
  3. static?
A
  1. All interface methods have to be public
  2. cannot be final
  3. cannot be static
59
Q

String a = “0123”;
System.out.println(a.charAt(3)); //1
System.out.println(a.charAt(4)); //2

A
//1 - 3
//2 - StringIndexOutOfBoundsException
60
Q

static method overrides non-static method?

A

cannot

only:

  • static method overrides static method
  • non-static method overrides non-static method
61
Q

overridden method –> which is related to actual class of object? Which to reference variable?

A

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
Q

valid?

====
interface A{}
interface B{}
interface C extends A, B{}

A

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
Q

can static method be abstract?

A

static methods cannot be abstract

64
Q

field declaration in the body of an interface is implicitly?

A

Every field declaration in the body of an interface is implicitly public, static and final

(method is not included)

65
Q

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();  }
}
A

It will compile but will throw AnotherException when run.

66
Q

System.out.println(true); //1

System.out.println(null); //2

A

System.out.println(true); //1 –> true

System.out.println(null); //2 –> compilation error

67
Q

which kind of variable remains uninitialized unless it is explicitly initialized?

A

local variable

68
Q

SecurityException is a Runtime Exception?

A

yes

69
Q

A programmer should never throw an Error explicitly

–> right?

A

yes

A programmer should never throw an Error explicitly.

but he can

70
Q

Wrapper is subclass of Object?

A

yes

wrapper Class = subclass of Object

71
Q
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?
A

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
Q

what is wrong?

for (int i=5; i=0; i–) { }

A

uses ‘=’ instead of ‘==’ for condition which is invalid. The loop condition must be of type boolean.

73
Q

what is wrong?

int j=5;
for(int i=0, j+=5; i

A

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
Q
What will the following code print?
void crazyLoop(){
   int c = 0;
   JACK: while (c  3) break JILL; else c++;
   }
}
A

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
Q

switch(8);

what is wrong?

A

invalid because a switch statement must have a body. The body may even be empty as shown

int x = 0;
switch(x){
}

76
Q

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
A

//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
Q

what is wrong?

interface A extends C{}
interface B{}
class C{}
A

interface cannot extends a class

78
Q

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?

A

No. OurClass contains a reference to YourClass

79
Q

double x = 5, double y;

correct?

A

no

double x = 5;
double y;

80
Q
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ì?

A

5 - 6

static m –> 6 - 6

81
Q

if (x = 3) {}
while (x = 5) {}

//assume that x is an int

what is wrong?

A

if(…) and while(…) needs a boolean

82
Q

public void method1(){
int i;
this.i = 4;
}

what is wrong?

A

i = local variable –> cannot this.i

83
Q

what is wrong?

public class TestClass{
	int m = 5;
    public static void main(String[] args){
    	TestClass.m = 5;
    } 
}
A

m must be static if want to use ClassName.variable

84
Q

what is wrong?

public class TestClass{
	int m = 5;
    public static void main(String[] args){
    	this.m = 5;
    } 
}
A

cannot use this. in a static context

85
Q

different in use of this.variable and ClassName.variable

A
  • 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