These are some tricky questions usually found in java questionnaire or interviews. Check the answers at the end.
These are very easy to answer if you have the liberty to try on a computer.
- Write an nearest equivalent of size operator in C. Hint: Use
Runtime class.
- 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 ?
- Math.max(a,b);
- (a>b)?a:b
- Is Array operations are faster or of Vector.
- 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);
}
- MyClass.java and empty file is valid source file. True/False?
- Which one of these statements are valid?
-
Char \u0061r a =’a’;
-
Char \u0062 =
’b’;
-
Char
c =’\u0063’;
- 1
b. 2 c.3 d. ALL
e. NONE
- Which one of these primitive
types are unsigned?
- int
- long
- char
- double
- float
- Java supports both multi dimension and nested
arrays. True/False
- public main(int number) { } is
a valid method. True/False?
- public static final main(String[]
args) {} is a valid method. True/False?
-
A class without a method can be run by JVM if its ancestor class has main.
True/False?
-
GC is a high priority thread. True/False?
- Can circular reference prevent
an object to be GCed?
- If there is an exception in
finalize method, will the object be garbage collected?
- An object is resurrected by
making other object refer to the dying object in finalize method. Will this
object be ever garbage collected?
- Can finalize method be
overloaded?
- Does the finalize method in
subclass invoke finalize method in super class?
- Which one throws arithmetic
exception:
- int i = 100/0;
- float f = 100.00/0.0
- Which one is not correct
-
x = = Float.NaN
-
Float.isNan(x);
-
Myobject .equals(float.NaN);
- What will be output from the
following statements:
- System.out.println(1+2+”3”);
- System.out.println
(“1”+2+3);
- Is the following statement correct:
char ch = 'd';
if(ch < 32.00){ }
- Determine the output:
- byte b=10;
b=b+10;
System.out.println("The value of b is " + b);
- byte b=10;
b+=10;
System.out.println("The value of b is " + b);
- char c = 3;
int a = 65;
char d = a;
System.out.println("The value of d is " + d);
-
float f = 1.3;
System.out.println("The value of f is "
+ f);
-
float f = 1.3f;
System.out.println("The value of f is " + f);
-
float f = 6/2;
System.out.println("The value of f is " + f);
-
float f = 6.0/2.0;
System.out.println("The value of f is " + f);
-
byte b;
final int a = 10;
b=a;
System.out.println("The value of b is "
+ b);
-
byte b;
final int a = 10;
final int x = a;
b = x;
System.out.println("The value of b is "
+ b);
-
int y;
final int z = y;
b = z;
System.out.println("The value of b is "
+ b);
-
public class precedence{
public static void main(String[] args) {
int i = 0;
i=i++;
i=i++;
i=i++;
i=i++;
System.out.println("The value of i is " + i);
int arr []= new int [5];
int index= 0;
arr [index]=
index = 3;
System.out.println("The value of first element is " + arr[0]);
System.out.println("The value of fourth element is " + arr[3]);
}
}
- if(-0.0==0.0){
System.out.println("-0.0==0.0");
} else {
System.out.println("-0.0!=0.0");
}
-
If 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/Child.
-
Local variables can not be declared static or final or transient
.True/False?
-
Final variables declared without initialization can be initialized in static initializer ( static final var) or in constructor( final var).
True/False?
-
What is the use of transient
variable? Can a transiant variable be static?
-
What is the use of volatile
variable?
-
Random access file extends from
File. True/False?
-
Map implements collection. True/False?
-
Dictionary is an interface or
class?
-
Can we declare derived class first
and then base class in java?
-
Can constructor throw exception?
-
Array whether local or class levels are always initialized. True/False?
-
Can we cast two derived class for
each other, both having same parent class. Yes/No
-
Can inner class have static
members? Yes/No
-
Does File class have any method to
read or write content in a file? Yes/No
-
Which ones are classes and which
ones are interfaces?
-
InputStream, OutputStream
-
DataInputStream, DataOutputStream
-
What is the rule regarding
overriding methods throwing exceptions?
-
Member variables are resolved
compiletime or runtime?
-
Can we override variables?
class S1{
public string S= “ S1”;
public string gets() {
returns S;
}
}
class S2 extend S1 {
public string S = “ S2”;
public string gets() {
return s;
}
}
public class shadow{
public static void main(String S[]) {
S1 S1 = new S1();
S2 S2= new S2();
System.out.println("Print
S1 " + s1.s);
System.out.println("Print
S1 " + S2.s);
S1=S2;
System.out.println("Print
S1 now " + S1.S) ;
System.out.println(
"Print S1.gets() now " + S1.gets());
}
}
-
If an overridden method calls super
class method which access class member variable, which variable will be used
base class or super class.
class S1 {
string S= “ S1”;
public string gets (){
return S;
}
void display () {
System.out.println("Display in S1 " + S);
}
}
class S2 extends S1{
string S= “ S2”;
void display(){
super.display();
System.out.println("Display in S2 " +S);
}
}
public class shadow 2 {
string s =” base”;
public static void main(String s[]) {
S2
s2=new S2();
S2.display ();
S1
s1=new S1();
System.out.println("Print
S1 " + S1.gets());
System.out.println("Print S2 " + S2.S2.gets());
}
}
-
Can we have static method in
interface?
-
Can an interface have variables?
Can these variables be transient?
-
Can an interface be final?
-
Can a class implement two
interfaces which has got methods with same name and signatures?
-
Can a class implement two
interfaces with same variable names.
-
Nested classes can extend only the
enclosing class and can not implement any interface? True/False
-
What are the different types of
inner classes. (Read answer).
- True
-
Static Runtime runtime=Runtime.getRuntime();
long start,end;
Object obj;
runtime.gc();
start=runtime.freememory();
obj=new object();
end= Runtime.freememory();
System.out.println(“sixe of obj”+ (start-end)+ “
bytes”);
[Note: Since GC can't be enforced in java the result is not always
predictable.]
- a
- b
- Array
- a. (100,200) b. (700,800) [Note:
Primitive are 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.
- True
- d. ALL
- c. char (All numeric data types are signed .char is the only unsigned integer type.)
- False. Java doesn't support
multi dimension arrays. It supports only nested arrays.
- True
- True
- True
- False. GC is a low priority
thread.
- No
- Exception in finalize method
doesn't prevent GC.
- Resurrection can happen in
finalize method which will prevent GC to reclaim the object memory. However
this could be done only once. Next time GC will not invoke finalize method
before garbage collection.
- Yes but only the following
version is called by garbage collector:
protected
void finalize() throws Throwable { };
-
Finalize is not implicitly chained. A finalize method in sub-class should
call finalize in super class explicitly as its last action for proper functioning
. Compilers does not enforce this check.
-
b float f = 100.00/0.0. Float
division by zero returns NAN (not a number) instead of exception.
-
a
-
a: 33 b:123
-
Correct.
-
a. Compilation error as
b=b+10 evaluates to int.
-
The value of b is 20
-
Compilation error
-
Compilation error
-
The value of f is 1.3
-
The value of f is 3.0
-
Compilation error as
6.0/2.0 evaluates to double.
-
The value of b is 10
-
The value of b is 10
-
Compilation error as the value of z is not
determined.
-
The value of i is 0
The value of first element is 3
The value of fourth element is 0
-
-0.0==0.0
-
Parent
-
True
-
True but
at most once.
-
Transient variables are not stored as objects persistence state .Not
serialized for security. Transient variables may not be final or static
. Compilers does not give any errors as static variables are
anyway not serialized.
-
Volatile can be applied only to variables. Not for static or final.
Declaring a variable volatile indicates that it might be modified
asynchronously, so that thread will get correct value. Used in
multi processor environment.
-
False,
Random access file descends from object and implements data input and
data output.
-
Map doesn't implement collection.
-
Dictionary is a class not an interface.
-
Yes.
-
Yes. Constructor can throw
exception.
-
True
-
No
-
No
-
No
-
a. Abstract Class
b. Interface
-
Overriding method can not throw
more generic exception than base method.
-
Compile Time
-
Yes but variables when overridden
shadows the super class variable.
Print S1 S1
Print S1 S2
Print S1 now S1
Print S1.gets() now S2
-
Methods
access variables only in the context of the class they belong to.
If subclass calls super class method, it will access super class variable.
Display in S1 S1
Display in S2 S2
Print S1 S1
Print S2 S1
-
All methods in an inter face are implicitly public, abstract and
never static.
-
All variables in an interface are implicitly static , public and
final. They cannot be transient or volatile. A class can shadow the interface variable with its variable while
implementing.
-
Interface
cannot be declared final as they are implicitly abstract.
-
Yes.
-
If both the interface have same variable and the variable is not
declared in implementing class , the compiler will throw ´field ambiguous
“ error.
-
False. Nested class can extend any class or implement any interface.
-
An inner class is part of the implementation of
its enclosing class (or classes). As such, it has access to the private
members of any enclosing class. Top-level nested classes are declared
with static keyword. Top level inner classes can be accessed / instantiated
without an instance of the outer class. Can access only static members of
outer class. Can’t access instance variables or methods of the enclosing
class. Non static inner classes which are declared without static keyword can
not exist without enclosing class. Can access all the features (even private)
of the enclosing outer class. Local classes are defined inside a block (could
be a method, a constructor, a local block, a static initializer or an instance
initializer). Cannot be specified with static modifier. A class can not have
non static inner interface.
All inner class except anonymous can be abstract or final.