Document 6532858

Transcription

Document 6532858
1 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
Test 2009 Spring Midterm Sample
Instructions: Write your name on the examination. DO write on the examination, use it for scratch paper. You will get the
examination back with your results.
This is a closed book, notes, neighbor examination. Select the best answer.
No, it is not a typo, all code appears as intended. Any text in this font is as intended. Do not ask the instructor about any
"typos" that is in this font. All output is to be considered what appears in the console.
When you have finished the examination, turn in both your answer sheet and the examination.
1. Which of the following statements is true?
A. Instance variables have to be assigned a value before use, but local variables do not.
B. Local variables have to be assigned a value before use, but instance variables do not. (Correct Answer)
C. Both types of variables must be assigned a value before use.
D. Neither type of variable must be assigned a value before use.
E. None of the above
2. Inside one object, which one of the following could cause an error?
A. A function, int create(), and a function string create(). (Correct Answer)
B. A function, int create(), and a function string create(string input).
C. A function, int create(), and a function int create(int total).
D. A function, int create(), and a function int create(string input).
E. None of the above
3. True or False? While loops can be nested.
A. True (Correct Answer)
B. False
4. True or False? A class can implement more than one interface.
A. True (Correct Answer)
B. False
5. In the class header public class TV.
A. public is the access modifier and TV is the name of the class (Correct Answer)
B. public is the name of the class and TV is the access modifier
6. True or False? int9 is a valid identifier.
A. True (Correct Answer)
B. False
7. The accessors are the _________________ methods
A. set
B. get (Correct Answer)
8. True or False? private fields and methods are part of the inheriting class object.
A. True (Correct Answer)
B. False
9. What keyword do we use when instantiating an object _________________
A. Create
B. =
C. new (Correct Answer)
D. Object
E. is
10. True or False? The conditional operator ?: can be used in expressions
A. True (Correct Answer)
B. False
11. True or False? A method from a subclass overriding a method from a superclass cannot call the superclass method.
A. True
B. False (Correct Answer)
12. Which of the following conditional tests is invalid?
A. int a = 1;
2/23/2010 8:47 PM
2 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
int b = 10;
While (a <= b){}
B. int a =1;
int b = 10;
while (b > a){}
C. int a = 5;
while (a == 5){}
D. int a = 5;
while (a = 5){} (Correct Answer)
E. None of the above
13. Which of the following codes exhibits the correct way of obtaining the length of a string object?
A. String s = "Java";
int l = s.size();
B. String s = "Java";
int l = size(s);
C. String s = "Java";
int l = length(s);
D. String s = "Java";
int l = s.length(); (Correct Answer)
E. None of the above
14. Given:
1: public class For {
2:
public void test() {
3:
for( ; ; ) {
4:
System.out.println("Inside the loop");
5:
}
6:
}
7: }
Will the code block compile?
A. Yes
B. No
15. Where is memory allocated for objects when they are instantiated?
A. On the stack.
B. On the heap. (Correct Answer)
C. On the stack or the heap depending on how the object is instantiated.
D. On the hard drive.
E. None of the above
16. How many times will a loop with the for loop header for( i = 1; i <= 6; i++ ) execute?
A. 1
B. 5
C. 6 (Correct Answer)
D. 7
17. When a loop condition is grade >= 90, what values would you test your loop with?
A. 88 and 89
B. 0 and 90
C. 89, 90 and 91 (Correct Answer)
D. 90, 91 and 92
18. True or False? A while loop can be nested inside a for loop and vice-versa.
A. True (Correct Answer)
B. False
19. Which one of these is not a primitive data type?
A. int
B. double
2/23/2010 8:47 PM
3 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
C. bool (Correct Answer)
D. long
E. char
20. integer, Boolean, float numbers are variables of the__________type
A. object reference
B. PRIMITIVE (Correct Answer)
C. STATIC
D. PRIMARY
E. None of the above
21. To access the number of elements in an array named grades, we use _________________ .
A. grades
B. grades.size( )
C. grades.size
D. grades.length( )
E. grades.length (Correct Answer)
22. Please mark the false statement below.
A. A class can have any number of instance variables.
B. A method can have many arguments.
C. Instance variables should be encapsulated to avoid unwanted/unexpected change in value.
D. A method can have more than one return variable. (Correct Answer)
E. None of the above
23. In a for loop header, statements are separated with _______.
A. Colons
B. Commas
C. Semi-colons (Correct Answer)
D. Preiods
24. When a class implements an interface, _________________ .
A. It does not have to implement the abstract methods of the interface.
B. It has to implement at least one of the abstract methods of the interface but not all of them.
C. It has to implement all of the abstract methods of the interface. (Correct Answer)
25. True or False? abstract methods can be static.
A. True
B. False (Correct Answer)
26. True or False? abstract methods can be called.
A. True
B. False (Correct Answer)
27. Let
int x=0;
int y=++x;
int z=x++;
After the last instruction is executed, the values of x, y and z are:
A. x=1, y=0, z=0
B. x=1, y=0, z=1
C. x=1, y=1, z=0
D. x=2, y=1, z=1 (Correct Answer)
E. None of the above
28. JVM stands for:
A. Java Virtual Memory
B. Job Virtual Machine
C. Java Virtual Mode
D. Java Veritable Machine
E. Java Virtual Machine (Correct Answer)
29. How long is the string returned by the following expression?
"Was it a car or a cat I saw?".substring(9, 12)
A. It's 2 characters in length
B. It's 3 characters in length (Correct Answer)
C. It's 4 characters in length
D. It's 5 characters in length
E. None of the above
2/23/2010 8:47 PM
4 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
30. True or False? char is a valid identifier.
A. True
B. False (Correct Answer)
31. True or False? We can pass an array parameter to a method.
A. True (Correct Answer)
B. False
32. What happens if Integer.parseInt() is passed something that's not a number, like "two"?
A. Nothing.
B. It will throw an exception at runtime. (Correct Answer)
C. It will do the best it can to interpret the string passed in and return some kind of integer.
D. None of the above
33. True or False? The first index of a String is 1
A. True
B. False (Correct Answer)
34. True or False? In a given class, there can be only one constructor.
A. True
B. False (Correct Answer)
35. What would be appropriate arguments to pass to this function:
public static void doStuff(int x, char y) { }
A. doStuff(34, "oh");
B. doStuff(3.14159, 'z');
C. doStuff('z', 42);
D. doStuff(42, 'z'); (Correct Answer)
E. None of the above
36. 1.The name of the Java compiler is:
A. java
B. javac (Correct Answer)
C. compiler
D. cc
E. jc
37. The conversion from a primitive numeric type to its wrapper class is called _________________ .
A. boxing
B. unboxing
C. autoboxing (Correct Answer)
38. When you use a while loop to count how many values meet a certain condition, you should initialize the variable holding
the count to ______ .
A. 0 (Correct Answer)
B. 1
C. NULL
D. There is no need to initialize it
39. True or False? By convention, method names, instance variables and object reference names start with a lower case
letter.
A. True (Correct Answer)
B. False
40. What will be the result of executing the following code?
1: public static void main(String args[]) {
2:
char digit = 'a';
3:
for (int i = 0; i < 10; i++) {
4:
switch (digit) {
5:
case 'x': {
6:
int j = 0;
7:
System.out.println(j);
8:
}
9:
default: {
10:
int j = 100;
11:
System.out.println(j);
12:
}
13:
}
14:
}
15:
int i = j;
2/23/2010 8:47 PM
5 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
16:
System.out.println(i);
17: }
A. 100 will be printed 11 times.
B. 100 will be printed 10 times and then there will be a runtime exception.
C. The code will not compile because the variable i cannot be declared twice within the main() method.
D. The code will not compile because the variable j cannot be declared twice within the switch statement.
E. None of the above. (Correct Answer)
41. Why use encapsulation?
A. To prevent your instance variables from being set to values that would break your code.
B. So that you don't break anyone's code in the future when you need to add more features.
C. both A and B (Correct Answer)
D. None of the above
42. Assuming the for loop header for( i = 0; i <= 7; i++ ), what will be value of i after we exit the loop?
A. 0
B. 6
C. 7
D. 8 (Correct Answer)
43. What keyword do we use in the class header that inherits from an interface?
A. inherits
B. interfaces
C. implements (Correct Answer)
D. modifies
44. True or False? A class must implement at least one interface.
A. True
B. False (Correct Answer)
45. Constructors
A. Are inherited but cannot be accessed
B. Are inherited and can be accessed
C. Are accessible but are not inherited (Correct Answer)
D. Are not inherited and cannot be accessed
46. True or False? An interface can contain one or more abstract methods.
A. True (Correct Answer)
B. False
47. How Many times will a Constructor execute.
A. Once at Initialization and Once during garbage collection
B. Whenever you call it with "dot" notation.
C. Once (Correct Answer)
D. Only when it is called from the Heap.
E. None of the above
48. True or False? In an if statement, curly braces around the true block are always mandatory
A. True
B. False (Correct Answer)
49. True or False? The 4 relational operators, >, >=, <, <= are binary operators.
A. True (Correct Answer)
B. False
50. True or False? The loop condition is a Boolean expression.
A. True (Correct Answer)
B. False
51. Which is the correct way to write a method that takes two parameters?
A. methodName( int a, int b ){ }
B. void methodName( int a, int b ){ } (Correct Answer)
C. void methodName( a, b ){ }
D. void methodName ( int a, int b )
E. None of the above
52. Which of the following lines will print false?
1.
2.
public class MyClass
{
2/23/2010 8:47 PM
6 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
3.
static String s1 = "I am unique!";
4.
public static void main(String args[])
5.
{
6.
String s2 = "I am unique!";
7.
String s3 = new String(s1);
8.
System.out.println(s1 == s2);
9.
System.out.println(s1.equals(s2));
10.
System.out.println(s3 == s1);
11.
System.out.println(s3.equals(s1));
12.
System.out.println(TestClass.s4 == s1);
13.
}
14.
}
15.
16. class TestClass
17. {
18.
static String s4 = "I am unique!";
19. }
A. Lines 10 and 12
B. Line 12 only
C. Lines 8 and 10
D. Line 8 only
E. None of the above (Correct Answer)
53. A method of a class can access
A. Only the instance variables of its class
B. Only its parameters
C. Only the local variables the method defines
D. The instance variables of its class, its parameters, and its local variables (Correct Answer)
54. What is the output of the following code? _________________ .
int age = 33;
if( age > 33 )
System.out.print( 1 );
System.out.println( 2 );
A. 0
B. 1
C. 2 (Correct Answer)
D. 12
E. (no output)
55. In the method header public static void main( String[] args )
A. args is an array of Strings (Correct Answer)
B. args is a String
C. args is a void array
D. main is an array
56. Assuming the class B inherits from class A, then
A. An object of class B is an object of class A (Correct Answer)
B. An object of class A is an object of class B
C. All of the above
D. None of the above
57. Given the code snippet
for (int i = 10; i > 0; i--) {
if (i % 2 == 0) {
System.out.println(i);
}
}
How many numbers are in the output?
A. 1
B. 2
C. 5 (Correct Answer)
D. 10
E. None of the above
58. True or False? By convention, class names start with a capital letter
A. True (Correct Answer)
B. False
59. Which of the following is false?
A. A static method can access a static variable.
B. A Java constant is a variable marked static and final.
2/23/2010 8:47 PM
7 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
C. A static method can be invoked without instantiating the class it is defined in.
D. A static variable cannot be accessed by any instantiated objects of that class. (Correct Answer)
E. None of the above
60. When class B inherits from class A....
A. A is the superclass and B is the subclass (Correct Answer)
B. A is the subclass and B is the superclass
61. How many times will a loop with the for loop header for( i = 0 i < 6; i++ ) execute?
A. 1
B. 5
C. 6 (Correct Answer)
D. 7
62. True or False? It is possible to have an array of objects.
A. True (Correct Answer)
B. False
63. True or False? For loops can be nested.
A. True (Correct Answer)
B. False
64. What is displayed when the following code is compiled and executed?
String s1 = new String("Test");
String s2 = new String("Test");
if (s1 == s2)
System.out.println("Same");
if (s1.equals(s2))
System.out.println("Equals");
A. Same
B. Equals (Correct Answer)
C. The code compiles, but nothing is displayed upon execution.
D. The code fails to compile.
65. .If you have an if/else if/else code sequence with 4 blocks, what is the minimal number of input values that you should
test your code with?
A. 1
B. 2
C. 3
D. 4 (Correct Answer)
E. 5
66. How many abstract methods must a class have to be classified as an abstract class?
A. 2
B. 1
C. 0 or more (Correct Answer)
D. 3 or more
E. None of the above
67. True or False? The body of a while loop always executes at least once.
A. True
B. False (Correct Answer)
68. What is the out put of the program below?
public class HiClass {
public static void main (String[] args){
System.out.print("Hi");
System.out.println(" cis");
System.out.print("class!");
}
2/23/2010 8:47 PM
8 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
}
A. Hi cis class!
B. Hi cis
class! (Correct Answer)
C. Hicisclass
D. Hicis class
E. None of the above
69. The rock class has a method "void doNothing()". The pebble class extends rock and has a method "int doNothing(int
windVelocity)". Is this a valid method override?
A. Yes
B. No, you are not allowed to change the argument list
C. No, you are not allowed to change the return type
D. No, but it is a valid method overload (Correct Answer)
E. None of the above
70. What is output when you attempt to compile and run the following code?
public class ScopeTest {
static int x = 2;
public static void main(String[] args) {
int x = 10;
ScopeTest s = new ScopeTest();
s.x++;
System.out.println(x);
ScopeTest.showint(2);
System.out.println(x);
s.x++;
s.showint(1);
}
public static void showint(int a) {
System.out.println(x);
}
}
A. 10
2
10
4
B. 10
3
10
4 (Correct Answer)
C. 10
3
10
3
D. It won't compile because the second declaration of int x in main is illegal.
E. None of the above
71. True or False? The conditional operator ?: has lower precedence that the = operator
A. True
B. False (Correct Answer)
72. True or False? We can instantiate an object from an abstract class.
A. True
B. False (Correct Answer)
73. True or False? When we use a while loop, we always know in advance how many times the loop body will be executed.
A. True
B. False (Correct Answer)
74. True or False? We cannot combine the declaration and instantiation of an array.
A. True
B. False (Correct Answer)
75. What will be output when you attempt to compile and run the following code?
class Base {
int i = 99;
public void amethod() {
System.out.println("Base.amethod()");
2/23/2010 8:47 PM
9 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
}
Base() {
amethod();
}
}
public class Derived extends Base {
int i = -1;
public static void main(String argv[]) {
Base b = new Derived();
System.out.println(b.i);
b.amethod();
}
public void amethod() {
System.out.println("Derived.amethod()");
}
}
A. Derived.amethod()
-1
Derived.amethod()
B. Derived.amethod()
99
Derived.amethod() (Correct Answer)
C. 99
Derived.amethod()
D. Base.amethod()
99
Derived.amethod()
E. Compile time error
76. If you have a derived class constructor that does not have an argument, how do you call its base-class constructor that
has an argument?
A. Create an instance of the base class in the derived class's constructor.
B. Use the super keyword and the appropriate argument list. (Correct Answer)
C. Pass the argument to the constructor of the derived class and the compiler will pass it on to the base class.
D. You're out of luck.
E. None of the above
77. To access the element at index i of an array named grades, we use _________________ .
A. grades
B. grades[ i ] (Correct Answer)
C. grades( i )
D. grades{ i }
78. True or False? In a for loop, we usually (although not always) use a loop control variable for counting.
A. True (Correct Answer)
B. False
79. True or False? public fields and methods are inherited.
A. True (Correct Answer)
B. False
80. True or False? Curly braces around a for loop body are optional if the for loop body consists of only one statement.
A. True (Correct Answer)
B. False
81. True or False? = is the equality operator.
A. True
B. False (Correct Answer)
82. True or False? A variable that is declared inside the true (or false) block of an if statement is still in scope after we exit
that block.
A. True
B. False (Correct Answer)
83. True or False? String is in the java.string package
A. True
B. False (Correct Answer)
84. True or False? Inside an if statement, statements inside the true block must be indented, otherwise the code will not
compile
A. True
2/23/2010 8:47 PM
10 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
B. False (Correct Answer)
85. True or False? If a class has an abstract method, then that class is automatically an interface.
A. True
B. False (Correct Answer)
86. True or False? Mutator methods are void methods.
A. True (Correct Answer)
B. False
87. What does the dot operator (.) do?
A. It is used to set the decimal point an integer.
B. It is used to access the methods and variables of objects. (Correct Answer)
C. It is used to mark the end of a statement.
D. It is used to separate names in a file (example: Hello.World.Doc)
E. None of the above
88. There must be 1 class with "main" method. This class must be of type
A. public, like "public class" (Correct Answer)
B. private, like "private class"
C. extend, like "extend class"
D. regular, like "class"
E. None of the above
89. What is the output of this code sequence?
String word = "Java";
System.out.println( word.charAt( 1 ) );
A. J
B. a (Correct Answer)
C. v
D. "
E. None of the above
90. Which of the following is the correct way of making a constant in java?
A. static int i = 10;
B. static final int i = 10; (Correct Answer)
C. const int i = 10;
D. final int i = 10;
E. None of the above
91. True or False? When comparing objects for equality, we typically use the == operator
A. True
B. False (Correct Answer)
92. Which statement will not generate an error?
A. float offset = 42.3f; (Correct Answer)
B. char letter = "great";
C. int books = "a";
D. boolean istrue = 346;
E. None of the above
93. True or False? a && b is true only when both a and b are true
A. True (Correct Answer)
B. False
94. True or False? An interface can contain a constructor.
A. True
B. False (Correct Answer)
95. Which of the following is a data type?
A. Byte
B. Void
C. Float
D. Both A and C (Correct Answer)
E. None of the above
96. Which of the following is an enhanced loop?
A. for (int i = 0; i < 12; i++) {}
B. while (i < 0) {}
2/23/2010 8:47 PM
11 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
C. for (int numbers : numberArray) {} (Correct Answer)
D. All of these
E. None of the above
97. What is the output of the following program?
public class Output {
public static void main(String[] args) {
Output o = new Output();
o.go();
}
public void go() {
int i = 3;
int t = 0;
for (int j = 3; j < 20; j++) {
t = j;
if (j % i == 0)
System.out.print(--t + " ");
else
System.out.print(t++ + " ");
}
}
}
A.
B.
C.
D.
E.
4 4 5 7 7 8 10 10 11 13 13 14 16 16 17 19 19
2 4 5 5 7 8 8 10 11 11 13 14 14 16 17 17 19 (Correct Answer)
4 3 4 7 6 7 10 9 10 13 12 13 16 15 16 19 18
2 5 6 5 8 9 8 11 12 11 14 15 14 17 18 17 20
None of the above
98. True or False? An interface can contain one or more static constants.
A. True (Correct Answer)
B. False
99. What will happen when you attempt to compile and run this code?
class Base {
public final void amethod() {
System.out.println("amethod");
}
}
public class Fin extends Base {
public static void main(String argv[]) {
Base b = new Base();
b.amethod();
}
}
A. Compile time error indicating that a class with any final methods must be declared final itself
B. Compile time error indicating that you cannot inherit from a class with final methods
C. Run time error indicating that Base is not defined as final
D. Success in compilation and output of "amethod" at run time. (Correct Answer)
E. None of the above
100. Consider the following code:
public class A {
public final boolean foo(int a, double b) {
// do stuff
}
}
Which of the following statements is true?
A. The following method B.foo(...) overrides the A.foo(...) method.
public class B extends A {
public int foo(int a, double b) {
// do stuff
}
}
B. The following method C.foo(...) overrides the A.foo(...) method.
public class C extends A {
public boolean foo(int a, double b) {
// do stuff
}
}
C. The following method D.foo(...) overloads the A.foo(...) method.
public class D extends A {
2/23/2010 8:47 PM
12 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
public int foo(int a, double b) {
// do stuff
}
}
D. The following method E.foo(...) overloads the A.foo(...) method.
public class E extends A {
public int foo(double b) {
// do stuff
}
} (Correct Answer)
E. None of the above
101. What is a class ?
A. A group of objects
B. A source for objects
C. A blueprint to create object (Correct Answer)
D. All above
E. None of the above
102. What Java keyword do we use in the constructor of a class inheriting from another class if we want to call the constructor
of the base class?
A. call
B. super (Correct Answer)
C. constructor
D. clone
103. What is the output of this code sequence?
String word = "Java";
System.out.println( word.length( ) );
A. 0
B. 1
C. 4 (Correct Answer)
D. 5
E. None of the above
104. What will happen when you attempt to compile and run the following code?
public static void main(String[] args) {
int Output = 10;
boolean b1 = false;
if ((b1 == true) && ((Output += 10) == 20)) {
System.out.println("We are equal " + Output);
} else {
System.out.println("Not equal! " + Output);
}
}
A. Compilation error, attempting to perform binary comparison on logical data type.
B. Compilation and output of "We are equal 10".
C. Compilation and output of "Not equal! 20". (Correct Answer)
D. Compilation and output of "Not equal! 10".
E. None of the above
105. Instance variables are stored on the....
A. heap (Correct Answer)
B. stack
C. register
D. ROM
E. None of the above
106. The StringBuilder and StringBuffer classes
A. provide a mutable character sequence.
B. are better at manipulating strings than the String class.
C. have the same interface.
D. all of the above. (Correct Answer)
E. None of the above
107. True or False? An interface can contain instance variables.
A. True
B. False (Correct Answer)
108. What will happen when you attempt to compile and run the following code
2/23/2010 8:47 PM
13 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
public class As {
int i = 10;
int j;
char z = 1;
boolean b;
public static void main(String argv[]) {
As a = new As();
a.amethod();
}
public void amethod() {
System.out.println(j);
System.out.println(b);
}
}
A. Compilation succeeds and at run time an output of 0 and false (Correct Answer)
B. Compilation succeeds and at run time an output of 0 and true
C. Compile time error - b is not initialized
D. Compile time error - z must be assigned a char value
E. None of the above
109. True or False? The default constructor takes no parameter.
A. True (Correct Answer)
B. False
110. What is the signature of the main method?
A. public static int main(String[] args)
B. public static void main(String args)
C. public static void main(String[] args) (Correct Answer)
D. public static int main(String args)
E. None of the above
111. When computing the maximum value in an array of integers, you should intialize the maximum value to
_________________.
A. 0
B. 1
C. the first element in the array (Correct Answer)
D. 100
112. Which of the following statements will result in a compiler error?
A. int _x = 7;
B. int 3x = 7; (Correct Answer)
C. int $x = 7;
D. int x3 = 7;
E. None of the above
113. True or False? Inside the block of a switch statement, the break statements are optional
A. True (Correct Answer)
B. False
114. How many times will a loop with the for loop header for( i = 5; i <= 15; i++ ) execute?
A. 5
B. 10
C. 11 (Correct Answer)
D. 15
E. 16
115. True or False? We can instantiate an object from an interface.
A. True
B. False (Correct Answer)
116. True or False? The argument list of a default constructor is empty
A. True (Correct Answer)
B. False
117. How big is an Integer in Java?
A. 32 bit (Correct Answer)
B. 32 byte
C. 64 byte
D. 64 bit
E. None of the above
2/23/2010 8:47 PM
14 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
118. An abstract method _________________
A. Has a “do-nothing” body
B. Does not have a body (Correct Answer)
C. Has a body, just like another method
119. True or False? You should always put a semicolon after the closing parenthesis in a for loop header.
A. True
B. False (Correct Answer)
120. What is displayed when the following is compiled and executed?
class Parent {
private void method1() {
System.out.println("Parent's method1()");
}
public void method2() {
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent {
public void method1() {
System.out.println("Child's method1()");
}
public static void main(String args[]) {
Parent p = new Child();
p.method2();
}
}
A. Compile time error
B. Run time error
C. prints :
Parent's method2()
Parent's method1() (Correct Answer)
D. prints :
Parent's method2()
Child's method1()
121. Why are packages important?
A. They help the overall organization of a project or library.
B. They give a name scoping to prevent collisions.
C. You can make your code only accessible to other classes in the same package.
D. All of the above. (Correct Answer)
E. None of the above
122. True or False? The body of a do/while loop always executes at least once.
A. True (Correct Answer)
B. False
123. True or False? Using the for loop header for( int i = 0; i < 5; i++ ), the variable i will still be in scope after
we exit the for loop.
A. True
B. False (Correct Answer)
124. True or False? Accessor methods return void.
A. True
B. False (Correct Answer)
125. What is a benefit of using polymorphism?
A. You don't have to change your code if you create a new subclass. (Correct Answer)
B. You can change an object from one class to another.
C. You can have multiple spouses.
D. Your code will compile faster.
E. None of the above
126. True or False? A constructor can be declared abstract.
A. True
B. False (Correct Answer)
127. Which one of these is not true about static Variables/Methods?
A. Static variables can not be changed. (Correct Answer)
2/23/2010 8:47 PM
15 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
B.
C.
D.
E.
Static variables are shared across instances of classes.
Static methods do not require an instance of the class to be called.
Static methods can't access non-static variables.
None of the above
128. True or False? static methods can reference non-static variables.
A. True
B. False (Correct Answer)
129. If an array has n elements, then the last index is ______.
A. n
B. n + 1
C. n - 1 (Correct Answer)
130. Java was introduced by:
A. Microsoft
B. IBM
C. Sun Microsystems (Correct Answer)
D. AT&T
E. MIT
131. 1.The result of (double) ( 3 / 2 ) is:
A. 1.0 (Correct Answer)
B. 1.5
C. 3.0
D. 2.0
E. 0.0
132. Consider the following statement:
String hannah = "Did Hannah see bees? Hannah did.";
Which expression refers to the letter b in the string referred to by hannah.
A. hannah.charAt(14)
B. hannah.charAt(15) (Correct Answer)
C. hannah.charAt(16)
D. hannah.charAt(17)
E. None of the above
133. True or False? It is possible to create user-defined classes in Java.
A. True (Correct Answer)
B. False
134. What will happen when you attempt to compile and run this code?
public class MyMain {
public static void main(String argv) {
System.out.println("Hello world");
}
}
A. The compiler will complain that main is a reserved word and cannot be used for a class
B. The code will compile and when run will print out "Hello world"
C. The code will compile but will complain at run time that no constructor is defined
D. The code will compile but will complain at run time that main is not correctly defined (Correct Answer)
E. None of the above
135. True or False? abstract methods can be private.
A. True
B. False (Correct Answer)
136. True or False? We can instantiate an array by assigning values when the array is declared.
A. True (Correct Answer)
B. False
137. True or False? Java is an object-oriented language.
A. True (Correct Answer)
B. False
138. True or False? static methods can call non-static methods.
A. True
B. False (Correct Answer)
2/23/2010 8:47 PM
16 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
139. How many times will a loop with the for loop header for( i = 5; i < 15; i++ ) execute?
A. 5
B. 9
C. 10 (Correct Answer)
D. 11
E. 15
140. When processing all the elements of an array named grades using a for loop, what is the for loop header?
A. for( int i = 1; i <= grades.length; i++ )
B. for( int i = 1; i < grades.length; i++ )
C. for( int i = 0; i <= grades.length; i++ )
D. for( int i = 0; i < grades.length; i++ ) (Correct Answer)
141. If you would like to create an integer named x and assign it's value to 12 which of the following would be a correct way to
do so?
A. int x == 12;
B. x = 12;
C. int x = 12; (Correct Answer)
D. x == 12;
E. None of the above
142. True or False? static methods can use the object reference this.
A. True
B. False (Correct Answer)
143. The conversion from a numeric wrapper class to the corresponding primitive numeric type is called _________________
.
A. boxing
B. unboxing (Correct Answer)
C. autoboxing
144. When you compile successfully the file Hi.java, the following file is created
A. Hi
B. Hi.javac
C. Hi.class (Correct Answer)
D. class
E. Hi.java
145. What are two ways you can use a class from a library?
A. type the FULL name of the class or import it with the import statement. (Correct Answer)
B. use the class in your code and hope it works.
C. hope your IDE puts it in your code automatically
D. write a copycat class that does everything the other class does.
E. None of the above
146. Which of the following classes has constructors with correct signatures?
A. public class Foo {
foo(){
// do stuff
}
Foo(int i){
// do stuff
}
}
B. public class Foo {
int Foo(){
// do stuff
}
Foo(int i){
// do stuff
}
}
C. public class Foo {
Foo(int j){
// do stuff
}
Foo(int i){
// do stuff
2/23/2010 8:47 PM
17 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
}
}
D. public class Foo {
Foo(){
// do stuff
}
Foo(int i){
// do stuff
}
} (Correct Answer)
E. None of the above
147. When is an instance variable deleted?
A. During garbage collection after the object it is a part of loses its last reference. (Correct Answer)
B. Immediately after the object it is a part of loses its last reference.
C. There is a chance it will be deleted during garbage collection, regardless of whether or not its object has a
reference.
D. Never; only local variables are deleted.
E. None of the above
148. Why won't this code compile?
class foo {
int i = 0;
static void doStuff() {
useInt();
}
void useInt() {
int j = i;
}
}
A.
B.
C.
D.
E.
Class foo cannot have static method doStuff().
Method doStuff() cannot call non-static method useInt(). (Correct Answer)
Method useInt() cannot call non-static variable i.
Methods cannot be declared to be static.
None of the above
149. What are the two things a variable must have?
A. A variable must have a type and a name (Correct Answer)
B. A variable must have a value and semi-colon.
C. A variable must have a numeric value.
D. A variable must have name and number.
E. None of the above
150. True or False? The body of a method must be surrounded by curly braces.
A. True (Correct Answer)
B. False
151. The enum functionality is built into which package?
A. java.enum
B. java.lang (Correct Answer)
C. java.io
D. java.util
E. java.text
152. What will be the output of the following code?
public class HelloWorld {
public void main(String[] args) {
System.out.println("Hello");
System.out.print("World!");
}
}
A. Hello World!
B. HelloWorld!
C. Hello
World!
D. Hello
World!
E. None of the above (Correct Answer)
2/23/2010 8:47 PM
18 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
153. What is the output of the following code? _________________ .
int age = 30;
if( age > 33 )
System.out.print( 1 );
else
System.out.print( 2 );
System.out.println( 3 );
A. 1
B. 2
C. 12
D. 123
E. 23 (Correct Answer)
154. What will happen when you attempt to compile and run the following code?
abstract class Employee {
String name;
public abstract float calcIncome();
}
class Manager extends Employee {
public void hire(String who) {
System.out.println( who + " hired by " + name );
}
public void fire(String who) {
System.out.println( who + " fired by " + name );
}
}
public class ManagerCheck {
public static void main(String args[]) {
Manager me = new Manager();
me.hire("newbie");
me.fire("nobody");
}
}
A. who hired by name and who fired by name
B. newbie hired by name and nobody fired by name
C. newbie hired by null and nobody fired by null
D. Compilations fails (Correct Answer)
155. What is a benefit of object-oriented programming?
A. The ability to quickly expand on existing code
B. The ability to reuse code easily and often
C. The ability to extend existing code without having to touch production code
D. All of the above (Correct Answer)
E. None of the above
156. Given this code:
int a = 2;
int b = 3;
int c;
c = (a + b) * 2 - b / 3;
What is the value of c?
A. 4
B. 6
C. 8
D. 9 (Correct Answer)
E. None of the above
157. When you use a while loop to compute the sum of several values, you should initialize the variables holding the sum to
______ .
A. 0 (Correct Answer)
B. 1
C. NULL
D. There is no need to initialize it
158. A javadoc comment ends with ________.
A. //
B. /*
C. /**
2/23/2010 8:47 PM
19 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
D. */ (Correct Answer)
159. True or False? Accessor methods take no parameter.
A. True (Correct Answer)
B. False
160. The Java language and libraries from Sun Microsystems are:
A. available for a low cost to students
B. cannot be used without a run time license fee
C. free and can be downloaded at http://java.sun.com (Correct Answer)
D. are supported only on Microsoft XP
E. None of the above
161. Which of the following options will run the PropertyCheck class and produce the output SCJA?
public class PropertyCheck {
public static void main(String args[]) {
String myprop = System.getProperty("myprop");
System.out.println(myprop);
}
}
A. java -Dmyprop = SCJA PropertyCheck
B. java PropertyCheck -Dmyprop=SCJA
C. java -Dmyprop=SCJA PropertyCheck (Correct Answer)
D. java - Dmyprop=SCJA PropertyCheck
E. java -Dmyprop=SCJA PropertyCheck.class
162. True or False? When using an if/else if structure, we can only use one else if block
A. True
B. False (Correct Answer)
163. What is the initial capacity of the following StringBuilder?
StringBuilder sb = new StringBuilder("Able was I ere I saw Elba.");
A. It's the length of the initial string : 26.
B. It's the length of the initial string + 8: 26 + 8 = 34.
C. It's the length of the initial string + 16: 26 + 16 = 42. (Correct Answer)
D. It's the length of the initial string + 32: 26 + 32 = 58.
E. None of the above
164. What keyword do you use in order to be able to use a Java class inside a program?
A. include
B. class
C. import (Correct Answer)
D. package
E. with
165. True or False? Methods defined in a Java interface are public and abstract by default.
A. True. (Correct Answer)
B. False.
166. True or False? Mutator methods take no parameter.
A. True
B. False (Correct Answer)
167. What is the difference between a for loop and a while loop?
A. A for loop cannot use a boolean test.
B. A for loop cannot be used inside a while loop.
C. A while loop only has a boolean test. (Correct Answer)
D. They are the same.
E. None of the above
168. What results from attempting to compile and run the following code?
public class Ternary {
public static void main(String args[]) {
int a = 5;
System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));
}
}
A. Prints: Value is - 9
2/23/2010 8:47 PM
20 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
B. Prints: Value is - 5
C. Compilation error
D. None of these (Correct Answer)
169. Assuming the for loop header for( i = 0; i < 20; i++ ), what will be value of i after we exit the loop?
A. 0
B. 19
C. 20 (Correct Answer)
D. 21
170. True or False? The naming convention for constants (final static variables) is to make the name all uppercase.
A. True (Correct Answer)
B. False
171. True or False? if statements can be nested
A. True (Correct Answer)
B. False
172. True or False? When calling the constructor of a superclass from the constructor of the subclass, the call to that
superclass constructor must be the first statement in the subclass constructor.
A. True (Correct Answer)
B. False
173. How many "return" statements can a method have?
A. none
B. only one
C. two
D. many (Correct Answer)
E. None of the above
174. True or False? Java is a high-level language.
A. True (Correct Answer)
B. False
175. Where is the error in the array declaration int [5] numbers;
A. There is no error
B. The square brackets should be empty (Correct Answer)
C. There should be no space between int and [
D. It should be int numbers[5];
176. True or False? When computing the maximum value among a number of values, we should initialize the maximum to 0.
A. True
B. False (Correct Answer)
177. Which of these is not a primitive type in Java?
A. Int
B. Float
C. Tall (Correct Answer)
D. Short
E. None of the above
178. True or False? tax Rate is a valid identifier.
A. True
B. False (Correct Answer)
179. True or False? When an object reference is first declared, its value is null.
A. True (Correct Answer)
B. False
180. A javadoc comment starts with ________.
A. //
B. /*
C. /** (Correct Answer)
D. */
181. True or False? Curly braces around the loop body are optional if the loop body consists of only one statement.
A. True (Correct Answer)
B. False
2/23/2010 8:47 PM
21 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
182. True or False? && and || have the same level of precedence.
A. True
B. False (Correct Answer)
183. True or False? Overriding a method and overloading a method means the same thing.
A. True
B. False (Correct Answer)
184. True or False? An abstract class cannot be extended.
A. True
B. False (Correct Answer)
185. What keyword do we use in the class header that inherits from another class?
A. inherits
B. extends (Correct Answer)
C. protected
D. modifies
186. a final variable means...
A. its the result of a math equation.
B. its the last value of the program.
C. the value can't be changed. (Correct Answer)
D. it can only be changed through using methods.
E. None of the above
187. The data type of the return value of the toString method is
A. Object
B. boolean
C. String (Correct Answer)
D. class
188. Whats wrong with this statement.
int [] my_array = new int[10];
my_array[10] = 5;
A. The array needs to be initialized first.
B. There is a conversion problem.
C. The index is beyond the array size (Correct Answer)
D. my_array is a reserved name in Java.
E. None of the above
189. True or False? private fields and methods are inherited.
A. True
B. False (Correct Answer)
190. The result of (double) 3 / 2 is
A. 1.0
B. 1.5 (Correct Answer)
C. 3.0
D. 2.0
E. 0.0
191. True or False? If a class has an abstract method, then that class is automatically abstract.
A. True (Correct Answer)
B. False
192. True or False? When using an if/else if structure, it is mandatory to have an else block
A. True
B. False (Correct Answer)
193. True or False? a || b is true only when either a or b is true but not both a and b are true.
A. True
B. False (Correct Answer)
194. What are Instance Variables?
A. Variables found under main().
2/23/2010 8:47 PM
22 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
B.
C.
D.
E.
An object's data. (Correct Answer)
any boolean value.
An object's method
None of the above
195. True or False? A while loop can be used for counting input values that have a certain property, for instance positive
values
A. True (Correct Answer)
B. False
196. what value does a reference variable have, when it is not referencing to any object?
A. 0
B. 1
C. null (Correct Answer)
D. random number
E. None of the above
197. True or False? Usually, an abstract class contains at least one abstract method.
A. True (Correct Answer)
B. False
198. The switch statement can be used instead of an if/else if statement.
A. always
B. only when comparing the value of an expression to a floating point value
C. only when comparing the value of an expression to a char or integer variable
D. only when comparing the value of an expression to a char or integer constant (Correct Answer)
E. only when comparing the value of an expression to a String value
199. Polymorphism means _______.
A. One form only
B. Two forms only
C. Multiple forms (Correct Answer)
D. No form
200. What is the concatenation operator?
A. .
B. +
C. concat
201. Which of the following expresses the condition: a is less than b and c?
A. a < b || a < c
B. a < b && c
C. a < b && a < c (Correct Answer)
202. In the instance variable declaration private int value;
A. private is the access modifier, int is the name of the instance variable, and value is the data type
B. private is the name of the instance variable, int is the data type, and value is the access modifier
C. private is the access modifier, int is the data type, and value is the name of the instance variable (Correct
Answer)
D. private is the data type, int is the access modifier, and value is the name of the instance variable
203. The Creator of Java is?
A. Dennis M Ritchie
B. James Gosling (Correct Answer)
C. Bruce Eckel
D. Brian W. Kernighan
E. None of the above
204. True or False? The length method of the String class takes no argument and returns an int
A. True (Correct Answer)
B. False
205. !( a
A.
B.
C.
D.
E.
> 3 ) is equivalent to _________________ .
a < 3
a <= 3 (Correct Answer)
!a < 3
!a <= 3
!(a <= 3)
2/23/2010 8:47 PM
23 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
206. True or False? To process all the elements of an array, we use a loop
A. True (Correct Answer)
B. False
207. True or False? When comparing two floating point numbers for equality, it is safe to use the == operator
A. True
B. False (Correct Answer)
208. True or False? Using a null object reference to call a method will not generate an error.
A. True
B. False (Correct Answer)
209. Given:
int i = 25;
int j = 27;
int k = 25;
What is the result of:
System.out.println( "i > j is " + (i > j) );
System.out.println( "i < k is " + (i < k) );
System.out.println( "j == k is " + (j == k) );
System.out.println( "i == k is " + (i == k) );
A. i > j is false
i < k is true
j == k is false
i == k is true
B. i > j is true
i < k is true
j == k is true
i == k is false
C. i > j is false
i < k is false
j == k is true
i == k is false
D. i > j is false
i < k is false
j == k is false
i == k is true (Correct Answer)
E. None of the above
210. True or False? When declaring an array, we use a set of square brackets.
A. True (Correct Answer)
B. False
211. A plain array is different than ArrayList because...?
A. It isn't different.
B. A plain array doesn't need to know the size of the array when it is created but the ArrayList does.
C. The ArrayList doesn't need to know the size of the array when it is created but the plain array does. (Correct
Answer)
D. An ArrayList cannot accept a size when created.
E. None of the above
212. True or False? abstract methods can be overridden.
A. True (Correct Answer)
B. False
213. Which statements will NOT compile?
A. long foo = 15;
int bar = foo; (Correct Answer)
B. int foo = 15;
int[] bar = new int[7];
bar[0] = foo;
C. Dog fido = new Dog();
Dog rover = new Dog();
rover = fido;
D. Dog[] kennel = new Dog[7];
E. None of the above
214. True or False? When asking the user for an integer value, it is possible that the user enters a character that is not an
2/23/2010 8:47 PM
24 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
integer; using a while loop, we can keep asking the user for an integer until the user enters one.
A. True (Correct Answer)
B. False
215. The data type of the return value of the equals method is
A. Object
B. boolean (Correct Answer)
C. equals
D. class
216. True or False? helloWorld is a valid identifier.
A. True (Correct Answer)
B. False
217. Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How
can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C).
A. super.test();
B. super.super.test();
C. ::test();
D. C.test();
E. It is not possible to invoke test() method defined in C from a method in A. (Correct Answer)
218. !( a
A.
B.
C.
D.
E.
> 38 && b <= 67 ) is equivalent to _________________ .
a <= 38 && !b <= 67
!a > 38 || !b <= 67
!( a > 38 ) && !( b <= 67 )
!( a > 38 ) || !( b <= 67 ) (Correct Answer)
a < 38 || b >= 67
219. What is the name for the relationship between a superclass and its subclasses?
A. Philanthropy
B. Ancestry
C. Instruction
D. Inheritance (Correct Answer)
E. None of the above
220. True or False? A method of a class can call other methods of that same class.
A. True (Correct Answer)
B. False
221. True or False? To compare two arrays for equality, you call the equals method with the first array and pass the second
array as the argument.
A. True
B. False (Correct Answer)
222. The mutators are the _________________ methods
A. set (Correct Answer)
B. get
223. What is the output of the following code? _________________ .
int age = 34;
if( age > 33 )
System.out.print( 1 );
System.out.println( 2 );
A. 0
B. 1
C. 2
D. 12 (Correct Answer)
E. (no output)
224. True or False? A method cannot return an array.
A. True
B. False (Correct Answer)
225. The following is NOT true about variables in Java
A. variable does not need type defined (Correct Answer)
B. variable must have a name
C. variable has a size
D. variables may hold primitives or references to objects
2/23/2010 8:47 PM
25 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
E. None of the above
226. True or False? The loop condition can be a complex condition involving several operators.
A. True (Correct Answer)
B. False
227. Local variables are stored on the....
A. heap
B. stack (Correct Answer)
C. register
D. ROM
E. None of the above
228. True or False? When an object reference is first declared, its value is null.
A. True (Correct Answer)
B. False
229. If class C extends B which in turn extends A and c, b, a are three instances of respective classes, which of next
sentences are true:
Choose all correct answers.
A. I can assign b to a (Correct Answer)
B. I can assign a to b
C. I can assign c to a (Correct Answer)
D. I can assign a to c
E. To an Object reference o, I can assign a or b or c (Correct Answer)
230. True or False? An abstract class is a class that is not completely implemented.
A. True (Correct Answer)
B. False
231. Which of the following is true about Classes?
A. There can be only one object for each class.
B. A class can contain methods and instance variables. (Correct Answer)
C. Each class can only have one method.
D. All of these.
E. None of the above
232. Assuming the class B inherits from class A and method foo belongs to class A, what do we mean by overriding method
foo in class B?
A. We are calling method foo in class B
B. We are not using method foo in class B
C. We are coding another version of method foo in class B (Correct Answer)
233. True or False? A double is more precise than a float.
A. True (Correct Answer)
B. False
234. What will happen when you attempt to compile and run the following code?
public class Static {
static {
int x = 5;
}
static int x,y;
public static void main(String args[]) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x;
}
}
A.
B.
C.
D.
E.
Compile time error
prints 1
prints 2
prints 3 (Correct Answer)
prints 4
2/23/2010 8:47 PM
26 of 26
http://dpearson-lenovo:9105/index.php?action=exam_questions&test_k...
235. True or False? An array can be an instance variable of a class.
A. True (Correct Answer)
B. False
236. True or False? In an array we can have a mix of integers and Strings.
A. True
B. False (Correct Answer)
237. True or False? protected fields and methods are inherited.
A. True (Correct Answer)
B. False
2/23/2010 8:47 PM