Quiz
1. Which library package would you import to use NumberFormat and DecimalFormat?
A) java.text
2. The expression 4 + 20 / (3 - 1) ∗ 2 is evaluated to ________.
A) 24
3. Which of the following expressions results in a value 1?
A) 37 % 6
4. If x is an int and y is a float, all of the following are legal except which assignment statement?
A) x = y;
5. Which of the following assignment statements are incorrect?
A)
1. i == j == k == 1;
2. i = 1 = j = 1 = k = 1;
6. What is y displayed in the following code?
public class Test {
public static void main(String[] args) {
int x = 1;
int y = x++ + x;
System.out.println("y is " + y);
}
}
A) y is 3.
7. A Java variable is the name of a
A) data value stored in memory that can change its value but cannot change its type during the program's execution
8. Suppose that String name = "Frank Zappa". What will the instruction
name.toUpperCase .replace('A', 'I'); return?
A) "FRINK ZIPPI"
9. What is output with the statement System.out.println(x+y); if x and y are int values where x=10 and y=5?
A) 15
10. The values of (double) 5 / 2 and (double) (5 / 2) are identical.
A) FALS
11. How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 10);
A) 11
12. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?
A) ((x < 100) && (x > 1)) || (x < 0)
13. In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
A) FALS
14. What is the output for y?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
System.out.println(y);
A) 45
15.Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the following conditions:
Condition 1: (x < y && x > 0)
Condition 2: (a != d || x != 5)
Condition 3: !(true && false)
Condition 4: (x > y || a == 'A' || d != 'A')
A) Conditions 2, 3 and 4 are all true, Condition 1 is not
16. The following loop is syntactically valid.
for (int j = 0; j < 1000; j++) j--;
A) TRUE
17. The statement if (a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;.
A) TRUE
18. The do loop differs from the while loop in that
A) the do loop will always execute the body of the loop at least once
19.An if statement may or may not have an else clause, but an else clause must be part of an if statement.
A) TRUE
20. Which of the following statements are true about Java loops?
A) all three loop statements are functionally equivalent
21.An object is an instance of a ________.
A) class
22.The keyword ________ is required to declare a class.
A) class
23. Analyze the following code:
class Circle {
private double radius;
public Circle(double radius) {
radius = radius;
}
}
A) The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.
24. ________ is invoked to create an object.
A) A constructor
25. Which of the following statements are true?
A)
1. A default constructor is provided automatically if no constructors are explicitly declared in the class.
2. The default constructor is a no-arg constructor.
26. Which of the following statements are true?
A)
1. Constructors are invoked using the new operator when an object is created.
2. Multiple constructors can be defined in a class.
3. Constructors must have the same name as the class itself.
4. Constructors do not have a return type, not even void.
27. What is the value of times displayed?
public class Test {
public static void main(String[] args) {
Count myCount = new Count();
int times = 0;
for (int i=0; i<100; i++)
increment(myCount, times);
System.out.println(
"myCount.count = " + myCount.count);
System.out.println("times = "+ times);
}
public static void increment(Count c, int times) {
c.count++;
times++;
}
}
class Count {
int count;
Count(int c) {
count = c;
}
Count() {
count = 1;
}
}
A) 0
28 .Given the following code fragment
String strA = "aBcDeFg";
String strB = strA.toLowerCase;
strB = strB.toUpperCase;
String strC = strA.toUpperCase;
A) strB.compareTo(strC) would yield 0
29. What is the advantage of putting an image in a JLabel instance?
A) It becomes part of the component and is laid out automatically
30. These two ways of setting up a String yield identical results:
a) String string = new String("123.45");
b) String string = "" + 123.45;
A) TRUE
31.Suppose your method does not return any value, which of the following keywords can be used as a return type?
A). void
32. Does the method call in the following method cause compile errors?
public static void main(String[] args) {
Math.pow(2, 4);
}
A). No
33. You should fill in the blank in the following code with ________.
public class Test {
public static void main(String[] args) {
System.out.print("The grade is ");
printGrade(78.5);
System.out.print("The grade is ");
printGrade(59.5);
}
public static ________ printGrade(double score) {
if (score >= 90.0) {
System.out.println('A');
}
else if (score >= 80.0) {
System.out.println('B');
}
else if (score >= 70.0) {
System.out.println('C');
}
else if (score >= 60.0) {
System.out.println('D');
}
else {
System.out.println('F');
}
}
}
A). 1. void
2. boolean
34. You should fill in the blank in the following code with ________.
public class Test {
public static void main(String[] args) {
System.out.print("The grade is " + getGrade(78.5));
System.out.print("\nThe grade is " + getGrade(59.5));
}
public static ________ getGrade(double score) {
if (score >= 90.0)
return 'A';
else if (score >= 80.0)
return 'B';
else if (score >= 70.0)
return 'C';
else if (score >= 60.0)
return 'D';
else
return 'F';
}
}
A). char
35.Given the following method
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}
What is the output of the call nPrint('a', 4)?
A). invalid call
36. Given the following method
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}
What is k after invoking nPrint("A message", k)?
int k = 2;
nPrint("A message", k);
A). 2
37.Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2) {
System.out.println("max(int, double) is invoked");
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, int num2) {
System.out.println("max(double, int) is invoked");
if (num1 > num2)
return num1;
else
return num2;
}
}
A). The program cannot compile because the compiler cannot determine which max method should be invoked.
38. (char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character ________.
A). between 'a' and 'z'
39. Which of the following is the best for generating random integer 0 or 1?
A). (int)(Math.random() + 0.5)
40. The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as ________.
A).1. encapsulation
2. information hiding
41. What is the output of the following code:
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " "); } }
A).C. 1 1 1 1 1 1
42. Which of the following are correct?
A). 1. String[] list = {"red", "yellow", "green"};
2. String[] list = new String[]{"red", "yellow", "green"};
43.Analyze the following code:
int[] list = new int[5]; list = new int[6];
A). The code can compile and run fine. The second line assigns a new array to list.
44. The reverse method is defined in this section. What is list1 after executing the following statements?
int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1);
A). list1 is 1 2 3 4 5 6
45. Analyze the following code:
public class Test {
public static void main(String[] args) {
boolean[][] x = new boolean[3][]; x[0] = new boolean[1];
x[1] = new boolean[2]; x[2] = new boolean[3];
System.out.println("x[2][2] is " + x[2][2]);
}
}
A). The program runs and displays x[2][2] is false.
46. Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr?
A). It stores 5 elements with legal indices between 0 and 4
47. The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only.
int foo = 0;
for (int j =0 ; j < list.length; j++)
if (list[j] > foo) foo = list[j];
A). It stores the largest value in list (the maximum) in foo
48. The statement int[ ] list = {5, 10, 15, 20};
A). initializes list to have 4 int values
49). To initialize a String array names to store the three Strings "Huey", "Duey" and "Louie", you would do
A). String[ ] names = {"Huey", "Duey", "Louie"};
50. In a two-dimensional array, both dimensions must have the same number of elements, as in[10][10].
A). False
51. Analyze the following code:
public class Test extends A {
public static void main(String[] args) {
Test t = new Test();
t.print();
}
}
class A {
String s;
A(String s) {
this.s = s;
}
public void print() {
System.out.println(s);
}
}
A). 1. The program would compile if a default constructor A(){ } is added to class A explicitly.
2. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed.
52. What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B() {
System.out.println(
"The default constructor of B is invoked");
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
A). "The default constructor of A is invoked" followed by "The default constructor of B is invoked"
53). Analyze the following code:
public class Test {
public static void main(String[] args) {
B b = new B();
b.m(5);
System.out.println("i is " + b.i);
}
}
class A {
int i;
public void m(int i) {
this.i = i;
}
}
class B extends A {
public void m(String s) {
}
}
A). The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.
54. What is the output of the following code?
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
@Override
public String getInfo() {
return "Student";
}
}
class Person {
public String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}
A). Person Student
55. Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ________.
A). true
56. Analyze the following code
// Program 1:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
// Program 2:
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
A). Program 1 displays false and Program 2 displays true.
57. For the questions below, consider the following class definition:
public class AClass
{
| protected int x; |
| protected int y; |
| public AClass(int a, int b) |
| { |
| x = a; |
| y = b; |
| } |
| public int addEm |
| { |
| return x + y; |
| } |
| public void changeEm |
| { |
| x++; |
| y--; |
| } |
| public String toString |
| { |
| return "" + x + " " + y; |
| } |
}
58). For the questions below, use the following partial class definitions:
public class A1
{
| public int x; |
| private int y; |
| protected int z; |
...
}
public class A2 extends A1
{
| protected int a; |
| private int b; |
...
}
public class A3 extends A2
{
| private int q; |
...
}
A). x, z, a, b
59. If class AParentClass has a protected instance data x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but can not redefine x to be a different type.
A). False
60. If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if C1 c = new C1 ; is performed at one point of the program, then a later instruction c.whichIsIt ; will invoke the whichIsIt method defined in C1.
A). False
61. An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
A). Error
62. The following code causes Java to throw ________.
int number = Integer.MAX_VALUE + 1;
A).no exceptions
63. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
String s = "abc";
System.out.println(s.charAt(3));
}
}
A). StringIndexOutOfBoundsException
64. Analyze the following code:
public class Test {
public static void main(String[] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
}
catch (Exception ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
}
A). The program has a compile error.
65. What is displayed on the console when running the following program?
public class Test {
public static void main(String[] args) {
try {
p();
System.out.println("After the method call");
}
catch (NumberFormatException ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
static void p() {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
}
}
A). The program displays NumberFormatException.
66. Which class do you use to read data from a text file?
A). Scanner
67. Which of the following statements are correct?
I:
try (PrintWriter output = new PrintWriter("output.txt")) {
output.println("Welcome to Java");
}
II:
try (PrintWriter output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
III:
PrintWriter output;
try (output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
IV:
try (PrintWriter output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
finally {
output.close();
}
A). 1. I
2. II
3. III
68. Which of the following statements are correct?
. I:
try (PrintWriter output = new PrintWriter("output.txt")) {
output.println("Welcome to Java");
}
II:
try (PrintWriter output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
III:
PrintWriter output;
try (output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
IV:
try (PrintWriter output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
finally {
output.close();
}
A). 1. I
2. II
3. III
69. The difference between a checked and an unchecked exception is
A). an unchecked exception requires no throws clause
70. If an exception is thrown and is not caught anywhere in the program, then the program terminates.
A). True
Comments
Post a Comment