Java - Unit 4,5 Quiz
Java - Unit 4,5 Quiz
1. import java.util.*;
2. class Arraylist
3. {
4. public static void main(String args[])
5. {
6. ArrayList obj = new ArrayList();
7. obj.add("A");
8. obj.add("B");
9. obj.add("C");
10. obj.add(1, "D");
11. System.out.println(obj);
12. }
13. }
a) [A, B, C, D]
b) [A, D, B, C]
c) [A, D, C]
d) [A, B, C]
import java.util.*;
class Output
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.add(0, "B");
System.out.println(obj.size());
}
}
a)0
b)1
c)2
d) Any Garbage Value
14. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("D");
obj.ensureCapacity(3);
obj.trimToSize();
System.out.println(obj.size());
}
}
a)1
b)2
c)3
d) 4
15. Map implements collection interface?
a) True
b) False
16. Which of the below does not implement Map interface?
a) HashMap
b) Hashtable
c) EnumMap
d) Vector
17. How can we remove an object from ArrayList?
a) remove() method
b) using Iterator
c) remove() method and using Iterator
d) delete() method
18. How to remove duplicates from List?
a) HashSet<String> listToSet = new HashSet<String>(duplicateList);
b) HashSet<String> listToSet = duplicateList.toSet();
c) HashSet<String> listToSet = Collections.convertToSet(duplicateList);
d) HashSet<String> listToSet = duplicateList.getSet();
27. How many bits are used for generating random numbers?
a) 32
b) 64
c) 48
d) 8
28. What will be the output of the following Java code snippet?
int a = random.nextInt(15) + 1;
a) Random number between 1 to 15, including 1 and 15
b) Random number between 1 to 15, excluding 15
c) Random number between 1 to 15, excluding 1
d) Random number between 1 to 15, excluding 1 and 15
30. Which of the following is the default Layout Manager for an Applet?
(a) FlowLayout
(b) BorderLayout
(c) GridLayout
(d) CardLayout
31. Which Scanner class method is used to read interger values from the user?
(a) next()
(b) nextInt()
(c) readInt()
(d) nextInteger()
31. Which Scanner class method is used to read string values from the user?
(a) nextLine()
(b) next()
(c) nextString()
(d) Both a and b
32. Which of these class object has an architecture similar to that of array?
a) Bitset
b) Map
c) Hashtable
d) All of the above
33. Which of these method is used to calculate number of bits required to hold the BitSet
object?
a) size()
b) length()
c) indexes()
d) numberofBits()
34. Which of these class object uses the key to store value?
a) Dictionary
b) Map
c) Hashtable
d) All of the mentioned
35. Which of these is the interface of legacy is implemented by Hashtable and Dictionary
classes?
a) Map
b) Enumeration
c) HashMap
d) Hashtable
36. Which of these is a method of class Date which is used to search whether object contains a
date before the specified date?
a) after()
b) contains()
c) before()
d) compareTo()
37. Which of these methods is used to retrieve elements in BitSet object at specific location?
a) get()
b) Elementat()
c) ElementAt()
d) getProperty()
38. Which of these class object can be used to form a dynamic array?
a) ArrayList
b) Map
c) Vector
d) ArrayList & Vector
39. Which of these methods is used to add elements in vector at specific location?
a) add()
b) set()
c) AddElement()
d) addElement()
40. Which of these methods can be used to delete the last element in a LinkedList object?
a) remove()
b) delete()
c) removeLast()
d) deleteLast()
Layout Manger and Event handling
1. Which of these packages contains all the classes and methods required for even handling
in Java?
a) java.applet
b) java.awt
c) java.event
d) java.awt.event
12. Which of these methods can be used to obtain the command name for invoking
ActionEvent object?
a) getCommand()
b) getActionCommand()
c) getActionEvent()
d) getActionEventCommand()
13. Which of these methods can be used to know which key is pressed?
a) getKey()
b) getModifier()
c) getActionKey()
d) getActionEvent()
16. Which of these methods can be used to obtain the coordinates of a mouse?
a) getPoint()
b) getCoordinates()
c) getMouseXY()
d) getMouseCordinates()
18. Which of these methods is used to obtain the object that generated a WindowEvent?
a) getMethod()
b) getWindow()
c) getWindowEvent()
d) getWindowObject()
23. Which of these packages contains all the event handling interfaces?
a) java.lang
b) java.awt
c) java.awt.event
d) java.event
24. Which of these interfaces handles the event when a component is added to a container?
a) ComponentListener
b) ContainerListener
c) FocusListener
d) InputListener
28. Which of these methods will respond when you click any button by mouse?
a) mouseClicked()
b) mouseEntered()
c) mousePressed()
d) all of the mentioned
32. Classes that provides an empty implementation of all methods in an event listener
Interface
a) Adaptor class
b) SubClass
c) Inner Class
d) None of the above
33. How do you change the current layout manager for container?
a) use setLayout()
b) once created you cannot change it
c) you set setLayoutManager()
d) use updateLayout()
36. What method is used to prevent a user from changing the size of a Frame() object?
a) setResizable(true)
b) setResizable(false)
c) setEditable(true)
d) setEditable(false)
37. What pattern does the FlowLayout layout manager use to add components to a container
a) Left to right, top to bottom
b) bottom to top, right to left
c) top to bottom, centered in each row
d) setSize(int height, int width
40. After adding components to the Panels of a GUI, what must be done next?
a) Nothing – the GUI work is done
b) The components must be instantiated
c) The Panels must be added to the Frame
d) The layout manager must now be set
41. Can GUI components inside of a Panel have a registered listener?
a) No – only those components directly added to a Frame can have listeners
b) No – only Frames can have listeners
c) Yes – it doen’t matter if a component is contained in a Panel
d) Yes – but the Panel must be the listener
42. Say that a Frame contains three Panels. Could each Panel have a different layout manager
a) No – they must all use the default layout manager
b) No – they must all use the same layout manager
c) Yes – each one must be different
d) Yes – each one use setLayout() with any layout manager
43. Which of the following commands will set a layout manager that divides the container into
3 rows and 4 columns?
a) setLayout(new GridLayout(3,4));
b) setLayout(new GridLayout(4,3));
c) setLayout(4,3)
d) setLayout(new FlowLayout(4,3));
44. How many GUI components can go into each cell with GridLayout?
a) None
b) 1
c) Only one Panel per cell
d) As many as you need
45. Which layout manager places the components in one of the five regions : north, south,
east, west and centre
a) Gridayout
b) FlowLayout
c) BorderLayout
d) CardLayout
46. Which of the following steps are must for any kind of event handling?
a) Implement the listener method to receive and process event related notifications
b) Adding mouse listener in the init() method.
c) Register the source to receive notifications about specific type of events.
d) Adding mouse motion listener in the main() method
47. Which of the following packages supports event handling routine, when we use swing
component in GUI programming?
a) javax.swing
b) java.awt
c) java,applet
d) java.util
48. Which of the following displays components row-by-row in the order in which they were
added to the JFrame
a) Gridayout
b) BorderLayout
c) FlowLayout
d) CardLayout
49. Using the FlowLayout manager, which is the correct way to add elements to a container?
a) add(component);
b) add(“Center”,component);
c) add(s,y,component);
50. which of the following class is used to create a pop-up list of items from which a user may
choose?
a) JComboBox
b) JList
c) JLabel
d) JCheckBox
51. Which of the following method is used to set a frame say f, with size 300 x 200 pixels
a) f.setSize(300,200);
b) f.setSize(200,300);
c) f.paint(300,200);
d) f.setVisible
52. What is (are) the way(s) to create a Frame using Java Swing?
a) by creating the object of the Frame class (association)
b) by extending the Frame class (inheritance)
c) by importing a package named JFrame
d) by declaring a class with the name JFrame
53. In Java AWT, TextArea and TextField are the subclass of
a) List
b) Label
c) TextComponent
d) TextBox
55. Which of the following package provides many methods for graphics programming?
a) java.awt
b) java.Applet
c) java.Graphics
d) java.io
59. Which of the following classes can be used to represent checkbox with a textual label that
can appear in a menu?
a) MenuBar
b) MenuItem
c) CheckBoxMenuItem
d) Menu
60. When we invoke repaint() for java.awt.Component object, the AWT invokes which of the
following methods
a) draw()
b) show()
c) update()
d) paint()