Interview Questions

Question 1.

Which of the following are valid Java Keywords

A. goto

B. final

C. NULL

D.finally

E. extend

F. main

Ans : goto, final, finally

Question 2.

True / False. Select if True
A. Instance variables are always initiated to defaults //True

B. Local variables are always initiated to defaults //False

C. Array Elements are always initiated to defaults //True

D. Object references that are not initialized
explicitly will have their value set to null always //False

Question 3.

Select all the following statements about ‘wait’ method that are true

A. wait() is static final method that cannot be overrided.

B. wait() is an instance method of object class.

C. wait() is an instance method of Thread class.

D.Thread must have lock an the object involved to call
wait().

Ans : B & D

Question 4.

Select the code segments(assuming is part of valid class) below that compile and run correctly with output: We are Equal

A. int i = 10;

long l = 10L;
if( i == l )
System.out.println(“We are Equal”); //Equal

B. int i = 10;
Integer ii = new Integer(10);
if( i == ii)
System.out.println(“We are Equal”); //Equal

C. int i = 10; char c = 10;
if( c == i)
System.out.println(“We are Equal”); //Equal

D. Integer ii = new Integer(10);
Integer jj = new Integer(10);
if(ii == jj)
System.out.println(“We are Equal”); //Not Equal

E. String s1 = “Null”;
String s2 = “Null”;
if( s1 == s2)
System.out.println(“We are Equal”); //Equal

F. String s1 = “Null”;
String s2 = new String(s1);
if( s1 == s2)
System.out.println(“We are Equal”); //Not Equal

Question 5.

Which of the following are true about overloading & overridding.

A. Overrided methods return different return types and

will be in different classes. //False

B. Overloaded methods supplement where as overridden
method replace the method it overrides. //True

C. Return type of overriden method must be identical to
return type of method it overrides. //True

D. Given a method
public void aMethod(int i,float f);
the method
public void aMethod(float f,int i);
is a possible overridden method. //False

Question 6.

Which of the follwing is true about static modifier.

A. static can be applied to : instance variables, methods,

code Segments and classes. //True

B. a static method cannot be overridden. //True

C. inner classes can be both static & private. //True

D. a static reference cannot be made through non static
method or code block. //True

E. abstract & static both can be applied together to a
method. //False

Question 7.

When can we use same method name in java for two different methods

A. If the two methods are in two unrelated clasess. //False

 

B. If the two methods are in the same class and they
differ in argument & return type. //True

C. If one method is in base class, other is in its sub
class and the two mehtods differ only in return type. //False

D. If one method is in base class, other is in its sub class
and the two mehtods differ both arguments list and in
return type. //True

Question 8.

Given the code, what is the output

public class demo{
static int i = 10;
public static void main(String[] arg){
static int i = 20;
System.out.println(“i is :”+i);
}
}

A. Code Does’t Compile. //True

B. Code Compiles but error will be generated at runtime.

C. Code Compiles & runs, output is
i is : 10

D. Code compiles & runs, output is
i is : 20

Question 9.

public class demo
{
public static void main(String[] args)
{
int i = 10;
int j = 10;
boolean b = false;

if( b = i == j)
System.out.println(“True”);
else
System.out.println(“False”);
}
}

Ans : True

 

Question 10.

class demo
{
public static void main(String []args)
{
Byte b1 = new Byte(“127”);
if(b1.toString() == b1.toString())
System.out.println(“True”);
else
System.out.println(“False”);
}
}

Ans : False

 

 

Posted in core java

Leave a comment