121 - 150 Flashcards

1
Q

what packages are imported automatically?

A

only java.lang

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

What letters, and in what order, will be printed when the following program is compiled and run?

public class FinallyTest{
   public static void main(String args[]) throws Exception{
       try{
          m1();
          System.out.println("A");
       }
       finally{
          System.out.println("B");
       }
       System.out.println("C");
   }
   public static void m1() throws Exception { throw new Exception(); }
}
A

It will print B and throw Exception.

An Exception is thrown in method m1() so println(“A”) will not be executed.
As there is no catch block the exception will not be handled and the main() method will propagate the exception. So println(“C”); will also not be executed.
‘finally’ block is always executed (even if there is a return in try but not if there is System.exit() ) so println(“B”) is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
public class TestClass { 
	public static void main(String[] args) { 
		String myStr = "good"; 
		char[] myCharArr = {'g', 'o', 'o', 'd' }; 
		String newStr = null; 
		for(char ch : myCharArr){ 
			newStr = newStr + ch; 
			} 
		System.out.println(newStr); 
		} 
	}
A

output: nullgood

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

can constructor be inherited?

A

Constructors are NEVER inherited

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

what is AssertationError?

A

AssertionErrors are thrown when assertions fail. Assertions fail because something the programmer believed to be true turns out not to be true. That means there is a bug in the code

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

break and continue –> work with label outside loop?

A

break can work with label (outside loop) but continue cannot

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

preference: widening, boxing / unboxing, var-args

class TestClass{
    void probe(int... x) { System.out.println("In ..."); }
void probe(Integer x) { System.out.println("In Integer"); }

void probe(long x) { System.out.println("In long"); }

void probe(Long x) { System.out.println("In LONG"); }
    public static void main(String[] args){
        Integer a = 4; new TestClass().probe(a);
        int b = 4; new TestClass().probe(b);
    }
}
A

widening > boxing / unboxing > var-args

output: In Integer and In long

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

what happens when both catch and finally block define return statements?

A

If both catch and finally blocks define return statements, the calling method will receive a value from the finally block

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

char a;
short b;

without casting –> OK?
A: a = b
B: b = a

same to byte and int?

A

no. Neither A nor B is correct –> need casting because their ranges are different

short: -32,768 to 32,767
char: 0 to 65,535

–> same to byte but not int because range of int is bigger than ranges of short, char and byte

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

run a constructor in the subclass –> what needs to be done with the superclass?

public class SubClass extends SuperClass{
     int i, j, k;
     public SubClass( int m, int n )     {  i = m ;  j = m ;  } //1
     public SubClass( int m )  {   super(m );   } //2
 }

Which of the following constructors MUST exist in SuperClass for SubClass to compile correctly?

A
  • run constructor in the superclass. Constructor in the subclass has no constructor from superclass –> implicit default constructor

  1. public SuperClass() –> the default no args constructor will not be provided because SuperClass has to define one arg constructor
  2. public SuperClass(int a) –> because it is called in the second constructor of SubClass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  • what is instanceof for?
  • when can use it?
  • null –> instance of?
  • operand
A
  • compares an object to a specified type
  • use it, when having a reference or parameter to an object that is of a super class or interface type and need to know whether the actual object has some other type
  • use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface –> null is not instance of anything
  • operand
    + left operand MUST be an object + not a primitive
    + right operand MUST be a class name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

narrowing primitive conversion:

  • bigger than int
  • equals or smaller than int
A
  • bigger than int —> need explicit casting
  • equals or smaller than int
    o implicit casting
    o right hand side must be
    + constant / final
    + or the value must fit the range of the left hand side
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

int[][] abc = new int[2][];

System.out.println(abc.length);

System.out.println(abc[0].length);

A

System.out.println(abc.length); –> 2

System.out.println(abc[0].length); –> NullPointerException

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

StringBuilder –> ensureCapacity?

A

Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:

  • The minimumCapacity argument.
  • Twice the old capacity, plus 2.

If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

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

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

A

no

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

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

why writng double d = 0b10_000D; is wrong?

A

A floating point number written in binary or hex cannot use any suffix for float. But a floating point number written in decimal can use the floating point suffices f, F, d, and D.
Thus, float dx = 0xff; is valid but the f here is not for indicating that it is a float but is interpreted as the hex digit F.

17
Q

catch both Exception and a subclass of Exception –> what should be used?

A

the most specific –> subclass

18
Q

What will be the output when the following program is run?

public class TestClass{
    char c;
    public void m1(){
        char[ ] cA = { 'a' , 'b'};
        m2(c, cA);
        System.out.println( ( (int)c)  + "," + cA[1] );
    }
    public void m2(char c, char[ ] cA){
        c = 'b';
        cA[1] = cA[0] = 'm';
    }
    public static void main(String args[]){
        new TestClass().m1();
    }
}
A

0,m

Arrays are Objects (i.e. cA instanceof Object is true) so are effectively passed by reference. So in m1() the change in cA[1] done by m2() is reflected everywhere the array is used. c is a primitive type and is passed by value. In method m2() the passed parameter c is different than instance variable ‘c’ as local variable hides the instance variable. So instance member ‘c’ keeps its default (i.e. 0) value.

19
Q

what does an unreachable statement cause?

example?

exception?

A
  • an unreachable statement causes a compilation error
  • example: while(false) {x = 3}
  • one exception: if(false) { … } is valid
20
Q

can specify visibility of local variables?

A

no. Cannot

A local variable (aka automatic variable) means a variable declared in a method. They don’t have any accessibility. They are accessible only from the block they are declared in.
Remember, they are not initialized automatically. You have to initialize them explicitly.

example: Local variables cannot be declared as private.

21
Q

what can an object be made eligible for garbage collection?

A

An object can be made eligible for garbage collection by making sure there are no references pointing to that object.

22
Q

what is the only modifier (excluding annotations) that is allowed in for-each loop?

A

final

example: for(final Object o2 :c){ }

23
Q

evaluation of the left-hand operand of a binary operator completes abruptly –> what happens?

A

no part of the right-hand operand appears to have been evaluated

24
Q
  • what does shadow means?

- can final variable be shadowed?

A
  • Shadowing refers to the practice in Java programming of using two variables with the same name within scopes that overlap. When you do that, the variable with the higher-level scope is hidden because the variable with lower-level scope overrides it. The higher-level variable is then “shadowed.”
  • final variable can be shadowed
25
Q

superclass = abstract –> what should derived class do?

A
  • derived class should implement all the abstract methods of its abstract base class
  • if it doesn’t, it must be defined as an abstract derived class
26
Q

function of Object class’s equals() method

A

Object class’s equals() method just checks whether the two references are pointing to the same location or not

27
Q

when is finally in try - catch statement not executed?

A

in case of System.exit()

28
Q

can constructor be abstract, static, final, native, or synchronized?

A

No

constructor cannot be abstract, static, final, native, or synchronized

29
Q
class Animal {} 
class Dog extends Animal { }
Dog d = new Dog();

d instanceof Animal –> ??

A

d instanceof Animal –> true

instanceof operator returns true even if the Right Hand Side is a super class

30
Q

null instanceof Object –> ?

A

false