Java Test Coding Interview Questions 1 | Code Factory


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);
	}
}
  1. True
  2. False
  3. -1
  4. 0
  5. 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");
		}
	}
}
  1. Compilation Error
  2. false
  3. if block
  4. 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");
		}
	}
}
  1. true
  2. false
  3. Compilation error
  4. 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]);
		
	}
}
  1. null
  2. false
  3. true
  4. 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 {
	
}
  1. The code will compile but will give exception to “cyclic inheritance involving CodeFactory” at runtime
  2. The code will give error to “cyclic inheritance involving CodeFactory” at compile time
  3. The code will compile and run w/o a problem
  4. 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");
	}
}
  1. Final Method
    Welcome
  2. Welcome
  3. Final method
  4. 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");
	}
}
  1. NullPointerException at runtime
  2. Object
  3. String
  4. 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");
	}
}
  1. Parent
  2. Child
  3. Parent
    Child
  4. 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);
	}
}
  1. Compilation error
  2. Runtime Exception
  3. true
  4. 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);
	}
}
  1. 4
  2. 3
  3. NullPointerException
  4. 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);
	}
}
  1. 17
    17
  2. 17
    12
  3. 10
    12
  4. 24
    17
  5. 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");
	}
}
  1. Inside long
  2. Compile time error
  3. Runtime error
  4. 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));
	}
}
  1. 4
  2. 5
  3. Compilation will fail
  4. -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());
	}
}
  1. [100, 1000, 10000]
    [10, 100, 1000, 10000]
    100
    4
  2. [100, 1000, 10000]
    [10, 100, 1000, 10000]
    [10, 100, 1000, 10000]
    5
  3. Compilation error
  4. [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");
	}
}
  1. The output will contain only “Main Thread”
  2. The output will contain one “My Thread” and one “Main Thread”
  3. The output will contain only “My Thread”
  4. 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;
		}
	}
}
  1. Compile will fail
  2. RuntimeException
  3. Value of i: 300
  4. 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");
	}
}
  1. check
  2. Compile time exception in line 2, as it is throwing CustomException
  3. Compile time exception in line 7, as the code should be included in a try-catch block
  4. 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);
	}
}
  1. 3, 4, 3
  2. 4, 3, 3
  3. 3, 3, 4
  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);
	}
}
  1. Compilation error
  2. NullPointerException
  3. ArrayIndexOutOfBoundException
  4. 0
  5. 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();
		}
	}
}
  1. Runtime error since static variable is used during serialization
  2. Compile time error
  3. An emptyfile temp.ser will be created at the given path
  4. 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]);
	}
}
  1. Compilation error
  2. ArrayIndexOutOfBoundException
  3. NullPointerEception
  4. 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]);
	}
}
  1. Compilation error
  2. Runtime Exception
  3. 0.0
  4. 0
  5. 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();
	}
}
  1. m1
  2. m2
  3. m1
    m2
  4. Null
  5. 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
	}
}
  1. Test2
  2. Test1
  3. Compile time execution in line 4, as a constructor cannot return any value
  4. 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();
	}
}
  1. Deadlock will occur
  2. It will print 12 12 12 12
  3. Compilation error
  4. 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 ? */
	}
}
  1. Foo.Bar b = new Foo.Bar();
  2. Foo.Bar b = f.new Bar();
  3. Bar b = new f.Bar();
  4. 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);
	}
}
  1. Parent Construction ✓
    Child Constructor
  2. Child Constructor
    Parent Construction
  3. Parent Construction
  4. 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();
	}
}
  1. Error in code
  2. Code is incomplete
  3. FruitSize
    FruitWeight
  4. 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();
	}
}
  1. In the baking method of VanillaCake ✓
    In the baking method of VanillaCake
  2. Error in code
  3. No output will be displayed
  4. 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);
	}
}
  1. value of x1 = 30 ✓
    value of x2 = 1
    value of x3 = 0
  2. value of x1 = 1
    value of x2 = 0
    value of x3 = 30
  3. value of x1 = 0
    value of x2 = 30
    value of x3 = 1
  4. 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;
	}
}
  1. 10
  2. 4
  3. 1234
  4. 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());
		}
	}
}
  1. null
    5
  2. 5
  3. RuntimeException, as null cannot be added into a HashSet
  4. 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
	}
}
  1. Compile time issue at line 11
  2. Compile time issue at line 12
  3. Compile time issue at line 17
  4. Compile time issue at line 18
  5. Compile and Runs fine, prints 3

34. Threads – Deadlock detection

It possible to implement a deadlock detection, when

  1. all exclusive locks are monitored and modelled as a directed UnSorted Tree
  2. all exclusive locks are monitored and modelled as a directed Sorted Tree
  3. all exclusive locks are monitored and modelled as a directed graph
  4. 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?

  1. java.io.streams
  2. java.io.stream
  3. java.util.streams
  4. 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);
	}
}
  1. Compilation Error
  2. 8
  3. 11
  4. false
  5. 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;
	}
}
  1. Hello 5
  2. Hello 6
  3. Hello
  4. 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
	}
}
  1. Compiler Error at Line 11
  2. Compiler Error at Line 12
  3. Compiler Error at Line 17
  4. Compiler Error at Line 18
  5. 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);
	}
}
  1. Compilation error
  2. Output: true
  3. Output: false
  4. false ✓
  5. 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);
	}
}
  1. A D C
  2. A B D
  3. A B C
  4. A, D, B, C ✓
  5. A, B, C, D

41. Design pattern

String implementation follows which of the below design pattern

  1. Decorator design pattern
  2. FlyWeight design pattern ✓
  3. Immutability design pattern
  4. 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);
	}
}
  1. Compiler error: Operator >> cannot be applied to negative numbers
  2. 2 and 1
  3. -2 and 2
  4. -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);
	}
}
  1. 10
  2. 11
  3. Runtime Error ✓
  4. 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);
	}
}
  1. 11
  2. 12
  3. 10
  4. 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);
	}
}
  1. 6 6 ✓
  2. 24 24
  3. 6 24
  4. 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);
	}
}
  1. 260
  2. 0
  3. -1
  4. 4 ✓
  5. 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);
	}
}
  1. 10
  2. 5
  3. 4
  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);
	}
}
  1. 6 2
  2. 8 2 ✓
  3. 5 2
  4. 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
	}
}
  1. Compilation error at line 8 ✓
  2. Compilation error at line 12
  3. Compilation error at line 16
  4. Compilation error at line 17 ✓
  5. Compilation error at line 24
  6. 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
	}
}
  1. 2
  2. 3
  3. 4
  4. 5 ✓
  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());
	}
}
  1. Compile issue at line 9
  2. Compile issue at line 16
  3. Compile issue at line 20
  4. Compile issue at line 25 ✓
  5. 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));
	}
}
  1. true ✓
    0
  2. false
    0
  3. true
    1
  4. 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);
	}
}
  1. 10
  2. 1
  3. 0
  4. 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;
	}
}
  1. 0
  2. 1 ✓
  3. 2
  4. 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();
	}
}
  1. Runtime Error
  2. Compilation Error
  3. RUN RUN ✓
  4. 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. [1, 3, 5, 8, 9]
  2. [3, 4, 1, 8, 9]
  3. [9, 8, 4, 3, 1]
  4. [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);
  1. ArrayIndexOutOfBoundsException
  2. ArrayStoreException ✓
  3. Compilation Error
  4. Code runs successfully

What will be the output of the following code snippet?

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s