Java Interview Questions
Java Interview Questions
true true
true false
false true
false false
What is the output of the following code
snippet?
int five = 5;
int two = 2;
int total = five + (five > 6 ? ++two : --two);
1
2
4
6
Which one is not a valid statement in Java?
double num = 2.718;
double num = 2._718;
double num = 2.7_1_8;
How many strings can be collected by Garbage Collector in the following code
snippet?
0
1
2
3
What is the output of the following code
snippet?
Integer int_data = new Integer(10);
System.out.print(int_data.byteValue());
System.out.print("-");
int int_data_2 = new Integer(10);
System.out.print(int_data_2.byteValue());
10-10
1010-1010
Does not compile
Run-time error
What line in this code snippet will give
compilation error?
double d1 = 5f; // c1
double d2 = 5.0; // c2
float f1 = 5f; // c3
float f2 = 5.0; // c4
c1
c2
c3
c4
What is the output of the following code
snippet?
public static void main(String... args) {
String car, bus = "petrol";
car = car + bus;
System.out.println(car);
}
petrol
petrolpetrol
Compilation error
Runtime error
Answer: Compilation error
C1
C2
C3
C4
What is the output of the following code
snippet?
public class Test {
public static void main(String[] args) {
for(int i=0; 0; i++) {
System.out.println("Hello World!");
}
}
}
Hello World!
no output
Compilation error
Runtime error
What is the output of the following code
snippet?
public class Code {
public static void main(String[] args) {
for(int i = 0; i < 1; i++) {
System.out.println(i+' ');
}
}
}
1
0
32
50
What is the output of the following code
snippet?
public class Code {
public static void main(String[] args)
{
if (true)
break;
}
}
No output
0
Compilation error
Runtime error
What is the output of the following code
snippet?
public class Code {
public static void main(String[] args)
{
int $_ = 5;
}
}
Java
Copy
No output
0
Compilation error
Runtime error
Answer: No output
}
public static void main(String arr){
}
}
Java
Copy
No output
0
Compilation error
Runtime error
Answer: No output
java
32
418
Compilation error
Answer: 418
if(num1 == num2){
System.out.println(0);
}
else{
System.out.println(1);
}
}
}
Java
Copy
0
1
Compilation error
Runtime error
Answer: 1
Object method
String method
Compilation error
Runtime error
Answer: String method
0
1
2
3
Answer: 0
class Code {
public static void main(String args[]) {
System.out.println(value());
}
static int value() {
return 1;
}
}
Java
Copy
class Main {
public static void PrintMain(Parent o)
{
o.Print();
}
public static void main(String[] args)
{
Parent x = new Parent();
Parent y = new Child();
Child z = new Child();
PrintMain(x);
PrintMain(y);
PrintMain(z);
}
}
Java
Copy
Object method
String method
Integer method
Compilation error
Answer: Compilation error
8
9
Compilation error
Runtime error
Answer: Compilaton error
8
9
Compilation error
Runtime error
Answer: Runtime error
void go()
{
type = "B ";
System.out.print(this.type + super.type);
}
CodeA CodeB A B
CodeA CodeA B B
CodeA CodeB B B
CodeB CodeB B B
Answer: CodeA CodeB B B
true true
false false
false true
true false
Answer: false true