UNIVERSITY OF GHANA
(All right' reserved)
BACHELOR OF SCIENCE IN ENGINEERING
SECOND SEMESTER EXAMINATION 2014/2015
CPEN 312: OBJECT ORIENTED PROGRAMMING USING JAVA (3 Credits)
TIME ALLOWED: TWO HOURS
INSTRUCTION: Answer all questions on the question paper. No ANSWER BOOKLET
needed for this paper.
SECTION A 123 MARKS]
TruelFalsc
Indicate whether the statement is true or false in the spaces (~ provided before the questions.
I. Application classes frequently instantiate objects that use the objects of other classes.
2. You can identifY a class that is an application because it contains a public static void main ()
method.
3. You can write your own constructor methods; but when you don't write a constructor method for a class
object, Java writes one for you.
4. A method's identifier must be more than one word, must have no embedded spaces, and cannot be a Java
keyword.
5. Parentheses in a method declaration contain parameters that are funneled into the method.
6. When a value is returned from a method, you are required to use the value when the method is called.
7. When you overload a Java method, you write multiple methods with a shared name.
8. When you write your own constructors, you cannot write versions that receive parameters.
9. An alternative to importing a class is to import an entire package of classes.
10. If you give the same name to a class's instance field and to a local method variable, the instance variable
overrides the method's local variable.
II. When you use the method name with a child object, the parent's version of the method is used.
Examiner: Isaac Kwadwo Nti, Phd Page 1 019
12. If a superclass contains only constructors that require arguments, you do not need to create a subclass
constructor.
13. A static method cannot be overridden in a subclass.
14. In a UML diagram, minus signs are inserted in front of each private field or method, and plus signs are
inserted in front of each public field or method.
15. Superclasses can be distinguished from their subclasses because a subclass is generally smaller than a
superclass; it usually has fewer fields and methods.
16. It is useful to override the parent class members when the superclass data fields and methods are not
completely appropriate for the subclass objects.
17. When a protected data field or method is created, it can be used within its own class or in any classes
extended from that class; but it cannot be used by outside classes.
18. An error is generated by the compiler when you attempt to override a static method with a nonstatic
method.
19. When you create a subclass of an abstract class, it's important to understand that you are required to code
a subclass method to override the empty superclass method that is inherited.
20. When you create a class that uses an interface, you include the keyword extends.
21. Abstract classes and interfaces are similar in that you cannot instantiate concrete objects from either one.
22. While a class can inherit from any abstract superclass, it can only implement one interface.
23. Java does not allow a class to inherit directly from two or more parents.
Examiner: Isaac Kwadwo Nti, Phd Page 2 of 9
SECTION B [ 77 MARKS)
Multiple Choice
IdentifY the choice that best completes the statement or answers the question. Write the LETTER (A, B, Cor DJ oj
. your choice in the spaces provided beJore the questions.
I. When you create a class by making it inherit from another class, you are provided with data fields and
_ _ automatically.
a. fonts c. class names
b. methods d. arrays
2. The class used as a basis for inheritance is the c.lass.
a. child c. base
b. extends d. derived
3. You use the keyword _ _ to achieve inheritance in Java.
a. inherit c. super
b. extends d. public
4. If a programming language does not support ~ the language is not considered object-oriented.
a. syntax c. loops
b. applets d. polymorphism
5. When you create a class and do not provide a(n) ~ Java automatically supplies you with a default one.
a. constructor c. header
b. argument d. name
6. Usually, the subclass constructor only needs to initialize the _ _ that are specific to the subclass.
a. objects c. methods
b. data fields d. constructors
7. Within a subclass, you can think of the keyword _ _ as the opposite of super.
a. sub c. p::::otected
b. this d. not
8. When you employ ~ your data can be altered only by the methods you choose and only in ways that
you can control.
a. virtual method calls c. information hiding
b. polymorphism d. inlining
9. Using the keyword _ _ provides you with an intermediate level of security between public and
private access.
a. protected c. super
b. this d. secure
10. The methods in a subclass can use all of the data fields and methods that belong to its parent, with one
exception: _ _ members of the parent class are not accessible within a child class's methods.
a. private c. final
b. public d. protected
II. If a _ _ method has the same name as a parent class method and you use the name with a child c1uss
object, the child method hides the original.
a. final c. protected
Examiner: Isaac Kwadwo Nti, Phd Page 3 of9
b. static d. private
12. You can use the _ _ modifier with methods when you don't want the method to be overridden.
a. override c. final
b. access d. end
13. You would like to make a member ofa class visible in all subclasses regardless of what package they are
in. Which one of the following keywords would achieve this?
a. privateorprotected c. protected
b. private d. public
14. A(n) _ _ class is one from which you cannot create any concrete objects, but from which you can
inherit.
a. abstract c. parent
b. inheritance d. final
15. You sometimes create an abstract class only so you can _ _ it to another class.
a. link c. extend
b. copy d. abstract
16. Which of the following statements creates an array of three Anima 1 references?
a. Animal[] ref = new Animal(3);
b. Animal[] new = ref Animal[3];
c. Animal[] animalRef = new Animal[3];
d. Animal[] ref = extend Animal[3];
17. When you define a class, if you do not explicitly extend another class, your class is an extension of the
class.
a. Object c. public
b. super d. abstract
18. [[Java did not allow you to _ _ classes, you would need to create every part ofa program from scratch.
a. extend c. abstract
b. override d. import
19. When you create a new subclass in Java, neither the superclass source code nor the superclass ___ is
changed.
a. bytecode c. argument
b. object d. possession
20. The capability to inherit from more than one class is called _ _.
a. multiple inheritance c. polyinheritance
b. collision d. dynamic method binding
21. When you create a class and use the implements clause to implement an interface but fail to code one
of the interface's methods, the compiler error generated indicates that you must declare your class to be
a. abstract c. public
b. static d. related
22. If you do not specify a package for a class, it is placed in an unnamed _ _ package.
a. system c. default
b. language d. general
Examiner: ISClac Kwadwo Nti, Phd Page 4 of 9
23. Which of the following is NOT true regarding abstract methods?
a. Abstract methods must be implemented in c. Abstract methods use the abstract
child classes. keyword in declarations.
b. Abstract methods have no body. d. Abstract methods are inherited by their
children.
24. The Swing classes are part ofa more general set ofUI programming capabilities that are collectively
called the _ _
a. JavaX Classes c. UI packages
b. Java Foundation Classes d. Java Extension Libraries
25. What class is the immediate parent of JFrame?
a. Container c. Frame
b. Component d. Window
26. You use the method to obtain the title ofa JFrame.
a. getTi tle () c. setTi tIe ()
b. title () d. setVisible ()
27. You can call a JFrame's setDefaul tCloseOperation () method and use _ _ as an argument to
keep the JFrame visible and continue running the program.
a. JFrame.EXIT ON CLOSE
b. WindowConstants.DISPOSE ON CLOSE
c. WindowConstants.DO NOTHING ON CLOSE
-
d. WindowConstants.HIDE ON CLOSE
- -
28. The JLabe I constructor _ _ creates a JLabe I instance with the specified image and horizontal
alignment.
a. JLabel(String text, int horizontalAlignment)
b. JLabel(Icon image)
c. JLabel ()
d. JLabel (Icon image, int horizonta.IAlignment)
29. What is the default layout manager for JFrame?
a. BorderLayout c. FrameLayout
b. FlowLayout d. GridLayout
30. What is the immediate parent class of JTextField?
a. JTextComponent c. Container
b. JComponent d. JFrame
31. A _ _ is a Component the user can click with a mouse to make a selection.
a. JCheckBox c. JLabel
b. JButton d. JComboBox
32. You define the text to be displayed in a tool tip by using the setToolTipText () method and passing
an appropriate _ _ to it.
a. Integer c. String
b. Date d. Boolean
33. Within an event-driven program, a component on which an event is generated is the _ _ of the event.
a. handler c. listener
b. driver d. source
Examiner: Isaac I(wadwo Nti, Phd Page 5 of9
34. The ActionListener interface contains the _ _ method specification.
a. actionOccurred(Action e)
b. actionPerformed(ActionEvent e)
c. actionEvent(ActionEvent e)
d. action(Event e)
3 S. The _ _ responds to keyboard focus events.
a. KeyListener c. ChangeListener
b. ActionListener d. FocusListener
36. The _ _ responds to keyboard events.
a. KeyListener c. ChangeListener
h. ActionListener d. FocusListener
37. The JButton, JCheckBox, JComboBox, and JRadioButton components are associated with the
method.
a. addAdjustmentListener() c. addWindowListener()
b. addchangeListener() d. addltemListener()
38. A method that executes because it is called automatically when an appropriate event occurs is an event
a. responder c. handler
b. listener d. source
39. The method of JCheckBox sets the state of the JCheckBox to true for selected or false for
unselected.
a. void setSelected(boolean) c. void setText(String)
b. boolean isSelected() d. String getText()
40. Within an implementation of the i temStateChanged () method, you can use the _ _ method to
determine which object generated the event, and the getStateChange () method to determine
whether the event was a selection or a deselection.
a. getSource() c. getltem()
b. . getEvent () d. getState ()
41. When you create a(n) ---' you can group several components, such as JCheckBoxes, so a user can
select only one at a time.
a. CheckGroup c. OptionBox
b. ButtonGroup d. OptionGroup
42. A _ _ is a component that combines two features: a display area showing an option, and a list box
containing additional options.
a. JCheckBox c. JButton
b. JComboBox d. JTextBox
43. The _ _ method of JComboBox returns the maximum number of items the combo box can display
without a scroll bar.
a. getSelectedltem() c. getMaximumRowCount()
b. getltemCount() d. getSelectedlndex()
44. Which of the following statements will correctly set a JFrame named myFrame to be visible?
a. myFrame.Visible = True c. myFrame.Visibility.True
EX2miner: Isaac Kwadwo Nti, Phd
Page 60f9
b. myFrame.setVisible(true) d. myFrame.true = Visible
45. Which ofthe following statements will change the value displayed in a JLabel named hello?
a. hello.setText{"Hello"); c. hello = setText("Hello");
b. setText. hello = "Hello II d. setText = hello
46. When constructing a Font object, which of the following arguments is NOT required?
a. typeface c. weight
b. style d. point size
47. A component can be made available or unavailable by passing true or false to the _ _ method.
L getSource() c. setSelected()
b. setEnabled() d. setBounds()
48. A _ _ is placed at the top of a container and contains user options.
a. glass pane c. content pane
b. menu bar d. containment hierarchy
49. Java automatically converts the add (), remove (), and setLayoutManager () statements to more
complete versions that include ___.
a. getContentPane() c. getJFrame()
b. glassPane() d. addAll()
50. Use the _ _ layout manager when you need to add components that are displayed one at a time.
a. BorderLayout c. GridBagLayout
b. GridLayout d. CardLayout
51. Use the _ _ layout manager when you add components to a maximum of five sections.
a. BorderLayout c. GridBagLayout
b. GridLayout d. CardLayout
52. What is the parent class of JPanel?
a. Object c. \.1Component
b. Component d. Container
53. When components in a Swing UI require more display area than they have been allocated, you can use a
_ _ container to hold the components and allow the user to display the components using scroll bars.
a. JScrollingPane c. JPanel
b. ScrollLayout d. JScrollPane
54. The parent class for all event objects is named ~ which descends from the Obj ect class.
a. EventObject c. ParentEvent
b. Event d. AWTEvent
55. Clicking an item in a list box results in aen) _ _.
a. ItemEvent c. ActionEvent
b. WindowEvent d. MouseEvent
56. Clicking a component results in aen) _ _.
a. ItemEvent c. ActionEvent
b. WindowEvent d. MouseEvent
57. The focusGained (FocusEvent) handler is defined in the interface.
a. FocusListener c. AdjustmentListener
Examiner: Isaac Kwadwo Nti, Phd Page 7 of 9
b. ComponentListener d. ActionListener
58. When you type "A", two _ _ key codes are generated: Shift and "a".
a. action c. event
b. virtual d. default
59. If you wanted to see the x-coordinate of a user click, you would use the _ _ method of the
MouseEvent class.
a. getClick () c. gety ()
b. getX () d. getHoriz ()
60. You use the getModifiers () method with an InputEvent object, and you can assign the return
value to a(n) _ _ variable.
a. String c. int
b. boolean d. double
61. The parent class of Mouse Event is _ _.
a. AWTEvent c. InputEvent
b. EventObject d. UserEvent
62. Each JMenu can contain options, called JMenultems, or can contain submenus that are _ _.
a. JMenuBars c. JSubMenus
b. JMenuChildren d. JMenus
63. The JMenus are added to the JMenuBar using the _ _ method.
a. addMenu() c. addNewMenu()
b. add () d. setMenu ()
64. The state ofa JCheckBoxMenultem or JRadioButtonMenultem can be determined with the
method.
a. state () c. getSelected ()
b. getState () d. isSelected ()
65. Which of the following statements will set the background color ofa button named stop to a color of
red?
a. stop. setBackground (Color. RED) i c. red. setBackground (Color. RED) i
b. stop.Backcolor = RED; d. setBack. stop. Color. RED;
66. Which of the following is NOT a method of the KeyListener interface?
a. keyTyped() c. keyClicked()
b. keyPressed() d. keyReleased()
67. A(n) _ _ implements all methods in an interface and provides an empty body for each method.
a. action key c. mnemonic
b. viewport d. adapter class
68. Which of the following statements will correctly add a JMenuBar named myBar to a JFrame?
a. my8ar = setJMenuBar c. JMenuBar.setJMenuBar(myBar)
b. setJMenuBar(myBar) d. JMenuBar = new
JMenuBar (myBar)
Examiner: isaac Kwadwo Nti, Phd Page 8 of 9
69. The repaint () method calls another method named ~ which calls the paint () method.
a. destroy () c. update ()
b. actionPerformed() d. super()
70. Ifa window is 200 pixels wide by 100 pixels tall, you can place a Button named pressMe in the
approximate center of the window with which of the following statements?
a. pressMe.setLocation(100,50);
b. pressMe.setPosition(100,50);
~ pressMe.setLocation(200,100);
d. pressMe.setPosition(200,100);
71. The method allows you to draw a String in a JFrame window.
a. drawString() c. drawLine()
b. set Font () d. setString ()
72. You can instruct a Graphics object to use a font by inserting the font as the argument in a _ _ method.
a. setColor () c. setCharacters ()
b. drawString() d. setFont()
73. The _ _ method requires four arguments and draws the outline of a rectangle using the current drawing
color.
a. clearRect () c. fillRect ()
b. drawRect () d. paintRect ()
74. The method draws what appears to be an empty rectangle.
a. clearRect () c. fillRect ()
b. emptyRect() d. drawRect()
75. The drawRoundRect () method requires _ _ arguments.
a. three c. six
b. four d. eight
76. When using the drawRoundRect () method, if you assign _ _ to the arc coordinates, the rectangle is
not rounded; instead, the comers are square.
a. -I c. 10
b. ° d. 99
77. You can instantiate an empty Polygon object using which of the following statements?
a. Polygon some Future Shape new Polygon(O,O, 0,0);
b. Polygon someFutureShape new Polygon();
c. Polygon someFutureShape new Polygon(O);
d. Polygon some Future Shape new Polygon{xPoints, yPoints,
xPoints.length) ;
Examiner: Isaac Kwadwo Nti, Phd Page 9 of 9