Test Your Java 8

Test Your Java 7 and Test Your Java 8 are advanced questions. Answers are given for each question at the end of each test. Answering these questions increases your knowledge in Java and helps you a lot when you appear for interviews. Solving these basic questions is your first step for Certification Exam preparation.

  1. The super() is used to have an explicit access to the methods and variables of superclass.
    a) True b) False
  2. .
         class Demo   
         {
           public void display()   
           {
    	 System.out.println("Hello 1");
           }
         }
         class Test extends Demo  
         {
    	void display()   
            {
    	  System.out.println("Hello 2");
    	}
    	public static void main(String args[])  
            {
    	  Test t1 = new Test();
    	  t1.display( ) ;
    	}
         }
    

    The above code
    a) prints Hello 1 b) prints Hello2 c) compiles, executes but no output d) raises a compilation error e) none

  3. We can stop a thread temporarily by calling stop( ) method on it.
    a) True b) False
  4. sleep(50);
    The above statement of a thread implies that the thread will get the processor-time
    a) any moment after 50 milliseconds. b) exactly after 50 milliseconds. c) even before 50 milliseconds if the processor is idle d) none
  5. When a read( ) method returns -1, it is to be understood that EOF is encountered. The same conclusion can be drawn with an EOFException also.
    a) True b) False
  6. What is the other immediate superclass of DataInputStream other than FilterInputStream ?
    a) InputStream b) DataInput c) Object d) FileInputStream e) none
  7. mkdir() is the command in Java to create a new directory.
    a) True b) False
  8. What is the return type of writeDouble( double d ), a method of RandomAccessFile.
    a) void b) int c) float d) double f ) boolean g) file object h) none
  9. The File method lastModified() returns
    a) date b) int c) double d) long e) specified by the programmer f) none
  10. javac –g HelloWorld.java
    The above compiler option is for
    a) grouping all the .class files while creating a package.
    b) encoding the international languages which Java supports.
    c) furnishing extra debugging information.
    d) none
  11. Color color1 = new Color(Color.red);
    The above statement produces same intensity of red color on any color monitor as Java is
    platform-independent.
    a) True b) False
  12. .
         import java.awt.*;
         class GetColorValues extends Frame        
         {
    	private float red = 0.5f, green = 0.5f,  blue = 0.5f;
    	public void paint(Graphics g)     
            {
    	  g.setColor(new Color(red,green,blue));
    	  g.drawString(g.getColor().toString(), 50 ,75);
    	}
    	public static void main(String args[])  
            {
    	  Frame f = new numbers();
    	  f.setVisible(true);  
              f.setSize(300,300);
            }   
          }
    

    What are the color values given by the drawString method of the above program ?
    a) 127, 127, 127 b) 0.5, 0.5, 0.5 c) depends on the underlying system d) does not compile as the program is error prone e) none

  13. Font size is measured in
    a) pixels b) points c) depends on the display structure of monitor
    e) depends on the resolution of monitor e) none
  14. g.drawRoundRect(50, 50, 100, 200, 100, 200);
    The above statement produces a
    a) rectangle b) rounded rectangle c) ellipse d) rounded square e) circle f) none
  15. The main purpose of Frame's dispose() method is
    Which one of the following statements is true ?
    a) to make the frame hide on the screen. b) to call back the frame whenever needed easily. c) to dispose the memory resources linked with the frame. d) to dispose the non-memory resources linked with the frame. e) none
  16. Java is platform-independent, that is a button created on a Windows-95 platform and a button created on a Unix or Macintosh platform look alike.
    a) True b) False
  17. .
         import java.awt.* ; 
         class Coincidence extends Frame
         {
    	Coincidence()  
            {
     	  Label label = new Label("After");  
    	  setLayout(null);
    	  label.setBounds(50, 50, 50, 50);   
    		add(label);
    	}
    	public void paint(Graphics g)  
            {
    	  g.drawString("After", 50, 50);
    	}
    	public static void main(String args[]) 
            {
    	  Frame f = new Coincidence(); 
      	  f.setVisible(true);  
    	  f.setSize(300, 300);
            }
          }
    

    In the above program both label object and drawString method displays the same string After.
    Because they are displayed at the same cooridinates 50, 50 and as the string is the same, one overlaps the other so that only one After is displayed.
    a) True b) False

  18. .
          class NewButton extends Frame implements ActionListener
          {
            Button btn1, btn2; 
            NewButton()   
            {
              btn1 = new Button("Button1");
              btn2 = new Button("Button2");
              btn1.addActionListener(this);
              add(btn1,"North");
            }
            public void actionPerformed(ActionEvent ae)
            { 
              add(btn2,"South");
            }
            public static void main(String args[])   
            {
              Frame f = new NewButton(); 
              f.setVisible(true);   
              f.setSize(300, 300); 
            }   
          }

    With click of Button1, Button2 will be added to the south of the Frame .
    a) True b) False

  19. getParameter( ) method of Applet returns
    a) only String b) String or any other data type and depends on the parameter passed from param tag c) none
  20. Which of the coding scheme(s) appletviewer supports.
    a) GIF b) JPEG c) a and b d) none
  21. Bytecode verification is not done in Applets.
    a) True b) False
  22. .
         import java.awt.*;  
         public class Suspicion extends Frame
         {
           Suspicion()   
           {
             setLayout(new GridLayout(3, 0));
    	 
             for(int i = 0; i <12; i++)    
    	   add(new Button("Button " + (i + 1)));
    
    	 setVisible(true);    
             setSize(300, 200);
           }
           public static void main(String args[]) 
           {
    	 new Suspicion();
           }
         }	
    

    The above code:
    a) compiles but raises an IllegalArgumentException b) does not compile as columns can not be 0 c) displays 12 buttons in 3 rows and 4 columns d) displays 12 buttons not in 3 rows and 4 columns e) none

  23. .
         import java.io.*;
         class A
         {
           public void display(StringBuffer sb2) throws  IOException   
           {
             FileInputStream fis = new FileInputStream("ABC.java");
    	 int k;
    	 while((k = fis.read()) ! = -1)
    	   sb2.append((char) k);
    	}
         }
         public class B extends A   
         {
           static StringBuffer sb1 = new StringBuffer();
           public void display() throws IOException   
           {
    	 FileInputStream fis = new FileInputStream("PQR.java");
    	 int k;
    	 while((k = fis.read()) != -1) 
    	  sb1.append((char) k);
           }
           public static void main(String args[]) throws IOException   
           {
    	 A a = new A();
    	 B b = new B();
    	 b.display();
    	 a.display(sb1);
    	 System.out.println(sb1);
           }    
         }

    The above program:
    a) does not compile as overridden method should have the same method signature b) object sb1 can’t be sent from subclass overridden method to super class method. c) displays the contents of PQR.java only. d) displays the contents of ABC.java and PQR.java. e) none

  24. .
         Random r = new Random(); 
          Vector v = new Vector(); 
          for(int i = 0 ; i < Math.abs(r.nextInt());  i++) 
             v.addElement(new Integer(r.nextInt())); 
    

    What code will print a list of all values in vector "v"?
    a) for(Enumeration e = v.elements(); e.hasMoreElements();)
    System.out.print(e.nextElement(i) + " ");

    b) while(v.hasMoreElements())
    System.out.print(v.nextElement() + " ");

    c) for(int i = 0; i < v.lastElement(); i++) System.out.print(v.elementAt(i).toString()); d) for(Enumeration e = v.elements(); e.hasMoreElements();) System.out.print(((Integer) e.nextElement()).intValue() + " "); e) for(int i = 0; i < v.capacity(); i++) System.out.print( v.elementAt( i ) ) ;

  25. Integer i = new Integer("5" ) ;
    System.out.println( Integer.parseInt(i.toString()) * i.intValue());

    The above code prints 25.
    a) True b) False

ANSWERS Test Your Java 8

1. b 2. d 3. b 4. a 5. a
6. b 7. a 8. a 9. d 10. c
11. b 12. a 13. b 14. c 15. d
16. b 17. b 18. b 19. a 20. c
21. b 22. c 23. d 24. b 25. a

Your Rating on Test Your Java 8

Correct Answers
1 to 5 : Poor
6 t0 10: Below average
11 t0 15: Average
16 to 20: Good
Above 20: Extraordinary

Pass your comments and suggestions on the improvement of this "Test Your Java 8".

Leave a Comment

Your email address will not be published.