151 - 180 Flashcards
which one is correct
int a[ ][ ] = new int [ ][4] ;
int[ ] a[ ] = new int[4][ ] ;
only int[ ] a[ ] = new int[4][ ] ; is correct
static block of the base class / class
non-static block of the base class / class
constructor of the base class / class
- Static blocks of the base class (only once, in the order they appear in the class).
- Static blocks of the class.
- Non-static blocks of the base class.
- Constructor of the base class.
- Non-static blocks of the class.
- Constructor of the class.
- Derived class’s static or non-static blocks are not executed if that class is not being used.
instance variable
static variable
local variable
–> which one is not automatically given default value?
local variable
char[] b = { 'j', 'a', 'v', 'a' }; String c = new String(b);
System.out.println(c); –> output?
java
can a class be imported statically?
no
a class cannot be imported statically
class B {B(){ } }
does B have a default constructor?
no
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(); } }
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
while(int k=5;k valid?
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
- primitives are object?
- array of primitives and of objects is object?
- An array of objects can store Objects of any class.
- Primitives (i.e. int, byte, char, short, boolean, long, double, and float) are NOT objects.
- An array (of primitives as well as of objects) is an Object.
meaning of using an asterisk (*) in import statement?
by using an asterisk (*), you can import all of the public members, classes, and interfaces of a package
can use import statement to access multiple classes or interfaces with the same names from different packages?
No
cannot use the import statement to access multiple classes or interfaces with the same names from different packages
int i = 5; float m = 5.0f; System.out.println(i == m); float n = 5.5f; System.out.println(i == n);
–> output
i == m –> true
i == n –> false
value of i will be promoted to a float i.e. 5.0, and so it returns false
….? method1() : method2()
- method1() and method2() are void
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
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;
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.
is ArrayList a subclass of AbstractList?
yes
ArrayList is a subclass of AbstractList