Wednesday, 7 September 2016

Java Interview Questions - 10

Below are the few set of java interview questions to get more knowledge on core java basics. For most of the questioned we need to find the output and for few questions have to get any compile time error or run-time error??

QUESTION - 1 - Output ??

public class ArrayTest {

 public static void main(String[] args) {
  
  int[] arr = {1,2,3,4};
  myMethod(arr[1], arr);
  System.out.println(arr[1] +" : "+arr[2]);
 }
 
 static void myMethod(int val, int arr[]){
  arr[val] = 100;
  val = 500;
 }
}



QUESTION - 2 - Output ??

import java.util.ArrayList;
import java.util.Collection;

public class CollectionTest {

 public static void main(String[] args) {
  Collection<Object> coll = new ArrayList<Object>();
  myMethod(coll, "java");
  myMethod(coll, 123);
 }
 
 static void myMethod(Collection<Object> coll, Object val){
  coll.add(val);
  System.out.println(coll);
 }
}



QUESTION - 3 - Output ??

public class DoubleNumberTest<D extends Number> {

 private D d;
 
 public DoubleNumberTest(D d) {
  this.d = d;
 }
 
 public double getDouble(){
  return d.doubleValue();
 }
 
 public static void main(String[] args) {
  DoubleNumberTest<Integer> obj = new DoubleNumberTest<Integer>(new Integer(24));
  System.out.println(obj.getDouble());
 }
}



QUESTION - 4 - Will it compile without any error ?

public class EnumTest {

 public EnumTest() {
  System.out.println("Inside Constructor");
  enumList();
 }
 
 public void enumList(){
  public enum myEnum {HIGH, MEDIUM, LOW}
 }
 
 public static void main(String[] args) {
  new EnumTest();
 }
}



QUESTION - 5 - Will it compile without any error ?

public class ExceptionTest {

 public static void main(String[] args) {
  
  try{
   int val = 1000;
   val = val / 0;
  
  }catch(Exception e){
   e.printStackTrace();
  
  }catch (ArithmeticException e) {
   e.printStackTrace();
  }
 }
}



QUESTION - 6 - Output ??

class Parent{
 int val = 100;
 
 public void myMethod(){
  System.out.print("Im Parent");
 }
}

class Child extends Parent{
 int val = 200;
 
 public void myMethod(){
  System.out.print("Im Child");
 }
}

public class Inheritance1Test {
 
 public static void main(String[] args) {
  
  Parent obj = new Child();
  System.out.print(obj.val+", ");
  obj.myMethod();
 }
}



QUESTION - 7 - Output ??

class Class1{
 public Class1() {
  System.out.println("Inside Class1");
 }
}

class Class2{
 public Class2() {
  System.out.println("Inside Class2");
 }
}

class Class3{
 public Class3() {
  System.out.println("Inside Class3");
 }
}

public class InheritanceTest {

 public static void main(String[] args) {
  new Class3();
 }
}



QUESTION - 8 - Will it serialize ??

import java.io.Serializable;

class Car implements Serializable{
 
}

class Colors {
 private String color;
 public Colors(String color) {
  this.color = color;
 }
}

public class SerializableTest implements Serializable {

 private Car car;
 private Colors[] colors = new Colors[4];
 
 public SerializableTest() {
  car = new Car();
  colors[0] = new Colors("red");
  colors[1] = new Colors("blue");
  colors[2] = new Colors("white");
  colors[3] = new Colors("black");
 }
 
 public static void main(String[] args) {
  
  /*
   * If we serialize obj, will Colors class also get Serializable ??
   */
  SerializableTest obj = new SerializableTest();
  
 }
}



QUESTION - 9 - Output ??

import java.util.HashSet;
import java.util.Set;

public class SetTest {

 public static void main(String[] args) {
  
  Set mySet1 = new HashSet();
  Set mySet2 = new HashSet();
  
  int j = 0;
  short i = 0;
  
  for(;i<100;i++,j++){
  
   mySet1.add(i);
   mySet1.remove(i-1);
   
   mySet2.add(j);
   mySet2.remove(j-1);   
  }  
  System.out.println("Int Set Size   : "+mySet1.size());
  System.out.println("Short Set Size : "+mySet2.size());
 }
}



QUESTION - 10 - Output ??

public class StringCompare {

 public static void main(String[] args) {
  
  String s1 = "java";
  String s2 = s1.intern();
  
  if(s1.equals(s1) && s1 == s2){
   System.out.println("first");
  }
  if(s1.equals(s1) && s1 != s2){
   System.out.println("second");   
  }
  if(!s1.equals(s1) && s1 == s2){
   System.out.println("third");
  }
  if(!s1.equals(s1) && s1 != s2){
   System.out.println("fourth");
  }
 } 
}



QUESTION - 11 - Will it compile without any error ?

public class StringMethod {

 private void myMethod(String str){
  System.out.println("Inside String method");
 }
 
 private void myMethod(StringBuilder str){
  System.out.println("Inside StringBuilder method");
 }
 
 private void myMethod(StringBuffer str){
  System.out.println("Inside StringBuffer method");
 }
 
 public static void main(String[] args) {
  new StringMethod().myMethod(null);
 }
}



QUESTION - 12 - Output ??

public class Thread1Test extends Thread {

 @Override
 public void run() {
  System.out.println("Inside run");
 }
 
 public static void main(String[] args) {
  Thread thread = new Thread(new ThreadTest());
  thread.start();
  thread.start();
 }
}



QUESTION - 13 - Output ??

public class ThreadTest extends Thread {

 @Override
 public void run() {
  System.out.println("Inside run");
 }
 
 public static void main(String[] args) {
  Thread thread = new Thread(new ThreadTest());
  thread.start();
  try {
   thread.join();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }
}



QUESTION - 14 - Output ??

public class TwoDArrayTest {

 public static void main(String[] args) {
  
  int[][] val = {{1,2,3}, {4,5,6}};
  int[][] valClone = val.clone();
  
  valClone[0][0] = 100;
  System.out.println(val[0][0]);
  System.out.println(valClone[0][0]);
  
  valClone[1] = new int[]{111, 222, 333};
  System.out.println(val[1][1]);
  System.out.println(valClone[1][1]);
 }
}



QUESTION - 15 - Output ??

import java.util.ArrayList;
import java.util.Collection;

public class ValueOfTest {

 public static void main(String[] args) {
  
  myMethod(new ArrayList<Object>());
  
 }
 
 public static void myMethod(Collection<Object> coll){
  
  Number n = null;
  coll.add(n);
  coll.add(Integer.valueOf(100));
  coll.add(Float.valueOf(200));
  
  System.out.println(new ArrayList<Object>().addAll(coll));
 }
}


ANSWERS:

1. 2 : 100

2.  [java]
     [java, 123]

3. 24.0

4. Compilation error - The member enum myEnum can only be defined inside a top-level class or interface or in a static context

5. Compilation error - Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception

6. 100, Im Child

7. Inside Class3

8. Won't serialize and we will get "java.io.NotSerializableException" when we access Color class

9. Int Set Size   : 100
    Short Set Size : 1

10. first

11. Compilation error - The method myMethod(String) is ambiguous for the type StringMethod

12. Prints "Inside run" and "IllegalThreadStateException" while starting thread again.

13. Inside run

14.  100
       100
       5
       222

15. true

No comments :

Post a Comment