logo

CuriousTab

CuriousTab

Discussion


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

  • Correct Answer
  • abcghidef 

    Explanation
    After line 7 executes, both s2 and s3 refer to a String object that contains the value "def". When line 8 executes, a new String object is created with the value "ghi", to which s2 refers. The reference variable s3 still refers to the (immutable) String object with the value "def".

    More questions

    • 1. 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
    • 2. Which four can be thrown using the throw statement?

      1. Error
      2. Event
      3. Object
      4. Throwable
      5. Exception
      6. RuntimeException

    • Options
    • A. 1, 2, 3 and 4
    • B. 2, 3, 4 and 5
    • C. 1, 4, 5 and 6
    • D. 2, 4, 5 and 6
    • Discuss
    • 3. Which three are valid method signatures in an interface?

      1. private int getArea();
      2. public float getVol(float x);
      3. public void main(String [] args);
      4. public static void main(String [] args);
      5. boolean setFlag(Boolean [] test);

    • Options
    • A. 1 and 2
    • B. 2, 3 and 5
    • C. 3, 4, and 5
    • D. 2 and 4
    • Discuss
    • 4. What will be the output of the program?
      class Bitwise 
      {
          public static void main(String [] args) 
          {
              int x = 11 & 9;
              int y = x ^ 3;
              System.out.println( y | 12 );
          }
      }
      

    • Options
    • A. 0
    • B. 7
    • C. 8
    • D. 14
    • Discuss
    • 5. Which two statements are true?

      1. Deadlock will not occur if wait()/notify() is used
      2. A thread will resume execution as soon as its sleep duration expires.
      3. Synchronization can prevent two objects from being accessed by the same thread.
      4. The wait() method is overloaded to accept a duration.
      5. The notify() method is overloaded to accept a duration.
      6. Both wait() and notify() must be called from a synchronized context.

    • Options
    • A. 1 and 2
    • B. 3 and 5
    • C. 4 and 6
    • D. 1 and 3
    • Discuss
    • 6. Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

    • Options
    • A. java.util.HashSet
    • B. java.util.LinkedHashSet
    • C. java.util.List
    • D. java.util.ArrayList
    • Discuss
    • 7. Which of the following statements is true?

    • Options
    • A. In an assert statement, the expression after the colon ( : ) can be any Java expression.
    • B. If a switch block has no default, adding an assert default is considered appropriate.
    • C. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
    • D. It is appropriate to handle assertion failures using a catch clause.
    • Discuss
    • 8. Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

    • Options
    • A. final
    • B. static
    • C. private
    • D. protected
    • E. volatile
    • Discuss
    • 9. Which is a valid declaration within an interface?

    • Options
    • A. public static short stop = 23;
    • B. protected short stop = 23;
    • C. transient short stop = 23;
    • D. final void madness(short stop);
    • Discuss
    • 10. Which four options describe the correct default values for array elements of the types indicated?

      1. int -> 0
      2. String -> "null"
      3. Dog -> null
      4. char -> '\u0000'
      5. float -> 0.0f
      6. boolean -> true

    • Options
    • A. 1, 2, 3, 4
    • B. 1, 3, 4, 5
    • C. 2, 4, 5, 6
    • D. 3, 4, 5, 6
    • Discuss


    Comments

    There are no comments.

Enter a new Comment