You are on page 1of 7

Practice exam 2010

MCQ
1. What layout manager should you use so that every component occupies the same size in the container?

A. a FlowLayout
B. a GridLayout
C. a BorderLayout
D. any layout

2. What layout allows to add button defined by button text, and NOT affected by the Frame size?

A. a FlowLayout
B. a GridLayout
C. a BorderLayout
D. any layout

3. Which statement sets a FlowLayout to frame jp

A. jp.setlayout(new FlowLayout());
B. jp.setLayout( new FlowLayout());
C. jp.setLayout(new Flowlayout());
D. jp.setLayout(FlowLayout());

4. What should you import to create a window which extends JFrame class

A. java. awt.*;
B javax.awt.JFrame;
C. javax.swing.*;
D. java.util.*;

5. The default layout out of a JFrame is

A. FlowLayout
B. GridLayout
C. BorderLayout
D. None

6. To set the background color to yellow in JPanel pl.

A. pl.setBackGround(Color.yellow)
B. pl.setBackground(Color.YELLOW)
C. pl.setBackgroundColor(Color.yellow)
D. setBackground(Color.YELLOW)

7. To set the font to Arial bold, 20-point in size to component c.

A. c.setFont(new Font("arial", Font.bold, 20))


B. c.setFont(new Font("ARIAL", BOLD, 20))
C. c.setFont(new Font("Arial", Font.BOLD, 20))
D. c.setFont( Font("Arial", Font.BOLD, 20))

8. To specify a font to be bold and italic, use the font style value

A. Font.PLAIN
B. Font.BOLD
C. Font.ITALIC
D. Font.BOLD + Font.ITALIC
9. To set text on button jbt to blue color you use

A. jbt.setColor(Color.blue);
B. jbt.setFontColor(Color.blue)
C. jbt.setForeground(Color.blue)
D. jbt.color = “blue”;

10. To change text on button jbt to “Submit” you use

A. jbt.setText(“Submit”);
B. jbt.changeText(“Submit”);
C. jbt.text = “Submit”;
D. jbt.put(“Submit”);

11.  To add a component c to a JPanel p, you use

A. p.add(c)
B. p.getContentPane(c)
C. p.insert(c)
D. p.append(c)

12. To create an image icon for a file flag.jpg you use

A. Icon icon = new Image("flag.jpg");


B. ImageIcon icon = new ImageIcon("flag.jpg");
C. ImageIcon = new ImageIcon("flag.jpg");
D. Icon icon = new Icon("flag.jpg");

13. Which event is generated by pressing a button

A. KeyEvent
B. MouseEvent
C. MouseMotionEvent
D. ActionEvent

14.  Which of the following statements registers a panel p as a listener for a button variable jbt?

A. addActionListener(p);
B. jbt.addActionListener(p);
C. jbt.addActionEventListener(p);
D. jbt.addEventListener(p);

15. Which interface should be implemented to listen for a button action event.

A. MouseListener
B. ActionListener
C. FocusListener
D. WindowListener

16. Which method reads the contents of the text field jtf.

A. jtf.getText(s)
B. jtf.getText()
C. jtf.getString()
D. jtf.findString()

17. Which method will display a string s into the text area jta.

A. jta.setText(s)
B. jta.appendText(s)
C. jta.append(s)
D. jta.insertText(s)
18. Which method returns the selected item on a JComboBox jcbo.
A. jcbo.getSelectedIndex()
B. jcbo.getSelectedItem()
C. jcbo.getSelectedIndices()
D. jcbo.getSelectedItems()

19. What event generates clicking a JCheckBox object

A. CheckBoxEvent
B. ItemEvent
C. ComponentEvent
D. ContainerEvent

20. The method __________ gets the text (or caption) of the button jbt.

A. jbt.text()
B. jbt.getText()
C. jbt.findText()
D. jbt.retrieveText().

21. To create a button with an image stored in a file pic.jpg you use

JButton btn = new JButton(new Image("pic.jpg"));


JButton btn = new JButton (Image("pic.jpg"));
JButton btn = new JButton ("pic.jpg");
JButton btn = new JButton (new ImageIcon("pic.jpg"));

22. Which event is generated by pressing a JMenuItem


KeyEvent
MouseEvent
MouseMotionEvent
ActionEvent

23. Your application implements ActionListener interface. Which statement registers a button bt
with ActionListener?

bt.addActionListener(this);
bt.add(this);
bt.addActionEvent (this);
bt.addEventListener(this);

24. Which statement is correct for JComboBox box

int num = box.getSelectedItem();


double num = box.getSelectedIndex();
int num = box.getSelectedIndex();

25. Class Aclass is subclass of Bclass. Class Aclass must start with
class Aclass implements Bclass{}
class Aclass extend Bclass{}
class Bclass extends Aclass{}
class Aclass extends Bclass{}

26. Which class allows binary reading of objects ObjectOutputStream


WriteObjectStream
ObjectOutputStream
DataOutput Stream
FileOutputStream

27. To sort ArrayList list you use


bubbleSort(list);
Collections.sort(list);
list.sort();
ArrayList.sort()

28. Class contains a method public double getMoney(int rate, double amount);
Which one is overloaded method
public double getMoney (int amount, double rate);
public double getMoney(int percentage, double amount);
public double getMoney (double rate, double amount);
none of above

29. To add an object obj to a ArrayList list


A. list.put(obj)
B. obj.insertTo(list);
C. list.add(obj);
D. list.add(Object obj);

30. The layout which allows to add not more than 5 components is
E. GridLayout (1, 5)
F. BorderLayout()
G. BorderLayout(5)
H. A and C

Short-answer questions (20 marks)

1. A user compiles and runs the Java application below that displays what an animal eats depending on its kind:
cat or dog. Write down all lines (input and output) that will be displayed in the console window (10 marks)

import java.util.*;

abstract class Animal


{
String name;

public Animal(String n){


name = n;
System.out.println("Animal class");
}
abstract void eat();
}
class Cat extends Animal{
public Cat(String n){
super(n);
System.out.println("Cat class");
}
void eat(){
System.out.println("Cat " + name + " loves sardines");
}
}

class Dog extends Animal


{
public Dog(String n){
super(n);
System.out.println("Dog class");
}
void eat(){
System.out.println("Dog " + name + " loves meaty bones");
}
}
public class TestAnimal{
public static void main(String[] args){
Animal an;
Scanner scan = new Scanner(System.in);
int anw= 1;
String name=null, type=null;
while ( anw > 0 ) {
System.out.println("Enter 'cat' or 'dog'");
type = scan.next();
System.out.println("Enter name");
name = scan.next();

if ( type.equals("cat"))
an = new Cat( name );
else
an = new Dog( name);
an.eat();
System.out.println("Enter 1 to continue or 0 to exit");
anw = scan.nextInt();
}
}
}

2. The following two classes contain 10 syntax errors, please find and correct them.
/**Selection sort in descending order*/
public class SelectionSort {
/** The method for sorting the numbers */
static selectionSort(int[] list {
for (int i = list.length - 1; i >= 1; i--) {
// Find the maximum in the list[0..i]
int currentMin = list[0]
int currentMinIndex = 0;

for (int j = 1, j <= i; j++) {


if (currentMin > list) {
currentMin = list[j];
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMaxIndex] if necessary;
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
/** The method for printing numbers */
static void printList(int list) {
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
/** Main method */
public static void main(String[] args) {
// Initialize the list
int myList = {5, 13, 76, 2, 21};
// Print the original list
System.println("My list before sort is: ");
printList(myList);
// Sort the list
selectionSort();
// Print the sorted list
System.out.println();
System.out.println("My list after sort is: ");
printList();
}
}

Program questions (5 + 5 = 10 marks)

1. Write a code for a class WaterTank with attributes fullness and capacity both in litres as double.
Write two constructors (default one and constructor with two parameters)
Write set/get methods for both attributes.
Write custom methods fillTank and drainTank with amount of water in litres as a parameter.
Return type for fillTank is boolean and it returns false if (amount +fullness) is greater than capacity,
otherwise it adds amount to fullness and returns true.
Return type for drainTank is also boolean and returns false if (fullness-amount) is less than zero,
otherwise it reduces fullness by amount and returns true.

2. Write a program which will display a window with a two text fields to enter first name and last name,
two labels (as prompt for text fields), and a button “Register”. Your class must extend JFrame. Add
action listener to the button. When the button is clicked the text entered in text fields is displayed on a
third label, for example “Welcome John Ward!!!”.

You might also like