Donate : Link
Medium Blog : Link
Applications : Link
1. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
String str1 = "CODE";
String str2 = "FACTORY";
String str3 = str1 + str2;
String str4 = "CODEFACTORY";
System.out.println(str3 == str4);
}
}
- True
- False ✓
- -1
- 0
- RuntimeException
2. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
int x=0;
if(x) {
System.out.println("if block");
} else {
System.out.println("else block");
}
}
}
- Compilation Error ✓
- false
- if block
- else block
3. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
boolean b1 = false;
boolean b2 = false;
if(b2 != b1 == !b2) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
- true
- false ✓
- Compilation error
- non of the given options
4. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
static Boolean b[] = new Boolean[5];
public static void main(String[] args) {
System.out.println(b[0]);
}
}
- null ✓
- false
- true
- Compilation will fail
5. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory extends Child {
public static void main(String[] args) {
CodeFactory codeFactory = new CodeFactory();
}
}
class Child extends CodeFactory {
}
- The code will compile but will give exception to “cyclic inheritance involving CodeFactory” at runtime
- The code will give error to “cyclic inheritance involving CodeFactory” at compile time ✓
- The code will compile and run w/o a problem
- The code will give an EceptionInitializeError at runtime
6. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
B obj = new B();
}
}
class A {
final void math() {
System.out.println("Final method");
}
}
class B extends A {
void math() {
System.out.println(" Welcome");
}
}
- Final Method
Welcome - Welcome
- Final method
- Compilation error ✓
7. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
new CodeFactory().display(null);
}
void display(Object obj) {
System.out.println("Object");
}
void display(String str) {
System.out.println("String");
}
}
- NullPointerException at runtime
- Object
- String ✓
- Compile time exception
8. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
Parent p = new Child();
p.test();
}
}
class Parent {
static void test() {
System.out.println("Parent");
}
}
class Child extends Parent {
static void test() {
System.out.println("Parent");
}
}
- Parent ✓
- Child
- Parent
Child - Child
Parent
9. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
float f1 = 10.20f;
float f2 = 10.20f;
System.out.println(f1 == f2);
}
}
- Compilation error
- Runtime Exception
- true ✓
- false
10. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
int a[] = {10, 20, 3, };
System.out.println(a.length);
}
}
- 4
- 3 ✓
- NullPointerException
- ArrayIndexOutOfBoundException
11. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
int x = 12;
public static void main(String[] args) {
CodeFactory cf = new CodeFactory();
cf.method(5);
System.out.println(cf.x);
}
void method(int x) {
x += x;
System.out.println(x);
}
}
- 17
17 - 17
12 - 10 ✓
12 - 24
17 - 24
10
12. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
new CodeFactory().get(10);
}
void get(long l) {
System.out.println("Inside long");
}
void get(double d) {
System.out.println("Inside double");
}
}
- Inside long ✓
- Compile time error
- Runtime error
- Inside double
13. What will be the output of the following code snippet?
package com.example.java.programming.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CodeFactory {
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(5);
list.add(4);
System.out.println(Collections.binarySearch(list, 4));
}
}
- 4
- 5
- Compilation will fail
- -4 ✓
14. What will be the output of the following code snippet?
package com.example.java.programming.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CodeFactory {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(100);
list.add(1);
list.add(1000);
list.add(10);
list.add(10000);
list.add(10);
NavigableSet<Integer> N = new TreeSet<>(list);
System.out.println(N.tailSet(10, false));
System.out.println(N.tailSet(10));
System.out.println(N.higher(10));
int i = N.pollFirst();
System.out.println(N.size());
}
}
- [100, 1000, 10000] ✓
[10, 100, 1000, 10000]
100
4 - [100, 1000, 10000]
[10, 100, 1000, 10000]
[10, 100, 1000, 10000]
5 - Compilation error
- [10, 100, 1000, 10000]
[10, 100, 1000, 10000]
[10, 100, 1000, 10000]
4
15. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
Thread t = new MyThread();
t.run();
t.start();
System.out.println("Main Thread");
}
}
class MyThread extends Thread {
public void run() {
System.out.println("MyThread");
}
}
- The output will contain only “Main Thread”
- The output will contain one “My Thread” and one “Main Thread”
- The output will contain only “My Thread”
- The output will contain one “Main Thread” and two “My Thread” ✓
16. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
int i = new CodeFactory().test();
System.out.println("Value of i: " + i);
}
int test() {
int i=0;
try {
i = 100 / 0;
return i;
} catch(Exception e) {
i = 200;
return i;
} finally {
i = 300;
// If we comment below return line
// then output will be 200
return i;
}
}
}
- Compile will fail
- RuntimeException
- Value of i: 300 ✓
- Value of i: 200
17. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
void check() throws CustomException { //line 2
System.out.println("check");
}
public static void main(String[] args) {
CodeFactory cf = new CodeFactory();
cf.check(); // line 7
}
}
class CustomException extends Exception {
CustomException() {
super("Custom Exception");
}
}
- check
- Compile time exception in line 2, as it is throwing CustomException
- Compile time exception in line 7, as the code should be included in a try-catch block ✓
- Custom exdeption will be thrown at runtime
18. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
int i = 4;
int ia[][][] = new int[i][i = 3][i];
System.out.println(ia.length + ", " + ia[0].length + ", " + ia[0][0].length);
}
}
- 3, 4, 3
- 4, 3, 3 ✓
- 3, 3, 4
- Compilation will fail
19. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
int []a[], b;
a = new int[5][];
b = new int[2];
System.out.println(a[0].length);
}
}
- Compilation error
- NullPointerException ✓
- ArrayIndexOutOfBoundException
- 0
- 5
Note: a consider as 2D array and b consider as 1D array.int []a[], b = int[] a[], b
So if we declare any variable then it is by default 1D array. and if we declare 1D array then it is 2D array.
20. What will be the output of the following code snippet?
package com.example.java.programming.test;
import java.io.Serializable;
public class Employee implements Serializable {
String firstname;
String lastname;
int empId;
static String contact;
Employee(String firstname, String lastname, int empId, String contact) {
this.firstname = firstname;
this.lastname = lastname;
this.empId = empId;
}
}
----------------------------
package com.example.java.programming.test;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class CodeFactory {
public static void main(String[] args) {
try {
Employee emp = new Employee("Abc", "Xyz", 123, "000");
FileOutputStream fileOS = new FileOutputStream("temp.ser");
ObjectOutputStream objectOS = new ObjectOutputStream(fileOS);
objectOS.writeObject(emp);
objectOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Runtime error since static variable is used during serialization
- Compile time error
- An emptyfile temp.ser will be created at the given path
- A temp.ser file will be created and will have Employee object details along with some random text ✓
21. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
int y[] = new int[2];
int a, []b;
b = y;
System.out.println(b[2]);
}
}
- Compilation error ✓
- ArrayIndexOutOfBoundException
- NullPointerEception
- 0
Note: declaration is not allow. we can declare like int a, b[].
22. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
float f[] = new float[5];
Object obj = f;
System.out.println(f[0]);
}
}
- Compilation error
- Runtime Exception
- 0.0 ✓
- 0
- 5
23. What will be the output of the following code snippet?
package com.example.java.programming.test;
interface T1 {
void m1() {};
}
interface T2 {
void m2();
}
class A implements T1, T2 {
public void m1() {
System.out.println("m1");
}
public void m2() {
System.out.println("m2");
}
}
public class CodeFactory {
public static void main(String[] args) {
A a = new A();
a.m2();
}
}
- m1
- m2
- m1
m2 - Null
- Compilation error ✓
24. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
int CodeFactory(int i) { // line 4
System.out.println("Test2");
return i;
}
CodeFactory() {
System.out.println("Test1");
}
public static void main(String[] args) {
CodeFactory cf = new CodeFactory(5); // line 14
}
}
- Test2
- Test1
- Compile time execution in line 4, as a constructor cannot return any value
- Compile time execution in line 14, as a parametized constructor is not present ✓
25. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory implements Runnable {
int x, y;
public void run() {
for(int i=0; i<10; i++)
synchronized(this) {
x = 12;
y = 12;
}
System.out.print(x + " " + y + " ");
}
public static void main(String[] args) {
CodeFactory cd = new CodeFactory();
Thread t1 = new Thread(cd);
Thread t2 = new Thread(cd);
t1.start();
t2.start();
}
}
- Deadlock will occur
- It will print 12 12 12 12 ✓
- Compilation error
- It will print 12 12
26. Which of the given statements when insrted at line 12 in the following code snippet creates an instance of ‘Bar’?
package com.example.java.programming.test;
class Foo {
class Bar {
}
}
public class CodeFactory {
public static void main(String[] args) {
Foo f = new Foo();
/* Line 12: Missing Statement ? */
}
}
- Foo.Bar b = new Foo.Bar();
- Foo.Bar b = f.new Bar(); ✓
- Bar b = new f.Bar();
- Bar b = new Bar();
27. What will be the output of the following code snippet?
package com.example.java.programming.test;
class Parent {
Parent() {
System.out.println("Parent Construction");
}
}
public class CodeFactory extends Parent {
CodeFactory(int i) {
System.out.println("Child constructor");
}
public static void main(String[] args) {
Parent p = new CodeFactory(3);
}
}
- Parent Construction ✓
Child Constructor - Child Constructor
Parent Construction - Parent Construction
- Child Constructor
28. What will be the output of the following code snippet?
package com.example.java.programming.test;
abstract class Fruit {
abstract void FruitSize();
public void FruitWeight() {
System.out.println("FruitWeight");
FruitSize();
}
}
class SweetFruit extends Fruit {
@Override
public void FruitSize() {
System.out.println("FruitSize");
}
}
public class CodeFactory {
public static void main(String[] args) {
SweetFruit sweetFruit = new SweetFruit();
sweetFruit.FruitSize();
sweetFruit.FruitWeight();
}
}
- Error in code
- Code is incomplete
- FruitSize
FruitWeight - FruitSize ✓
FruitWeight
FruitSize
29. What will be the output of the following code snippet?
package com.example.java.programming.test;
class VanillaCake {
public static void VanillaCake() {}
public static void frosting() {
System.out.println("In the frosting method of VanillaCake");
}
public static void baking() {
System.out.println("In the baking method of VanillaCake");
}
}
class WeddingCake extends VanillaCake {
public static void WeddingCake() {}
public static void putWeddingDollOnCake() {
System.out.println("Putting Doll on WeddingCake");
}
}
public class CodeFactory {
public static void main(String[] args) {
VanillaCake vanCake = new VanillaCake();
WeddingCake wedCake = new WeddingCake();
vanCake.baking();
wedCake.baking();
}
}
- In the baking method of VanillaCake ✓
In the baking method of VanillaCake - Error in code
- No output will be displayed
- In the baking method of VanillaCake
30. What will be the output of the following code snippet?
package com.example.java.programming.test;
class OuterClass {
int x = 0;
class InnerClass {
int x = 1;
void test(int x) {
System.out.println("value of x1 = " + x);
System.out.println("value of x2 = " + this.x);
System.out.println("value of x3 = " + OuterClass.this.x);
}
}
}
public class CodeFactory {
public static void main(String[] args) {
OuterClass oc = new OuterClass();
OuterClass.InnerClass ic = oc.new InnerClass();
ic.test(30);
}
}
- value of x1 = 30 ✓
value of x2 = 1
value of x3 = 0 - value of x1 = 1
value of x2 = 0
value of x3 = 30 - value of x1 = 0
value of x2 = 30
value of x3 = 1 - value of x1 = 30
value of x2 = 0
value of x3 = 1
31. What will be the output of the following code snippet?
package com.example.java.programming.test;
public class CodeFactory {
public static void main(String[] args) {
System.out.println(SomeFunction(1234));
}
static int SomeFunction(int n) {
int result = 0;
while(n>0) {
result += n%10;
n /= 10;
}
return result;
}
}
- 10 ✓
- 4
- 1234
- 4321
32. What will be the output of the following code snippet?
package com.example.java.programming.test;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class CodeFactory {
public static void main(String[] args) {
Set s = new TreeSet();
s.add(null);
s.add(5);
Iterator iter = s.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
}
}
- null
5 - 5
- RuntimeException, as null cannot be added into a HashSet
- NullPointerEception at runtime ✓
Note: TreeSet not allow null value. TreeSet internally use NavigableMap which does not allow any null key or value.
33. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static int temp1 = 1;
private static int temp2 = 2;
public int temp3 = 3;
private int temp4 = 4;
public class inner {
private int getSum() { // line 11
return (temp1 + temp2); // line 12
}
}
public static void main(String[] args) {
Test.inner obj = new Test().new inner(); // line 17
System.out.println(obj.getSum()); // line 18
}
}
- Compile time issue at line 11
- Compile time issue at line 12
- Compile time issue at line 17
- Compile time issue at line 18
- Compile and Runs fine, prints 3 ✓
34. Threads – Deadlock detection
It possible to implement a deadlock detection, when
- all exclusive locks are monitored and modelled as a directed UnSorted Tree
- all exclusive locks are monitored and modelled as a directed Sorted Tree
- all exclusive locks are monitored and modelled as a directed graph ✓
- all exclusive locks are monitored and modelled as a directed Circular Queue
35. Streams API
Streams API is available in which package of Java 8?
- java.io.streams
- java.io.stream
- java.util.streams
- java.util.stream ✓
36. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
System.out.println(1 +
2 ==+
3 +
+
+ 5);
}
}
- Compilation Error
- 8
- 11
- false ✓
- true
37. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
int p=5;
System.out.println("Hello");
5<6?5:6;
}
}
- Hello 5
- Hello 6
- Hello
- Compilation error ✓
38. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static int temp1 = 1;
private static int temp2 = 2;
public int temp3 = 3;
private int temp4 = 4;
public static class Inner {
private int getSum() { // line 11
return (temp1 + temp2 + temp3 + temp4); // line 12
}
}
public static void main(String[] args) {
Test.Inner obj = new Test().new Inner(); // line 17
System.out.println(obj.getSum()); // line 18
}
}
- Compiler Error at Line 11 ✓
- Compiler Error at Line 12
- Compiler Error at Line 17 ✓
- Compiler Error at Line 18
- Compiles and Runs fine, Prints 10
39. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
System.out.println("Output: " + s1 == s2);
}
}
- Compilation error
- Output: true
- Output: false
- false ✓
- true
40. What will be the output of the following code snippet?
package com.codeFactory.test;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList obj = new ArrayList<>();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add(1, "D");
System.out.println(obj);
}
}
- A D C
- A B D
- A B C
- A, D, B, C ✓
- A, B, C, D
41. Design pattern
String implementation follows which of the below design pattern
- Decorator design pattern
- FlyWeight design pattern ✓
- Immutability design pattern
- Factory design pattern
42. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
int x = -4;
System.out.print(x >> 1);
System.out.print(" AND ");
int y = 4;
System.out.println(y >> 2);
}
}
- Compiler error: Operator >> cannot be applied to negative numbers
- 2 and 1
- -2 and 2
- -2 and 1 ✓
43. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
String temp = "10.87";
int a = Integer.parseInt(temp);
System.out.println(a);
}
}
- 10
- 11
- Runtime Error ✓
- Compilation Error
44. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
System.out.println(1 +
2 ==+
3 +
+
+ 5 +
++6 +
--7);
}
}
- 11
- 12
- 10
- Compilation Error ✓
45. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
int b = 96;
System.out.println(b >> 4);
System.out.println(b >>> 4);
}
}
- 6 6 ✓
- 24 24
- 6 24
- 24 6
46. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
int a = 260;
byte b = (byte)a;
System.out.println(b);
}
}
- 260
- 0
- -1
- 4 ✓
- None of the above
47. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
int x = 10;
for(int i = 10; i>=x; i--)
int x = 10 - i;
System.out.println(x);
}
}
- 10
- 5
- 4
- Compilation Error ✓
48. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
int x = 0;
int y = 0;
for(int z = 0; z < 5; z++) {
if((++x > 2) || (++y > 2)) {
x++;
}
}
System.out.println(x + " " + y);
}
}
- 6 2
- 8 2 ✓
- 5 2
- 7 2
49. What will be the output of the following code snippet?
package com.codeFactory.test;
public class FinalParameter {
double attribute = 8;
final int finalAttribute = 9;
public void foo(final int finalAttribute) {
this.finalAttribute = finalAttribute; // line 8
}
public void foo(final double attribute) {
this.attribute = attribute; // line 12
}
public void foo(final FinalParameter finalParameter) {
finalParameter.attribute = 10.0; // line 16
finalParameter.finalAttribute = 11; // line 17
}
public static void main(String[] args) {
FinalParameter finalParameter = new FinalParameter();
finalParameter.foo(8.0);
finalParameter.foo(9);
finalParameter.foo(finalParameter); // line 24
}
}
- Compilation error at line 8 ✓
- Compilation error at line 12
- Compilation error at line 16
- Compilation error at line 17 ✓
- Compilation error at line 24
- Compiles and runs fine
50. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static void main(String[] args) {
System.out.println(1 * 2 + 3 ^ 4 / 5);
// ^ -> XOR - Sets each bit to 1 iff only one of the two bits is 1
}
}
- 2
- 3
- 4
- 5 ✓
- Compilation error
51. What will be the output of the following code snippet?
package com.codeFactory.test;
public class Test {
public static int temp1 = 1;
private static int temp2 = 2;
private int temp4 = 4;
public class Inner {
private int temp5 = 5; // line 9
private int getSumof2() {
return (temp1 + temp2);
}
int getSumof4() {
return (temp1 + temp2 + temp4); // line 16
}
public int getSumof5() {
return (temp1 + temp2 + temp5); // line 20
}
}
public static void main(String[] args) {
Test.Inner obj = new Test.Inner(); // line 25
System.out.println(obj.getSumof2());
System.out.println(obj.getSumof4());
System.out.println(obj.getSumof5());
}
}
- Compile issue at line 9
- Compile issue at line 16
- Compile issue at line 20
- Compile issue at line 25 ✓
- Compiles and runs fine
52. What will be the output of the following code snippet?
package com.example.java.programming;
public class Test {
public static void main(String... strings) throws Exception {
int a1 = 1;
Integer a2 = 1;
System.out.println(a1 == a2);
System.out.println(a2.compareTo(a1));
}
}
- true ✓
0 - false
0 - true
1 - false
1
53. What will be the output of the following code snippet?
public class Test {
public static void main(String... strings) throws Exception {
int result = 0;
Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("tRuE");
Boolean b4 = new Boolean("false");
if (b1 == b2) result = 1; // false
if (b1.equals(b2)) result = result + 10; // true
if (b2 == b4) result = result + 100; // false
if (b2.equals(b4)) result = result + 1000; // false
if (b2.equals(b3)) result = result + 10000; // true
System.out.println(result);
}
}
- 10
- 1
- 0
- 10010 ✓
54. How many objects are eligible for garbage collection after execution of line 8?
public class Test
{
public static void main(String [] args)
{
Test t1 = new Test();
Test t2 = m1(t1); // line 6
Test t3 = new Test();
t2 = t3; // line 8
}
static Test m1(Test temp)
{
temp = new Test();
return temp;
}
}
- 0
- 1 ✓
- 2
- 3
Explanation: By the time line 8 has executed, the only object without a reference is the one generated i.e as a result of line 6.
55. What will be the output of the following code snippet?
public class Test extends Thread implements Runnable {
public void run() {
System.out.println("RUN");
}
public static void main(String... strings) {
Test obj = new Test();
obj.run();
obj.start();
}
}
- Runtime Error
- Compilation Error
- RUN RUN ✓
- None of the above
56. What will be the output of the following code snippet?
public class Test {
public static void main(String... strings) {
TreeSet t = new TreeSet();
t.add("3");
t.add("9");
t.add("1");
t.add("4");
t.add("8");
System.out.println(t);
}
}
- [1, 3, 5, 8, 9]
- [3, 4, 1, 8, 9]
- [9, 8, 4, 3, 1]
- [1, 3, 4, 8, 9] ✓
57. What will be the output of the following code snippet?
Object[] names = new String[3];
names[3] = new Integer(0);
- ArrayIndexOutOfBoundsException
- ArrayStoreException ✓
- Compilation Error
- Code runs successfully
What will be the output of the following code snippet?

