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 a = "ABCD"; 
    String b = a.toLowerCase(); 
    b.replace('a','d'); 
    b.replace('b','c'); 
    System.out.println(b);
    


  • Options
  • A. abcd
  • B. ABCD
  • C. dccd
  • D. dcba

  • Correct Answer
  • abcd 

    Explanation
    String objects are immutable, they cannot be changed, in this case we are talking about the replace method which returns a new String object resulting from replacing all occurrences of oldChar in this string with newChar.

    b.replace(char oldChar, char newChar);

    But since this is only a temporary String it must either be put to use straight away i.e.

    System.out.println(b.replace('a','d'));

    Or a new variable must be assigned its value i.e.

    String c = b.replace('a','d');


    More questions

    • 1. 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
    • Discuss
    • 2. What will be the output of the program?
      package foo; 
      import java.util.Vector; /* Line 2 */
      private class MyVector extends Vector 
      {
          int i = 1; /* Line 5 */
          public MyVector() 
          { 
              i = 2; 
          } 
      } 
      public class MyNewVector extends MyVector 
      {
          public MyNewVector () 
          { 
              i = 4; /* Line 15 */
          } 
          public static void main (String args []) 
          { 
              MyVector v = new MyNewVector(); /* Line 19 */
          } 
      }
      

    • Options
    • A. Compilation will succeed.
    • B. Compilation will fail at line 3.
    • C. Compilation will fail at line 5.
    • D. Compilation will fail at line 15.
    • Discuss
    • 3. What will be the output of the program?
      TreeSet map = new TreeSet();
      map.add("one");
      map.add("two");
      map.add("three");
      map.add("four");
      map.add("one");
      Iterator it = map.iterator();
      while (it.hasNext() ) 
      {
          System.out.print( it.next() + " " );
      }
      

    • Options
    • A. one two three four
    • B. four three two one
    • C. four one three two
    • D. one two three four one
    • Discuss
    • 4. Which three statements are true?

      1. Assertion checking is typically enabled when a program is deployed.
      2. It is never appropriate to write code to handle failure of an assert statement.
      3. Assertion checking is typically enabled during program development and testing.
      4. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
      5. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.

    • Options
    • A. 1, 2 and 4
    • B. 2, 3 and 5
    • C. 3, 4 and 5
    • D. 1, 2 and 5
    • Discuss
    • 5. What will be the output of the program?
      class s implements Runnable 
      { 
          int x, y; 
          public void run() 
          { 
              for(int i = 0; i < 1000; i++) 
                  synchronized(this) 
                  { 
                      x = 12; 
                      y = 12; 
                  } 
              System.out.print(x + " " + y + " "); 
          } 
          public static void main(String args[]) 
          { 
              s run = new s(); 
              Thread t1 = new Thread(run); 
              Thread t2 = new Thread(run); 
              t1.start(); 
              t2.start(); 
          } 
      }
      

    • Options
    • A. DeadLock
    • B. It print 12 12 12 12
    • C. Compilation Error
    • D. Cannot determine output.
    • Discuss
    • 6. What will be the output of the program?
      public class ThreadTest extends Thread 
      { 
          public void run() 
          { 
              System.out.println("In run"); 
              yield(); 
              System.out.println("Leaving run"); 
          } 
          public static void main(String []argv) 
          { 
              (new ThreadTest()).start(); 
          } 
      }
      

    • Options
    • A. The code fails to compile in the main() method
    • B. The code fails to compile in the run() method
    • C. Only the text "In run" will be displayed
    • D. The text "In run" followed by "Leaving run" will be displayed
    • Discuss
    • 7. Which statement is true?

    • Options
    • A. A static method cannot be synchronized.
    • B. If a class has synchronized code, multiple threads can still access the nonsynchronized code.
    • C. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
    • D. When a thread sleeps, it releases its locks.
    • Discuss
    • 8. Which of the following are legal lines of code?

      1. int w = (int)888.8;
      2. byte x = (byte)1000L;
      3. long y = (byte)100;
      4. byte z = (byte)100L;

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 3 and 4
    • D. All statements are correct.
    • Discuss
    • 9. What will be the output of the program?
      int I = 0;
      label:
          if (I < 2) {
          System.out.print("I is " + I);
          I++;
          continue label;
      }
      

    • Options
    • A. I is 0
    • B. I is 0 I is 1
    • C. Compilation fails.
    • D. None of the above
    • Discuss
    • 10. What will be the output of the program?
      interface Foo141 
      { 
          int k = 0; /* Line 3 */
      } 
      public class Test141 implements Foo141 
      {
          public static void main(String args[]) 
          {
              int i; 
              Test141 test141 = new Test141(); 
              i = test141.k; /* Line 11 */
              i = Test141.k; 
              i = Foo141.k; 
          } 
      }
      

    • Options
    • A. Compilation fails.
    • B. Compiles and runs ok.
    • C. Compiles but throws an Exception at runtime.
    • D. Compiles but throws a RuntimeException at runtime.
    • Discuss


    Comments

    There are no comments.

Enter a new Comment