Core Java Interview Questions Part II
Which one is faster in java ?
for(int i = 100000; i > 0; i--) { }
for(int i = 1; i < 100001; i++) { }
Which one is faster in java ?
for(int i = 100000; i > 0; i--) { }
for(int i = 1; i < 100001; i++) { }
Answer is a)
for(int i = 100000; i > 0; i--)
{
}
Are array operations faster or Vector operations?
Array operations are faster.
You have reference variable of parent class type and you assign a child class object to that variable and invoke static method. Which method will be invoked, parent or child?
Parent method will be invoked first.
What will be the value of Point p after methods in a and b if the value before method call is (700, 800)?
static void changePoint ( Point p) { p.x = 100; p.y=200; }static void changePoint(Point p) { p = new Point(100,200); }
Answer :
(100,200)
(700,800)
[Note : Primitive type is passed by value in method parameter and objects are passed by value of the reference. In a method if the object values are changed , it will reflect, but if we try to change the reference itself its original reference/object will not change, as only copy of the reference is changed.]
Which one of these primitive types are unsigned?
int
long
char
double
float
Which one of these primitive types are unsigned?
int
long
char
double
float
c) char (All numeric data types are signed. 'char' is the only unsigned integer type.)
Can inner class have static members?
No, inner class cannot have static members.
Can constructor throw exception?
Yes. [Constructor can throw exception.]
What is the rule regarding overriding methods throwing exceptions?
Overriding methods cannot throw more generic exceptions than base method.
Which one of the following is not correct?
x = = Float.NaN
Float.isNan(x);
Myobject .equals(float.NaN);
Which one of the following is not correct?
x = = Float.NaN
Float.isNan(x);
Myobject .equals(float.NaN);
a) x = = Float.NaN
Can finalize method be overloaded?
Yes, finalize method can be overloaded but only the below given version is called by garbage collector : protected void finalize() throws Throwable { };
Nested classes can extend only the enclosing class and cannot implement any interface. True/False?
False. [Nested class can extend any class or implement any interface.]
Can we declare derived class first and then base class in Java?
Yes, we can declare derived class first and then base class in Java.
GC is a high priority thread. True/False?
False. [GC is a low priority thread.]
Final variables declared without initialization can be initialized in static initializer (static final var) or in constructor( final var). True/False?
True, but the most once and not more than that.
Dictionary is an interface or a class?
Dictionary is a class and not an interface.
Member variables are resolved at compile time or runtime?
Member variables are resolved at compile time.
Map implements collection. True/False?
Map does not implement collection.
Is this statement correct: char ch = 'd'; if(ch < 32.00){ }
Yes the above statement is correct.
What will be output from the following statements :
System.out.println(1+2+”3”);
System.out.println(“1”+2+3);
What will be output from the following statements :
System.out.println(1+2+”3”);
System.out.println(“1”+2+3);
Answer :
33
123
Random access file extends from File. True/False?
False [ Random access file descends from object and implements data input and data output.]
Can an interface be final?
No, interface cannot be declared final as they are implicitly abstract.
Can we have static method in interface?
All methods in an interface are implicitly public, abstract but never static.
Java supports both multi-dimensional and nested arrays. True/False?
False [Java does not support multi-dimensional arrays. It only supports nested arrays.]
public main(int number) { } is a valid method or not?
No it is not a valid method.
Is this method valid or not? public static final main(String[] args){ }
Yes it is a valid method.