Home / Java Programming / Java.lang Class :: Finding the output

Java Programming :: Java.lang Class

  1. What will be the output of the program?

    String x = new String("xyz"); String y = "abc"; x = x + y; 
    How many String objects have been created?

  2. A.
    2
    B.
    3
    C.
    4
    D.
    5

    View Answer

    Workspace

    Discuss Discuss in Forum


  3. What will be the output of the program?

    public class WrapTest  {     public static void main(String [] args)      {         int result = 0;         short s = 42;         Long x = new Long("42");         Long y = new Long(42);         Short z = new Short("42");         Short x2 = new Short(s);         Integer y2 = new Integer("42");         Integer z2 = new Integer(42);          if (x == y) /* Line 13 */             result = 1;         if (x.equals(y) ) /* Line 15 */             result = result + 10;         if (x.equals(z) ) /* Line 17 */             result = result + 100;         if (x.equals(x2) ) /* Line 19 */             result = result + 1000;         if (x.equals(z2) ) /* Line 21 */             result = result + 10000;          System.out.println("result = " + result);     } } 

  4. A.
    result = 1
    B.
    result = 10
    C.
    result = 11
    D.
    result = 11010

    View Answer

    Workspace

    Discuss Discuss in Forum


  5. What will be the output of the program?

    public class ObjComp  {     public static void main(String [] args )      {         int result = 0;         ObjComp oc = new ObjComp();         Object o = oc;          if (o == oc)               result = 1;         if (o != oc)               result = result + 10;         if (o.equals(oc) )               result = result + 100;         if (oc.equals(o) )               result = result + 1000;          System.out.println("result = " + result);     } } 

  6. A.
    1
    B.
    10
    C.
    101
    D.
    1101

    View Answer

    Workspace

    Discuss Discuss in Forum


  7. What will be the output of the program?

    public class Example  {     public static void main(String [] args)      {         double values[] = {-2.3, -1.0, 0.25, 4};         int cnt = 0;         for (int x=0; x < values.length; x++)          {             if (Math.round(values[x] + .5) == Math.ceil(values[x]))              {                 ++cnt;             }         }         System.out.println("same results " + cnt + " time(s)");     } } 

  8. A.
    same results 0 time(s)
    B.
    same results 2 time(s)
    C.
    same results 4 time(s)
    D.
    Compilation fails.

    View Answer

    Workspace

    Discuss Discuss in Forum


  9. What will be the output of the program?

    public class Test178  {      public static void main(String[] args)      {         String s = "foo";          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");          }      }  } 

  10. A.
    AAACCC
    B.
    AAADDD
    C.
    BBBCCC
    D.
    BBBDDD

    View Answer

    Workspace

    Discuss Discuss in Forum


  11. What will be the output of the program?

    String x = "xyz"; x.toUpperCase(); /* Line 2 */ String y = x.replace('Y', 'y'); y = y + "abc"; System.out.println(y); 

  12. A.
    abcXyZ
    B.
    abcxyz
    C.
    xyzabc
    D.
    XyZabc

    View Answer

    Workspace

    Discuss Discuss in Forum


  13. What will be the output of the program?

    int i = (int) Math.random(); 

  14. A.
    i = 0
    B.
    i = 1
    C.
    value of i is undetermined
    D.
    Statement causes a compile error

    View Answer

    Workspace

    Discuss Discuss in Forum


  15. What will be the output of the program?

    class A  {      public A(int x){}  }  class B extends A { }  public class test  {      public static void main (String args [])      {         A a = new B();          System.out.println("complete");      }  } 

  16. A.
    It compiles and runs printing nothing
    B.
    Compiles but fails at runtime
    C.
    Compile Error
    D.
    Prints "complete"

    View Answer

    Workspace

    Discuss Discuss in Forum


  17. What will be the output of the program?

    int i = 1, j = 10;  do  {     if(i++ > --j) /* Line 4 */     {         continue;      }  } while (i < 5);  System.out.println("i = " + i + "and j = " + j); /* Line 9 */ 

  18. A.
    i = 6 and j = 5
    B.
    i = 5 and j = 5
    C.
    i = 6 and j = 6
    D.
    i = 5 and j = 6

    View Answer

    Workspace

    Discuss Discuss in Forum


  19. What will be the output of the program?

    public class ExamQuestion7  {       static int j;      static void methodA(int i)     {         boolean b;          do         {              b = i<10 | methodB(4); /* Line 9 */             b = i<10 || methodB(8);  /* Line 10 */         }while (!b);      }      static boolean methodB(int i)     {         j += i;          return true;      }      public static void main(String[] args)     {         methodA(0);          System.out.println( "j = " + j );      }  } 

  20. A.
    j = 0
    B.
    j = 4
    C.
    j = 8
    D.
    The code will run with no output

    View Answer

    Workspace

    Discuss Discuss in Forum