Java - Test 2
No. of Questions - 20
Time - 40 Minutes
Negative Marking- No

 

Answers

Question 1.

In the code fragment below, line 4 is executed.
1.      String s1 = “xyz”;
2.      String s2 = “xyz”;
3.      If (s1 = = s2)
4.         System.out.println(“Line 4”)

A.   True
B.    False
C.  An exception is thrown
D.   A compilation error occurs
E.   The operating systems stops responding

Question 2.

A Java program creates a check box using the code listed below. The program is run on two different platforms. Which of the statements following the code are true? (Choose one or more)

1.      Checkbox cb = new Checkbox(“Autosave”);
2.      Font f = new Font(“Courier”, Font.PLAIN, 14);
3.      cb.setFont(f); 

A. The check box will be the same size on both platforms, because Courier is a standard Java font.
B. The check box will be the same size on both platforms, because Courier is a fixed-width font. 
C.  The check box will be the same size on both platforms, provided both platforms have identical    14-point Courier fonts.                     
D. The check box will be the same size on both platforms, provided both platforms have identical check-box decorations.
E. There is no way to guarantee that the buttons will be the same size on both platforms.

Question 3.

 What is the result of compiling and running the following application?

 
1.      Import java.awt.*;
2.
3.      public class Q3 extends Frame {
4.          Q3( ) {
5.               / / Use Grid layout manager.
6.              setSize(300, 300);
7.              setLayout(new GridLayout(1, 2));
8.       
9.             / / Build and add 1st panel.
10.         Panel p1 = new Panel ( );
11.         p1.setLayout(new FlowLayout(FlowLayout.RIGHT));
12.         p1.add(new Button(“Hello”));
13.         add(p1);
14.   
15.        / / Build and add 2nd panel.
16.        Panel p2 = new Panel ( );
17.        p2.setLayout(new FlowLayout(FlowLayout.LEFT));
18.        p2.add(new Button(“Goodbye”));
19.       add(p2);
20.       }
21.   
22.       public static void main(String args[ ]) {
23.           Q3 that = new Q3 ( );
24.            that.setVisible(true);
25.       }
26.  }

A. The program crashes with an exception at line 7, because the frame’s default layout manager cannot be overridden.
B. The program crashes with an exception at line 7, because a Grid layout manager must have at least two rows and two columns.
C.  The program displays two buttons, which are just large enough to encompass their labels. The buttons appear at the top of the frame. The “Hello” button is just to the left of the vertical midline of the frame; the “Goodbye” button is just to the right of the vertical midline of the frame.
D. The program displays two large buttons. The “Hello” button occupies the entire left half of the frame, and the “Goodbye” button occupies the entire right half of the frame.
E. The program displays two buttons, which are just wide enough to encompass their labels. The buttons are as tall as the frame. The “Hello” button is just to the left of the vertical midline of the frame; the “Goodbye” button is just to the right of the vertical midline of the frame.  

Question 4.

If a frame uses its default layout manager and does not contain any panels, then all the components within the frame are the same width and height.

A. True
B. False
C. Component on the top will be greater in size than the bottom component
D.  Component at the bottom will be greater in size than the top  component
E. The components cannot be used as the frame does not have any panels

Question 5. A component subclass that has executed enableEvents( ) to enable processing of a certain kind of event cannot also use an adapter as a listener for the same kind of event.

A. True 
B. False
C. The Listener will have to be declared separately.  
D. Each process will have to run as a separate thread
E. The operation will be successful provided each thread is made to sleep and resume alternately
Question 6. A text field is constructed and then given a foreground color of white and a 64-point bold serif font. The text field is then added to an applet that has a foreground color of red, background color of blue, and 7-point plain sans-serif font. Which one statement below is true about the text field?

A. Foreground color is black, background color is white, font is 64-point bold serif.
B. Foreground color is red, background color is blue, font is 64-point bold serif.
C. Foreground color is red, background color is blue, font is 7-point bold serif.
D. Foreground color is white, background color is blue, font is 7-point bold serif.
E. Foreground color is white, background color is blue, font is 64-point bold serif.
Question 7.

How would you set the color of a graphics context called g to cyan?


A.  g.setColor(Color.cyan);
B.  g.setCurrentColor(cyan);
C.  g.setColor("Color.cyan");
D.  g.setColor("cyan");
E.  g.setColor(new Color(cyan)); 

Question 8.

When the GUI thread calls paint( ) in order to repair exposure damage, the paint( ) method must determine what was damaged and set its clip region appropriately.

A.  True
B.  False
C.  The refresh() method must be called immediately after the paint() method.
D.  The paintcanvas() sets the clip region appropriately.
E.  GUI thread calls repaint( ) in order to repair exposure damage.

Question 9. What does the following paint( ) method draw?

1.public void paint(Graphics g) {
2.g.drawOval(100, 100, 44);
3.}

A.  A circle at (100, 100) with radius of 44
B.  A circle at (100, 44) with radius of 100
C.  A circle at (100, 44) with radius of 44
D.  The code does not compile
E.   An ellipse at(50,22) with radius of 22

Question 10. Which of the statements below are true? (Choose none, some, or all.)

A. UTF characters are all 8 bits.  
B. UTF characters are all 16 bits. 
C. UTF characters are all 24 bits.   
D. Unicode characters are all 16 bits.  
E.  Bytecode characters are all 16 bits.

Question 11. You execute the code below in an empty directory. What is the result?
  1. File f1 = new File("dirname");
  2. File f2 = new File(f1, "filename");

A. A new directory called dirname is created in the current working directory. 
B.  A new directory called dirname is created in the current working directory. A new file called filename is created in directory dirname.
C.    A new directory called dirname and a new file called filename are created, both in the current working directory.
D.  A new file called filename is created in the current working directory.
E.  No directory is created, and no file is created. 

Question 12.

The File class contains a method that changes the current working directory.

A.  True
B.  False
C.  The Directory Class   contains a method that changes the current working directory.
D.  The File class contains a method called chgcwd() that changes the current working directory.
E.   Output streams have to be used to change the current working directory.

Question 13. How many bytes does the following code write to file destfile?
  1. try {
  2. FileOutputStream fos = new FileOutputStream("destfile");
  3. DataOutputStream dos = new DataOutputStream(fos);
  4. dos.writeInt(3);
  5. dos.writeDouble(0.0001);
  6. dos.close( );
  7. fos.close( );
  8. }
  9. catch (IOException e) { }

A.  2
B.  8
C.  12
D.  16
E.   The number of bytes depends on the underlying system.

Question 14. What is the result of attempting to compile and execute the code fragment below? Assume that the code fragment is part of an application that has write permission in the current working directory. Also assume that before execution, the current working directory does not contain a file called datafile.
  1. try {
  2. RandomAccessFile raf = new RandomAccessFile _ ("datafile"), "rw";
  3. BufferedOutputStream bos = new BufferOutput _ Stream(raf);
  4. DataOutputStream dos = new DataOutputStream(bos);
  5. dos.writeDouble(Math.PI);
  6. dos.close( );
  7. bos.close( );
  8. raf.close( );
  9. } catch (IOException e) { }

A.  The code fails to compile.
B.  The code compiles, but throws an exception at line 3.
C.  The code compiles and executes, but has no effect on the local file system.
D.  The code compiles and executes; afterward, the current working directory contains a file called datafile.
E.   The code compiles, but throws an exception at line 5.

Question 15. What does the following code draw?
  1. g.setColor(Color.black);
  2. g.drawLine(10, 10, 10, 50);
  3. g.setColor(Color.red);
  4. g.drawRect(100, 100, 150, 150);


A.  A red vertical line that is 40 pixels long and a red square with sides of 150 pixels.
B.  A black vertical line that is 40 pixels long and a red square with sides of 150 pixels.
C.  A black vertical line that is 50 pixels long and a red square with sides of 150 pixels.
D.  A red vertical line that is 50 pixels long and a red square with sides of 150 pixels.
E.  A black vertical line that is 40 pixels long and a red square with sides of 100 pixels.  

Question 16.

What code would you use to construct a 24-point bold serif font?

A.  new Font(Font.SERIF, 24, Font.BOLD);
B.  new Font("Serif", 24, "Bold");
C.  new Font("Bold", 24, Font.SERIF);
D.  new Font("Serif", Font.BOLD, 24);
E.  new Font("SERIF", Font.BOLD, 24);

Question 17. What does the following paint( ) method draw?
  1. public void paint(Graphics g) {
  2. g.drawString("qusetion #9", 10, 0);
  3. }

A.  The string "question#9", with its top-left corner at 10, 0.
B.  A little squiggle coming down from the top of the component, a little way in from the left edge.
C.  The string "question#9", with its top-left corner at 0,10.
D.  The Program fails to compile
E.   An IO exception is thrown and the system fails to respond.

Question 18. You want to construct a text area that is 80 character-widths wide and 10 character-heights tall. What code do you use?

A.  new TextArea(80, 10)
B.  new TextArea(10, 80)
C.   new Text(Area,10,80)
D.   new Text(l,b,10,80)
E.  None of the above

Question 19. A text field has a variable-width font. It is constructed by calling new TextField("iiiii"). What happens if you change the contents of the text field to "wwwww"? (Bear in mind that i is one of the narrowest characters, and w is one of the widest.)


A.  The text field becomes wider.
B.  The text field becomes narrower.
C.   The text field stays the same width; to see the entire contents you will have to scroll by using the and keys.
D.  The text field stays the same width,to see the entire contents you will have to scroll by using the text field’s horizontal scroll bar.
E.   The program throws and OutOfBounds() exception 

Question 20. Which statement or statements are true about the code fragment listed below? (Assume that classes F1 and F2 both implement the FocusListener interface.)
  1. TextField tf = new TextField("Not a trick question");
  2. FocusListener flis1 = new F1( );
  3. FocusListener fils2 = new F2( );
  4. tf.addFocusListener(flis1);
  5. tf.addFocusListener(flis2);
  6. tf.removeFocusListener(flis1);

A.  Lines 2 and 3 generate compiler errors.
B.  Line 6 generates a compiler error.
C.  Line 5 throws an exception at run time.
D.  Line 6 throws an exception at run time.
E. The code compiles cleanly and executes without throwing an exception.

 

  scores