LONG QUIZ 1B
Name: Year & Section: :
Email:
LONG QUIZ 1B
True/False
Indicate whether the
statement is true or false.
1. Just like the nesting of loops, Java allows the nesting of methods.
2. In Java, !,
&&, and || are called logical
operators.
3. The data type float is a floating-point data
type.
4. In the class
String, the substring method inserts a string into another string.
5. Suppose x = 10 and y = 20.
The value of the expression ((x >= 10) && (y <= 20)) is
true.
6. The height and width of a window are measured in inches.
7. Like a do...while loop,
the body of a while loop executes at least once.
8. Suppose that the following statement is included in a program.
import static java.lang.Math.*;
After this statement, a static method of the class Math can be called using
just the method name.
9. The value of the expression Integer.parseInt("+782") is
"782".
10. In Java, there is one way to make an application program create a window.
11. The return statement must be the last line of the
method.
12. When writing output to a file, if the file is not closed at the end of the
program, you may not be able to
view your output properly.
13. The method finalize automatically executes when the
class object goes out of scope.
14. In Java, extends is a reserved word.
15. If lengthTF is a JTextField and pane is a container, then the
statement
pane.add(lengthTF); adds the text field to the content pane of a
window.
16. When a program executes, the first statement to execute is always the first
statement in the main
method.
17. In a format specifier, if the flag is set to 'l', then the output of the result is left justified.
18. The expression 'A' <= 'B' evaluates to
false while the expression 'A' < 'B' evaluates to
true.
19. To handle window events, you first create a class that implements the interface
LONG QUIZ 1B
WindowListener and then create and register objects of that
class.
20. If an exception occurs in a try block and that
exception is caught by a catch block, then the
remaining catch blocks associated with that try block are
ignored.
21. The JList GUI component generates events that call
the actionStateChanged method.
22. In Java, case and switch
are reserved words, but break is not a reserved word.
23. Both System.out.println and System.out.print can be used to output a string on the
standard output
device.
24. The output of the following Java code is: Stoor.
int count = 5;
System.out.print("Sto");
do
{
System.out.print('o');
count--;
}
while (count >=
5);
System.out.println('r');
25. Wrapper class objects are mutable.
26. Java automatically initializes all variables.
27. The class JFrame
contains the method setSize.
28. Given the declaration
int[] list = new
int[50];
the statement
System.out.println(list[0] + "..." +
list[49]);
outputs all 50 components of the array list.
29. The first step in solving a problem using
object-oriented design is to identify objects.
30. The class ActionListener contains only one method,
actionEvent.
31. If a formal parameter is a variable of a primitive data type, then after copying
the value of the actual
parameter, there is no connection between the formal and actual
parameter.
32. Including a semicolon before the action statement in a one-way selection causes
a syntax error.
33. Statements that might generate an exception are placed in a catch block.
34. The method size in the class Vector returns the number of elements in the
vector.
35. Given int num; the value of num is directly stored in its memory space.
36. Operators of the same precedence are evaluated from right to left.
file:///E/PROGRAM%20LECTURE/COMP%2020083%20OOP%20LECTRE/Exam/long_quiz1b.htm[13/03/2020 5:47:22 PM]
LONG QUIZ 1B
37. Java stores two-dimensional arrays in a row order form in computer
memory.
38. Given the declaration
public class MyClass
{
private int
x;
public void print()
{
System.out.println("x = " +
x);
}
private void setX(int y)
{
x = y;
}
}
MyClass myObject = new MyClass();
The following statement is
legal.
myObject.print();
39. If ++x is used in an
expression, first the expression is evaluated, and then the value of x is
incremented
by 1.
40. The classes Integer, Float, and Double are called wrapper classes.
41. The signature of a method consists of only its formal parameter list.
42. To compare the values of two Integer objects for
equality, we can use the method equals of the
class Integer.
43. You can create an array of reference variables to manipulate objects.
44. The operators = and ==
have the same order of precedence.
45. The components of a class are called fields.
46. Suppose that you have the following statements.
String str1 = "cat";
String str2 = "cats";
The statement str1.equals(str2); is true.
47. Every program with a try block must end with a finally block.
48. Within a method, an identifier used to name a variable in the outer block of the
method can be used to
name any other variable in an inner block of the method.
49. The statement System.exit(0); is found at the end of every working
Java program.
50. A value such as 'd' is called a character
constant.
Multiple Choice
LONG QUIZ 1B
Identify the
choice that best completes the statement or answers the question.
1. What is stored in alpha after the following code
executes?
int[] alpha = new int[5];
for (int j
= 0; j < 5; j++)
{
alpha[j] = 2 * j;
if (j % 2 ==
1)
alpha[j - 1] = alpha[j] +
j;
}
a. alpha = {3, 2, 9, 6, 8} c. alpha = {0, 3,
4, 7, 8}
b. alpha = {0, 2, 4, 6, 8} d. alpha
= {0, 2, 9, 6, 8}
2. Which method would you most likely use to find the location of an element in the
vector?
a. size c. lastElement
b. elementAt d. indexOf
3. Which of the following statements is NOT true about GUI programs?
a. Every window has a title, width, and height.
b. A layout must be
created before components can be added to a content pane.
c. All components are
added directly to the GUI window.
d. Every GUI program requires a
window.
4. Given the method heading
public static void
strange(int a, int b)
and the declaration
int[]
alpha = new int[20];
int[] beta = new int[25];
Which
of the following is a valid call to the method strange?
a. strange(alpha[5], beta);
b. strange(alpha, beta[20]);
c. strange(alpha[10], alpha[15]);
d. strange(alpha[0], beta[25]);
5. The value of the expression 5 + 10 % 4 - 3 is
____.
a. 0 c. 4
b. 2 d. 5
6. Which of the following declares an array of int named
beta?
a. int beta = int[]; c. new int
beta[];
b. int
beta; d. int[] beta;
7. Which of the following is the newline character?
a. \b c. \r
b. \l d. \n
8. When does the method finalize execute?
a. When the class object is created
b. At the end of every program
c. When the class
object goes out of scope
LONG QUIZ 1B
d. At the start of every
program
9. Which of the following is NOT a reserved word in Java?
a. num c. double
b. static d. throws
Write a program to input the radius of the base and height of a cylinder, and
calculate and print the
surface area, volume, and area of the base of the cylinder.
10. Which words from the problem statement above could we use to determine the
operations for this
program?
a. height, cylinder, radius c. radius, cylinder,
print
b. input, calculate, print d. cylinder, program, volume
public static double secret(int first, double
second)
{
double temp;
if (second >
first)
temp = first * second;
else
temp = first - second;
return temp;
}
11. What is the name of the method in the accompanying figure?
a. double c. second
b. secret d. first
12. If you want to provide your own code to terminate the program when the window is closed, or if you
want the program to take a different action when the window closes, then you use
the interface
____.
a. WindowListener c. CloseListener
b. MouseListener d. ActionListener
static final int EndVal = -1;
int double;
int num = console.nextInt();
while (num != EndVal)
{
double = num *
2;
System.out.println(double);
num = console.nextInt();
}
13. The above code is an example of a(n) ____ while loop.
a. flag-controlled c. sentinel-controlled
b. counter-controlled d. EOF-controlled
double[] as = new
double[7];
double[] bs;
bs = as;
14. How many objects are present after the code fragment in the accompanying figure
is executed?
a. 1 c. 7
LONG QUIZ 1B
b. 2 d. 14
switch (lastInitial)
{
case 'A':
System.out.println("section 1");
break;
case 'B':
System.out.println("section 2");
break;
case 'C':
System.out.println("section 3");
break;
case 'D':
System.out.println("section 4");
break;
default:
System.out.println("section 5");
}
15. Based on the code above, what is the output if lastInitial =
'C'?
a. section 1 c. section
3
b. section 2 d. section
5
public class Secret
{
private int
x;
private static int y;
public static int count;
public int z;
public Secret()
{
x =
0;
z = 1;
}
public Secret(int a)
{
x = a;
}
public Secret(int a, int b)
{
x = a;
y = b;
}
public String
toString()
{
return ("x =
" + x + ", y = " + y + ",
count = " + count);
}
public static void
incrementY()
{
y++;
}
}
16. What does the default constructor do in the class definition in the accompanying
figure?
a. Sets the value of x to 0 and
the value of z to 1
LONG QUIZ 1B
b. There is no default
constructor.
c. Sets the value of x to 0
d. Sets the value of z
to 1
17. To handle window events, if the class containing the application program does
not extend the definition
of another class, you can make that class extend the definition of the
class ____.
a. WindowListener c. Adapter
b. MouseAdapter d. WindowAdapter
18. Which of the following has the highest value?
a. 'H' c. 'b'
b. '-' d. '5'
19. Where is the continue statement NOT usually
used?
a. for loops c. while
loops
b. switch structures d. do...while loops
int x = 27;
int y = 10;
do
x
= x / 3;
while (x >= y);
20. How many times does the statement above execute?
a. none c. twice
b. once d. three times
21. Which of the following statements is true?
a. The class Throwable, which is derived from the class Exception, is the superclass of
the class
Object.
b. The class Exception, which is derived from the class Object, is the superclass of the
class
Throwable.
c. The class Throwable,
which is derived from the class Object, is the superclass of the
class Exception.
d. None of these
22. Which of the following methods is NOT part of the
class JFrame?
a. getText c. setVisible
b. setTitle d. setSize
public static char methodHeading(int n, double
num)
23. Based on the method heading in the accompanying figure, what is the return type
of the value
returned?
a. int c. num
b. char d. static
24. The expression (int)8.7 evaluates to ____.
a. 8 c. 9.0
b. 8.0 d. 9
25. Class members consist of all of the following EXCEPT ____.
a. pre-defined methods c. named constants
b. variable declarations d. methods
LONG QUIZ 1B
int i;
for (i = 0; i <= 10;
i++)
System.out.println("*");
System.out.println("!");
26. Which of the following is the initial expression in the for loop above?
a. i = 0;
b. System.out.println("*");
c. i <=
10;
d. i++
27. Which of the following is an area of secondary storage used to hold
information?
a. file c. buffer
b. constant d. variable
28. Suppose that alpha and beta
are int variables. The statement alpha =
--beta; is
equivalent to the statement(s) ____.
a. beta = beta - 1; c. alpha = beta;
alpha = beta - 1; beta = beta - 1;
b. beta = beta - 1; d. beta = beta - 1;
alpha = beta; alpha = 1 - beta;
29. The ____ rules of a programming language tell you which statements are legal, or
accepted by the
programming language.
a. logical c. syntax
b. semantic d. grammatical
Write a program that takes as input the pay rate and hours worked of an employee
and calculates the
pay of the employee.
30. Based on the problem statement above, which of the following would most likely
be chosen as the
class?
a. Employee c. Hours_Worked
b. Pay d. Pay_Rate
31. If a negative value is used for an array index, ____.
a. an ArrayIndexOutOfBoundsException is
thrown
b. the program terminates automatically without throwing an
exception
c. the last index of the array is automatically accessed instead
d. a NumberFormatException is thrown
32. Which package is automatically imported by the Java system?
a. java.util c. java.swing
b. java.lang d. java.io
33. When is a finally block executed?
a. Always after the execution of a try block, regardless of
whether or not an exception is
thrown
b. Only when an exception is thrown by a try block
c. Only when there are no exceptions
thrown
d. At the end of a program
34. Which of the following is NOT true about return
statements?
a. A value-returning method returns its value via the return
statement.
LONG QUIZ 1B
b. A method can have more than one return
statement.
c. return statements can be used in void methods to return values.
d. Whenever a return
statement executes in a method, the remaining statements are skipped
and the method
exits.
int x;
x = (1 <= 3 && 'K' >= 'F') ? 5 : 12
35. Based on the code above, what is the value of x?
a. 1 c. 5
b. 3 d. 12
char[][] table = new
char[10][5];
36. What is the value of table[2].length?
a. 0 c. 10
b. 5 d. 15
37. How many finally blocks can there be in a try/catch structure?
a. There must be one finally block.
b. There can be zero or
one finally blocks following the last catch
block.
c. There can be one finally block following each catch block.
d. There is no limit to the number of finally blocks following the last catch
block.
MysteryClass
-first: int
-second: double;
+MysteryClass()
+MysteryClass(int)
+MysteryClass(double);
+MysteryClass(int,
double)
+setData(int, double): void
+getFirst(): int
+getSecond(): double
+doubleFirst():
int
+squareSecond(): double
+print(): void
+equals(MysteryClass):
boolean
+makeCopy(MysteryClass): void
+getCopy():MysteryClass
38. According to the UML class diagram in the accompanying figure, which of the
following is a private
member of the class MysteryClass?
a. MysteryClass c. doubleFirst
b. second d. print
39. According to the UML class diagram in the accompanying figure, which method is
public and
doesn’t return anything?
a. equals(MysteryClass) c. print()
b. doubleFirst() d. getCopy()
40. The program that tests a method is called a ____ program.
a. driver c. stub
b. debugging d. tester
import java.util.*;
public class
ExceptionExample1
file:///E/PROGRAM%20LECTURE/COMP%2020083%20OOP%20LECTURE/Exam/long_quiz1b.htm[13/03/2020 5:47:22 PM]
LONG QUIZ 1B
{
static Scanner console = new
Scanner(System.in);
public static void main(String[] args)
{
int dividend, divisor,
quotient;
try
{
System.out.print("Enter dividend: ");
dividend = console.nextInt();
System.out.println();
System.out.print("Enter divisor:
");
divisor = console.nextInt();
System.out.println();
quotient = dividend / divisor;
System.out.println("quotient =
" +
quotient);
}
catch
(ArithmeticException aeRef)
{
System.out.println("Exception" +
aeRef.toString());
}
catch (InputMismatchException imeRef)
{
System.out.println("Exception "
+ imeRef.toString());
}
catch( IOException
ioeRef)
{
System.out.println("Exception "
+ ioeRef.toString());
}
}
}
41. Which of the following will cause the first exception to occur in the code in
the accompanying figure?
a. This code will not compile, so an exception cannot be triggered.
b. If the quotient is
zero
c. If the divisor is zero
d. If the dividend is
zero
int x, y;
if (x < 4)
y = 2;
else if (x > 4)
{
if (x >
7)
y = 4;
else
y = 6;
}
else
y =
8;
42. Based on the code above, what is the value of y if
x = 9?
a. 2 c. 6
b. 4 d. 8
43. What is the output of the following statement?
System.out.printf("%.2f", 48.9);
LONG QUIZ 1B
a. 48.00 c. 49
b. 48.90 d. 49.00
44. What is the value of the following statement?
Math.pow(3,4)
a. 7 c. 34
b. 12 d. 81
45. What is the main use of inner classes?
a. To implement cascaded method calls
b. To provide information
hiding
c. To handle events
d. To alter the behavior of the outer
class
46. Which of the following is a valid char value?
a. '$_' c. 'n\'
b. “a” d. '%'
If Shape is a class and
you create a new class Rectangle that extends Shape, then Shape is
a(n) ____ and Rectangle is a(n) ____.
47. Which word goes in the first blank in the sentence above?
a. subclass c. package
b. superclass d. inherited class
48. Which of the following does not have an entry condition?
a. do...while loop c. sentinel-controlled
while loop
b. EOF-controlled while
loop d. for loop
49. The declaration int a, b, c; is equivalent to which
of the following?
a. int a , b c; c. int a b
c;
b. int abc; d. int a;
int b;
int c;
50. What is the default definition of the method toString?
a. It creates a string that is the name of the object’s class, followed by the hash code of the
object.
b. It creates a string
that is the name of the program.
c. It creates a string that is the name of the
package, followed by the name of the class,
followed by the hash of the
object.
d. There is no default definition; you must define the method
yourself.