151 - 180 Flashcards

1
Q

which one is correct

int a[ ][ ] = new int [ ][4] ;

int[ ] a[ ] = new int[4][ ] ;

A

only int[ ] a[ ] = new int[4][ ] ; is correct

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

static block of the base class / class

non-static block of the base class / class

constructor of the base class / class

A
  1. Static blocks of the base class (only once, in the order they appear in the class).
  2. Static blocks of the class.
  3. Non-static blocks of the base class.
  4. Constructor of the base class.
  5. Non-static blocks of the class.
  6. Constructor of the class.
  7. Derived class’s static or non-static blocks are not executed if that class is not being used.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

instance variable
static variable
local variable

–> which one is not automatically given default value?

A

local variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
char[] b = { 'j', 'a', 'v', 'a' }; 
String c = new String(b);

System.out.println(c); –> output?

A

java

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

can a class be imported statically?

A

no

a class cannot be imported statically

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

class B {B(){ } }

does B have a default constructor?

A

no

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

What will be the output of the following program ?
class CorbaComponent{
String ior;
CorbaComponent(){ startUp(“IOR”); }
void startUp(String s){ ior = s; }
void print(){ System.out.println(ior); }
}

class OrderManager extends CorbaComponent{
   OrderManager(){  }
   void startUp(String s){  ior = getIORFromURL(s);   }
   String getIORFromURL(String s){  return "URL://"+s; }
}
public class Application{
   public static void main(String args[]){ start(new OrderManager()); }
   static void start(CorbaComponent cc){ cc.print(); }
}
A

It will print URL://IOR

The method selection is done on the basis of the actual class of the object (which is OrderManager here). So OrderManager’s startUp is called, which sets the ior variable to URL://IOR

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

while(int k=5;k valid?

A

not. Not valid

In Java, a while or do/while construct takes an expression that returns a boolean. But unlike a for loop, you cannot put instantiation and increment sections in the while condition. Therefore, for(int k=5;k

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

- array of primitives and of objects is object?

A
  1. An array of objects can store Objects of any class.
  2. Primitives (i.e. int, byte, char, short, boolean, long, double, and float) are NOT objects.
  3. An array (of primitives as well as of objects) is an Object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

meaning of using an asterisk (*) in import statement?

A

by using an asterisk (*), you can import all of the public members, classes, and interfaces of a package

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

can use import statement to access multiple classes or interfaces with the same names from different packages?

A

No

cannot use the import statement to access multiple classes or interfaces with the same names from different packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
int i = 5;
	float m = 5.0f;
	System.out.println(i == m);
	float n = 5.5f;	
	System.out.println(i == n);

–> output

A

i == m –> true

i == n –> false

value of i will be promoted to a float i.e. 5.0, and so it returns false

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

….? method1() : method2()

  • method1() and method2() are void
A

No. It does not compile

  • it is not permitted for either the second or the third operand expression of the ? operator to be an invocation of a VOID method
  • -> non-VOID method = okay
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest{
  public static void main(String args[]){
     B b = new B();
     B1 b1 = new B1();
     B2 b2 = new B2();
     // insert statement here
  }
}

which one is correct?

  • b1 = (B1) b;
  • b1 = (B) b1;
A

correct: b1 = (B1) b;

b1 = (B) b1; –> won’t compile. By casting b1 to B, you are telling the compiler that b1 points to an object of class B. But you are then trying to assign this reference to b1, which is of class B1. Compiler will complain against this assignment because there is no guarantee that an object of class B will also be of class B1.

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

is ArrayList a subclass of AbstractList?

A

yes

ArrayList is a subclass of AbstractList

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

what can be stored in an ArrayList?

A

only Objects can be stored

no Primitive

17
Q

limit of for-each loop (3)

A
  • cannot be used to initialize an array + modify its elements
  • cannot be used to delete or remove the elements of a collection
  • cannot iterate over multiple collections or arrays in the same for-each loop –> cannot define multiple looping variables in a for-each loop
18
Q

output?

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

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

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

void probe(Long x) { System.out.println("In Long"); }
    public static void main(String[] args){
        double a = 10; 
        new TestClass().probe(a); 
    }
}
A

In Number

Here, we have four overloaded probe methods but there is no probe method that takes a double parameter. However, a double will be boxed into a Double and class Double extends Number. Therefore, a Double can be passed to the method that takes Number. A Double can also be passed to a method that takes Object, but Number is more specific than Object therefore probe(Number ) will be called.

19
Q

what is the problem here?

		   float f = 0.0f;
		   try{
		      f = Float.valueOf(s).floatValue();
		      return f ;
		   }
		   catch(NumberFormatException nfe){
		      System.out.println("Invalid input " + s);
		      f = Float.NaN ;
		      return f;
		   }
		   finally { System.out.println("finally");  return f ;}
		   return f;
		}
A

the last return f; (after finally) is unreachable

how to solve?

  1. remove return in finally
  2. remove either return in try or in catch
20
Q

which one is wrong?

String abc = “abc”;
String 2abc = “2abc”;
String _abc = “_abc”;

A

only String 2abc = “2abc”; is wrong

an identifier must not start with a digit

21
Q

What will be the result of attempting to compile and run the following program?

class TestClass{
   public static void main(String args[]){
      boolean b = false;
      int i = 1;
      do{
         i++ ;
      } while (b = !b);
      System.out.println( i );
   }
}
A

3

22
Q

does String has methods:

  • reverse( )
  • hashCode( )
  • trim( )
A

have:

  • hashCode( )
  • trim( )

don’t have

  • reverse( ) –> StringBuilder
23
Q
  • can an interface extend from multiple interfaces?

- can a class extend from multiple classes?

A
  • an interface can extend from multiple interfaces

- a class cannot extend from multiple classes

24
Q

Consider :

 class A {  public void perform_work(){}  }
 class B extends A {  public void perform_work(){}  }
 class C extends B {  public void perform_work(){}  }
How can you let perform_work() method of A to be called from an instance method in C?
A

impossible

There is no way to go more than one level up for methods

–> this problem doesn’t occur for instance variables because variable are never overridden. They are shadowed. So to access any of the super class’s variable, you can unshadow it using a cast. For example, ((A) c).data; This will give you the data variable defined in A even if it is shadowed in B as well as in C

25
Q

what is overload method?

A
  • define a method argument list with either a different number or type of method parameters
  • only share the same name
26
Q

what does default constructor in the derived class call? (if no other contructors are defined)

A

calls the no-args constructor of the super class.

27
Q

constructor:

  • have empty bodies?
  • can be static, final, synchronized, native and abstract?
  • can be applied access modifiers?
A
  • constructors cannot have empty bodies (i.e. they cannot be abstract)
  • cannot be static, final, synchronized, native and abstract
  • can apply public, private, protected to a constructor
28
Q

_ –> how to use it?

A

_

only occur in between two digits

long y = 123_456_L; --> wrong
long z = _123_456L; --> wrong
float f1 = 123_.345_667F; --> wrong
float f2 = 123_345_667F; --> valid
float f2 = 123\_\_345_667F; --> valid
29
Q

precedence: comparison operators - mathematical operators

A

Comparison operators have lower precedence than mathematical operators

30
Q

valid declaration?

int a = ‘a’;

A

yes

System.out.println(a); –> 97