logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Operators and Assignments See What Others Are Saying!
  • Question
  • Which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
    1. boolean test = (Component instanceof t);
    2. boolean test = (t instanceof Ticker);
    3. boolean test = t.instanceof(Ticker);
    4. boolean test = (t instanceof Component);
        
    import java.awt.*;
    class Ticker extends Component 
    {
        public static void main (String [] args) 
        {
            Ticker t = new Ticker();
            /* Missing Statements? */
        }
    }
    


  • Options
  • A. 1 and 4
  • B. 2 and 3
  • C. 1 and 3
  • D. 2 and 4

  • Correct Answer
  • 2 and 4 

    Explanation
    (2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct because Component is part of the hierarchy of t, because Ticker extends Component.

    (1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.


    More questions

    • 1. 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