Saturday, December 21, 2013

java interview questions and answwers.

java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.java interview questions and answwers.


Java Interview Questons and  Answers

What is the output for the below code ?
1. public class A {
2. int add(int i, int j){
3. return i+j;
4. }
5.}
6.public class B extends A{
7. public static void main(String argv[]){
8. short s = 9;
9. System.out.println(add(s,6));
10. }
11.}
Options are
A.Compile fail due to error on line no 2
B.Compile fail due to error on line no 9
C.Compile fail due to error on line no 8
D.15
Answer :
B is the correct answer.
Cannot make a static reference to the non-static method add(int, int) from the type A. The
short s is autoboxed correctly, but the add() method cannot be invoked from a static
method because add() method is not static

What is the output for the below code ?
public class A {
int k;
boolean istrue;
static int p;
public void printValue() {
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}
public class Test{
public static void main(String argv[]){
A a = new A();
a.printValue();
}
}
Options are
A.0 false 0
B.0 true 0
C.0 0 0
D.Compile error - static variable must be initialized before use.


Answer :
A is the correct answer.
Global and static variable need not be initialized before use. Default value of global and
static int variable is zero. Default value of boolean variable is false. Remember local
variable must be initialized before u
  

What is the output for the below code ?
public class Test{
int _$;
int $7;
int do;
public static void main(String argv[]){
Test test = new Test();
test.$7=7;
test.do=9;
System.out.println(test.$7);
System.out.println(test.do);
System.out.println(test._$);
}
}
Options are
A.7 9 0
B.7 0 0
C.Compile error - $7 is not valid identifier.
D.Compile error - do is not valid identifier.
Answer :
D is the correct answer.
$7 is valid identifier. Identifiers must start with a letter, a currency character ($), or
underscore ( _ ). Identifiers cannot start with a number. You can't use a Java keyword as
an identifier. do is a Java keyword.
What is the output for the below code ?
package com;
class Animal {
public void printName(){
System.out.println("Animal");
}
}
package exam;
import com.Animal;
public class Cat extends Animal {
public void printName(){
System.out.println("Cat");
}
}
package exam;
import com.Animal;
public class Test {
public static void main(String[] args){
Animal a = new Cat();
a.printName();
}
}
Options are
A.Animal
B.Cat
C.Animal Cat
D.Compile Error
Answer :
D is the correct answer.
Cat class won't compile because its superclass, Animal, has default access and is in a
different package. Only public superclass can be accessible for different package.

What is the output for the below code ?
public class A {
int i = 10;
public void printValue() {
System.out.println("Value-A");
};
}
public class B extends A{
int i = 12;
public void printValue() {
System.out.print("Value-B");
}
}
public class Test{
public static void main(String argv[]){
A a = new B();
a.printValue();
System.out.println(a.i);
}
}
Options are
A.Value-B 11
B.Value-B 10
C.Value-A 10
D.Value-A 11
Answer :
B is the correct answer.
If you create object of subclass with reference of super class like ( A a = new B();) then
subclass method and super class variable will be executed.
What is the output for the below code ?
public enum Test {
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
private int mm;
Test(int hh, int mm) {
assert (hh >= 0 && hh <= 23) : "Illegal hour.";
assert (mm >= 0 && mm <= 59) : "Illegal mins.";
this.hh = hh;
this.mm = mm;
}
public int getHour() {
return hh;
}
public int getMins() {
return mm;
}
public static void main(String args[]){
Test t = new BREAKFAST;
System.out.println(t.getHour() +":"+t.getMins());
}
}
Options are
A.7:30
B.Compile Error - an enum cannot be instantiated using the new operator.
C.12:30
D.19:45
Answer :
B is the correct answer.
As an enum cannot be instantiated using the new operator, the constructors cannot be
called explicitly. You have to do like Test t = BREAKFAST;

What is the output for the below code ?
public class A {
static{System.out.println("static");}
{ System.out.println("block");}
public A(){
System.out.println("A");
}
public static void main(String[] args){
A a = new A();
}
Options are
A.A block static
B.static block A
C.static A
D.A
Answer :
B is the correct answer.
First execute static block, then statement block then constructor.

What is the output for the below code ?
1. public class Test {
2. public static void main(String[] args){
3. int i = 010;
4. int j = 07;
5. System.out.println(i);
6. System.out.println(j);
7. }
8. }
Options are
A.8 7
B.10 7
C.Compilation fails with an error at line 3
D.Compilation fails with an error at line 5
Answer :
A is the correct answer.
By placing a zero in front of the number is an integer in octal form. 010 is in octal form
so its value is 8.
What is the output for the below code ?
1. public class Test {
2. public static void main(String[] args){
3. byte b = 6;
4. b+=8;
5. System.out.println(b);
6. b = b+7;
7. System.out.println(b);
8. }
9. }
Options are
A.14 21
B.14 13
C.Compilation fails with an error at line 6
D.Compilation fails with an error at line 4
Answer :
C is the correct answer.
int or smaller expressions always resulting in an int. So compiler complain about Type
mismatch: cannot convert from int to byte for b = b+7; But b += 7; // No problem
because +=, -=, *=, and /= will all put in an implicit cast. b += 7 is same as b = (byte)b+7
so compiler not complain.
What is the output for the below code ?
public class Test {
public static void main(String[] args){
String value = "abc";
changeValue(value);
System.out.println(value);
}
public static void changeValue(String a){
a = "xyz";
}
}
Options are
A.abc
B.xyz
C.Compilation fails
D.Compilation clean but no output
Answer :
A is the correct answer.
Java pass reference as value. passing the object reference, and not the actual object itself.
Simply reassigning to the parameter used to pass the value into the method will do
nothing, because the parameter is essentially a local variable.

No comments:

Post a Comment