JAVA INTERVIEW QUESTIONS
1. What will be
the result of attempting to compile and run the following class?
public class Assignment {
public static void
main(String[] args) {
int a, b, c;
b = 10;
a = b = c = 20;
System.out.println(a);
}
}
- The
code will fail to compile, since the compiler will recognize that the
variable c in the assignment statement a = b = c = 20; has not been initialized.
- The
code will fail to compile because the assignment statement a = b = c = 20; is illegal.
- The
code will compile correctly and will display 10
when run.
- The code will
compile correctly and will display 20 when run.
2. Which
statements are true?
a. The result of the expression (1
+ 2 + "3")
would be the string "33".
b.
The result of the expression ("1" + 2 + 3) would be
the string "15".
c.
The result of the expression (4 + 1.0f) would be the float value 5.
d.
The result of the expression ('a' + 1) would be the char value 'b'.
3. What will
the following program print when run?
// Filename: MyClass.java
public class MyClass {
public static void
main(String[] args) {
B b = new
B("Test");
}
}
class A {
A() {
this("1",
"2");
}
A(String s, String t) {
this(s + t);
}
A(String s) {
System.out.println(s);
}
}
class B extends A {
B(String s) {
System.out.println(s); }
B(String s, String t) { this(t
+ s + "3"); }
B() { super("4"); };
}
Select the one correct answer.
- It
will simply print Test.
- It
will print Test followed by Test.
- It
will print 123 followed by Test.
- It will print 12
followed by Test.
4. Which one of
these is not a legal member declaration within a class?
a.
static int a;
b.
final Object[] fudge = { null };
c.
abstract int t;
d. native void sneeze();
5. Which
statements are true about modifiers?
a.
Fields can be declared synchronized.
b. Non-abstract methods can be declared in abstract classes.
c. Classes can be declared native.
d.
Abstract classes can be declared final.
6.
What is the purpose of final variables
a. Making a constant
b.
To hide a variable in child class
c.
To protect from inherited classes
d.
Making a variable of fixed memory
7.
The mother of all classes in Java
a. java.lang.Object
b.
java.lang.Class.object
c.
java.lang.System.Object
d. java.Object
8.
Java does not support
a. Operator overloading
b.
Function overloading
c. Constructor overloading
d.
Method overloading
9.
_________used to access the parent class
members
a. extend
b.
public
c.
this
d. super
10. What will be
the result of attempting to compile and run the following program?
public class Polymorphism {
public static void main(String[] args) {
A ref1 = new C();
B ref2 = (B) ref1;
System.out.println(ref2.f());
}
}
class A {
int f() {
return 0;
}
}
class B extends A {
int f() {
return 1;
}
}
class C extends B {
int f() {
return 2;
}
}
Select the
one correct answer.
a.
The program will fail to compile.
b.
The program will compile without error and print 0 when run.
c.
The program will compile without error and print 1 when run.
d.
The program will compile without error and print 2 when run.