Exam JP2011(Java Programming) MidTerm Test 5/6 2011

advertisement
Exam
JP2011(Java Programming) MidTerm Test
Name____________________________
1. 25 % 1 is ________.
a) 1
b) 2
2. -24 % -5 is ________.
a) 3
b) -3
5/6 2011
RegNo _________________________________
8. What is the printout of the following switch statement?
c) 3
c) 4
d) 4
d) -4
e) 0
char ch = ʹbʹ;
switch (ch) {
case ʹaʹ: System.out.print(ch);
case ʹbʹ: System.out.print(ch);
case ʹcʹ: System.out.print(ch);
case ʹdʹ: System.out.print(ch);
}
a) abcd
b) bb
c) bcd
e) 0
3. Which of these data types requires the most amount of memory?
a) long
b) int
c) byte
d) short
4. What is the printout of the following code:
e) bbb
9. The following code displays ________.
double x = 5.5;
int y = (int)x;
System.out.println(ʺx is ʺ + x + ʺ and y is ʺ + y);
a) x is 5.5 and y is 5.0
b) x is 6 and y is 6
c) x is 5 and y is 6
d) x is 5.5 and y is 5
e) x is 6.0 and y is 6.0
5. Suppose x is 1. What is x after x -= 1?
a) 0
b) 1
c) 2
d) b
double temperature = 50;
if (temperature >= 100) System.out.println(ʺtoo hotʺ);
else if (temperature <= 40) System.out.println(ʺtoo coldʺ);
else System.out.println(ʺjust rightʺ);
a) too cold
c) too hot
d) -1
b) just right
d) too hot too cold just right
10. What is the number of iterations in the following loop:
e) -2
for (int i = 1; i < n; i++) { // iteration
}
6. The expression ʺJava ʺ + 1 + 2 + 3 evaluates to ________.
a) java 123
b) Java 123
c) Java6
d) Java123
e) Illegal expression
a) n + 1
b) n - 1
c) 2*n
d) n
11. (int)(ʹaʹ + Math.random() * (ʹzʹ - ʹaʹ + 1)) returns a random number ________.
a) between (int)ʹaʹ and (int)ʹzʹ
b) between ʹaʹ and ʹzʹ
c) between ʹaʹ and ʹyʹ
d) between 0 and (int)ʹzʹ
7. What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0?
a) true
b) false
c) There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true.
1
12. Analyze the following code fragments that assign a boolean value to the
variable even.
15. Analyze the following fragment:
double sum = 0;
double d = 0;
while (d != 10.0) {
d += 0.1;
sum += sum + d;
}
a) The program never stops because d is always 0.1 inside the loop.
b) The program may not stop because of the phenomenon referred to as
numerical inaccuracy for operating with floating-point numbers.
c) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9
d) The program does not compile because sum and d are declared double,
but assigned with integer value 0.
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) All three are correct, but Code 2 is preferred.
c) All three are correct, but Code 1 is preferred.
d) All three are correct, but Code 3 is preferred.
e) Code 3 has a compile error, because you attempt to assign number to
even.
16. Will the following program terminate?
int balance = 10;
while (true) {
if (balance < 9) break;
balance = balance - 9;
}
a) Yes
13. 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) 8
b) 10
c) 9
d) 11
17. Which of the following statements are correct to invoke the printMax
method in Listing 6.5 in the textbook? (Choose all that apply.)
a) printMax(new double[ ]{1, 2, 3});
b) printMax(1.0, 2.0, 2.0, 1.0, 4.0);
c) printMax(1, 2, 2, 1, 4);
d) printMax(new int[ ]{1, 2, 3});
e) 0
14. How many times will the following code print ʺWelcome to Javaʺ?
18. What is Math.ceil(3.6)?
a) 4.0
b) 3
int count = 0;
do { System.out.println(ʺWelcome to Javaʺ);
count++;
} while (count < 10);
a) 9
b) 10
c) 0
d) 11
b) No
e) 8
2
c) 5.0
d) 3.0
19. The selectionSort method is defined in this section. What is list1 after
executing the following statements?
22. What is k after the following block executes?
{ int k = 2;
nPrint(ʺA messageʺ, k); }
System.out.println(k);
double[ ] list1 = {3.1, 3.1, 2.5, 6.4};
selectionSort(list1);
a) list1 is 3.1, 2.5, 3.1, 6.4
c) list1 is 3.1, 3.1, 2.5, 6.4
b) list1 is 6.4, 3.1, 3.1, 2.5
d) list1 is 2.5 3.1, 3.1, 6.4
a) 1
b) 2
c) 0
d) k is not defined outside the block. So, the program has a compile error
20. Analyze the following code:
class Test {
public static void main(String[ ] args) {
System.out.println(xmethod(5));
}
23. In the following code, what is the printout for list2?
class Test {
public static void main(String[ ] args) {
int[ ] list1 = {1, 2, 3};
int[ ] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = 0; i < list2.length; i++)
System.out.print(list2[i] + ʺ ʺ);
} }
public static int xmethod(int n, long t) {
System.out.println(ʺintʺ);
return n;
}
public static long xmethod(long n) {
System.out.println(ʺlongʺ);
return n;
} }
a) 0 1 2
b) 1 1 1
c) 0 1 3
d) 1 2 3
24. Which of the following statements is valid? (Choose all that apply.)
a) double d[ ] = new double[30];
b) int[ ] i = {3, 4, 3, 2};
c) int i = new int(30);
d) char[ ] c = new char();
e) char[ ] c = new char[4]{ʹaʹ, ʹbʹ, ʹcʹ, ʹdʹ};
a) The program displays long followed by 5.
b) The program displays int followed by 5.
c) The program runs fine but displays things other than 5.
d) The program does not compile because the compiler cannot distinguish
which xmethod to invoke.
21. The keyword ________ is required to declare a class.
a) class
b) public
c) private
d) All of the above.
3
25. Show the output of the following code:
29. Analyze the following code:
public class Test {
public static void main(String[ ] args) {
int[ ] x = {1, 2, 3, 4, 5};
increase(x);
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]);
}
}
int[ ] y = {1, 2, 3, 4, 5};
increase(y[0]);
System.out.println(x[0] + ʺ ʺ + y[0]);
}
a) The program has a compile error because new boolean[3][ ] is wrong.
b) The program runs and displays x[2][2] is true.
c) The program runs and displays x[2][2] is false.
d) The program has a runtime error because x[2][2] is null.
e) The program runs and displays x[2][2] is null.
public static void increase(int[ ] x) {
for (int i = 0; i < x.length; i++)
x[i]++;
}
public static void increase(int y) {
y++;
}
}
a) 2 1
b) 2 2
c) 1 2
d) 1 1
30. When invoking a method with an object argument, ________ is passed.
a) the reference of the object
b) a copy of the object
c) the object is copied, then the reference of the copied object
d) the contents of the object
e) 0 0
31. Suppose a method p has the following heading:
26. The default value for data field of a boolean type, numeric type, object type
is ________, respectively.
a) false, 1, null
b) true, 0, null
c) true, 1, Null
d) true, 1, null
e) false, 0, null
public static int[ ][ ] p()
What return statement may be used in p()?
a) return int[ ]{1, 2, 3};
b) return new int[ ][ ]{{1, 2, 3}, {2, 4, 5}};
c) return new int[ ]{1, 2, 3};
d) return {1, 2, 3};
e) return 1;
27. Suppose you wish to provide an accessor method for a boolean property
finished, what signature of the method should be?
a) public void getFinished()
b) public boolean isFinished()
c) public void isFinished()
d) public boolean getFinished()
28. An object is an instance of a ________.
a) class
b) method
c) program
d) data
4
34. Suppose Character x = new Character(ʹaʹ), ________ returns true. (Choose all
that apply.)
a) x.compareToIgnoreCase(ʹAʹ)
b) x.equals(ʹaʹ)
c) x.equals(ʺaʺ)
d) x.equalsIgnoreCase(ʹAʹ)
e) x.equals(new Character(ʹaʹ))
32. What is the value of myCount.count 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);
35. Analyze the following code: (Choose all that apply.)
System.out.println(
ʺmyCount.count = ʺ + myCount.count);
System.out.println(ʺtimes = ʺ+ times);
}
public class Test {
public static void main(String[ ] args) {
A a = new A();
a.print();
}}
public static void increment(Count c, int times) {
c.count++;
times++;
} }
class A {
String s;
A(String s) { this.s = s; }
void print() { System.out.println(s); }
}
class Count {
int count;
Count(int c) { count = c; }
Count() { count = 1; }
}
a) 99
b) 100
c) 101
a) The program would compile and run if you change A a = new A() to A
a = new A(ʺ5ʺ).
b) The program has a compilation error because class A does not have a
default constructor.
c) The program compiles and runs fine and prints nothing.
d) The program has a compilation error because class A is not a public
class.
d) 98
33. Assume s is ʺABCABCʺ, the method ________ returns a new string
ʺaBCaBCʺ.
a) s.replace(ʺABCABCʺ, ʺaBCaBCʺ)
b) s.replace(ʹaʹ, ʹAʹ)
c) s.toLowerCase(s)
d) s.replace(ʹAʹ, ʹaʹ)
e) s.toLowerCase()
36. ________ is invoked to create an object.
a) A method with a return type
b) The main method
c) A constructor
d) A method with the void return type
5
37. Analyze the following code and choose the best answer:
41. What is the output of the following code?
public class Foo {
private int x;
public class Test { public static void main(String[ ] args) {
String s1 = ʺWelcome to Java!ʺ;
String s2 = s1;
public static void main(String[ ] args) {
Foo foo = new Foo();
System.out.println(foo.x);
}
}
if (s1 == s2)
System.out.println(ʺs1 and s2 reference to the same String objectʺ);
else
System.out.println(ʺs1 and s2 reference to different String objectsʺ);
}
}
a) Since x is private, it cannot be accessed from an object foo.
b) You cannot create a self-referenced object; that is, foo is created inside
the class Foo.
c) Since x is an instance variable, it cannot be directly used inside a main
method. However, it can be accessed through an object such as foo in
this code.
d) Since x is defined in the class Foo, it can be accessed by any method
inside the class without using an object. You can write the code to
access x without creating an object such as foo in this code.
a) s1 and s2 reference to different String objects
b) s1 and s2 reference to the same String object
42. Analyze the following code: (Choose all that apply.)
public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new Object();
System.out.println(a1);
System.out.println(a2);
} }
38. Which of the following is the correct header of the main method? (Choose all
that apply.)
a) public static void main(String x[ ])
b) public static void main(String[ ] args)
c) public static void main(String args[ ])
d) static void main(String[ ] args)
e) public static void main(String[ ] x)
class A {
int x;
public String toString() { return ʺAʹs x is ʺ + x; }
}
39. Which of the following statements creates an instance of File on Window for
the file c:\t.txt?
a) new File(ʺc://txt.txtʺ)
b) new File(ʺc:/txt.txtʺ)
c) new File(ʺc:\\txt.txtʺ)
d) new File(ʺc:\txt.txtʺ)
a) When executing System.out.println(a2), the toString() method in the
Object class is invoked.
b) The program cannot be compiled, because System.out.println(a1) is
wrong and it should be replaced by System.out.println(a1.toString());
c) When executing System.out.println(a1), the toString() method in the
Object class is invoked.
d) When executing System.out.println(a1), the toString() method in the A
class is invoked.
40. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the
following methods will cause the list to become [Beijing, Chicago,
Singapore]?
a) x.add(ʺChicagoʺ)
b) x.add(1, ʺChicagoʺ)
c) x.add(0, ʺChicagoʺ)
d) x.add(2, ʺChicagoʺ)
6
48. Suppose A is an abstract class, B is a concrete subclass of A, and both A and
B have a default constructor. Which of the following is correct? (Choose all
that apply.)
a) A a = new A();
b) B b = new B();
c) A a = new B();
d) B b = new A();
43. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the
following method will cause runtime errors? (Choose all that apply.)
a) x.remove(2)
b) x.get(2)
c) x.set(2, ʺNew Yorkʺ);
d) x.get(1)
e) x.size()
49. What is the output of the following code:
44. You can create an ArrayList using ________.
a) ArrayList()
b) new ArrayList()
c) new ArrayList[ ]
d) new ArrayList[100]
public class Test {
public static void main(String[ ] args) {
Object o1 = new Object();
Object o2 = new Object();
System.out.print((o1 == o2) + ʺ ʺ + (o1.equals(o2)));
} }
45. Which of the following statements are true? (Choose all that apply.)
a) A method may be implemented in several subclasses. The Java Virtual
Machine dynamically binds the implementation of the method at
runtime.
b) You can always pass an instance of a subclass to a parameter of its
superclass type. This feature is known as polymorphism.
c) The compiler finds a matching method according to parameter type,
number of parameters, and order of the parameters at compilation
time.
d) Dynamic binding can apply to static methods.
e) Dynamic binding can apply to instance methods.
a) false true
b) false false
c) true true
d) true false
50. Object-oriented programming allows you to derive new classes from
existing classes. This is called ________.
a) generalization
b) abstraction
c) encapsulation
d) inheritance
51. Analyze the following code: (Choose all that apply.)
46. Which of the following statements are true? (Choose all that apply.)
a) A method can be overridden in the same class.
b) If a method overrides another method, these two methods must have
the same signature.
c) If a method overloads another method, these two methods must have
the same signature.
d) A method can be overloaded in the same class.
ArrayList list = new ArrayList();
list.add(ʺBeijingʺ);
list.add(ʺTokyoʺ);
list.add(ʺShanghaiʺ);
list.set(3, ʺHong Kongʺ);
a) The last line in the code causes a runtime error because there is no
element at index 3 in the array list.
b) If you replace the last line by list.add(4, ʺHong Kongʺ), the code will
compile and run fine.
c) If you replace the last line by list.add(3, ʺHong Kongʺ), the code will
compile and run fine.
d) The last line in the code has a compile error because there is no element
at index 3 in the array list.
47. To create an instance of BigDecimal for 454.45, use
a) new BigDecimal(ʺ454.45ʺ);
b) BigInteger(454.45);
c) BigInteger(ʺ454.45ʺ);
d) new BigInteger(454.45);
7
52. Assume Calendar calendar = new GregorianCalendar(). ________ returns the
number of days in a month.
a) calendar.get(Calendar.WEEK_OF_YEAR)
b) calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
c) calendar.get(Calendar.MONTH_OF_YEAR)
d) calendar.get(Calendar.MONTH)
e) calendar.get(Calendar.WEEK_OF_MONTH)
56. Which statements are most accurate regarding the following classes?
class A {
private int i;
protected int j; }
class B extends A {
private int k;
protected int m;}
53. Given the following code, find the compile error. (Choose all that apply.)
a) An object of B contains data fields j, m.
b) An object of B contains data fields k, m.
c) An object of B contains data fields i, j, k, m.
d) An object of B contains data fields j, k, m.
public class Test {
public static void main(String[ ] args) {
m(new GraduateStudent()); m(new Student());
m(new Person()); m(new Object());
}
public static void m(Student x) { System.out.println(x.toString()); }
}
57. Which of the following statements are true? (Choose all that apply.)
a) If you compile an interface without errors, a .class file is created for the
interface.
b) If you compile an interface without errors, but with warnings, a .class
file is created for the interface.
c) If you compile a class without errors but with warnings, a .class file is
created.
d) If you compile a class with errors, a .class file is created for the class.
class GraduateStudent extends Student { }
class Student extends Person {
public String toString() { return ʺStudentʺ; } }
class Person extends Object {
public String toString() { return ʺPersonʺ; }}
58. ________ is a reference type. (Choose all that apply.)
a) An array type
b) A primitive type
c) An interface type
d) A class type
a) m(new Person()) causes an error
b) m(new Object()) causes an error
c) m(new Student()) causes an error
d) m(new GraduateStudent()) causes an error
59. Which of the following classes are immutable? (Choose all that apply.)
a) Double
b) BigDecimal
c) String
d) Integer
e) BigInteger
54. Suppose A is an interface, B is a concrete class with a default constructor that
implements A. Which of the following is correct? (Choose all that apply.)
a) A a = new B();
b) B b = new B();
c) B b = new A();
d) A a = new A();
60. To assign a double variable d to a float variable x, you write
a) x = (int)d;
b) x = d;
c) x = (long)d
d) x = (float)d;
55. ________ is a reference type. (Choose all that apply.)
a) An interface type
b) A class type
c) A primitive type
d) An array type
8
Answer Key
Testname: JP2011MIDTERMTESTV2
1. e
2. d
3. a
4. d
5. a
6. b
7. c
8. e
9. b
10. b
11. a
12. d
13. b
14. b
15. b
16. a
17. a, b, c
18. a
19. d
20. a
21. a
22. d
23. a
24. a, b
25. a
26. e
27. b
28. a
29. c
30. a
31. b
32. c
33. d
34. b, e
35. a, b
36. c
37. c
38. a, b, c, e
39. c
40. b
41. b
42. a, d
43. a, b, c
44. b
45. a, b, c, e
46. b, d
47. a
48. b, c
49. b
50. d
51. a, c
52. b
53. a, b
54. a, b
55. a, b, d
56. c
57. a, b, c
58. a, c, d
59. a, b, c, d, e
60. d
9
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
10
Download