0% found this document useful (0 votes)
2 views

Java Interview Questions

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Interview Questions

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

×

Java Interview Questions: with detailed answers Book


What is the output of the following code
snippet?
String og = "opengenus";
String og2 = new String(og);

System.out.println((og==og2) + " "+ (og.equals(og2)));

 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?

public static void main(String[] fruits) {


String str1 = new String("open");
String str2 = new String("source");
String str2 = new String("opengenus");
str3 = str1;
str2 = str3;
str1 = str2;
}

 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

Which code statement give compilation


error?
double num1, int num2 = 1; // C1
int num1, num2; // C2
int num1, num2 = 1; // C3
int num1 = 2, num2 = 1; // C4

 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

In Java, an identifier can start with an alphabet, underscore ( _) or


dollar sign ($). Hence, $_ is a valid identifier/ variable in this case.
What is the output of the following code
snippet?
public class Code{
public static void main(String[] arr){

}
public static void main(String arr){
}
}
Java
Copy

 No output
 0
 Compilation error
 Runtime error
Answer: No output

main() function can be overloaded in Java. The main() function


that has String[] will be the entry point and will be called by Java.

What is the output of the following code


snippet?
public class Code {
public static void main(String[] args)
{
System.out.println('j' + 'a' + 'v' + 'a');
}
}
Java
Copy

 java
 32
 418
 Compilation error
Answer: 418

As each character is enclosed in single quotes, it is considered as


a character and not a string by Java. Hence, the concatenation
will result in a character. As the string "java" cannot fit in a
character, the characters are converted to ASCII value before
concatenation that is addition.

106 + 97 + 118 + 97 = 418

What is the output of the following code


snippet?
public class Code{
public static void main(String[] arr){
Integer num1 = 400;
Integer num2 = 400;

if(num1 == num2){
System.out.println(0);
}
else{
System.out.println(1);
}
}
}
Java
Copy

 0
 1
 Compilation error
 Runtime error
Answer: 1

Integer class support the range of -128 to 127. If the number is


within the range, autoboxing is applied. This means the same
reference is assigned for the same number as they are from the
same pool. As 400 is outside the range, different references are
assigned.

What is the output of the following code


snippet?
public class Code {
public static void main(String[] args) {
method(null);
}
public static void method(Object o) {
System.out.println("Object method");
}
public static void method(String s) {
System.out.println("String method");
}
}
Java
Copy

 Object method
 String method
 Compilation error
 Runtime error
Answer: String method

Null is not an object in Java.

Java compiler prefer the method which has more specific


parameters.
String is object of the class java.lang.String. Hence, String is more
specific than Object class. Therefore, null is matched as a string
object.

What is the output of the following code


snippet?
class Code {
String args[] = { "1", "2" };
public static void main(String args[]) {
System.out.println(args.length);
}
}
Java
Copy

 0
 1
 2
 3
Answer: 0

args is the command line arguments and is not related to the


global variable args. So, if you run the above code without
passing any command line argument, you will get the output as 0.

What is the output of the following code


snippet?
class Code {
public static void main(String args[]) {
System.out.println(value());
}
int value() {
return 1;
}
}
Java
Copy
 0
 1
 Compilation error
 Runtime error
Answer: Compilaton error

Following is the compilation error on compiling the above code:

opegenus.java:3: error: non-static method value() cannot be referenced from


a static context
System.out.println(value());
^
1 error

The problem is that in Java, we cannot call a non-static function


from a static function. Therefore, the fix is to make the function
value() static.

The correct Java code is:

class Code {
public static void main(String args[]) {
System.out.println(value());
}
static int value() {
return 1;
}
}
Java
Copy

What is the output of the following code


snippet?
class Code {
public static void main(String args[]) {
System.out.println(value());
}
static int value() {
static int data = 0;
return data;
}
}
Java
Copy
 0
 1
 Compilation error
 Runtime error
Answer: Compilaton error

Following is the compilation error on compiling the above code:

opengenus.java:6: error: illegal start of expression


static int data = 0;
^
opengenus.java:7: error: illegal start of type
return data;
^
opengenus.java:7: error: <identifier> expected
return data;
^
opengenus.java:9: error: class, interface, or enum expected
}
^
4 errors

The problem is that in Java, we cannot have static local variables.


The alternative is to use static class members.

The fix will be to change "data" variable to be non-static or make


"data" variable a static class member.

What is the output of the following code


snippet?
class Parent {
public void Print()
{
System.out.println("Parent");
}
}

class Child extends Parent {


public void Print()
{
System.out.println("Child");
}
}

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

 Child, Child, Child


 Parent, Child, Child
 Parent, Child, Parent
 Parent, Parent, Child
Answer: Parent, Child, Child

The first print statement prints "Parent" as the parent reference is


passed to it.

The second print statement prints "Child" because of run time


polymorphism which is enabled in Java by default.

The third print statement is passed the reference of Child and


hence, the print statement of Child is called. Note that the
concept of object slicing which is present in C++ is not valid
in Java.

What is the output of the following code


snippet?
public class Code {
public static void main(String[] args) {
method(null);
}
public static void method(Object o) {
System.out.println("Object method");
}
public static void method(Integer i) {
System.out.println("Integer method");
}
public static void method(String s) {
System.out.println("String method");
}
}
Java
Copy

 Object method
 String method
 Integer method
 Compilation error
Answer: Compilation error

The code gives the following compilation error:

opengenus.java:3: error: reference to method is ambiguous


method(null);
^
both method method(Integer) in Code and method method(String) in Code
match
1 error

Null is not an object in Java.

Java compiler prefer the method which has more specific


parameters.
String is object of the class java.lang.String. Hence, String is more
specific than Object class but it is equally specific to Integer class.
Therefore, null is unable to match to any function as it is unable
to choose between String and Integer.

What is the output of the following code


snippet?
public class Code
{
public static void main(String args[])
{
StringBuffer str1 = new StringBuffer("open");
StringBuffer str2 = str1;
str1.append("genus");
System.out.println(str1 + " " + str2 + " " + (str1 == str2));
}
}
Java
Copy

 opengenus open false


 opengenus opengenus false
 opengenus open true
 opengenus opengenus true
Output: opengenus opengenus true

This is because StringBuffer objects are mutable. str2 is pointing


to str1 object and not a copy of str1. So, when str1 is modified,
str2 points to it so its value is also modified.

If we replace StringBuffer with String, output will be "opengenus


open false" as String objects are not mutable and new objects are
created when we modified them.

What is the output of the following code


snippet?
public class Code
{
public static void main(String args[])
{
int y = 08;
y = y + 2;
System.out.println(y);
}
}
Java
Copy

 8
 9
 Compilation error
 Runtime error
Answer: Compilaton error

The code will give the following compilation error:

opengenus.java:5: error: integer number too large


int y = 08;
^
1 error

Any number starting with 0 is considered an octal number which


has digits from 0 to 7. So, 08 is invalid and hence, the code will
fail to compile.

What is the output of the following code


snippet?
class Code extends Thread
{
public void run()
{
System.out.print("thread 1");
}
public static void main(String args[])
{
Code thread1 = new Code();
thread1.start();
thread1.stop();
thread1.start();
}
}
Java
Copy

 8
 9
 Compilation error
 Runtime error
Answer: Runtime error

The code will give the following runtime error:

Exception in thread "main" java.lang.IllegalThreadStateException at


java.lang.Thread.start

The issue is that a thread cannot be started twice.

What is the output of the following code


snippet?
class CodeA
{
public String type = "A ";
public CodeA() {
System.out.print("CodeA ");
}
}

public class CodeB extends CodeA


{
public CodeB() {
System.out.print("CodeB ");
}

void go()
{
type = "B ";
System.out.print(this.type + super.type);
}

public static void main(String[] args)


{
new CodeB().go();
}
}
Java
Copy

 CodeA CodeB A B
 CodeA CodeA B B
 CodeA CodeB B B
 CodeB CodeB B B
Answer: CodeA CodeB B B

CodeB().go() executes in two phases:

 CodeB class constructor is called followed by CodeA class


constructor as CodeB extends CodeA.
 go() function is called on CodeB object which overrides the
variable "type" to B and hence, the same value "B" is printed
twice. In this code snippet, super keyword is not playing any role.
What is the output of the following code
snippet?
public class Code
{
public static void main(String[] args)
{
Integer a = 128, b = 128;
System.out.println(a == b);

Integer c = 100, d = 100;


System.out.println(c == d);
}
}
Java
Copy

 true true
 false false
 false true
 true false
Answer: false true

In the function valueOf() in Integer, the range is -128


(IntegerCache.low) to 127 (IntegerCache.high) so numbers
outside this range will not give expected result. Therefore, the
objects will value 100 equates to be equal.

With this article at OpenGenus, you must have a strong


preparation by practicing Java Interview Questions.

© 2024 All rights reserved ™


OpenGenus IQ
Contact - Email: [email protected]
Primary Address: JR Shinjuku Miraina Tower, Tokyo, Shinjuku 160-
0022, JP
Office #2: Commercial Complex D4, Delhi, Delhi 110017, IN Top
PostsLinkedInTwitter
Android App
Apply for Internship
This site uses Google AdSense ad intent links. AdSense automatically generates these links and
they may help creators earn money.

You might also like