Assignment Mix 118
Assignment Mix 118
Assignment Mix 118
A. Enter an integer, a space, a double value, and then the Enter key.
B. Enter an integer, two spaces, a double value, and then the Enter key.
C. Enter an integer, an Enter key, a double value, and then the Enter key.
D. Enter a numeric value with a decimal point, a space, an integer, and then the Enter key.
B. class
C. 9X
D. 8+9
E. radius
3 Which of the following are correct names for variables according to Java naming conventions?
A. radius
B. Radius
C. RADIUS
D. findArea
E. FindArea
B. causes underflow
C. causes no error
2
6 To add number to sum, you write (Note: Java is case-sensitive)
A. number += sum;
D. sum += number;
A. 0
B. 1
C. 5
D. 6
8 What is y displayed?
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 1.
B. y is 2.
C. y is 3.
D. y is 4.
B. int t = 23;
C. short s = 10;
D. int t = (int)false;
E. int t = 4.5;
3
10 What is the printout of System.out.println('z' - 'a')?
A. 25
B. 26
C. a
D. z
11 If a program compiles fine, but it produces incorrect result, then the program suffers __________.
A. a compilation error
B. a runtime error
C. a logic error
B. Double.parsedouble(s);
C. double.parse(s);
D. Double.parseDouble(s);
i = (i + 4);
}
}
B. The program cannot compile because i does not have an initial value when it is used in i = i + 4;
4
C. The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4;
5
Part 2: Chapter 3 Selections
15 Which of the following code displays the area of a circle if the radius is positive.
A. if (radius != 0) System.out.println(radius * radius * 3.14159);
16 Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement? (Please indent the statement correctly first.)
if (x > 0)
if (y > 0)
System.out.println("x > 0 and y > 0");
else if (z > 0)
System.out.println("x < 0 and z > 0");
D. no printout.
Code 2:
boolean even = (number % 2 == 0);
B. System.halt(0);
C. System.exit(0);
D. System.quit(0);
E. System.stop(0);
C. (x > 0) || (x < 0)
D. (x != 0) || (x = 0)
21 Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10).
A. 9
B. 10
C. 11
int x;
double d = 1.5;
switch (d) {
case 1.0: x = 1;
case 1.5: x = 2;
case 2.0: x = 3;
}
A. The program has a compile error because the required break statement is missing in the switch statement.
B. The program has a compile error because the required default case is missing in the switch statement.
D. No errors.
A. -10
B. 0
C. 10
D. 20
E. Illegal expression
7
24 Analyze the following code fragments that assign a boolean value to the variable even.
Code 1:
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
even = (number % 2 == 0) ? true: false;
Code 3:
even = number % 2 == 0;
A. Code 2 has a compile error, because you cannot have true and false literals in the conditional expression.
B. Code 3 has a compile error, because you attempt to assign number to even.
25 How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
A. 8
B. 9
C. 10
D. 11
E. 0
26 How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++ < 10) {
System.out.println("Welcome to Java");
}
A. 8
B. 9
C. 10
D. 11
E. 0
8
27 How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);
A. 8
B. 9
C. 10
D. 11
E. 0
A. The program has a compile error because the adjustment is missing in the for loop.
B. The program has a compile error because the control variable in the for loop cannot be of the double type.
C. The program runs in an infinite loop because d<10 would always be true.
29 Do the following two statements in (I) and (II) result in the same value in sum?
(I):
for (int i = 0; i<10; ++i) {
sum += i;
}
(II):
for (int i = 0; i<10; i++) {
sum += i;
}
A. Yes
B. No
A. Yes
B. No
B. The program compiles despite the semicolon (;) on the for loop line, and displays 4.
C. The program compiles despite the semicolon (;) on the for loop line, and displays 14.
D. The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);
A. 100
B. 20
C. 10
D. 45
A. 5
B. 6
C. 7
D. 8
A. 15
B. 16
C. 17
D. 18
10
35 Will the following program terminate?
int balance = 10;
while (true) {
if (balance < 9) continue;
balance = balance - 9;
}
A. Yes
B. No
36 Suppose the input for number is 9. What is the output from running the following program?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
int i;
boolean isPrime = true;
for (i = 2; i < number && isPrime; i++) {
if (number % i == 0) {
isPrime = false;
}
}
System.out.println("i is " + i);
if (isPrime)
System.out.println(number + " is prime");
else
System.out.println(number + " is not prime");
}
}
A. i is 3 followed by 9 is prime
C. i is 4 followed by 9 is prime
B. int
C. double
D. public
11
38 The signature of a method consists of ____________.
A. method name
D. parameter list
40 Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which
stores elements in last-in first-out fashion.
A. a heap
B. storage area
C. a stack
D. an array
41 When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.
A. method invocation
B. pass by value
C. pass by reference
D. pass by name
A. aaaaa
B. aaaa
C. aaa
D. invalid call
12
43 Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(m(2));
}
public static int m(int num) {
return num;
}
public static void m(int num) {
System.out.println(num);
}
}
A. The program has a compile error because the two methods m have the same signature.
B. The program has a compile error because the second m method is defined, but not invoked in the main method.
B. a method variable
C. a block variable
D. a local variable
45 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. information hiding
B. encapsulation
C. method hiding
D. simplifying method
B. 0.5
C. 0.0
D. 1.0
47 What is Math.ceil(3.6)?
A. 3.0
B. 3
C. 4.0
D. 5.0
13
48 What is Math.floor(3.6)?
A. 3.0
B. 3
C. 4
D. 5.0
50 __________ is to implement one method in the structure chart at a time from the top to the bottom.
A. Bottom-up approach
B. Top-down approach
D. Stepwise refinement
6 Array Basics
51 Which of the following is incorrect?
A. int[] a = new int[2];
52 Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]?
A. i
B. (int)(Math.random() * 100))
C. i + 10
D. i + 6.5
E. Math.random() * 100
14
D. char[] c = new char();
A. 0
B. 1
C. 2
D. 3
E. 4
55 Analyze the following code:
public class Test {
public static void main(String[] args) {
int[] x = new int[5];
int i;
for (i = 0; i < x.length; i++)
x[i] = i;
System.out.println(x[i]);
}
}
C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.
D. The program has a compile error because i is not defined in the last statement in the main method.
A. 1 2 3
B. 1 1 1
C. 0 1 2
D. 0 1 3
C. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.
60 When you return an array from a method, the method returns __________.
A. a copy of the array
16
61 Which of the following declarations are correct?
A. public static void print(String... strings, double... numbers)
62 Use the selectionSort method presented in this section to answer this question. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the
content of list after the first iteration of the outer loop in the method?
63 The __________ method sorts the array scores of the double[] type.
A. java.util.Arrays(scores)
B. java.util.Arrays.sorts(scores)
C. java.util.Arrays.sort(scores)
D. Njava.util.Arrays.sortArray(scores)
64 Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return?
A. 0
B. -1
C. 1
D. 2
E. -2
66 Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length?
A. 2, 3, and 3
B. 2, 3, and 4
C. 3, 3, and 3
17
D. 3, 3, and 4
E. 2, 2, and 2
67 What is the printout of the following program?
public class Test {
public static void main(String[] args) {
int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};
int v = values[0][0];
for (int row = 0; row < values.length; row++)
for (int column = 0; column < values[row].length; column++)
if (v < values[row][column])
v = values[row][column];
System.out.print(v);
}
}
A. 1
B. 3
C. 5
D. 6
E. 33
68 What is the output of the following code?
public class Test {
public static void main(String[] args) {
int[][] matrix =
{{1, 2, 3, 4},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15}};
for (int i = 0; i < 4; i++)
System.out.print(matrix[i][1] + " ");
}
}
A. 1 2 3 4
B. 4 5 6 7
C. 1 3 8 12
D. 2 5 9 13
E. 3 6 10 14
69 Suppose a method p has the following heading:
public static int[][] p()
What return statement may be used in p()?
A. return 1;
A. 3 33
B. 1 1
C. 5 6
D. 5 33
E. 33 5
D. Constructors are invoked using the new operator when an object is created.
19
74 Analyze the following code:
public class Test {
public static void main(String[] args) {
A a = new A();
a.print();
}
}
class A {
String s;
A(String s) {
this.s = s;
}
void print() {
System.out.println(s);
}
}
A. The program has a compilation error because class A is not a public class.
B. The program has a compilation error because class A does not have a default constructor.
D. The program would compile and run if you change A a = new A() to A a = new A("5").
75 The default value for data field of a boolean type, numeric type, object type is ___________, respectively.
A. true, 1, Null
B. false, 0, null
C. true, 0, null
D. true, 1, null
E. false, 1, null
D. A variable of a reference type holds a reference to where an object is stored in the memory.
B. private variables
C. instance variables
D. class variables
20
78 You should add the static keyword in the place of ? in Line ________ in the following code:
1 public class Test {
2 private int age;
3
4 public ? int square(int n) {
5 return n * n;
6}
7
8 public ? int getAge() {
9}
10}
A. in line 4
B. in line 8
D. none
B. a class method
C. an instance method
D. an object method
A. The code has a compile error because xMethod does not return a value.
B. The code has a compile error because xMethod is not declared static.
81 What is the printout of the second println statement in the main method?
public class Foo {
int i;
static int s;
public static void main(String[] args) {
Foo f1 = new Foo();
System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
Foo f2 = new Foo();
System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
21
Foo f3 = new Foo();
System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
}
public Foo() {
i++;
s++;
}
}
A. f2.i is 1 f2.s is 1
B. f2.i is 1 f2.s is 2
C. f2.i is 2 f2.s is 2
D. f2.i is 2 f2.s is 1
82 What code may be filled in the blank without causing syntax or runtime errors:
public class Test {
java.util.Date date;
public static void main(String[] args) {
Test test = new Test();
System.out.println(_________________);
}
}
A. test.date
B. date
C. test.date.toString()
D. date.toString()
A. The program has a compilation error because the NClass class has a private constructor.
B. The program does not compile because the parameter list of the main method is wrong.
C. The program compiles, but has a runtime error because t has no initial value.
22
}
}
B. The variable t is private and therefore cannot be accessed in the main method.
A. 1234567 1234567
B. 1234567 7654321
C. 7654321 1234567
D. 7654321 7654321
23
The String Class
88 Which of the following statements is preferred to create a string "Welcome to Java"?
A. String s = "Welcome to Java";
90 Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
A. String s = new String("new string");
B. String s3 = s1 + s2
C. s1 >= s2
D. int i = s1.length
E. s1.charAt(0) = '5'
91 The StringBuilder methods _____________ not only change the contents of a string buffer, but also returns a reference to the string
buffer.
A. delete
B. append
C. insert
D. reverse
E. replace
B. PrintWriter
C. Scanner
D. System
B. PrintWriter
C. Scanner
D. System
B. inheritance
C. abstraction
D. generalization
B. A subclass is usually extended to contain more functions and more detailed information than its superclass.
A. Nothing displayed
25
B. "The default constructor of B is invoked"
A. The program has a compilation error, because m is overridden with a different signature in B.
B. The program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.
C. The program has a runtime error on b.i, because i is not accessible from b.
D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.
100 Which of the following statements are true?
A. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its
superclass.
B. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish
them.
C. It is a compilation error if two methods differ only in return type in the same class.
D. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are
completely unrelated.
E. A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined
in the superclass is hidden.
26
101 Which of the following statements are true?
A. A method can be overloaded in the same class.
C. If a method overloads another method, these two methods must have the same signature.
D. If a method overrides another method, these two methods must have the same signature.
A. Person Person
B. Person Student
C. Stduent Student
D. Student Person
27
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. true false
B. false true
C. true true
D. false false
105 What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but
are accessible to any subclasses in any package?
A. public
B. private
C. protected
28
C. that a variable of supertype can refer to a subtype object.
GUI Components
109 Swing components that don't rely on native GUI are referred to as ___________.
A. lightweight components
B. heavyweight components
C. GUI components
D. non-GUI components
B. Swing components
C. GUI components
D. Non-GUI components
A. 1 2 3
B. 1 3 2
C. 2 1 3
D. 3 2 1
C. Both button OK and button Cancel are displayed and button OK is displayed on the left side of button OK.
D. Both button OK and button Cancel are displayed and button OK is displayed on the right side of button OK.
A. 1.
B. 2.
C. 3.
D. 0.
114 Suppose a JFrame uses the GridLayout(2, 2). If you add six buttons to the frame, how many columns are displayed?
A. 1
B. 2
C. 3
D. 4
115 Suppose a JFrame uses the GridLayout(0, 2). If you add six buttons to the frame, how many columns are displayed?
A. 1
B. 2
C. 3
D. 4
D. new Color(1, 2, 3)
30
117 The method __________ sets the font (Helvetica, 20-point bold) in component C.
A. c.setFont(new Font("Helvetica", Font.bold, 20))
B. p.getContentPane(c)
C. p.insert(c)
D. p.append(c)
31