logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Objects and Collections See What Others Are Saying!
  • Question
  • What will be the output of the program?
    public class Test 
    { 
        public static void main (String args[]) 
        {
            String str = NULL; 
            System.out.println(str); 
        } 
    }
    


  • Options
  • A. NULL
  • B. Compile Error
  • C. Code runs but no output
  • D. Runtime Exception

  • Correct Answer
  • Compile Error 

    Explanation
    Option B is correct because to set the value of a String variable to null you must use "null" and not "NULL".

    More questions

    • 1. Which statement is true?

    • Options
    • A. A try statement must have at least one corresponding catch block.
    • B. Multiple catch statements can catch the same class of exception more than once.
    • C. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
    • D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
    • Discuss
    • 2. Which statement is true about assertions in the Java programming language?

    • Options
    • A. Assertion expressions should not contain side effects.
    • B. Assertion expression values can be any primitive type.
    • C. Assertions should be used for enforcing preconditions on public methods.
    • D. An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.
    • Discuss
    • 3. 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
    • 4. What will be the output of the program?
      class Super 
      { 
          public Integer getLength() 
          {
              return new Integer(4); 
          } 
      } 
      
      public class Sub extends Super 
      { 
          public Long getLength() 
          {
              return new Long(5); 
          } 
      
          public static void main(String[] args) 
          { 
              Super sooper = new Super(); 
              Sub sub = new Sub(); 
              System.out.println( 
              sooper.getLength().toString() + "," + sub.getLength().toString() ); 
          } 
      }
      

    • Options
    • A. 4, 4
    • B. 4, 5
    • C. 5, 4
    • D. Compilation fails.
    • Discuss
    • 5. Which statement is true for the class java.util.HashSet?

    • Options
    • A. The elements in the collection are ordered.
    • B. The collection is guaranteed to be immutable.
    • C. The elements in the collection are guaranteed to be unique.
    • D. The elements in the collection are accessed using a unique key.
    • Discuss
    • 6. What will be the output of the program?
      public class Test 
      { 
          private static int[] x; 
          public static void main(String[] args) 
          { 
              System.out.println(x[0]); 
          } 
      }
      

    • Options
    • A. 0 
    • B. null
    • C. Compile Error
    • D. NullPointerException at runtime
    • Discuss
    • 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)");
          }
      }
      

    • Options
    • A. same results 0 time(s)
    • B. same results 2 time(s)
    • C. same results 4 time(s)
    • D. Compilation fails.
    • Discuss
    • 8. What will be the output of the program?
      public class ThreadDemo 
      { 
          private int count = 1; 
          public synchronized void doSomething() 
          { 
              for (int i = 0; i < 10; i++) 
                  System.out.println(count++); 
          } 
          public static void main(String[] args) 
          { 
              ThreadDemo demo = new ThreadDemo(); 
              Thread a1 = new A(demo); 
              Thread a2 = new A(demo); 
              a1.start(); 
              a2.start(); 
          } 
      } 
      class A extends Thread 
      { 
          ThreadDemo demo; 
          public A(ThreadDemo td) 
          { 
              demo = td; 
          } 
          public void run() 
          { 
              demo.doSomething(); 
          } 
      }
      

    • Options
    • A. It will print the numbers 0 to 19 sequentially
    • B. It will print the numbers 1 to 20 sequentially
    • C. It will print the numbers 1 to 20, but the order cannot be determined
    • D. The code will not compile.
    • Discuss
    • 9. Which statement is true?

    • Options
    • A. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
    • B. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
    • C. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
    • D. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
    • Discuss
    • 10. 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


    Comments

    There are no comments.

Enter a new Comment