Java Questions And Answers VI

 

Q. 36

Which of the following methods are defined on the Graphics class:

  1. drawLine(int, int, int, int)
  2. drawImage(Image, int, int, ImageObserver)
  3. drawString(String, int, int)
  4. add(Component);
  5. setVisible(boolean);
  6. setLayout(Object);

Select all correct answers.      

Ans:A,B,C

Q. 37

Which of the following layout managers honours the preferred size of a component:

  1. CardLayout
  2. FlowLayout
  3. BorderLayout
  4. GridLayout

Select all correct answers.

Ans:B

 

Q. 38

Given the following code what is the effect of a being 5:

 

public class Test {

public void add(int a) {

loop: for (int i = 1; i < 3; i++){

for (int j = 1; j < 3; j++) {

if (a == 5) {

break loop;

}

System.out.println(i * j);

}

}

}

}

 

  1. Generate a runtime error
  2. Throw an ArrayIndexOutOfBoundsException
  3. Print the values: 1, 2, 2, 4
  4. Produces no output

Select the most appropriate answer.

Ans:D.

 

Q. 39

What is the effect of using a wait() method on an object

  1. If a notify() method has already been sent to that object then it has no effect
  2. The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
  3. An exception will be raised
  4. The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.

Select the most appropriate answer.

Ans:B

 

Q. 40

The layout of a container can be altered using which of the following methods:

  1. setLayout(aLayoutManager);
  2. addLayout(aLayoutManager);
  3. layout(aLayoutManager);
  4. setLayoutManager(aLayoutManager);

Select all correct answers.

Ans:A

 

Q. 41

Using a FlowLayout manager, which is the correct way to add elements to a container:

  1. add(component);
  2. add("Center", component);
  3. add(x, y, component);
  4. set(component);

Select the most appropriate answer.

Ans:A

 

Q. 42

Given that a Button can generate an ActionEvent which listener would you expect to have to implement, in a class which would handle this event?

  1. FocusListener
  2. ComponentListener
  3. WindowListener
  4. ActionListener
  5. ItemListener

Select the most appropriate answer.

Ans:D

 

Q. 43

Which of the following, are valid return types, for listener methods:

  1. boolean
  2. the type of event handled
  3. void
  4. Component

Select the most appropriate answer.

Ans:C

 

Q. 44

Assuming we have a class which implements the ActionListener interface, which method should be used to register this with a Button?

  1. addListener(*);
  2. addActionListener(*);
  3. addButtonListener(*);
  4. setListener(*);

Select the most appropriate answer.

Ans:B

 

Q. 45

In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:

  1. paint()
  2. repaint()
  3. paint(Graphics)
  4. update(Graphics)
  5. None – you should never cause paint(Graphics) to execute

Select the most appropriate answer.

Ans:B

Q. 51

Consider the following example:

 

class First {

public First (String s) {

System.out.println(s);

}

}

public class Second extends First {

public static void main(String args []) {

new Second();

}

}

 

What is the result of compiling and running the Second class?

  1. Nothing happens
  2. A string is printed to the standard out
  3. An instance of the class First is generated
  4. An instance of the class Second is created
  5. An exception is raised at runtime stating that there is no null parameter constructor in class First.
  6. The class second will not compile as there is no null parameter constructor in the class First

Select the most appropriate answer.