logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Java.lang Class See What Others Are Saying!
  • Question
  • 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

  • Correct Answer
  • xyzabc 

    Explanation
    Line 2 creates a new String object with the value "XYZ", but this new object is immediately lost because there is no reference to it. Line 3 creates a new String object referenced by y. This new String object has the value "xyz" because there was no "Y" in the String object referred to by x. Line 4 creates a new String object, appends "abc" to the value "xyz", and refers y to the result.

    More questions

    • 1. What will be the output of the program?
      public class Test 
      {  
          public static void main(String args[])
          { 
              class Foo 
              {
                  public int i = 3;
              } 
              Object o = (Object)new Foo();
              Foo foo = (Foo)o;
              System.out.println("i = " + foo.i);
          }
      }
      

    • Options
    • A. i = 3
    • B. Compilation fails.
    • C. i = 5
    • D. A ClassCastException will occur.
    • Discuss
    • 2. What will be the output of the program?
      class Test116 
      { 
      static final StringBuffer sb1 = new StringBuffer(); 
      static final StringBuffer sb2 = new StringBuffer(); 
      public static void main(String args[]) 
      { 
          new Thread() 
          { 
              public void run() 
              { 
                  synchronized(sb1) 
                  { 
                      sb1.append("A"); 
                      sb2.append("B"); 
                  } 
              } 
          }.start(); 
      
          new Thread() 
          { 
              public void run() 
              { 
                  synchronized(sb1) 
                  { 
                      sb1.append("C"); 
                      sb2.append("D"); 
                  } 
              } 
          }.start(); /* Line 28 */
      
          System.out.println (sb1 + " " + sb2); 
          } 
      }
      

    • Options
    • A. main() will finish before starting threads.
    • B. main() will finish in the middle of one thread.
    • C. main() will finish after one thread.
    • D. Cannot be determined.
    • Discuss
    • 3. What will be the output of the program?
      public class X 
      {  
          public static void main(String [] args) 
          {
              try 
              {
                  badMethod();  
                  System.out.print("A"); 
              }  
              catch (Exception ex) 
              {
                  System.out.print("B");  
              } 
              finally 
              {
                  System.out.print("C"); 
              } 
              System.out.print("D"); 
          }  
          public static void badMethod() 
          {
              throw new Error(); /* Line 22 */
          } 
      }
      

    • Options
    • A. ABCD
    • B. Compilation fails.
    • C. C is printed before exiting with an error message.
    • D. BC is printed before exiting with an error message.
    • Discuss
    • 4. What will be the output of the program?
      class BoolArray 
      {
          boolean [] b = new boolean[3];
          int count = 0;
      
          void set(boolean [] x, int i) 
          {
              x[i] = true;
              ++count;
          }
      
          public static void main(String [] args) 
          {
              BoolArray ba = new BoolArray();
              ba.set(ba.b, 0);
              ba.set(ba.b, 2);
              ba.test();
          }
      
          void test() 
          {
              if ( b[0] && b[1] | b[2] )
                  count++;
              if ( b[1] && b[(++count - 2)] )
                  count += 7;
              System.out.println("count = " + count);
          }
      }
      

    • Options
    • A. count = 0
    • B. count = 2
    • C. count = 3
    • D. count = 4
    • Discuss
    • 5. What will be the output of the program?
      public class X 
      {  
          public static void main(String [] args) 
          {
              try 
              {
                  badMethod(); /* Line 7 */
                  System.out.print("A"); 
              } 
              catch (Exception ex) /* Line 10 */
              {
                  System.out.print("B"); /* Line 12 */
              } 
              finally /* Line 14 */
              {
                  System.out.print("C"); /* Line 16 */
              }  
              System.out.print("D"); /* Line 18 */
          } 
          public static void badMethod() 
          {
              throw new RuntimeException(); 
          } 
      }
      

    • Options
    • A. AB
    • B. BC
    • C. ABC
    • D. BCD
    • Discuss
    • 6. What will be the output of the program?
      public class MyProgram 
      {
          public static void main(String args[])
          {
              try 
              {
                  System.out.print("Hello world ");
              }
              finally 
              {
                  System.out.println("Finally executing ");
              }
          }
      }
      

    • Options
    • A. Nothing. The program will not compile because no exceptions are specified.
    • B. Nothing. The program will not compile because no catch clauses are specified.
    • C. Hello world.
    • D. Hello world Finally executing
    • Discuss
    • 7. Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden?

      1. If the equals() method returns true, the hashCode() comparison == must return true.
      2. If the equals() method returns false, the hashCode() comparison != must return true.
      3. If the hashCode() comparison == returns true, the equals() method must return true.
      4. If the hashCode() comparison == returns true, the equals() method might return true.

    • Options
    • A. 1 and 4
    • B. 2 and 3
    • C. 3 and 4
    • D. 1 and 3
    • Discuss
    • 8. Which three are methods of the Object class?

      1. notify();
      2. notifyAll();
      3. isInterrupted();
      4. synchronized();
      5. interrupt();
      6. wait(long msecs);
      7. sleep(long msecs);
      8. yield();

    • Options
    • A. 1, 2, 4
    • B. 2, 4, 5
    • C. 1, 2, 6
    • D. 2, 3, 4
    • Discuss
    • 9. Which two can be used to create a new Thread?

      1. Extend java.lang.Thread and override the run() method.
      2. Extend java.lang.Runnable and override the start() method.
      3. Implement java.lang.Thread and implement the run() method.
      4. Implement java.lang.Runnable and implement the run() method.
      5. Implement java.lang.Thread and implement the start() method.

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 1 and 4
    • D. 3 and 4
    • Discuss
    • 10. What is the numerical range of a char?

    • Options
    • A. -128 to 127
    • B. -(215) to (215) - 1
    • C. 0 to 32767
    • D. 0 to 65535
    • Discuss


    Comments

    There are no comments.

Enter a new Comment