Java Class Quiz Set
(103) The catch clause has _____ parameter(s) a. zero b. one c. two d. unfixed number, determined by user
b
(103R) The execution of a throw statement is referred to as _____ a. catching a block b. trying a block c. handling an exception d. throwing an exception
z
(105-9) Assume that a LinkedList is used for a queue implementation and has the strings "1","2","3","4","5" added to it in that order. The peek method results in the string (Answer verified) a. "1" b. "2" c. "4" d. "5"
a. "1"
(105-10) Given an empty ArrayDeque of String objects, myDeque, the strings "1","2","3","4","5" added to it using the addFirst and addLast methods alternatively as shown below. myDeck.addFirst("1"); myDeck.addLast("2"); myDeck.addFirst("3"); myDeck.addLast("4"); After invoking the removeFirst and removeLast methods, the peek method results in a. 1 b. 2 c. 3 d. 4
a. 1 peek() returns the head of the queue for ArrayDeque. head of the queue is index position 0
(EASY) (103) The method trim() of the String class trims of a. Leading white spaces b. Trailing white spaces c. Leading and trailing white spaces d. None of the choices is true
c
(105-6) Given an empty LinkedList of Integer objects, myList, the values 1,2,3,4,5 are added to it using the addLast method myList.addLast(1); myList.addLast(2); myList.addLast(3); myList.addLast(4); myList.addLast(5); The expression myList.get(1) + myList.get(2) results in (Answer verified) a. 9 b. 7 c. 5 d. 3
c. 5
(105-8) Assume that a LinkedList is used for a queue implementation and has the strings "1","2","3","4","5" added to it in that order. After invoking the remove, the peek method results in the String (Answer verified) a. "1" b. "2" c. "4" d. "5"
d. "2"
(105-7) Suppose a Stack of String objects, myStack, has the strings "1","2","3","4","5" pushed onto it in that order. After invoking the peek() method, the pop() method results in the string (Answer verified) a. "1" b. "2" c. "4" d. "5"
d. "5"
(103R) In Java, when you open a text file you should account for a possible a. FileNotFoundException b. FileFullException c. FileNoteReadyException d. All of the given choices
z
(102) A variable whose meaning is confined to a method definition is called an/a a. local variable b. global variable c. none of the choices d. instance variable
a
(102) A variable whose meaning is continued to an object of a class is called a. instance variable b. local variable c. global variable d. static variable
a
(102) A method that performs some actions but does not return a value can be declared a _____ method. a. null b. void c. public d. private
b
(103R) A runtime exception is a _____ a. checked exception b. unchecked exception c. offending exception d. none of the given choices
b
(103R-1) Among the given choices, which block immediately follows a try block? a. try b. catch c. fail d. final
b
(102) The Object class and the Integer class both have toString() methods. Note that the Object class is the root class of Java hierarchy. Analyze the following code. class Test { public static void main(String[] args) { Object x = new Integer(2); System.out.println(x.toString()); } } a. The program has syntax errors because and Integer object is assigned to x b. When x.toString() is invoked, the toString() method in the Object class is used c. When x.toString() is invoked, the toString() method in the Integer class is used d. None of the choices given here are valid
c
(102R-2) You are given an Employee class with public instance variables age (of type int) and salary (of type double) and a public instance method getBonus() which returns a double value. Which of the following statement would compile and execute without errors? (Answer verified) a. Employee.getBonus; b. Employee e; e.age = 20; c. Employee e = new Employee; e.getBonus(); d. Employee e = new Employee(); e.salary = e.age;
d a - incorrect, method is missing () b - incorrect, constructor not properly defined c - incorrect, constructor is missing ()
(104-14) Consider the following array declaration inside the body of a method. String [] values = new String[10]; Invoking the lenth method on the array element, values[0], right after the above declaration results in: a. A compilation error since there is no initial value assigned to it b. An exception that will be thrown at runtime c. The value 0 d. The value -1
idk
(104-15) Consider the following ArrayList declaration and its iterator inside the body of a method. ArrayList myList = new ArrayList(); Iterator it = myList.iterator(); Invoking the it.next() method right after the above declaration results in: a. A compilation error since there is no initial value assigned to it b. An exception that will be thrown at runtime c. The value NULL d. The value false
idk
(104-17) Assume the signature of the method xMethod is as follows: public static void xMethod(double[] a) Which of the following could be used to invoke xMethod(double[] a)? a. xMethod(5); b. xMethod({3,4}); c. xMethod(new int[2]); d. xMethod(new double[2]);
idk
(104-18) Consider the code button.addActionListener(this); which of the following is NOT correct? a. The "this" referred to in the code must be an implementation of one or more listeners. b. The code will register the button with a listener c. The listener that "this" refers to should have the method actionPerformed(ActionEvent e) implemented d. One must declare the button to implement one or more Listeners
idk
(104-2) Consider the following array declaration inside the body of a method. int[] values = new int [10]; Accessing the array element, values [0], right after the above declaration results in: a. A compilation error since there is initial value assigned to it b. An exception that will be thrown at runtime c. The value 0 d. The value -1
idk
(104-5) Consider the following ArrayList declaration inside the body of a method. ArrayList myList = new ArrayList(); Accessing the element, myList.get(), right after the above declaration results in: a. A compilation error since there is no initial value assigned to it. b. An exception that will be thrown at runtime c. The value NULL d. The value 0
idk
(104-16) Suppose we have an array of String objects (with at least one element) identified by the variable 'names'. Which of the following loops will CORRECTLY process each element in the array? a. for (int i = 0; i < names.length; i++)... b. for (int i = 0; i < names.length(); i++)... c. int i = 0; while (i<= names.length) { ... i++;} d. None of the above
idk, not c
(104-4) In event-driven programming, a Java program includes one or several of the following steps: 1. Create a component object and add it to the container 2. Specify which class implements a component's event listener 3. Implement an event handler method and specify what program method calls it 4. Register the component as a listener for the application frame. Which steps actually have to be included? a. 1 and 2 b. 3 and 4 c. 1 and 3 d. 2 and 4
not b
(103) Suppose s1 and s2 are two non-empty string with the length of s1 greater than length of s2. Which of the following statements or expressions will generate a compile error? a. String s3 = s1 + s2; b. char c = s1.substring(0,1); c. s2 = s1.replace('s','s'); d. s1 = s2.substring(s1.length());
z
(103) The StringTokenizer class is used for a. examining each character in the given string b. examining the individual tokens in the given string based on the specified delimiters c. finding the positions of the delimiters in the given string d. converting the given string from one format to another format
z
(103) The code new StringBuffer("abcd").reverse().substring(1,2) results in the following string; a. "d" b. "c" c. "b" d. "a"
z
(103) Tokenizing the string "a;b;c;d" using the default delimiter rsults in a. 1 token b. 2 tokens c. 3 tokens d. 4 tokens
z
(103) What is the value of variable s after the following program: String s1 = new String("Make the world better as always"); char s = s1.charAt(length()-2); a. s b. y c. b d. a
z
(103) What is true about the following partial code? if ("String".toString() == "String") System.out.println("Equal"); else a. The code will compile and print "Equal" b. The code will compile and print "Not Equal" c. The code will cause a compiler error
z
(103) Which of the following is correct about the String class? a. The replace() method will modify the string object which is calling the method b. The trim() method will remove the white space at the end of the current string object c. substring() method will return a new string object, instead of modifying the current string object d. String class is equivalent to string class
z
(103) Which of the following statement is correct about Exception? a. Exception is not a class b. An exception must be caught before being thrown c. An exception can be either handled or be declared as thrown d. try{} block must always be used to capture an exception
z
(103) Which of the following statements is true about exception handling? a. Exception is not a type of class b. When you have a try{} clause, you must use catch as well c. Some exceptions do not require the programmer to handle using try-catch d. When a program would possibly throw an exception must be caught using a try clause
z
(102R-1) public class CS520 { private int i = 20; public static void main (String args[]) { CS520 x = new CS520(); System.out.println(x.i); } } Which of the follow is correct? (Answer verified) a. It compiles and prints out 20 b. It does not compile as the class CS520 and has no constructor defined c. It does not compile because the variables i is defined as private in class CS520 d. It compiles and prints out 0
a
(102R-6) Which of the following is NOT correct about accessor and mutator method? a. It is customary to start the name of Accessor and Mutator methods with Acc and Mut, respectively b. Accessor method is used to access the state of an object c. Mutator method is used to change the state of an object d. Accessor and Mutator methods are generally public
a
(103) Tokenizing the string "a,b;c,d;e,f" using the semi-colon delimiter results in a. 3 tokens b. 4 tokens c. 5 tokens d. 6 tokens
a
(103) Which of the following are the CORRECT ways of declaring a string? a. String s = new String("Hello"); b. String s = 'hello'; c. char[] s = "Hello"; d. String s = 123;
a
(103R) Given String str = "Java Programming!"; the statement System.out.println(str.substring(5,8)); produces the output _____ a. Pro b. rog c. ram d. ogr
a
(103R) Which operator is used to concatenate two strings? a. + b. - c. * d. /
a
(EASY) (103) Which one of the following classes from the java.io package can be used for writing text data to a file? (Answer verified) a. PrintWriter b. BufferedReader c. FileReader d. File
a BufferedReader and FileReader read text based data
(EASY) (104-20) Suppose a HashSet of String objects, mySet, has the strings "1","2","3","4","5" added to it in that order. The same set of strings is added one more time. The size of the mySet will be (Answer verified) a. 5 b. 10 c. any number between 5 and 10 d. none of the given choices
a - HashSets cannot have duplicate values
(102R-3) If your class does not have the toString() method, which of the following will happen when the object is printed to the console? a. Your program will run just fine b. Your program will compile, but not execute c. Your program will not compile d. You must have a toString() method
a*
(102R-4) Consider the following code fragment: Rectangle r1 = new Rectangle(); r1.setColor(Color.blue); Rectangle r2 = r1; r2.setColor(Color.red); After the above piece of code is executed, what are the colors of r1 and r2 (in this order)? a. Color.blue Color.red b. Color.blue Color.blue c. Color.red Color.red d. Color.red Color.blue
a*
(104-8) Consider the following ArrayList declaration inside the body of a method. (Answer verified) ArrayList myList = new ArrayList(); Which of the following statements are true before the above ArrayList is modified? More than one choice may be applicable. a. The size of the above ArrayList is 0 b. The capacity of the above ArrayList is 10 c. The capacity of the above ArrayList is 0 d. The size of the above ArrayList is 10
a,b,d
(EASY) (103) Suppose String s = "Java"; which of the following will changes s to "java". More than once choice is valid (Answer verified) a. s.toLowerCase(); b. s.toUpperCase(); c. s = s.toLowerCase(); d. s = s.replace('J', 'j');
a,c,d
(102) The modifier private means that an instance variable can be accessed by name outside of the class definition a. True b. False
b
(102) What is the output of running the class C class A { public A() { System.out.println("The default constructor of A is invoked"); } } class B extends A { public B(String s) { System.out.println(s); } } public class C { public static void main(String[] args) { B b = new B("The constructor of B is invoked"); } } a. The constructor of B is invoked b. The default constructor of A is invoked and the constructor of B is invoked c. The default constructor of A is invoked d. None of the given choices
b
(102) Which of the following does not overload the method: public int myMethod (double a, int i) a. public int myMethod (int i, double a) b. public double myMethod (double b, int j) c. public int myMethod (double a, double b, int i) d. public int myMethod (int a, int i)
b
(102R-5) Consider the following code: class MyClass { int i = 5; static int j = 7; public static void printSomething () { System.out.println(); System.out.println(); } } What is the result if the MyClass.printSomething() is called from another class? a. i: 5, j:7 b. A compiler error occurs as i is not static c. A compiler error occurs as j is static d. A runtime exception is thrown
b
(103) If an object must be written to and read back from a file in its entirety, the class definition of that object must a. implement the Serializable interface b. extend the Serializable interface c. provide the corresponding read and write methods d. implement the default constructor
b
(103) Tokenizing the string "a,b;c,d;e,f" using the comma delimiter results in a. 3 tokens b. 4 tokens c. 5 tokens d. 6 tokens
b
(103R-2) A ____ block executes regardless of whether an exception occurs or not a. final b. finally c. catch d. none of the given choices
b
(103R-3) Given String str = "Java Programming!"; the following statement returns false. str == "Java Programming!"; a. True b. False
b
(104-1) A button control should have a registered _____ associated with it to take some action when the user clicks on it. (Answer verified) a. pixel b. listener c. JFrame d. none of the choices
b
(104-3) Suppose an ArrayList of String objects, myList, has the strings "1", "2", "3" added to it in that order. The code ListIterator it = myList.listIterator(); while (it.hasNext()) { System.out.print(it.next()); it.previous(); } results in the following output: (Answer verified) a. A syntax or compilation error b. An infinite number of 1's c. An infinite number of 2's d. 123
b
(EASY) (102R-10) The ________ of an object define its potential behaviors. a. Class b. Methods c. Variables d. Attributes
b
(EASY) (104-21) A ___ is the unit of space on the screen that Java uses for graphics. a. bit b. pixel c. none of the choices d. dot
b
(EASY) (104-21) Consider the following array declaration inside the body of the method int [][]values = {{1},{2,3},{4,5,6}}; The length of the values array is: a. 1 b. 3 c. 6 d. none of the given choices
b
(MEDIUM) (104-13) Which of the following draws a circle whose center position is (400,400) and whose radius is 50? (Answer verified) a. g.drawCircle(350,350,100,100); b. g.drawOval(350,350,100,100); c. g.drawCircle(400,400,50,50); d. g.drawOval(400,400,50,50);
b - there is no drawCircle; drawOval(x, y, width, height); radius is from center of circle out
(105-4) Given an empty ArrayDeque of String objects, myDeck, the strings "1", "2", "3", "4" are added to it using the addFirst and addLast methods alternatively shown below myDeck.addFirst("1"); myDeck.addLast("2"); myDeck.addFirst("3"); myDeck.addLast("4"); The expression myDeck.removeFirst() + myDeck.removeLast() results in (Answer verified) a. 12 b. 34 c. 13 d. 7
b. 34
(105-1) Given an empty LinkedList of Integer objects, myList, the values 1,2,3,4,5 are added to it using the addFirst method myList.addFirst(1); myList.addFirst(2); myList.addFirst(3); myList.addFirst(4); myList.addFirst(5); The expression myList.get(1) + myList.get(2) results in (Answer verified) a. 9 b. 7 c. 5 d. 3
b. 7
(105-3) Given an empty LinkedList of Integer objects, myList, the values 1,2,3,4,5 are added to it using the addLast method. (Answer verified) myList.addLast(1); myList.addLast(2); myList.addLast(3); myList.addLast(4); myList.addLast(5); The expression myList contains("2") returns true (Answer verified) a. True b. False
b. False
(EASY) (104R-6) What is the correct expression for accessing the 5th element in an array named colors? (Answer verified) a. colors[3] b. colors[4] c. colors[5] d. colors[6]
b. colors[4] Arrays start at 0
(EASY) (104R-5) The correct syntax for accessing the length of an array named numbers is: (Answer verified) a. number.length() b. numbers.length c. numbers d. none of these choices
b. numbers.length
(104R-3) Any change to graphics being displayed on the screen required a call to the method __ to update the graphics (Answer verified) a. paint b. repaint c. update d. all of the choices
b. repaint
(102) A class can be defined by extending another class. It is called ____. a. encapsulation b. polymorphism c. inheritance d. attributes
c
(102) Consider the following code: class MyClass { String hello = "Hello, Dear"; void printMessage() { System.out.println(hello); } } class TestMyClass { public static void main(String[] args) { MyClass mc = new MyClass(); mc.printMessage(); } } a. A compiler error occurs at line 9 because no such method (constructor) for class MyClass has been declared b. A runtime exception occurs at line 4 c. The code compiles and executes fine, and the output is "Hello, Dear" d. The code compiles and executes fine, and there is no output
c
(102) What is the difference between static variable and instance variable? a. static variable cannot be changed while instance variable can b. static variable is unique for each class instance, while an instance variable is shared by all instances c. static variable is shared by all the instances of the class, while instance variable is not d. static variable, once given a value, can no longer change
c
(102) What is the output generated when Test is run considering the following code? class Animal { public Animal() { System.out.println("Animal");} } class Horse extends Animal { public Horse() { System.out.println("Horse"); } } class Donkey extends Horse { public Donkey() {System.out.println("Donkey");} } class Test { public static void main(String[] args) {new Donkey();} } a. Donkey b. Donkey, Horse, Animal c. Animal, Horse, Donkey d. None of the given choices
c
(102) Which of the following statements is NOT correct about constructor? a. Constructor can ONLY be called when a new instance of the class has to be created b. Constructor can have multiple overloaded versions c. If you do not declare a constructor, then the class has no constructor, therefore cannot be instantiated d. It always has the same name as the name of the class
c
(104-10) Consider the following array declaration inside the body of a method. (Answer verified) int[][] values = {{1},{2,3},{4,5,6}}; The expression (values[0] + values[1]) results in: a. 3 b. 6 c. A syntax or compilation error d. None of the given choices
c
(104-7) Which of the following initializes the elements of an array named myDoubles? (Answer verified) a. double myDoubles [double] = {0.0, 1.0, 1.5, 2.0, 2.5}; b. double myDoubles [5] = new double (0.0, 1.0, 1.5, 2.0, 2.5); c. double [] myDoubles = {0.0, 1.0, 1.5, 2.0, 2.5}; d. array myDoubles[double]= {0.0, 1.0, 1.5, 2.0, 2.5};
c
(EASY) (104-19) Suppose a HashSet of String objects, mySet, has the strings "1","2","3","4","5" added to it in that order. When iterating over the elements in this set, the output... (Answer verified) a. will be in the order 1,2,3,4,5 b. will be in the order 5,4,3,2,1 c. order of the elements cannot be guaranteed d. will be in the order 1,3,5,2,4
c
(EASY) (104-9) Consider the following array declaration inside the body of a method. (Answer verified) int[][]values = {{1}, {2,3}, {4,5,6}}; The expression (values[1][1] + values[2][2]) results in: a. 3 b. 6 c. 9 d. 11
c 1 2 3 4 5 6 position (1,1) = 3 position (2,2) = 6 3+6=9
(105-5) Suppose a Stack of String objects, myStack, has the strings "1","2","3","4","5" pushed onto it in that order. After invoking the pop() method, the peek() method results in the string (Answer verified) a. "1" b. "2" c. "4" d. "5"
c. "4"
(104R-1) Consider the following array myArray with elements {7,9,-3,6,1,-1}. What is the value of myArray [myArray[1] - myArray[0]]? (Answer verified) a. 7 b. 9 c. -3 d. 6
c. -3
(104R-9) To place an element in an ArrayList position for the first time, you usually use the method: (Answer verified) a. put() b. set() c. add() d. place()
c. add()
(104R-7) To use the Java class ArrayList, you must import the package (Answer verified) a. java.awt b. java.io c. java.util d. java.container
c. java.util
(104R-8) An ArrayIndexOutOfBounds error is (Answer verified) a. compile error b. syntax error c. logic error d. all of the given choices
c. logic error
(104R-2) You can use the Java method __ from the Color class to change the drawing color (Answer verified) a. changeColor b. getColor c. setColor d. useColor
c. setColor
(102) Analyze the following code: class Test { public static void main(String[] args) { System.out.println(xMethod((double)5)); } public static int xMethod (int n) { System.out.println("int"); return n; } public static long xMethod (long n) { System.out.println("long"); return n; } } a. The program displays int followed by 5 b. The program displays long followed by 5 c. The program runs and displays nothing d. The program does not compile
d
(102) Consider the following code: which of the following code fragments, using the class defined above, would generate a compile error? The ellipses {...} are used here to indicate the presence of some code class Test { Test() {..} Test (int x) {..} void method() {..} } a. Test t = new Test(); t.method(); b. Test t = new Test(5); t.method(); c. Test() d. Test t = new Test();
d
(102) When you want the parameters in a method to be the same as the instance variables, you use the ____ key word. a. String b. double c. int d. this
d
(102) Which of the following is NOT correct about declaring a class? a. The body of the class is bracketed by {} b. The name of the class must be after the "class" keyword c. It can contain instance variables and instance methods d. It must be declared with at least one instance variable
d
(102) Which of the following statements is true about the static modifier? a. A static variable cannot change its value b. A static method is often written in non-Java language and exists outside of JVM c. A static method can access instance variables d. A static variable is shared by all the instances of the class
d
(102R-7) Which of the following is an incorrect statement about imperative (procedural) programming and object oriented programming? a. Object oriented programming can stimulate real-world situation more effectively b. Imperative programming and Object Oriented Programming can occur within the same program c. In object oriented programming, Objects become the fundamental units and have behavior, or specific purpose, associated with them d. Imperative programming focuses on the behaviors of objects instead of the state of objects
d
(104-6) The __ is an object that waits for an event to occur and responds in some way when it does. (Answer verified) a. JFrame b. GUI c. Component d. Listener
d
(102R-8) In the class Car, you want to create an instance method isSpeeding to check whether an object of type Car is speeding or not. One of the instance fields of the Car class is a double variable, speed, equal to the current speed of the Car object. The class also lists a constant static double field, SPEED_LIMIT, equal to the legal speed limit for the driving conditions of the Car object. What method signature would be best for isSpeeding? a. public void isSpeeding (double speed) b. public boolean isSpeeding() c. public isSpeeding() d. public boolean isSpeeding (speed, SPEED_LIMIT)
d*
(102R-9) Which of the following statements is false? a. An instance of an int is the same size on all machines that a Java program is run on b. The operator == should not be used to compare strings c. You can override a base class non-final method in a derived class d. You must define a constructor in a class
d*
(105-2) Suppose a Stack of String objects, myStack, has the strings "1","2","3","4","5" are pushed onto it in that order. The myStack.pop() method results in the string (Answer verified) a. "1" b. "2" c. "4" d. "5"
d. "5"
(104R-10) A button fires events known as: (Answer verified) a. static events b. passive events c. dynamic events d. action events
d. action events
(104R-4) The ActionListener interface requires that the method __be implemented (Answer verified) a. actionEvent() b. actionFired() c. actionRegistered() d. actionPerformed()
d. actionPerformed()
(104-11) Given the sequence of the following statements, g.drawLine(50,100,150,100); g.drawLine(50,50,150,50); g.drawLine(150,50,150,100); g.drawLine(50,50,50,100); which of the following can replace the above sequence? a. g.drawLine(150,50,100,50); b. g.drawLine(50,100,100,50); c. g.drawLine(50,50,100,50); d. g.drawLine(100,100,50,50);
idk
(104-12) Suppose an ArrayList of String objects, myList, has the strings "1", "2", "3" added to it in that order. The code Iterator it = myList.iterator(); while (it.hasNext()) { System.out.print(it.next()); } results in the following output: (Answer verified) a. A syntax or compilation error b. 123 c. 321 d. None of the given choices
idk
(103) Consider the following code fragment: public class ExceptionHandlingNote { public static void main(String[] args) {String x; System.out.println(x);} } a. It prints out 0 b. It prints out null c. It prints out nothing d. None of the choices is true
z
(103) Consider the following line of code: String str = new String("Hello"); Which of the following lines of code modify the string to which str refers? a. str.substring(2); b. str.replace('H','M'); c. str.trim(); d. None of the choices
z
(103) Which one of the following classes from the java.io package can be used for writing objects to a file? a. ObjectOutputStream b. BufferedReader c. FileReader d. File
z
(103R) Exceptions that are subject to the catch or declare rules are called ______ a. checked exceptions b. unchecked exceptions c. fatal exceptions d. all of the given choices
z
(103R) If a method does not catch an exception, then it must at least warn programmers that any invocation of the method might possibly throw an exception. This warning is done using _____ a. exception handler b. throws cause c. try block d. catch block
z
(103R) The class ObjectOutputStream contains all of the following methods except: a. writeInt b. writeChar c. writeDouble d. println
z
(103R) The output stream connected to the computer screen is a. System.exit b. System.quit c. System.in d. System.out
z
(103R) The stream that is automatically available to your Java code is _____ a. System.out b. System.in c. System.err d. All of the given choices
z
(103R) Try blocks contain that could possibly a. handle an exception b. throw an exception c. catch an exception d. display an exception
z
(103R) When an exception is thrown, the code in the surrounding try block continues executing and then the catch block begins execution a. True b. False
z
(103R) When the method readLine() tries to read beyond the end of a file, it returns the value of _____ a. 1 b. -1 c. 0 d. null
z