Homechevron_rightEngineeringchevron_rightRPSC Programmerchevron_rightProgrammer Javachevron_rightWhat will be the output of the following Java program? ...

What will be the output of the following Java program? ...

  • Q.

    What will be the output of the following Java program?

    1.     public class BoxDemo
    2.     {
    3.         public static  void addBox(U u, java.util.List> boxes)
    4.         {
    5.            Box box = new Box<>();
    6.            box.set(u);
    7.            boxes.add(box);
    8.         }
    9.         public static  void outputBoxes(java.util.List> boxes)
    10.         {
    11.             int counter = 0;
    12.             for (Box box: boxes)
    13.             {
    14.                 U boxContents = box.get();
    15.                 System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
    16.                 counter++;
    17.             }
    18.         }
    19.         public static void main(String[] args)
    20.         {
    21.             java.util.ArrayList> listOfIntegerBoxes = new java.util.ArrayList<>();
    22.             BoxDemo.addBox(Integer.valueOf(10), listOfIntegerBoxes);
    23.             BoxDemo.outputBoxes(listOfIntegerBoxes);
    24.         }
    25.     }

     

  • filter_dramaExplanation
    Answer is : D

    None.

Discussion

    No one started the discussion yet. Break the ice and start the conversation.
    Please Login to be part of the discussion.

Similar Questions

  • 1.

    Which of these methods return a smallest whole number greater than or equal to variable X?

  • filter_dramaExplanation
    Answer is : A

    ceil(double X) returns the smallest whole number greater than or equal to variable X.

  • 2.

    Which of these class is used to read and write bytes in a file?

  • filter_dramaExplanation
    Answer is : C

    None.

  • 3.

    What will be the output of the following Java program?

    1.     class newthread extends Thread
    2.     {
    3. Thread t;
    4. String name;
    5. newthread(String threadname)
    6.         {
    7.     name = threadname;
    8.     t = new Thread(this,name);
    9.     t.start();
    10. }
    11. public void run()
    12.         {
    13.         }
    14. 
       
    15.     }
    16.     class multithreaded_programing
    17.     {
    18.         public static void main(String args[])
    19.         {
    20.     newthread obj1 =  new newthread("one");
    21.     newthread obj2 = new newthread("two");
    22.             try
    23.             {
    24.                 obj1.t.wait();
    25.                 System.out.print(obj1.t.isAlive());
    26.             }
    27.             catch(Exception e)
    28.             {
    29.     System.out.print("Main thread interrupted");
    30.             }
    31.         }
    32.     }

     

  • filter_dramaExplanation
    Answer is : C

    obj1.t.wait() causes main thread to go out of processing in sleep state hence causes exception and “Main thread interrupted” is printed.

  • 4.

    Can we create an instance of Enum outside of Enum itself?

  • filter_dramaExplanation
    Answer is : A

    ordinal() method provides number to the variables defined in Enum.

  • 5.

    Which of these methods is defined in MouseMotionAdapter class?

  • filter_dramaExplanation
    Answer is : A

    The MouseMotionAdapter class defines 2 methods – mouseDragged() and mouseMoved.

Programmer JavaTopics

leaderboardLeaderboard
  • Rahul Kumar

    191 Points

  • VIKRAM JEET

    54 Points

  • GEETHIKA CHOWDARY

    53 Points

  • sunita saini

    52 Points

  • Zain

    49 Points