Java Code Snippets Questions

Java Code Snippets Questions : Candidate who is interested to understand java code snippet based question then this article is for you . This article is all about the Java Snippets based Interview Questions. Question dump is prepared by those expert or professional who are already working in this area from long time.

In the below section, we have posted all of the Java Snippets that may help you at the time of interview for any private company or organization. We suggest you to refer each and every question before facing your interview.

Java Code Snippets Questions 2021 :- Complete List

Question 1. What is the result for the below code snippet:

public static void main(String[] args) {
Object obj =new int[] { 1,2,3 };
int[] someArray = (int[])obj;
for (int i: someArray) System.out.print(i +“ “)
}

Options:

A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.

Answer: Compilation fails because of an error in line 12.

Question 2. What is the result?

public class A {
public String doit(int x, int y) {
return “a”;
}

public String doit(int… vals) {
return “b”;
}
}
Given:
A a=new A();
System.out.println(a.doit(4, 5));
Options :
A. Line 26 prints “a” to System.out.
B. Line 26 prints ‘b” to System.out.
C. An exception is thrown at line 26 at runtime.
D. Compilation of class A will fail due to an error in line 6.

Answer: A

Question 3. What is the result?

Given:
public class A {
public void doit() {
}
public String doit() {
return “a”;
}
public double doit(int x) {
return 1.0;
}
}
Options:
A. An exception is thrown at runtime.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 4.
D. Compilation succeeds and no runtime errors with class A occur.

Answer: C

Question 4. What code, should be inserted to create an instance of the Point class
defined in Line?

class Line {
public static class Point { }
}

class Triangle {
// insert code here
}

Options :
A. Point p = new Point();
B. Line.Point p = new Line.Point();
C. The Point class cannot be instatiated at line 15.
D. Line 1 = new Line() ; 1.Point p = new 1.Point();

Answer: B

Question 5. What is the result when this code is executed?

class One {
public One() { System.out.print(1); }
}
class Two extends One {
public Two() { System.out.print(2); }
}
class Three extends Two {
public Three() { System.out.print(3); }
}
public class Numbers{
public static void main( String[] argv) { new Three(); }
}
Options
A. 1
B. 3
C. 123
D. 321
E. The code rims with no output.

Answer: D

Question 6. What is the result when method testIfA is invoked?

public void testIfA() {
if(testIfB(”True”)) {
System.out.println(”True”);
} else {
System.out.println(”Not true”);
}
}
public Boolean testIfB(String str) {
return Boolean.valueOf(str);
}
Options
A. True
B. Not true
C. An exception is thrown at runtime.
D. Compilation fails because of an error at line 12.
E. Compilation fails because of an error at line 19.

Answer: A

Question 7. What is the result of the below snippet?

public static void main(String[] args) {
Integer i = uew Integer(1) + new Integer(2);
switch(i) {
case 3: System.out.println(”three”); break;
default: System.out.println(”other”); break;
}
}
Options:
A. three
B. other
C. An exception is thrown at runtime.
D. Compilation fails because of an error on line 12.
E. Compilation fails because of an error on line 13.
F. Compilation fails because of an error on line 15.

Answer: A

Question 8. What is the result?

public static void main(String[] args) {
String str = “null’;
if (str == null) {
System.out.println(”null”);
} else (str.length() == 0) {
System.out.println(”zero”);
} else {
System.out.println(”some”);
}
}
Options:
A. null
B. zero
C. some
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D

Question 9. What is the result?

Float pi = new Float(3.14f);
if(pi>3) {
System.out.print(”pi is bigger than 3. “);
}
else {
System.out.print(”pi is not bigger than 3. “);
}
finally {
System.out.println(”Have a nice day.”);
}
options:
A. Compilation fails.
B. pi is bigger than 3.
C. An exception occurs at runtime.
D. pi is bigger than 3. Have a nice day.
E. pi is not bigger than 3. Have a nice day.

Answer: A

Question 10. What is the result of below code?

int x=0;
int y 10;
do {
y–;
++x;
} while (x < 5);
System.out.print(x + “,“ + y);
Options:
A. 5,6
B. 5,5
C. 6,5
D. 6,6

Answer: B

Multiple Choice Questions from Other Front-End Technologies

Check MCQ Questions from HTML, XML, Android – Click Here

Leave a Comment