Top Java aptitude test question or Top Java programming aptitude interview questions. Below you see top aptitude question which is asked in a company during Java interview.

 Java programming interview questions
 Java programming interview questions


1)What is the output of the following Java programming?
class A {
  int num1;

  void show() {
   System.out.println(num1);
  }

 }
 class B extends A {
  int num2;

  void show() {
   System.out.println(num2);
  }

 }
 public class Test {

  public static void main(String[] args) {
   B objB = new B();
   objB.num1=1;
   objB.num2=2;
   objB.show();
  }

 }

A- 1
B- 2
C- 1 and 2 Both
D- Compile-time Error

OUTPUT:

2)What is the output of the following Java programming?
public class Test {

 public static void main(String[] args) {
  String s = "hello";
  Object o = (Object) s;

  if (s.equals(o)) {
   System.out.print("AAA");
  } else {
   System.out.print("BBB");
  }

  if (o.equals(s)) {
   System.out.print("CCC");
  } else {
   System.out.print("DDD");
  }

 }
}

A- AAACCC
B- AAADDD
C- BBBCCC
D- BBBDDD

OUTPUT:

3)What is the output of the following Java programming?
import java.util.Arrays;
import java.util.Comparator;

class Sort implements Comparator<Integer> {

 @Override
 public int compare(Integer o1, Integer o2) {
  return o1.compareTo(o2);
 }
}

public class Test {
 public static void main(String[] args) {
  Integer intArray[] = {2,3,1};
  Arrays.sort(intArray,new Sort());
  for (int integer : intArray) {
   System.out.print(integer+" ");
  }
 }

}

A- 3 2 1
B- 2 3 1
C- Compile Time Fail
D- 1 2 3

OUTPUT:


4) What is the output of the following Java programming?
public class Test {
 public static void show() {
  String s1 = "hello6";
  String s2 = "hello" + "6";
  System.out.print(s1 == s2);
 }

 public static void display() {
  String s1 = "hello6";
  String s2 = "hello" + s1.length();
  System.out.print(s1 == s2);
 }

 public static void main(String[] args) {
  show();
  display();
 }

}

A- True True
B- True False
C- False True
D- False False

OUTPUT:

5) What is the output of the following Java programming?
public class Test {

 public static void main(String[] args) {
  try {
   System.out.print("Hello" + " " + 1 / 0);
  } catch (ArithmeticException e) {
   System.out.print("World");
  }
 }

}

A- Hello
B- World
C- Hello World
D- HelloWorld

OUTPUT:

6) What is the output of the following Java programming?
public class Test {

 public static void main(String[] args) {
  try {
   System.out.print("Hello" + " " + 0 / 1);
  } catch (ArithmeticException e) {
   e.printStackTrace();
  }
 }

}

A- java.lang.ArithmeticException: / by zero
B- Hello
C- Hello 0
D- Compile-Time Error

OUTPUT:

7) What is the output of the following Java programming?
public class JavaProgram {

private int num,num2;

public JavaProgram(int num,int num2) {
 num=num;
 num2=num2;
}

 public static void main(String[] args) {
  JavaProgram jp=new JavaProgram();
 System.out.println(jp.num+" "+jp.num2);
 }

}

A- 0 0
B- num1 and num2 cannot be resolved
C- Runtime-Error
D- Compile-Time Error

OUTPUT:

8) What is the output of the following Java programming?
public class JavaProgram {
 int num = 20;

 void next(JavaProgram jp) {
  jp.num = 40;
  jp = new JavaProgram();
  jp.num = 60;
  jp = null;
 }

 public static void main(String[] args) {
  JavaProgram jp = new JavaProgram();
  jp.next(jp);
  System.out.println(jp.num);

 }

}

A- 40
B- 60
C- 20
D- Compile-Time Error

OUTPUT:

9) What is the output of the following Java programming?
public class JavaProgram {

 public static void main(String[] args) {

  int num = 1;
  while (num < 10) {
   System.out.print(num + " ");
   if (num == 5) {
    break;
   }
   ++num;
   return;

  }
 }

}

A- 1 2 3 4 5 6 7 8 9
B- 1 2 3 4 5
C- 1
D- Compile-Time Error

OUTPUT:

10) What is the output of the following Java programming?
class test {
 static {
  System.out.print("I am static block of Test ");
 }
}

public class JavaProgram {
 test t = new test();

 public static void main(String[] args) {
  System.out.print("Hello I am Main Method ");
 }

 static {
  System.out.print("I am static block of JavaProgram ");
 }
}


A- I am static block of the test, I am static block of JavaProgram, Hello I am Main Method
B- I am static block of JavaProgram, Hello I am Main Method, I am static block of test
C- Hello I am Main Method, I am static block of JavaProgram
D- I am static block of JavaProgram, Hello I am Main Method

OUTPUT:

11) What is the output of the following Java programming?
class test extends Exception {
}

class test2 extends test {

}

public class JavaProgram {

 public static void main(String[] args) {
  try {
   throw new test2();
  }catch (test e) {
   System.out.print("Exception of Test ");
  }
  catch (Exception e) {
   System.out.print("Main Exception");
  }
 }
}

A- Exception of Test, Main Exception
B- Exception of Test
C-Code Compile Fails
D- Main Exception

OUTPUT:

12) What is the output of the following Java programming?
public class JavaProgram {
 
 public int show() {
  try {

   int i = 5 / 0;

  } catch (Exception e) {

   return 2;

  } finally {

   return 3;
  }
 }

 public static void main(String[] args) {
  JavaProgram jp = new JavaProgram();
  System.out.print(jp.show());

 }
}

A- java.lang.ArithmeticException: / by zero
B- return 2
C- return 3
D- java.lang.ArithmeticException: / by zero and return 3

OUTPUT:

13) What is the output of the following Java programming?
public class JavaProgram {
 
 static void test1(String str1, String str2) {
     str1 += "0";
     str2 += "0";
 }

 static String test2() {
     String str1 = "30", str2 = "30";
     test1( str1, str2);
     return str1 + str2;
 }

 public static void main(String[] args) {
   System.out.println(new JavaProgram().test2());

 }
}

A- 00
B- 0
C- 60
D- 3030

OUTPUT:

14) What is the output of the following Java programming?
import java.util.HashSet;
import java.util.Set;

public class JavaProgram {

 public static void main(String[] args) {
  boolean bool[] = new boolean[4];
  
  Set set = new HashSet<>();
  bool[0] = set.add("One");
  bool[1] = set.add(new Double(10.2));
  bool[2] = set.add(20);
  bool[3] = set.add(null);
  
  for (boolean b : bool) {
   System.out.print(b + " ");
  }

 }
}

A- true true true true
B- Compile Fails
C- One 10.2 20 null
D- Runtime Exception

OUTPUT:

15) What is the output of the following Java programming?
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class JavaProgram {
 public static void main(String[] args) {

  List<Object> arrayList = new CopyOnWriteArrayList<>();
  arrayList.add("One");
  arrayList.add(null);
  arrayList.add(20);
  Iterator<Object> iterator = arrayList.iterator();

  while (iterator.hasNext()) {

   arrayList.add("Bye");

  }
  System.out.println("ArrayList Size: " + arrayList.size());
 }
}


A- ArrayList Size: 6
B- ConcurrentModificationException
C- ArrayList Size: 3
D-Infinite loop

OUTPUT:

I hope you enjoy this Code, do comment aptitude questions which you know!

5 Comments

  1. What is the output of this
    program?
    class output {
    public static void
    main(String args[])
    {
    String s1 = "Hello";
    String s2 = new String(s1);
    String s3 = "HELLO";.
    System.out.println(s1.equals(s2) +" " + s2.equals(s3));

    ReplyDelete
  2. What is the output of this
    program?
    class output {
    public static voidmain(String args[])
    {
    String chars[] = {"a","b", "c", "a", "c"};
    for (int i = 0; i <chars.length; ++i)
    for (int j = i + 1; j <chars.length; ++j)

    if(chars[i].compareTo(chars[j]) ==0)

    System.out.print(chars[j]);

    ReplyDelete

Post a Comment

Previous Post Next Post