Wednesday, 23 November 2016

What is the output of following program?

What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.  
  5.  public static void show(){
  6.  
  7.         System.out.println("Static method called");
  8.  }
  9.  
  10.  public static void main(String[] args)  {
  11.  
  12.         A obj=null;
  13.         obj.show();
  14.  
  15.     }
  16.  
  17. }

 

  1. Static method called

Exaplanation:

  • We can call static methods using reference variable which is pointing to null because static methods are class level so we can either call using class name and reference variable which is pointing to null.

What is the output of following program?

What is the output of following program?

a++ or a-- is postfix operation, meaning that the value of a will get changed after the evaluation of expression.

++a or --a is prefix operation, meaning that the value of a will get changed before the evaluation of expression.

lets assume this;

a = 4;

b = a++; // first b will be 4, and after this a will be 5

// now a value is 5
c = ++a; // first a will be 6, then 6 will be assigned to c
  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.  
  5.  static int a = 1111;
  6.  static
  7.  {
  8.         a = a-- - --a;
  9.  }
  10.     
  11. {
  12.         a = a++ + ++a;
  13.  }
  14.  
  15.  public static void main(String[] args)  {
  16.  
  17.        System.out.println(a);
  18.  
  19.     }
  20.  
  21. }

  1. 2

What is the output of following program?

What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.  
  5.  int GetValue()
  6.  {
  7.         return (true ? null : 0);
  8.  }
  9.  
  10.  public static void main(String[] args)  {
  11.  
  12.    A obj= new A();

  13.       obj.GetValue();
  14.  
  15.     }
  16.  
  17. }

 

  1. Exception in thread "main" java.lang.NullPointerException

What is the output of following program?

What is the output of following program?


  1. package com.instanceofjava;
  2.  
  3. public class A{

  4.   
  5.  public static void main(String[] args)  {
  6.  
  7.    Integer i1 = 128;
  8.  
  9.    Integer i2 = 128;
  10.  
  11.      System.out.println(i1 == i2);
  12.  
  13.    Integer i3 = 127;
  14.    Integer i4 = 127;
  15.  
  16.       System.out.println(i3 == i4);
  17.  
  18.     }
  19.  
  20. }

 

  1. false
  2. true

What is the output of following program?

. What is the output of following program?


  1. package com.instanceofjava;
  2. class A
  3. {
  4.     
  5. void method(int i)
  6. {
  7.  
  8.  }
  9.  
  10.     }
  11.  
  12.  class B extends A
  13.  {
  14.  
  15. @Override
  16. void method(Integer i)
  17.  {
  18.  
  19.  }
  20.           


  21. }

 

  1. Compile time Error:The method method(Integer) of type B must override or implement a supertype method

Which line will throw compile time error? 8 or 9?

 Which line will throw compile time error? 8 or 9?


  1. package com.instanceofjava;
  2. class A
  3. {
  4.     
  5. public static void main(String [] args)
  6. {
  7.  
  8.   Integer i = new Integer(null);
  9.   String s = new String(null);
  10.  
  11.  }
  12.  
  13. }

 

  1. Compile time Error at line number 9:The constructor String(String) is ambiguous

System.out.println(null)

Program #1: what will happen when we print System.out.println(null)

System%2Bout%2Bprinln%2Bnull

Program #2: what will happen when we print System.out.println(null) by type casting
   

  1. package com.systemoutprintn;
  2. public class Test {
  3.  
  4.     /**
  5.      * @Website: www.instanceofjava.com
  6.      * @category: System.out.println(null)
  7.      */
  8.  
  9. public static void main(String[] args) {
  10.  
  11.          System.out.println((String)null);//null
  12.          System.out.println((Object)null);//null
  13.          System.out.println((char[])null);
  14.             
  15.  
  16. }
  17.  
  18. }

 Output:
  

  1. null
  2. null
  3. Exception in thread "main" java.lang.NullPointerException
  4.     at java.io.Writer.write(Unknown Source)
  5.     at java.io.PrintStream.write(Unknown Source)
  6.     at java.io.PrintStream.print(Unknown Source)
  7.     at java.io.PrintStream.println(Unknown Source)
  8.     at com.systemoutprintn.Test.main(Test.java:13)