Sitemap
Imran Khan

Imran Khan

Tech Enthusiast

Java Expected Output

4 min readNov 3, 2022

--

Below are the questions to predict output which will help us to understand java concepts in detail and how things actually works.

Created list of 15 logical questions covering most of Java modules.

Question 1

try {
int count = 0;
System.out.println(1 / count);
} catch (Exception ae) {
System.out.println(">>>>> Exception");
} catch (ArithmeticException ae) {
System.out.println(">>>>> ArithmeticException");
}

Question 2

String a = "a";
String b = "a";
String c = new String("a");
String d = new String("a");


System.out.println(a == b);
System.out.println(b == c);
System.out.println(a == c);
System.out.println(c == d);

Question 3

class A {
public void printName() {
System.out.println("A");
}
}

class B extends A {
public void printName() {
System.out.println("B");
}
}

public class Practice {
public static void main(String[] args) {
A a = new B();
a.printName();
}
}

Question 4

class A {
public void printSum() {
System.out.println("A");
}
}

class B extends A {
public void printName() {
System.out.println("B");
}
}

public class Practice {
public static void main(String[] args) {
A a = new B();
a.printName();
}
}

Question 5

try {
int count = 0;
System.out.println(1 / count);
} finally {
System.out.println(">>>>> finally");
}

Question 6

Question 7

System.out.println(20 + 20 + "John");
System.out.println("John" + 20 + 20);
System.out.println("John" + 20 * 20);

Question 8

public static void main(String[] args) {
System.out.println(printName());
}

String printName() {
return "John";
}

Question 9

public static void main(String args[]) {
System.out.println(fun());
}
static int fun() {
static int a = 0;
return ++a;
}

Question 10

public static void main(String[] args) {
method(null);
}

public static void method(String str) {
System.out.println("String method called");
}

public static void method(Object obj) {
System.out.println("Object method called");
}

Question 11

public class Test {

Test() {
System.out.println("Constructor called");
super();
}

public static void main(String[] args) {
System.out.println("Before main method");
Test test = new Test();
System.out.println("After main method");
}
}

Question 12

public static int method() {
try{
return 10;
} finally {
System.out.println("Finally");
}
}

public static void main(String[] args) {
System.out.println(method());
}

Question 13

public static void main(String[] args) {
int abc = 4;
System.out.println(abc++ + ++abc);
}

Question 14

public static void method(Object o) {
if (true) {
break;
}
}

Question 15

Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("A");
System.out.println(set);

Find below answers for all above questions:

Answer 1

It will give compile time error as it is not allowed to declare parent Exception class before child exception.

Answer 2

This is because of string pooling where new keyword will point to both new location and variable. For more details:

Get Imran Khan’s stories in your inbox

Join Medium for free to get updates from this writer.

true
false
false
false

Answer 3

A is having an instance of B and hence it will print line from B class printName() method. This is best example of runtime polymorphism or function overriding.

B

Answer 4

It will give compile time error as there is no printName() method available in class A to access. Neither it is a case of function overloading nor overriding.

Answer 5

Press enter or click to view image in full size

Answer 6

static block get call on class load and non static block will get call at the time of instance creation using new keyword.

Static block before main() method
Static block after main() method
Non Static block before main() method
Non Static block after main() method

Answer 7

It follows BODMAS rule and resolve *(multiplication) before +(addition)

40John
John2020
John400

Answer 8

It will give compile time error as non static method can not get call inside static method. Using class instance variable we can access non static variable or method inside static method.

Answer 9

It will give compile time error as local static variables are not allowed.

Answer 10

null is not type of Object.

String method called

Answer 11

It will give compile time error as super() always needs to call at first line.

Answer 12

Finally
10

Answer 13

abc++ will increment value by one internally. But, internally abc variable will still have 4 value assigned.

For ++abc, the value already got incremented by one as part of abc++ and become 5. It will again get increment by one and become 6. Same value will get assign to abc as 6. At the end of the statement execution, 4 + 6 will become 10.

10

Answer 14

It will give compile time error as break statement always must come within loop.

Answer 15

It will print below output and override the previous value and override the the same with consider the

[A, B]

I hope you found out this article interesting and informative. Please share it with your friends to spread the knowledge.

You can follow me for upcoming blogs follow.
Thank you!

--

--