Java Code Snippets: Guess the Output

Here are some fun basic Java code snippets to refresh the fundamental understanding. Guess the output! How many did you get right? 1.➡️ public class OverloadExample {   public void show(Object obj) {     System.out.println("Cat");   }   public void show(String str) {     System.out.println("Dog");   }   public static void main(String[] args) {     OverloadExample example = new OverloadExample();     example.show(null);   } } 2.➡️ public class ConstructorTest {   void ConstructorTest() {     System.out.println("Hello");   }   public static void main(String[] args) {     ConstructorTest ct = new ConstructorTest();   } } 3.➡️ public class IncrementTest {   public static void main(String[] args) {     int x = 5;     System.out.println(x++ + ++x);   } } 4.➡️ public class LoopTest {   public static void main(String[] args) {     int i = 0;     for(; i < 3; i++);     System.out.println(i);   } } 5.➡️ public class BooleanTest {   public static void main(String[] args) {     boolean b1 = true;     boolean b2 = false;     System.out.println(b1 & b2);     System.out.println(b1 && b2);   } } 6.➡️ class Main {   public static void main(String[] args) {     boolean b = false;     System.out.println(b & test());     System.out.println("============================");     System.out.println(b && test());   }   public static boolean test() {   System.out.println("Called");   return false; } } 7.➡️ public class CastTest {   public static void main(String[] args) {     double d = 9.78;     int i = (int) d;     System.out.println(i);   } } 8.➡️ public class PrePostTest {   public static void main(String[] args) {     int x = 2;     int y = x++ * 3 + ++x;     System.out.println(y);   } } 9.➡️ public class CharTest {   public static void main(String[] args) {     char c = 'A';     c += 1;     System.out.println(c);   } } 10. ➡️public class LogicalTest {   public static void main(String[] args) {     int a = 5, b = 10;     System.out.println(a < b || b++ < 15);     System.out.println(b);   } }

For 1- Cat hoping null is object 2-hello 3-11 4-i i i 5- no idea for bitwise operation False 7- 9 8-9 9- B 10- true 11 Let me know who many has correct outbof 9🤩

To view or add a comment, sign in

Explore content categories