logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Java.lang Class Comments

  • Question
  • What will be the output of the program?

    System.out.println(Math.sqrt(-4D));


  • Options
  • A. -2
  • B. NaN
  • C. Compile Error
  • D. Runtime Exception

  • Correct Answer
  • NaN 

    Explanation
    It is not possible in regular mathematics to get a value for the square-root of a negative number therefore a NaN will be returned because the code is valid.

    Java.lang Class problems


    Search Results


    • 1. What will be the output of the program?
      String d = "bookkeeper";
      d.substring(1,7);
      d = "w" + d;
      d.append("woo");  /* Line 4 */
      System.out.println(d);
      

    • Options
    • A. wookkeewoo
    • B. wbookkeeper
    • C. wbookkeewoo
    • D. Compilation fails.
    • Discuss
    • 2. 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 */
      

    • Options
    • A. i = 6 and j = 5
    • B. i = 5 and j = 5
    • C. i = 6 and j = 6
    • D. i = 5 and j = 6
    • Discuss
    • 3. What will be the output of the program?
      int i = (int) Math.random();
      

    • Options
    • A. i = 0
    • B. i = 1
    • C. value of i is undetermined
    • D. Statement causes a compile error
    • Discuss
    • 4. What will be the output of the program?
      public class StringRef 
      {
          public static void main(String [] args) 
          {
              String s1 = "abc";
              String s2 = "def";
              String s3 = s2;   /* Line 7 */
              s2 = "ghi";
              System.out.println(s1 + s2 + s3);
          }
      }
      

    • Options
    • A. abcdefghi
    • B. abcdefdef
    • C. abcghidef
    • D. abcghighi
    • Discuss
    • 5. 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);
      

    • Options
    • A. abcXyZ
    • B. abcxyz
    • C. xyzabc
    • D. XyZabc
    • Discuss
    • 6. What will be the output of the program?
      public class NFE 
      {
          public static void main(String [] args) 
          {
          String s = "42";
              try 
              {
                  s = s.concat(".5");  /* Line 8 */
                  double d = Double.parseDouble(s);
                  s = Double.toString(d);
                  int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
                  System.out.println(x);
              }
              catch (NumberFormatException e) 
              {
                  System.out.println("bad number");
              }
          }
      }
      

    • Options
    • A. 42
    • B. 42.5
    • C. 43
    • D. bad number
    • Discuss
    • 7. What will be the output of the program?
      String s = "ABC"; 
      s.toLowerCase(); 
      s += "def"; 
      System.out.println(s);
      

    • Options
    • A. ABC
    • B. abc
    • C. ABCdef
    • D. Compile Error
    • Discuss
    • 8. What will be the output of the program?
      public class ExamQuestion6 
      {
          static int x; 
          boolean catch()
          {
              x++; 
              return true; 
          } 
          public static void main(String[] args)
          {
              x=0; 
              if ((catch() | catch()) || catch()) 
                  x++; 
              System.out.println(x); 
          } 
      }
      

    • Options
    • A. 1
    • B. 2
    • C. 3
    • D. Compilation Fails
    • Discuss
    • 9. What will be the output of the program?
      try 
      {
          Float f1 = new Float("3.0");
          int x = f1.intValue();
          byte b = f1.byteValue();
          double d = f1.doubleValue();
          System.out.println(x + b + d);
      }
      catch (NumberFormatException e) /* Line 9 */
      {
          System.out.println("bad number"); /* Line 11 */
      }
      

    • Options
    • A. 9.0
    • B. bad number
    • C. Compilation fails on line 9.
    • D. Compilation fails on line 11.
    • Discuss
    • 10. What will be the output of the program (in jdk1.6 or above)?
      public class BoolTest 
      {
          public static void main(String [] args) 
          {
              Boolean b1 = new Boolean("false");
              boolean b2;
              b2 = b1.booleanValue();
              if (!b2) 
              {
                  b2 = true;
                  System.out.print("x ");
              }
              if (b1 & b2) /* Line 13 */
              {
                  System.out.print("y ");
              }
              System.out.println("z");
          }
      }
      

    • Options
    • A. z
    • B. x z