Java oca Flashcards
What will this print?
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
Java parses expression from left to right, mind the short circuit operators. The && operator is not performed.
When overriden method is called from variable with super class reference (but actual class is subclass), will it fetch method from super class/own or the subclass.
It will fetch the overriding method from subclass, fields are from superclass, static methods are also fetched from declared class.
C b1 = (B) b1; Why will this not compile
Because b1 is cast to a reference type other than reference type needed. There is no guarantee that a B can be assigned to C, this could be possible (based on casting rules) with double cast (C)(B)b1.
Can LocalDate(Time) be constructor like, new LocalDAte()?
No, because LocalDate(Time) as only private constructors. You need to use one of its static methods instead.
Which of these two is correct
System.out.println(LocalDate.now()
.with(TemporalAdjusters.next(DayOfWeek.TUESDAY)));
vs
System.out.println(new LocalDate()
.adjust(TemporalAdjusters.next(DayOfWeek.TUESDAY)));
The first one, LocalDAte has a .with operator, but not .adjust.
For instanting of object, which constructors run, from reference type or actual object. Which constructor(s) run first, super or child class.
Actual object and superclass (always, if not excplicit implicit, super() call).
Why won’t this run:
Class A{
A(int i){some code};
}
Class B extends Class a{}
Because Class B’s (implicit) constructor will make implicit super() call.
public void ifTest(boolean flag){
if (flag) //1
if (flag) //2
System.out.println(“True False”);
else // 3
System.out.println(“True True”);
else // 4
System.out.println(“False False”);
}
What will be the outcome if rune with argument false? True? Under what condtion will it run True True?
False false, True false, none, because if false the second if, and thus not the first else, will not be entered.
What will be the content of newargs?
FunWithArgs fwa = new FunWithArgs();
String[][] newargs = {args};
{{“a”, “b” , “c”}}
If overridden method has throws class, does overriding method need it?
Whether method call needs throws clause or try/catc blocks depends on either referency type or actual object, which one?
No.
Whether a call needs a throws clause/try catch block to handle an exception s based on reference type, not actual object, as actual object is only known at runtime (important in overrding situation)
class XXX{
public void m() throws Exception{
throw new Exception();
}
}
class YYY extends XXX{
public void m(){ }
public static void main(String[] args) { \_\_\_\_\_\_\_\_ obj = new \_\_\_\_\_\_(); obj.m(); } }
Fill the blanks with XXX YYY or YYY YYY, which one is correct.
YYY YYY, because reference type decides whether there is an exception thrown/handled that needs to be taken care of be calling method.
If it would be XXX YYY, main method should throw/handle exception.
Can an overiding method throw any exception?
No it can only, but does not have to, throw exception from throws clause overridden method, or any of subexception from the one thrown in overridden method.
What is correct order of initialization:
- Static constants, variables, blocks in order they appear. This is class initializaton, only happens when class is used first time (either object created or static method/field used).
if class has superclass this step is processed first for super class. - non static constants, variables, and blocks.
- constructor.
int i = 5;
float f = 5.5f;
float f2 = 5.0f;
What is the outcome of:
i == f;
i == f2;
I will be promoted to float, so 5.0. So false and true.
if main method contians .
X x = new X();
x.apply(LOGICID); // LOGICID is a static method.
Which of these two imports is needed, assume paths are correct:
import static com.foo.X.;
import com.foo.;
Both, because LOGICID is directly accessed without classname (X.LOGICID) static import is needed, but regular import is also needed because an object of x is created.
Import static com.foo.X.LOGICID would also suffice for the static import.
int i =5;
float f =5.5f;
double d = 3.8;
outcome of:
(int) (f + d) == (int) f + (int) d;
becomes 9 ==8, so false.
What is the outcome?
public class Noobs {
public void m(int a){
System.out.println(“In int “);
}
public void m(char c){
System.out.println(“In char “);
}
public static void main(String[] args) {
Noobs n = new Noobs();
int a = ‘a’;
char c = 6;
n.m(a);
n.m(c);
In int
in char
type of primitive is leading, note that ‘a’ to int has numerical value, and 6 for char will be converted to some char.
An int cannot be passed into char.
A char can be passed to both int and char, but char is more specific.
In switch block, does default to be at the end of all case options?
no
Can main method refer to this.x?
No, main method is static, so it cannot reference instance fields directly, it can only do so by creating an object.
Why won’t this work?:
public void generateReport( int n ){
String local; // 1
if( n > 0 ) local = “good”; //2
System.out.println( s1+” = “ + local ); //3
}
}
Local variable does not get default value, if only intialized in if compiler cannot be sure it will be intitialized. Make it an instance variable so it gets a default value, or at an else part where it will be initialized as well (if initialization in both if and else compiler can be sure it will be initialized).
Which of these are not part of StringBuilder Class:
trim();
ensureCapacity(int);
append(boolean);
reverse();
setlength(int);
Trim(), it is only part of String.
byte b = 2;
short s= 2;
? x = s * b;
What would be the outcome, which data type
int 4, because anything lower than int will be converted to int in arithmetic operator.
int i = 1;
short s = 2;
s += i; //what would be the outcome?
3, compound assignment operators have implicit cast, and 3 fits in short.
Does “String”.replace(‘g’, ‘G’) create a new object.
and “String”.replace(‘g’, ‘g’)?
Yes and no, if there is no change (case sensitive) some object is returned. This implies that:
“String”.replace(‘g’,’G’)==”StrinG”; // is false
“String”.replace(‘g’, ‘g’)==”String”; // is true
“String”.replace(‘g’,’G’) == “String”.replace(‘g’,’G’); // is false
- p){
return p.test(list);
}
checkList(new ArrayList(), (ArrayList al) -> al.isEmpty());