61 - 90 Flashcards

1
Q

Passed by?

  • primitive
  • object
A

o primitive –> value
o object –> object

Java passes all primitive data types by value. This means that a copy is made, so that it cannot be modified. When passing Java objects, you’re passing an object reference, which makes it possible to modify the object’s member variables. If you want to pass a primitive data type by reference, you need to wrap it in an object

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

String is immutable –> what does this mean?

A
  1. created –> content not modified but can call method
  2. methods that return a modified String value
    - return a new String object
    - original String value remains the same
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

access modifiers for local variables

A

only final can be used

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

ArrayList - 2:

  • indexOf(Object o)
  • lastIndexOf(Object o)
  • .clone()
  • contains(Object o)
  • get(int index)
A

o indexOf(Object o) –> returns the index of the first occurrence of the specified element in this list + return –1 if this list does not contain the element

o lastIndexOf(Object o) –> returns the index of the last occurrence of the specified element in this list + return –1 if this list does not contain the element

o .clone() –> returns a value of the type Object

o contains(Object o) –> returns true if this list contains the specified element

o get(int index) –> returns the element at the specified position in this list

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

ArrayList - 1:

  • remove(int index) & remove(Object o)
  • addAll(Collection extends E> c)
  • addAll(int index, Collection extends E> c)
  • clear()
A

o remove(int index) –> removes the element at the specified position

o remove(Object o) –> removes the first occurrence of the specified element if it’s present

o addAll(Collection extends E> c) –> appends all of the elements in the specified collection to the end of this list in the order in which they are returned by the specified collection’s Iterator

o addAll(int index, Collection extends E> c) –> inserts all of the elements in the specified collection into this list, starting at the specified position

o clear() –> remove all the ArrayList elements –> after removing all elements –> print out [ ] not null –> # set to null

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

try-catch-finally –> what does the calling method receive when?

  1. both catch & finally define return statements
  2. finally modifies value returned from catch
    1. catch return primitive
    1. catch return object
A
  1. finally
    1. finally cannot modify
    1. finally can modify
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what does getClass() return?

A

always returns the Class object for the actual object on which the method is called irrespective of the type of the reference

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

setLength()

o method of String or StringBuilder?
o newLength argument = current length

A

o StringBuilder
o newLength argument length is changed to the specified length
o newLength argument >= current length –> sufficient null characters (‘\u0000’) are appended so that length becomes the newLength argument

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

A || B khác gì A | B

A && B khác gì A & B

false && false = ?
false || false = ?
true || false = ?

A

‘&’ performs both tests, while ‘&&’ only performs the 2nd test if the first is also true

A || B khác gì A | B

A && B khác gì A & B

false && false = false
false || false = false
true || false = true

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

access reference variable - object –> when depends on:

  • reference variable
  • object

how must variables, methods be declared?

A

static + khác –> reference variable

non-static method –> object

variables, methods must be declared in both superclass and derived class

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

what happens when derived classes extend abstract class?

A

o implement all abstract method from base class

o derived classes have to be defined as abstract class

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

3 categories of exception. Which ones do not have to be thrown?

A

only checked is required to be thrown. The two others should not (but can) be thrown

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

variables defined in the body of interface are implicitly?

Method in interface? (3)

A

variables - abstract and public, static, final

method
+ must be public
+ no final + no static

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

interface implement interface and extend class?

A

no. Interface cannot implement class but can extends many other interfaces with keyword extends

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

constructors:

o name?
o static, final, abstract?
o how many constructors per class?
o return type?
o what is default constructor? Is it important?
A
o same name as the name of the class in which they’re defined
o can many but should not be
o no return type, even void --> return type = method
o cannot be static, final, abstract
o in the absence of a user-defined constructor --> Java inserts automatically + no argument. Without a constructor, a class won't run
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

initializer block:

  • what is? A part of method?
  • parameter method?
  • execute for what?
  • what happen when define both initializer blocks and constructor in a class?
A
  • an initializer block is defined within a class, not as a part of a method
  • cannot accept method parameters
  • executes for every object that’s created for a class
  • call both + initializer blocks execute prior to the constructors
17
Q

byte a;
byte b;

s = a * b –> s thuộc type gì?

A

operands of mathematical operators are ALWAYS promoted to AT LEAST int and the return value will be AT LEAST int

18
Q

without casting, can something bigger than int be assigned to int and something smaller than int?

A

no

19
Q

default type of floating number?

When may float be written without f in suffix and when must?

A
  • double
  • should add suffic F or f if have decimal
    right:
    float i = 123;
    float j = 123.5f;

wrong:
float m = 123.5;

20
Q

output

double m = 50 / 100;
double i = 50.0 / 100;
double j = 50 / 100.0;
double k = 50.0 / 100.0;

A

output khi in

double m = 50 / 100; –> 0.0 –> default code number is int
double i = 50.0 / 100; –> 0.5
double j = 50 / 100.0; –> 0.5
double k = 50.0 / 100.0; –> 0.5

21
Q

for loop –> 3 fields = blank?

  • initialization statement –> can define many variables? Type?
  • Condition blank? Value?
  • update statement –> many? Order of execution? Call to other methods?
A

3 fields can be blank

  • initialization statement –> can define many variables + must have the same type?
  • Condition blank –> true
  • update statement –> can have many + execute in the order in which they appear + can include call to other methods
22
Q

equals & == khác gì nhau?

A

refer to the same object? ==

23
Q

what is instance method & class method?

A

instance method = non-static method

class method = static method

24
Q

o can size of an array be expanded or reduced once it is allocated?

o declaring array = create variable referring to ?

o can array be data type of null?

o can a char array be assigned to an int array?

A

o no

o null

o no

o no

25
Q

String String = “a”;

String char = “a”;

A

String String = “a”; –> ok

String char = “a”; –> not ok

26
Q

binary number –> properties (3)

A
  • gồm ký tự 0 và 1
  • 0b + ….
  • cannot use suffix L, F,…
27
Q

can call both call super(…) and this(…) from a constructor?

A

can either call super(…) or this(…) but not both from a constructor

28
Q

main method:

  • can args array be null?
  • program runs without any arguments –> what does args point to?
A

o can never be null

o if the program is run without any arguments, args points to a String array of length 0

29
Q
chaining to use a value of a variable at the time of declaration --> allowed?
------------
int a = b = c = 100;
------------
int a, b, c;
a = b = c = 100;
----------
int b = 0, c = 0;
int a = b = c = 100;
A
no
-----
int a = b = c = 100; ==> wrong
-------
the other two = okay
30
Q

variable arguments:

  • int… args or int args…
  • meaning + no parameter –> length? Null?
A

o int… args
o can take any number of arguments (even 0)
o no parameter –> length = 0 + NOT null